Skip to content

Commit

Permalink
feat(buffer): allow to configure options for the float buffer
Browse files Browse the repository at this point in the history
Aside from the users config options, we also add some sensible defaults:
- modifiable: false
- readonly: true
- bufhidden: delete
- swapfile: false
  • Loading branch information
Goose97 committed Dec 4, 2024
1 parent 0f5d4ab commit 5bf403f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
24 changes: 22 additions & 2 deletions lua/timber/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,25 @@ end

---@param lines string[]
---@param window_opts table
---@param buffer_opts table?
---@return integer floating_winnr
---@return integer floating_bufnr
local function open_floating_window(lines, window_opts)
local function open_floating_window(lines, window_opts, buffer_opts)
buffer_opts = vim.tbl_extend(
"force",
{ modifiable = false, readonly = true, bufhidden = "delete", swapfile = false },
buffer_opts or {}
)

-- Create buffer for main content
local floating_bufnr = vim.api.nvim_create_buf(false, true)
local floating_winnr = vim.api.nvim_open_win(floating_bufnr, false, window_opts)
vim.api.nvim_buf_set_lines(floating_bufnr, 0, -1, true, lines)

for option, value in pairs(buffer_opts) do
vim.api.nvim_set_option_value(option, value, { buf = floating_bufnr })
end

-- q to close the floating window
vim.api.nvim_buf_set_keymap(
floating_bufnr,
Expand Down Expand Up @@ -512,7 +523,16 @@ local function show_placeholder_full_content(placeholder, opts)
footer_pos = "right",
})

local bufnr, winnr = open_floating_window(lines, window_opts)
-- TODO: handle multiple sources
local source_name = placeholder.entries[1].source_name
local source = require("timber.watcher").get_source(source_name)

if not source then
utils.notify(string.format("Unrecognized watcher source '%s'", source_name), "warn")
return
end

local bufnr, winnr = open_floating_window(lines, window_opts, source.buffer)
vim.api.nvim_win_set_hl_ns(winnr, M.log_placeholder_ns)

for _, i in ipairs(separators) do
Expand Down
15 changes: 13 additions & 2 deletions lua/timber/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

local sources = require("timber.watcher.sources")
local buffers = require("timber.buffers")
local utils = require("timber.utils")

local M = { MARKER = "🪵", ID_LENGTH = 3 }
local M = { MARKER = "🪵", ID_LENGTH = 3, sources = {} }

function M.generate_unique_id()
local chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expand All @@ -32,10 +33,11 @@ function M.generate_marker_pairs()
return start, end_, id
end

---@param source_specs Timber.Watcher.SourceSpecs
---@param source_specs Timber.Watcher.SourceSpec[]
function M.setup(source_specs)
math.randomseed(os.time())

M.sources = source_specs
sources.setup({
sources = source_specs,
on_log_capture = function(log_entry)
Expand All @@ -44,4 +46,13 @@ function M.setup(source_specs)
})
end

---Get a source by name
---@param name string
---@return Timber.Watcher.SourceSpec? source
function M.get_source(name)
return utils.array_find(M.sources, function(source)
return source.name == name
end)
end

return M
7 changes: 5 additions & 2 deletions lua/timber/watcher/sources.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
---Manage sources for watcher
---Sources are data sources that can output log result. Supported sources are:
--- 1. Filesystem
--- 2. Neotest

---@alias Timber.Watcher.SourceSpecs (Timber.Watcher.Sources.FilesystemSpec | Timber.Watcher.Sources.NeotestSpec)[]
---@alias Timber.Watcher.SourceSpec (Timber.Watcher.Sources.FilesystemSpec | Timber.Watcher.Sources.NeotestSpec)

---@class Timber.Watcher.Sources.FilesystemSpec
---@field name string
---@field path string
---@field buffer table<string, any>? A table of buffer options to apply to the float buffer.
---@field type "filesystem"

---@class Timber.Watcher.Sources.NeotestSpec
---@field name string
---@field buffer table<string, any>? A table of buffer options to apply to the float buffer.
---@field type "neotest"

---@class Timber.Watcher.Sources.Opts
---@field sources Timber.Watcher.SourceSpecs
---@field sources Timber.Watcher.SourceSpec[]
---@field on_log_capture fun(log_entry: Timber.Watcher.LogEntry)

local M = { sources = {} }
Expand Down
54 changes: 54 additions & 0 deletions tests/timber/timber_buffers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,60 @@ describe("timber.buffers.open_float", function()
end,
})
end)

it("sets the buffer options from source config with defaults", function()
watcher.setup({
{
type = "neotest",
name = "Test",
buffer = {
filetype = "timbertest",
},
},
})

local id = watcher.generate_unique_id()

helper.assert_scenario({
input = string.format(
[[
const foo = "bar"
console.log("%s%s|")
const bar = "foo"
]],
watcher.MARKER,
id
),
input_cursor = false,
filetype = "typescript",
action = function()
helper.wait(20)
buffers.receive_log_entry({
log_placeholder_id = id,
payload = "foo_123456789_123456890",
source_name = "Test",
timestamp = os.time(),
})
-- Open the float window, and focus to it
vim.cmd("normal! 2G")
buffers.open_float()
vim.cmd("wincmd w")
end,
expected = function()
-- Options from config
assert.equals("timbertest", vim.api.nvim_get_option_value("filetype", { buf = 0 }))

-- Default options
assert.equals(false, vim.api.nvim_get_option_value("modifiable", { buf = 0 }))
assert.equals(true, vim.api.nvim_get_option_value("readonly", { buf = 0 }))
assert.equals("delete", vim.api.nvim_get_option_value("bufhidden", { buf = 0 }))
assert.equals(false, vim.api.nvim_get_option_value("swapfile", { buf = 0 }))
end,
})

-- Close the float window
vim.cmd("q!")
end)
end)

describe("given the placeholder has NO entries", function()
Expand Down

0 comments on commit 5bf403f

Please sign in to comment.