fix: more consistent current buffer commands

This commit is contained in:
əlemi 2024-09-06 19:49:52 +02:00
parent 4cfa7baa28
commit 373b155864
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 20 additions and 7 deletions

View file

@ -10,7 +10,7 @@ local user_buffer_name = {}
local ticks = {} local ticks = {}
---@param name string name of buffer to attach to ---@param name string name of buffer to attach to
---@param buffer? integer if provided, use given buffer (will clear content) ---@param buffer integer buffer to use for attaching (will clear content)
---@param content? string if provided, set this content after attaching ---@param content? string if provided, set this content after attaching
---@param nowait? boolean skip waiting for initial content sync ---@param nowait? boolean skip waiting for initial content sync
---@return BufferController ---@return BufferController
@ -18,10 +18,6 @@ local function attach(name, buffer, content, nowait)
if buffer_id_map[name] ~= nil then if buffer_id_map[name] ~= nil then
error("already attached to buffer " .. name) error("already attached to buffer " .. name)
end end
if buffer == nil then
buffer = vim.api.nvim_create_buf(true, false)
vim.api.nvim_set_current_buf(buffer)
end
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer }) vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
vim.api.nvim_buf_set_name(buffer, name) vim.api.nvim_buf_set_name(buffer, name)
local controller = session.workspace:attach_buffer(name):await() local controller = session.workspace:attach_buffer(name):await()

View file

@ -110,7 +110,12 @@ local joined_actions = {
attach = function(path, bang) attach = function(path, bang)
if path == nil then error("missing buffer name") end if path == nil then error("missing buffer name") end
local buffer = nil local buffer = nil
if bang then buffer = vim.api.nvim_get_current_buf() end if bang then
buffer = vim.api.nvim_get_current_buf()
else
buffer = vim.api.nvim_create_buf(true, false)
vim.api.nvim_set_current_buf(buffer)
end
buffers.attach(path, buffer) buffers.attach(path, buffer)
end, end,

View file

@ -44,8 +44,9 @@ M.open = function(state, path, extra)
end end
if selected.type == "buffer" then if selected.type == "buffer" then
local window = utils.get_appropriate_window(state) local window = utils.get_appropriate_window(state)
local buf = vim.api.nvim_win_get_buf(window) local buf = vim.api.nvim_create_buf(true, false)
vim.api.nvim_set_current_win(window) vim.api.nvim_set_current_win(window)
vim.api.nvim_win_set_buf(window, buf)
buf_manager.attach(selected.name, buf) buf_manager.attach(selected.name, buf)
return return
end end
@ -56,6 +57,17 @@ M.open = function(state, path, extra)
error("unrecognized node type") error("unrecognized node type")
end end
M.move = function(state, path, extra)
local selected = state.tree:get_node()
if selected.type == "buffer" then
local window = utils.get_appropriate_window(state)
local buf = vim.api.nvim_win_get_buf(window)
buf_manager.attach(selected.name, buf)
return
end
error("only buffers can be moved to current file")
end
M.add = function(_state) M.add = function(_state)
if session.client == nil then if session.client == nil then
vim.ui.input({ prompt = "server address" }, function(input) vim.ui.input({ prompt = "server address" }, function(input)