summary refs log tree commit diff
path: root/.vim/lua/internal/find.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2024-05-15 09:00:31 +0200
committerRobert Günzler <r@gnzler.io>2024-05-15 09:00:31 +0200
commit3dbe74f2eef627e2dc19b79fe06753d5dc09003f (patch)
treeb78d1cdab62cec9ed4bbe35447108547b935ef03 /.vim/lua/internal/find.lua
parent0ab72c1113139c196115c974eadaded449002c32 (diff)
update nvim
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
-rw-r--r--.vim/lua/internal/find.lua106
1 files changed, 106 insertions, 0 deletions
diff --git a/.vim/lua/internal/find.lua b/.vim/lua/internal/find.lua
new file mode 100644
index 0000000..32bdea5
--- /dev/null
+++ b/.vim/lua/internal/find.lua
@@ -0,0 +1,106 @@
+local M = {}
+
+local get_searchdirs = function(dirs)
+  dirs = dirs or {}
+  if type(dirs) == 'table' and #dirs > 0 then
+    return dirs
+  elseif type(dirs) == 'string' and (dirs == ':workspace') then
+    dirs = vim.lsp.buf.list_workspace_folders()
+  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 #dirs == 0 and (ft == 'dirbuf' or ft == 'NvimTree' or ft == 'alpha') then
+    dirs = { '%:p:h' }
+  end
+  -- ask user
+  if #dirs == 0 then
+    vim.ui.input({ prompt = 'Search directories: ', default = '%:p:h' }, function(result)
+      dirs = { result }
+    end)
+  end
+  -- default to something reasonable
+  if #dirs == 0 then
+    dirs = { '%:p:h' }
+  end
+  -- finally return
+  return dirs
+end
+
+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 search_shim(fn, search_dirs, opts, cb)
+  opts = opts or {}
+  opts.search_dirs = get_searchdirs(search_dirs)
+  opts.prompt_title = vim.fn.expand(table.concat(opts.search_dirs, ','))
+  onchoice(opts, cb)
+  fn(opts)
+end
+
+function M.live_grep(search_dirs, opts, cb)
+  search_shim(require('telescope.builtin').live_grep, search_dirs, opts, cb)
+end
+
+function M.find_files(search_dirs, opts, cb)
+  -- convenience aliases
+  if type(search_dirs) == 'string' and search_dirs == 'data' then
+    search_dirs = vim.fn.stdpath('data')
+  end
+  search_shim(require('telescope.builtin').find_files, search_dirs, opts, cb)
+end
+
+function M.setup(opts)
+  vim.keymap.set('n', ';f', function()
+    vim.notify('use gsf', vim.log.levels.ERROR)
+  end, { desc = 'find files' })
+  vim.keymap.set('n', ';g', function()
+    vim.notify('use gsg', vim.log.levels.ERROR)
+  end, { desc = 'live grep' })
+
+  vim.keymap.set('n', 'gsg', function()
+    M.live_grep(':workspace')
+  end, { desc = 'live grep in workspace' })
+  vim.keymap.set('n', 'gsG', M.live_grep, { desc = 'live grep ask' })
+  vim.keymap.set('n', 'gs-', function()
+    M.live_grep('%:p:h')
+  end, { desc = 'live grep in parent' })
+
+  vim.keymap.set('n', 'gsf', function()
+    M.find_files(':workspace')
+  end, { desc = 'find files in workspace' })
+  vim.keymap.set('n', 'gsF', M.find_files, { desc = 'find files ask' })
+
+  vim.api.nvim_create_user_command('Grep', function(o)
+    M.live_grep(table.concat(o.fargs))
+  end, {
+    desc = 'live grep (rg)',
+    nargs = '*',
+    complete = 'dir',
+  })
+
+  vim.api.nvim_create_user_command('Find', function(o)
+    M.find_files(table.concat(o.fargs))
+  end, {
+    desc = 'find files (fd)',
+    nargs = '*',
+    complete = 'dir',
+  })
+end
+
+return M