fix: goto_file_selection performance issues, do not reload buffer if it is already loaded (#541)

This commit is contained in:
oberblastmeister
2021-02-27 10:01:02 -05:00
committed by GitHub
parent 49650f5d74
commit 84732d1d78

View File

@@ -2,7 +2,6 @@ local a = vim.api
local log = require('telescope.log')
local path = require('telescope.path')
local state = require('telescope.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))
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.
---@param prompt_bufnr number: The prompt bufnr
---@param command string: The command to use to open the file.
@@ -53,8 +70,10 @@ set.edit = function(prompt_bufnr, command)
if not entry then
print("[telescope] Nothing currently selected")
return
else
end
local filename, row, col
if entry.filename then
filename = entry.path or entry.filename
@@ -81,33 +100,19 @@ set.edit = function(prompt_bufnr, command)
col = tonumber(sections[3])
end
local preview_win = state.get_status(prompt_bufnr).preview_win
if preview_win then
a.nvim_win_set_config(preview_win, {style = ''})
end
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
edit_buffer(command, entry_bufnr)
else
filename = path.normalize(vim.fn.fnameescape(filename), vim.fn.getcwd())
filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd())
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)
-- check if we didn't pick a different buffer
-- prevents restarting lsp server
if vim.api.nvim_get_current_buf() ~= vim.fn.bufnr(filename) then
vim.cmd(string.format("%s %s", command, filename))
end
if row and col then
@@ -117,8 +122,6 @@ set.edit = function(prompt_bufnr, command)
end
end
end
vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename))
end
end
-- ==================================================