Skip to content

Commit

Permalink
fix: Wipe unused temp buffer on remote edit
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Nov 2, 2023
1 parent de26d82 commit d1d6eff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
18 changes: 4 additions & 14 deletions lua/orgmode/capture/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,17 @@ function Capture:_refile_to(opts)
apply_properties(opts)

local is_same_file = opts.file == utils.current_file_path()
local cur_win = vim.api.nvim_get_current_win()

local item = opts.item
if is_same_file and item then
vim.cmd(string.format('silent! %d,%d move %s', item.range.start_line, item.range.end_line, tostring(opts.file)))
return true
end

local edit_file = utils.edit_file(opts.file)

if not is_same_file then
local bufnr = vim.fn.bufadd(opts.file)
vim.api.nvim_open_win(bufnr, true, {
relative = 'editor',
width = 1,
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
height = 2,
row = 99999,
col = 99999,
zindex = 1,
style = 'minimal',
})
edit_file.open()
end

remove_buffer_empty_lines(opts)
Expand All @@ -374,8 +365,7 @@ function Capture:_refile_to(opts)
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, opts.lines)

if not is_same_file then
vim.cmd('silent! wq!')
vim.api.nvim_set_current_win(cur_win)
edit_file.close()
end

if item and item.file == utils.current_file_path() then
Expand Down
16 changes: 3 additions & 13 deletions lua/orgmode/parser/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,11 @@ function Files.update_file(filename, action)
end)
end

local bufnr = vim.fn.bufadd(filename)
vim.api.nvim_open_win(bufnr, true, {
relative = 'editor',
width = 1,
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
height = 2,
row = 99999,
col = 99999,
zindex = 1,
style = 'minimal',
})
local edit_file = utils.edit_file(filename)
edit_file.open()

return Promise.resolve(action(file)):next(function(result)
vim.cmd('silent! wq!')
vim.api.nvim_set_current_win(cur_win)
edit_file.close()
return result
end)
end
Expand Down
31 changes: 31 additions & 0 deletions lua/orgmode/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,35 @@ function utils.pad_right(str, amount)
return string.format('%s%s', str, string.rep(' ', spaces))
end

---@param filename string
function utils.edit_file(filename)
local buf_not_already_loaded = vim.fn.bufexists(filename) ~= 1
local cur_win = vim.api.nvim_get_current_win()

return {
open = function()
local bufnr = vim.fn.bufadd(filename)
vim.api.nvim_open_win(bufnr, true, {
relative = 'editor',
width = 1,
-- TODO: Revert to 1 once the https://github.com/neovim/neovim/issues/19464 is fixed
height = 2,
row = 99999,
col = 99999,
zindex = 1,
style = 'minimal',
})
end,
close = function()
vim.cmd('silent! w')
if buf_not_already_loaded then
vim.cmd('silent! bw!')
else
vim.cmd('silent! q!')
end
vim.api.nvim_set_current_win(cur_win)
end,
}
end

return utils

0 comments on commit d1d6eff

Please sign in to comment.