From 6a7dd9b39962d55bcd27ddf59b66f94680e174db Mon Sep 17 00:00:00 2001 From: James Trew Date: Sat, 6 Jan 2024 12:31:07 -0500 Subject: [PATCH] fix: restore cursor to correct position on picker `close` --- lua/telescope/actions/init.lua | 5 +---- lua/telescope/actions/set.lua | 21 ++------------------- lua/telescope/make_entry.lua | 8 ++++---- lua/telescope/utils.lua | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 5acc8da..4a94411 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -379,15 +379,12 @@ end actions.close = function(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr) local original_win_id = picker.original_win_id - local cursor_valid, original_cursor = pcall(a.nvim_win_get_cursor, original_win_id) actions.close_pum(prompt_bufnr) require("telescope.pickers").on_close_prompt(prompt_bufnr) pcall(a.nvim_set_current_win, original_win_id) - if cursor_valid and a.nvim_get_mode().mode == "i" and picker._original_mode ~= "i" then - pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1 }) - end + utils.set_window_cursor(original_win_id) end --- Close the Telescope window, usually used within an action
diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index cb8dc79..1f65122 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -13,7 +13,6 @@ local a = vim.api -local log = require "telescope.log" local Path = require "plenary.path" local state = require "telescope.state" local utils = require "telescope.utils" @@ -197,7 +196,7 @@ action_set.edit = function(prompt_bufnr, command) -- prevents restarting lsp server if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then filename = Path:new(filename):normalize(vim.loop.cwd()) - pcall(vim.cmd, string.format("%s %s", command, vim.fn.fnameescape(filename))) + pcall(vim.cmd[command], vim.fn.fnameescape(filename)) end end @@ -208,23 +207,7 @@ action_set.edit = function(prompt_bufnr, command) end) end - local pos = vim.api.nvim_win_get_cursor(0) - if col == nil then - if row == pos[1] then - col = pos[2] + 1 - elseif row == nil then - row, col = pos[1], pos[2] + 1 - else - col = 1 - end - end - - if row and col then - local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, { row, col }) - if not ok then - log.debug("Failed to move to cursor:", err_msg, row, col) - end - end + utils.set_window_cursor(a.nvim_get_current_win(), row, col) end --- Scrolls the previewer up or down. diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index d66f781..52b135e 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -25,9 +25,9 @@ --- - bufnr number (optional): will be interpreted by the default `` action as open --- this buffer --- - lnum number (optional): lnum value which will be interpreted by the default `` ---- action as a jump to this line +--- action as a jump to this line (1 index) --- - col number (optional): col value which will be interpreted by the default `` ---- action as a jump to this column +--- action as a jump to this column (1 index) --- --- For more information on easier displaying, see |telescope.pickers.entry_display| --- @@ -694,9 +694,9 @@ function make_entry.gen_from_treesitter(opts) node_text = node_text, filename = get_filename(bufnr), - -- need to add one since the previewer substacts one + -- convert lnum/col to 1-index (treesitter uses 0-index) lnum = start_row + 1, - col = start_col, + col = start_col + 1, text = node_text, start = start_row, finish = end_row, diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 32df244..b2adfd6 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -603,4 +603,37 @@ utils.__separate_file_path_location = function(path) return path, 0, 0 end +---@param winid integer +---@param row integer +---@param col integer +local function protected_win_set_cursor(winid, row, col) + vim.schedule(function() + local ok, err_msg = pcall(vim.api.nvim_win_set_cursor, winid, { row, col }) + if not ok then + log.debug("Failed to move to cursor:", err_msg, row, col) + end + end) +end + +--- set the cursor of a window to its original, or provided position +---@param winid integer winid of original window +---@param row? integer possible new row position +---@param col? integer possible new col position. must pass row position for col to be used. +utils.set_window_cursor = function(winid, row, col) + if row and col then + protected_win_set_cursor(winid, row, col - 1) -- entry.col is 1-index but nvim api uses 0-index + return + end + + if row then + protected_win_set_cursor(winid, row, 0) + else + local cursor_valid, cursor_pos = pcall(vim.api.nvim_win_get_cursor, winid) + if not cursor_valid then + return + end + protected_win_set_cursor(winid, cursor_pos[1], cursor_pos[2]) + end +end + return utils