From 84732d1d780f1aecb799502be97ec3a403066268 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Sat, 27 Feb 2021 10:01:02 -0500 Subject: [PATCH] fix: goto_file_selection performance issues, do not reload buffer if it is already loaded (#541) --- lua/telescope/actions/set.lua | 123 +++++++++++++++++----------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index bd4f1b3..f55f3e7 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -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,71 +70,57 @@ set.edit = function(prompt_bufnr, command) if not entry then print("[telescope] Nothing currently selected") 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 - local filename, row, col - if entry.filename then - filename = entry.path or entry.filename + filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd()) - -- 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]) + -- 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 - 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 - 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 + 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 - vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename)) end end