Not for normies
refactor(nvim): created telescope extension for all files to make keymap simple
2 files changed, +48 -18
+1-18nvim/lua/keymaps.lua
| @@ -17,24 +17,7 @@ easymap("n", "<leader>m", ':exe "resize ".float2nr(&lines*0.8)<cr>', "80% Window | ||
| 17 | 17 | easymap("n", "<leader>s", ":Scratch<cr>", "Scratch") |
| 18 | 18 | easymap("n", "<leader>g", ":Telescope git_bcommits<cr>", "Buffer Commits") |
| 19 | 19 | easymap("n", "<leader>d", ":Telescope git_status<cr>", "Git Diff") |
| 20 | -vim.keymap.set('n', '<leader>.', function() | |
| 21 | - require("telescope.builtin").find_files({ | |
| 22 | - hidden = true, | |
| 23 | - no_ignore = true, | |
| 24 | - previewer = false, | |
| 25 | - find_command = { 'rg', '--files', '--hidden', '--no-ignore', '--glob', '!.git/*', '--glob', '!target/*' }, | |
| 26 | - attach_mappings = function(b, map) | |
| 27 | - local actions = require "telescope.actions" | |
| 28 | - local action_state = require "telescope.actions.state" | |
| 29 | - map({ "i", "n" }, "<C-o>", function() | |
| 30 | - local selection = action_state.get_selected_entry() | |
| 31 | - vim.cmd("!xdg-open " .. selection.path) | |
| 32 | - actions.close(b) | |
| 33 | - end) | |
| 34 | - return true | |
| 35 | - end | |
| 36 | - }) | |
| 37 | -end, { desc = 'All Files' }) | |
| 20 | +easymap("n", "<leader>.", ":Telescope all_files<cr>", "All Files") | |
| 38 | 21 | for i = 1, 9 do |
| 39 | 22 | easymap("n", "<leader>" .. i, i .. "gt", "Tab " .. i) |
| 40 | 23 | end |
+47-0nvim/lua/telescope/_extensions/all_files.lua
| @@ -0,0 +1,47 @@ | ||
| 1 | +local pickers = require("telescope.pickers") | |
| 2 | +local finders = require("telescope.finders") | |
| 3 | +local conf = require("telescope.config").values | |
| 4 | +local actions = require("telescope.actions") | |
| 5 | +local action_state = require("telescope.actions.state") | |
| 6 | + | |
| 7 | +local function open_with_system(prompt_bufnr) | |
| 8 | + local entry = action_state.get_selected_entry() | |
| 9 | + actions.close(prompt_bufnr) | |
| 10 | + if not entry then return end | |
| 11 | + local path = entry.path or entry.filename or entry.value | |
| 12 | + if not path then return end | |
| 13 | + | |
| 14 | + local cmd | |
| 15 | + if vim.fn.has("mac") == 1 then | |
| 16 | + cmd = { "open", path } | |
| 17 | + elseif vim.fn.has("win32") == 1 then | |
| 18 | + cmd = { "cmd.exe", "/C", "start", "", path } | |
| 19 | + elseif vim.fn.executable("wslview") == 1 then | |
| 20 | + cmd = { "wslview", path } | |
| 21 | + else | |
| 22 | + cmd = { "xdg-open", path } | |
| 23 | + end | |
| 24 | + vim.fn.jobstart(cmd, { detach = true }) | |
| 25 | +end | |
| 26 | + | |
| 27 | +local search = function(opts) | |
| 28 | + opts = opts or {} | |
| 29 | + pickers.new(opts, { | |
| 30 | + prompt_title = "All Files", | |
| 31 | + finder = finders.new_oneshot_job({ | |
| 32 | + "rg", "--files", "--hidden", "--no-ignore", | |
| 33 | + "--glob", "!.git/*", "--glob", "!target/*", | |
| 34 | + }, opts), | |
| 35 | + sorter = conf.generic_sorter(opts), | |
| 36 | + attach_mappings = function(prompt_bufnr, map) | |
| 37 | + map({ "i", "n" }, "<C-o>", function() | |
| 38 | + open_with_system(prompt_bufnr) | |
| 39 | + end) | |
| 40 | + return true | |
| 41 | + end, | |
| 42 | + }):find() | |
| 43 | +end | |
| 44 | + | |
| 45 | +return require("telescope").register_extension({ | |
| 46 | + exports = { all_files = search }, | |
| 47 | +}) |