From 828a37fe1f008dbfd70cd7fc0f7ba9d0bc75da2a Mon Sep 17 00:00:00 2001 From: Vhyrro Date: Fri, 23 Dec 2022 21:14:15 +0100 Subject: [PATCH] fix(core.looking-glass): buffer being closed for no reason after leaving buffer --- .../modules/core/looking-glass/module.lua | 120 ++++++++++-------- 1 file changed, 65 insertions(+), 55 deletions(-) diff --git a/lua/neorg/modules/core/looking-glass/module.lua b/lua/neorg/modules/core/looking-glass/module.lua index b618fc4ee..bd78165fb 100644 --- a/lua/neorg/modules/core/looking-glass/module.lua +++ b/lua/neorg/modules/core/looking-glass/module.lua @@ -46,69 +46,79 @@ module.public = { return true end - local cursor_pos = vim.api.nvim_win_get_cursor(0) + local cursor_pos = vim.api.nvim_win_get_cursor(source_window) - vim.schedule(function() - if vim.api.nvim_get_current_buf() ~= source then - return - end + if vim.api.nvim_get_current_buf() ~= source then + return + end + + -- Get the positions of both the extmarks (this has to be in the schedule function else it returns + -- outdated information). + local extmark_begin = vim.api.nvim_buf_get_extmark_by_id(source, namespace, start_extmark, {}) + local extmark_end = vim.api.nvim_buf_get_extmark_by_id(source, namespace, end_extmark, {}) + + -- Both extmarks will have the same row if the user deletes the whole code block. + -- In other words, this is a method to detect when a code block has been deleted. + if extmark_end[1] == extmark_begin[1] then + vim.api.nvim_buf_delete(target, { force = true }) + vim.api.nvim_buf_clear_namespace(source, namespace, 0, -1) + return true + end - -- Get the positions of both the extmarks (this has to be in the schedule function else it returns - -- outdated information). - local extmark_begin = vim.api.nvim_buf_get_extmark_by_id(source, namespace, start_extmark, {}) - local extmark_end = vim.api.nvim_buf_get_extmark_by_id(source, namespace, end_extmark, {}) + -- Make sure that the cursor is within bounds of the code block + if cursor_pos[1] > extmark_begin[1] and cursor_pos[1] <= (extmark_end[1] + 1) then + -- For extra information grab the current node under the cursor + local current_node = module.required["core.integrations.treesitter"] + .get_ts_utils() + .get_node_at_cursor(source_window, true) - -- Both extmarks will have the same row if the user deletes the whole code block. - -- In other words, this is a method to detect when a code block has been deleted. - if extmark_end[1] == extmark_begin[1] then + if not current_node then vim.api.nvim_buf_delete(target, { force = true }) vim.api.nvim_buf_clear_namespace(source, namespace, 0, -1) return true end - -- Make sure that the cursor is within bounds of the code block - if cursor_pos[1] > extmark_begin[1] and cursor_pos[1] <= (extmark_end[1] + 1) then - -- For extra information grab the current node under the cursor - local current_node = - module.required["core.integrations.treesitter"].get_ts_utils().get_node_at_cursor(0, true) - - -- If we are within bounds of the code block but the current node type is not part of a ranged - -- tag then it means the user malformed the code block in some way and we should bail - if not current_node or not current_node:type():match("^ranged_verbatim_tag.*") then - vim.api.nvim_buf_delete(target, { force = true }) - vim.api.nvim_buf_clear_namespace(source, namespace, 0, -1) - return true - end - - local lines = vim.api.nvim_buf_get_lines(source, extmark_begin[1] + 1, extmark_end[1], true) - - for i, line in ipairs(lines) do - lines[i] = line:sub(extmark_begin[2] + 1) - end - - -- Now that we have full information that we are in fact in a valid code block - -- take the lines from within the code block and put them in the buffer - vim.api.nvim_buf_set_lines(target, 0, -1, false, lines) - - local target_line_count = vim.api.nvim_buf_line_count(target) - - -- Set the cursor in the target window to the place the text is being changed. - -- Useful to keep up with long ranges of text. - -- - -- This check exists as sometimes the cursor position can be larger than the size of the - -- target buffer which causes errors. - if cursor_pos[1] - extmark_begin[1] > target_line_count then - vim.api.nvim_win_set_cursor(target_window, { target_line_count, cursor_pos[2] }) - else - -- Here we subtract the beginning extmark's row position from the current cursor position - -- in order to create an offset that can be applied to the target buffer. - vim.api.nvim_win_set_cursor( - target_window, - { cursor_pos[1] - extmark_begin[1], cursor_pos[2] } - ) - end + -- If we are within bounds of the code block but the current node type is not part of a ranged + -- tag then it means the user malformed the code block in some way and we should bail + if + not module.required["core.integrations.treesitter"].find_parent( + current_node, + "^ranged_verbatim_tag.*" + ) + then + vim.api.nvim_buf_delete(target, { force = true }) + vim.api.nvim_buf_clear_namespace(source, namespace, 0, -1) + return true end - end) + + local lines = vim.api.nvim_buf_get_lines(source, extmark_begin[1] + 1, extmark_end[1], true) + + for i, line in ipairs(lines) do + lines[i] = line:sub(extmark_begin[2] + 1) + end + + -- Now that we have full information that we are in fact in a valid code block + -- take the lines from within the code block and put them in the buffer + vim.api.nvim_buf_set_lines(target, 0, -1, false, lines) + + local target_line_count = vim.api.nvim_buf_line_count(target) + + -- Set the cursor in the target window to the place the text is being changed. + -- Useful to keep up with long ranges of text. + -- + -- This check exists as sometimes the cursor position can be larger than the size of the + -- target buffer which causes errors. + if cursor_pos[1] - extmark_begin[1] > target_line_count then + vim.api.nvim_win_set_cursor(target_window, { target_line_count, cursor_pos[2] }) + else + -- Here we subtract the beginning extmark's row position from the current cursor position + -- in order to create an offset that can be applied to the target buffer. + vim.api.nvim_win_set_cursor( + target_window, + { cursor_pos[1] - extmark_begin[1] - 1, cursor_pos[2] } + ) + end + end end, }) @@ -133,7 +143,7 @@ module.public = { vim.api.nvim_win_set_cursor( source_window, - { cursor_pos[1] + extmark_begin[1], cursor_pos[2] + extmark_begin[2] } + { cursor_pos[1] + 1 + extmark_begin[1], cursor_pos[2] + extmark_begin[2] } ) end), })