fix: goto_file_selection performance issues, do not reload buffer if it is already loaded (#541)
This commit is contained in:
@@ -2,7 +2,6 @@ local a = vim.api
|
|||||||
|
|
||||||
local log = require('telescope.log')
|
local log = require('telescope.log')
|
||||||
local path = require('telescope.path')
|
local path = require('telescope.path')
|
||||||
local state = require('telescope.state')
|
|
||||||
|
|
||||||
local action_state = require('telescope.actions.state')
|
local action_state = require('telescope.actions.state')
|
||||||
|
|
||||||
@@ -43,6 +42,24 @@ set.select = function(prompt_bufnr, type)
|
|||||||
return set.edit(prompt_bufnr, action_state.select_key_to_edit_key(type))
|
return set.edit(prompt_bufnr, action_state.select_key_to_edit_key(type))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local edit_buffer
|
||||||
|
do
|
||||||
|
local map = {
|
||||||
|
edit = 'buffer',
|
||||||
|
new = 'sbuffer',
|
||||||
|
vnew = 'vert sbuffer',
|
||||||
|
tabedit = 'tab sb',
|
||||||
|
}
|
||||||
|
|
||||||
|
edit_buffer = function(command, bufnr)
|
||||||
|
command = map[command]
|
||||||
|
if command == nil then
|
||||||
|
error('There was no associated buffer command')
|
||||||
|
end
|
||||||
|
vim.cmd(string.format("%s %d", command, bufnr))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Edit a file based on the current selection.
|
--- Edit a file based on the current selection.
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
---@param command string: The command to use to open the file.
|
---@param command string: The command to use to open the file.
|
||||||
@@ -53,71 +70,57 @@ set.edit = function(prompt_bufnr, command)
|
|||||||
if not entry then
|
if not entry then
|
||||||
print("[telescope] Nothing currently selected")
|
print("[telescope] Nothing currently selected")
|
||||||
return
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local filename, row, col
|
||||||
|
|
||||||
|
if entry.filename then
|
||||||
|
filename = entry.path or entry.filename
|
||||||
|
|
||||||
|
-- TODO: Check for off-by-one
|
||||||
|
row = entry.row or entry.lnum
|
||||||
|
col = entry.col
|
||||||
|
elseif not entry.bufnr then
|
||||||
|
-- TODO: Might want to remove this and force people
|
||||||
|
-- to put stuff into `filename`
|
||||||
|
local value = entry.value
|
||||||
|
if not value then
|
||||||
|
print("Could not do anything with blank line...")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(value) == "table" then
|
||||||
|
value = entry.display
|
||||||
|
end
|
||||||
|
|
||||||
|
local sections = vim.split(value, ":")
|
||||||
|
|
||||||
|
filename = sections[1]
|
||||||
|
row = tonumber(sections[2])
|
||||||
|
col = tonumber(sections[3])
|
||||||
|
end
|
||||||
|
|
||||||
|
local entry_bufnr = entry.bufnr
|
||||||
|
|
||||||
|
require('telescope.actions').close(prompt_bufnr)
|
||||||
|
|
||||||
|
if entry_bufnr then
|
||||||
|
edit_buffer(command, entry_bufnr)
|
||||||
else
|
else
|
||||||
local filename, row, col
|
filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd())
|
||||||
if entry.filename then
|
|
||||||
filename = entry.path or entry.filename
|
|
||||||
|
|
||||||
-- TODO: Check for off-by-one
|
-- check if we didn't pick a different buffer
|
||||||
row = entry.row or entry.lnum
|
-- prevents restarting lsp server
|
||||||
col = entry.col
|
if vim.api.nvim_get_current_buf() ~= vim.fn.bufnr(filename) then
|
||||||
elseif not entry.bufnr then
|
vim.cmd(string.format("%s %s", command, filename))
|
||||||
-- TODO: Might want to remove this and force people
|
|
||||||
-- to put stuff into `filename`
|
|
||||||
local value = entry.value
|
|
||||||
if not value then
|
|
||||||
print("Could not do anything with blank line...")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if type(value) == "table" then
|
|
||||||
value = entry.display
|
|
||||||
end
|
|
||||||
|
|
||||||
local sections = vim.split(value, ":")
|
|
||||||
|
|
||||||
filename = sections[1]
|
|
||||||
row = tonumber(sections[2])
|
|
||||||
col = tonumber(sections[3])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local preview_win = state.get_status(prompt_bufnr).preview_win
|
if row and col then
|
||||||
if preview_win then
|
local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col})
|
||||||
a.nvim_win_set_config(preview_win, {style = ''})
|
if not ok then
|
||||||
end
|
log.debug("Failed to move to cursor:", err_msg, row, col)
|
||||||
|
|
||||||
local entry_bufnr = entry.bufnr
|
|
||||||
|
|
||||||
require('telescope.actions').close(prompt_bufnr)
|
|
||||||
|
|
||||||
if entry_bufnr then
|
|
||||||
if command == 'edit' then
|
|
||||||
vim.cmd(string.format(":buffer %d", entry_bufnr))
|
|
||||||
elseif command == 'new' then
|
|
||||||
vim.cmd(string.format(":sbuffer %d", entry_bufnr))
|
|
||||||
elseif command == 'vnew' then
|
|
||||||
vim.cmd(string.format(":vert sbuffer %d", entry_bufnr))
|
|
||||||
elseif command == 'tabedit' then
|
|
||||||
vim.cmd(string.format(":tab sb %d", entry_bufnr))
|
|
||||||
end
|
|
||||||
else
|
|
||||||
filename = path.normalize(vim.fn.fnameescape(filename), vim.fn.getcwd())
|
|
||||||
|
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
|
||||||
if filename ~= vim.api.nvim_buf_get_name(bufnr) then
|
|
||||||
vim.cmd(string.format(":%s %s", command, filename))
|
|
||||||
bufnr = vim.api.nvim_get_current_buf()
|
|
||||||
a.nvim_buf_set_option(bufnr, "buflisted", true)
|
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user