mirror of
https://github.com/zoriya/telescope.nvim.git
synced 2026-08-16 02:45:09 +00:00
fix: restore cursor to correct position on picker close
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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|
|
||||
---
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user