Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore cursor to correct position on picker close #2850

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2451,9 +2451,9 @@ entry) which contains the following important keys:
- 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
`<cr>` 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
`<cr>` action as a jump to this column (1 index)

For more information on easier displaying, see
|telescope.pickers.entry_display|
Expand Down
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
6 changes: 4 additions & 2 deletions lua/telescope/builtin/__internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ internal.commands = function(opts)
local cr = vim.api.nvim_replace_termcodes("<cr>", true, false, true)
cmd = cmd .. cr
end
vim.cmd [[stopinsert]]
vim.api.nvim_feedkeys(cmd, "t", false)
vim.schedule(function()
vim.cmd [[stopinsert]]
vim.api.nvim_feedkeys(cmd, "t", false)
end)
end)

return true
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 @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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