summary refs log tree commit diff
path: root/.vim/lua/internal
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-12-02 18:26:15 +0100
committerRobert Günzler <r@gnzler.io>2022-12-02 18:26:15 +0100
commit6d7c8da2ee4380d1104423ec66a651c9b138868d (patch)
treef28904564b63aee1bd7a8ce6b6e3d37f773b7b16 /.vim/lua/internal
parent277766ec23da6c7a2399d60416aeb04af3b72aba (diff)
vim/misc: improve grep/find
Diffstat (limited to '.vim/lua/internal')
-rw-r--r--.vim/lua/internal/misc.lua79
1 files changed, 38 insertions, 41 deletions
diff --git a/.vim/lua/internal/misc.lua b/.vim/lua/internal/misc.lua
index 6f79508..a3045f4 100644
--- a/.vim/lua/internal/misc.lua
+++ b/.vim/lua/internal/misc.lua
@@ -93,66 +93,63 @@ function M.sort_lines(_, preview_ns, preview_bufnr)
   end
 end
 
-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)
+  if type(dirs) == 'table' and #dirs > 0 then
+    return dirs
+  elseif type(dirs) == 'string' and not (dirs == "") then
+    dirs = vim.split(dirs, ',')
+  end
   -- 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
-    dirs = '%:p:h'
+  if #dirs == 0 and (ft == 'dirbuf' or ft == 'alpha') then
+    dirs = {'%:p:h'}
   end
-  if not dirs then
-    vim.ui.input('Search directories: ', function(result) dirs = result end)
+  -- ask user
+  if #dirs == 0 then
+    vim.ui.input({prompt = 'Search directories: '}, function(result) dirs = {result} end)
   end
   -- default to something reasonable
-  if not dirs then dirs = '%:p:h' end
-  return vim.split(vim.fn.expand(dirs), ',')
+  if #dirs == 0 then dirs = {'%:p:h'} end
+  -- finally return
+  for i = 1, #dirs do
+    dirs[i] = vim.fn.expand(dirs[i])
+  end
+  return dirs
 end
 
-function M.live_grep(dirs, opts, on_choice)
+function onchoice(opts, cb)
+  if not cb then return end
+  opts.attach_mappings = function(prompt_bufnr)
+    require('telescope.actions').select_default:replace(function()
+      local selection = require('telescope.actions.state').get_selected_entry()
+      if selection == nil then return end
+      require('telescope.actions').close(prompt_bufnr)
+      on_choice(selection.path)
+    end)
+    return true
+  end
+end
+
+function M.live_grep(dirs, opts, cb)
   opts = opts or {}
   local search_dirs = get_searchdirs(dirs)
   opts.search_dirs = search_dirs
-  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)
+  opts.prompt_title = 'Grep: '.. vim.inspect(search_dirs)
+  onchoice(opts, cb)
+  require('telescope.builtin').live_grep(opts)
 end
 
-function M.find_files(dirs, opts, on_choice)
+function M.find_files(dirs, opts, cb)
+  -- convenience aliases
   if dirs == 'plugins' then dirs = vim.fn.stdpath('data') .. '/site/pack/' end
 
   opts = opts or {}
   local search_dirs = dirs
   if type(dirs) == 'string' then search_dirs = get_searchdirs(dirs) end
   opts.search_dirs = search_dirs
-  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)
+  opts.prompt_title = 'Find: '.. vim.inspect(search_dirs)
+  onchoice(opts, cb)
+  require('telescope.builtin').find_files(opts)
 end