summaryrefslogtreecommitdiff
path: root/lua/telescope/actions/set.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/telescope/actions/set.lua')
-rw-r--r--lua/telescope/actions/set.lua115
1 files changed, 59 insertions, 56 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
- else
- 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, ":")
+ end
- filename = sections[1]
- row = tonumber(sections[2])
- col = tonumber(sections[3])
+ 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
- local preview_win = state.get_status(prompt_bufnr).preview_win
- if preview_win then
- a.nvim_win_set_config(preview_win, {style = ''})
+ if type(value) == "table" then
+ value = entry.display
end
- local entry_bufnr = entry.bufnr
+ local sections = vim.split(value, ":")
- require('telescope.actions').close(prompt_bufnr)
+ filename = sections[1]
+ row = tonumber(sections[2])
+ col = tonumber(sections[3])
+ end
- 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
+ local entry_bufnr = entry.bufnr
+
+ require('telescope.actions').close(prompt_bufnr)
+
+ if entry_bufnr then
+ edit_buffer(command, entry_bufnr)
+ else
+ filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd())
+
+ -- 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
- 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