Skip to content

Commit fab4220

Browse files
committed
It's embarassing how much I relied on chatgpt but it's sort of working!
1 parent 35c6fde commit fab4220

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

init.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,75 @@ require('lazy').setup({
392392

393393
-- [[ Configure Telescope ]]
394394
-- See `:help telescope` and `:help telescope.setup()`
395+
396+
local pickers = require 'telescope.pickers'
397+
local finders = require 'telescope.finders'
398+
local conf = require('telescope.config').values
399+
function get_directory(path)
400+
return path:match '^(.*[/\\])' or ''
401+
end
402+
403+
local dynamic_find_files = function(opts)
404+
opts = opts or {}
405+
local current_buffer_path = vim.api.nvim_buf_get_name(0)
406+
opts.default_text = vim.fn.fnamemodify(current_buffer_path, ':h') .. '/'
407+
408+
pickers
409+
.new(opts, {
410+
prompt_title = 'Dynamic Find Files',
411+
finder = finders.new_dynamic {
412+
fn = function(prompt)
413+
-- return { 'a', 'b', prompt }
414+
local files = {}
415+
416+
local path = get_directory(prompt)
417+
-- TODO: Now I have to install luarocks and luafilesystem for this?
418+
for entry in require('lfs').dir(path) do
419+
if entry ~= '.' and entry ~= '..' then
420+
local full_path = path .. entry
421+
local attr = lfs.attributes(full_path)
422+
if attr then
423+
if attr.mode == 'directory' then
424+
table.insert(files, full_path .. '/')
425+
elseif attr.mode == 'file' then
426+
table.insert(files, full_path)
427+
end
428+
end
429+
end
430+
end
431+
return files
432+
end,
433+
},
434+
sorter = conf.file_sorter(opts),
435+
attach_mappings = function(prompt_bufnr, map)
436+
map('i', '<Tab>', function()
437+
local picker = require('telescope.actions.state').get_current_picker(prompt_bufnr)
438+
local selection = picker:get_selection()
439+
if selection then
440+
local new_prompt = selection[1] or selection
441+
-- TODO: set_prompt is undocumented
442+
picker:set_prompt(new_prompt, true)
443+
end
444+
end)
445+
map('i', '<Backspace>', function()
446+
local picker = require('telescope.actions.state').get_current_picker(prompt_bufnr)
447+
local curr_prompt = picker:_get_prompt()
448+
if curr_prompt == '' then
449+
return
450+
elseif curr_prompt:sub(-1) == '/' then
451+
local without_trailing = curr_prompt:sub(1, -2)
452+
local head = without_trailing:match '^(.*)/'
453+
picker:set_prompt((head or '') .. '/', true)
454+
else
455+
return picker:set_prompt(curr_prompt:sub(1, -2), true)
456+
end
457+
end)
458+
return true
459+
end,
460+
})
461+
:find()
462+
end
463+
395464
require('telescope').setup {
396465
-- You can put your default mappings / updates / etc. in here
397466
-- All the info you're looking for is in `:help telescope.setup()`
@@ -424,6 +493,7 @@ require('lazy').setup({
424493
vim.keymap.set('n', '<leader>sr', builtin.resume, { desc = '[S]earch [R]esume' })
425494
vim.keymap.set('n', '<leader>s.', builtin.oldfiles, { desc = '[S]earch Recent Files ("." for repeat)' })
426495
vim.keymap.set('n', '<leader>ff', builtin.find_files, { desc = '[S]earch [F]iles' })
496+
vim.keymap.set('n', '<leader>mm', dynamic_find_files, { desc = '[S]earch [F]iles' })
427497
vim.keymap.set('n', '<leader>pf', builtin.git_files, { desc = '[P]earch [F]iles' })
428498
vim.keymap.set('n', '<leader>bb', builtin.buffers, { desc = '[B]earch [B]uffers' })
429499
vim.keymap.set('n', '<leader>*', builtin.live_grep, { desc = '[S]earch by [G]rep' })

0 commit comments

Comments
 (0)