feat: open buffers in various directions (#2463)

Fix the problem I reported in <https://github.com/nvim-telescope/telescope.nvim/issues/1725#issuecomment-1502548033>.
Supporting the abbreviations of the commands to specify the direction makes too
many combinations, so I added only their unabbreviated names.

In addition, make the error message more detailed for users that passes
unsupported command.
This commit is contained in:
YAMAMOTO Yuji
2023-05-24 18:14:03 +09:00
committed by GitHub
parent 28e667f05a
commit a709dbb5d5

View File

@@ -72,18 +72,36 @@ do
edit = "buffer",
new = "sbuffer",
vnew = "vert sbuffer",
["leftabove new"] = "leftabove sbuffer",
["leftabove vnew"] = "leftabove vert sbuffer",
["rightbelow new"] = "rightbelow sbuffer",
["rightbelow vnew"] = "rightbelow vert sbuffer",
["topleft new"] = "topleft sbuffer",
["topleft vnew"] = "topleft vert sbuffer",
["botright new"] = "botright sbuffer",
["botright vnew"] = "botright vert sbuffer",
tabedit = "tab sb",
}
edit_buffer = function(command, bufnr)
command = map[command]
if command == nil then
error "There was no associated buffer command"
local buf_command = map[command]
if buf_command == nil then
local valid_commands = vim.tbl_map(function(cmd)
return string.format("%q", cmd)
end, vim.tbl_keys(map))
table.sort(valid_commands)
error(
string.format(
"There was no associated buffer command for %q.\nValid commands are: %s.",
command,
table.concat(valid_commands, ", ")
)
)
end
if command ~= "drop" and command ~= "tab drop" then
vim.cmd(string.format("%s %d", command, bufnr))
if buf_command ~= "drop" and buf_command ~= "tab drop" then
vim.cmd(string.format("%s %d", buf_command, bufnr))
else
vim.cmd(string.format("%s %s", command, vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr))))
vim.cmd(string.format("%s %s", buf_command, vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr))))
end
end
end
@@ -91,7 +109,21 @@ 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.
-- Valid commands include: "edit", "new", "vedit", "tabedit"
-- Valid commands are:
-- - "edit"
-- - "new"
-- - "vedit"
-- - "tabedit"
-- - "drop"
-- - "tab drop"
-- - "leftabove new"
-- - "leftabove vnew"
-- - "rightbelow new"
-- - "rightbelow vnew"
-- - "topleft new"
-- - "topleft vnew"
-- - "botright new"
-- - "botright vnew"
action_set.edit = function(prompt_bufnr, command)
local entry = action_state.get_selected_entry()