Skip to content

Commit

Permalink
fix: restore cursor to correct position on picker close
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestrew committed Jan 7, 2024
1 parent 87e92ea commit 6cabb46
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
5 changes: 1 addition & 4 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>
Expand Down
21 changes: 2 additions & 19 deletions lua/telescope/actions/set.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
--- - bufnr number (optional): will be interpreted by the default `<cr>` action as open
--- this buffer
--- - lnum number (optional): lnum value which will be interpreted by the default `<cr>`
--- 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 `<cr>`
--- 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|
---
Expand Down Expand Up @@ -707,9 +707,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,
Expand Down
33 changes: 33 additions & 0 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,37 @@ utils.list_find = function(func, list)
end
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

0 comments on commit 6cabb46

Please sign in to comment.