summary refs log tree commit diff
path: root/.vim/lua/internal/misc.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-10-14 20:57:03 +0200
committerRobert Günzler <r@gnzler.io>2022-10-14 21:01:15 +0200
commit2d2009413f8be624a7cca95cf702e32d9315cbef (patch)
treef024c1b5b27b886530e86221593e3cfdc3672d7e /.vim/lua/internal/misc.lua
parent6d7e524d3d7a71da2cea61b335a59e9544e2a6f7 (diff)
vim: move custom modules under internal/
Diffstat (limited to '')
-rw-r--r--.vim/lua/internal/misc.lua (renamed from .vim/lua/misc.lua)48
1 files changed, 40 insertions, 8 deletions
diff --git a/.vim/lua/misc.lua b/.vim/lua/internal/misc.lua
index 6c642f4..5a2ae21 100644
--- a/.vim/lua/misc.lua
+++ b/.vim/lua/internal/misc.lua
@@ -93,7 +93,11 @@ function M.sort_lines(_, preview_ns, preview_bufnr)
   end
 end
 
-local function get_searchdirs(dirs)
+local telescope_builtin = require('telescope.builtin')
+local telescope_actions = require('telescope.actions')
+local telescope_action_state = require('telescope.actions.state')
+
+local get_searchdirs = function(dirs)
   -- skip asking if we're looking at known filetypes
   local ft = vim.api.nvim_buf_get_option(0, 'filetype')
   if not dirs and (ft == 'dirbuf' or ft == 'alpha') then
@@ -107,20 +111,48 @@ local function get_searchdirs(dirs)
   return vim.split(vim.fn.expand(dirs), ',')
 end
 
-function M.live_grep(dirs, opts)
+function M.live_grep(dirs, opts, on_choice)
   opts = opts or {}
   local search_dirs = get_searchdirs(dirs)
   opts.search_dirs = search_dirs
-  opts.prompt_title = 'Grep: '.. vim.inspect(search_dirs)
-  return require('telescope.builtin').live_grep(opts)
+  opts.prompt_title = 'Grep: '.. table.concat(search_dirs)
+  if on_choice then
+    opts.attach_mappings = function(prompt_bufnr)
+      telescope_actions.select_default:replace(function()
+        local selection = telescope_action_state.get_selected_entry()
+        if selection == nil then return end
+        telescope_actions.close(prompt_bufnr)
+        on_choice(selection.path)
+      end)
+      return true
+    end
+  end
+  telescope_builtin.live_grep(opts)
 end
 
-function M.find_files(dirs, opts)
+function M.find_files(dirs, opts, on_choice)
+  if dirs == 'plugins' then dirs = vim.fn.stdpath('data') .. '/site/pack/' end
+
   opts = opts or {}
-  local search_dirs = get_searchdirs(dirs)
+  local search_dirs = dirs
+  if type(dirs) == 'string' then search_dirs = get_searchdirs(dirs) end
   opts.search_dirs = search_dirs
-  opts.prompt_title = 'Files: '.. vim.inspect(search_dirs)
-  return require('telescope.builtin').find_files(opts)
+  opts.prompt_title = 'Find: '.. table.concat(search_dirs)
+  if on_choice then
+    opts.attach_mappings = function(prompt_bufnr)
+      telescope_actions.select_default:replace(function()
+        local selection = telescope_action_state.get_selected_entry()
+        if not selection then
+          vim.notify('Nothing selected', vim.log.levels.WARN)
+          return
+        end
+        telescope_actions.close(prompt_bufnr)
+        on_choice(selection.path)
+      end)
+      return true
+    end
+  end
+  telescope_builtin.find_files(opts)
 end