From cbc831a5caaa3cf9db15eadd8d03216ea49d1ac1 Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Tue, 23 May 2023 14:59:47 -0700 Subject: [PATCH] neovim: Use Telescope community multi-open binding --- neovim/init.lua | 130 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 110 insertions(+), 20 deletions(-) diff --git a/neovim/init.lua b/neovim/init.lua index 6b48747..9444d6d 100644 --- a/neovim/init.lua +++ b/neovim/init.lua @@ -119,7 +119,7 @@ plugins = { local builtin = require('telescope.builtin') - find_files = function(opts) + local function find_files(opts) opts = opts or {} opts['hidden'] = true return builtin.find_files(opts) @@ -137,26 +137,116 @@ plugins = { vim.keymap.set('n', ';b', builtin.buffers) vim.keymap.set('n', ';*', builtin.grep_string) end, - opts = { - defaults = { - vimgrep_arguments = { - "rg", - "--color=never", - "--no-heading", - "--with-filename", - "--line-number", - "--column", - "--smart-case", - -- Include hidden files, but continue ignoring the .git directory itself. - "--hidden", - "--iglob", "!.git", + opts = function(_plugin, _config) + local actions = require("telescope.actions") + local action_state = require("telescope.actions.state") + local transform_mod = require("telescope.actions.mt").transform_mod + + -- https://github.com/nvim-telescope/telescope.nvim/issues/1048#issuecomment-1225975038 + local function multiopen(prompt_bufnr, method) + local method = method or "default" + + local edit_file_cmd_map = { + vertical = "vsplit", + horizontal = "split", + tab = "tabedit", + default = "edit", } - }, - pickers = {}, - extensions = { - aerial = {}, - }, - }, + local edit_buf_cmd_map = { + vertical = "vert sbuffer", + horizontal = "sbuffer", + tab = "tab sbuffer", + default = "buffer", + } + + local picker = action_state.get_current_picker(prompt_bufnr) + local multi_selection = picker:get_multi_selection() + + if #multi_selection > 1 then + require("telescope.pickers").on_close_prompt(prompt_bufnr) + pcall(vim.api.nvim_set_current_win, picker.original_win_id) + + for i, entry in ipairs(multi_selection) do + local filename, row, col + + if entry.path or entry.filename then + filename = entry.path or entry.filename + + row = entry.row or entry.lnum + col = vim.F.if_nil(entry.col, 1) + elseif not entry.bufnr then + local value = entry.value + if not value then + goto continue + 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 + + if entry_bufnr then + if not vim.api.nvim_buf_get_option(entry_bufnr, "buflisted") then + vim.api.nvim_buf_set_option(entry_bufnr, "buflisted", true) + end + local command = i == 1 and "buffer" or edit_buf_cmd_map[method] + pcall(vim.cmd, string.format("%s %s", command, vim.api.nvim_buf_get_name(entry_bufnr))) + else + local command = i == 1 and "edit" or edit_file_cmd_map[method] + if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then + filename = require("plenary.path"):new(vim.fn.fnameescape(filename)):normalize(vim.loop.cwd()) + pcall(vim.cmd, string.format("%s %s", command, filename)) + end + end + + if row and col then + pcall(vim.api.nvim_win_set_cursor, 0, { row, col-1 }) + end + + ::continue:: + end + else + actions["select_" .. method](prompt_bufnr) + end + end + + return { + defaults = { + mappings = { + i = { + [""] = multiopen, + }, + n = { + [""] = multiopen, + }, + }, + vimgrep_arguments = { + "rg", + "--color=never", + "--no-heading", + "--with-filename", + "--line-number", + "--column", + "--smart-case", + -- Include hidden files, but continue ignoring the .git directory itself. + "--hidden", + "--iglob", "!.git", + }, + }, + pickers = {}, + extensions = { + aerial = {}, + }, + } + end, }, { "nvim-telescope/telescope-fzf-native.nvim",