navigation-file: use ripgrep
This commit is contained in:
parent
ca5c511a10
commit
6ede4f1a7f
@ -1,42 +1,36 @@
|
|||||||
vim.keymap.set('n', '<leader>f', function()
|
-- Minimal fuzzy finder + content search in pure Neovim Lua
|
||||||
vim.cmd('FuzzyLive')
|
-- Requires: optional `fdfind` or `fd`, optional `rg` (ripgrep)
|
||||||
end, { desc = 'Open fuzzy file finder' })
|
|
||||||
|
|
||||||
-- Minimal realtime fuzzy finder in pure Neovim Lua (no plugins)
|
|
||||||
local Fuzzy = {}
|
local Fuzzy = {}
|
||||||
|
|
||||||
function Fuzzy.open()
|
--------------------------------------------------------------------
|
||||||
if Fuzzy.active then
|
-- 🧩 Helper: collect file list (fd > globpath)
|
||||||
Fuzzy.close()
|
--------------------------------------------------------------------
|
||||||
end
|
local function get_file_list()
|
||||||
Fuzzy.active = true
|
local handle = io.popen('fdfind --type f 2>/dev/null || fd --type f 2>/dev/null')
|
||||||
|
|
||||||
-- Collect all files once (cached for speed)
|
|
||||||
-- Fuzzy.files = vim.fn.globpath('.', '**/*', 0, 1)
|
|
||||||
|
|
||||||
local handle = io.popen('fdfind --type f --hidden --exclude .git')
|
|
||||||
if handle then
|
if handle then
|
||||||
local result = handle:read('*a')
|
local result = handle:read('*a')
|
||||||
handle:close()
|
handle:close()
|
||||||
Fuzzy.files = vim.split(result, '\n', { trimempty = true })
|
if result ~= '' then
|
||||||
else
|
return vim.split(result, '\n', { trimempty = true })
|
||||||
Fuzzy.files = {}
|
end
|
||||||
end
|
end
|
||||||
Fuzzy.matches = Fuzzy.files
|
return vim.fn.globpath('.', '**/*', 0, 1)
|
||||||
Fuzzy.cursor = 1
|
end
|
||||||
|
|
||||||
-- Create input and result buffers
|
--------------------------------------------------------------------
|
||||||
Fuzzy.input_buf = vim.api.nvim_create_buf(false, true)
|
-- 🧩 Helper: open a floating pair of windows (input + results)
|
||||||
Fuzzy.result_buf = vim.api.nvim_create_buf(false, true)
|
--------------------------------------------------------------------
|
||||||
|
local function open_float(prompt)
|
||||||
|
local input_buf = vim.api.nvim_create_buf(false, true)
|
||||||
|
local result_buf = vim.api.nvim_create_buf(false, true)
|
||||||
|
|
||||||
-- Floating window sizes
|
local width = math.floor(vim.o.columns * 0.7)
|
||||||
local width = math.floor(vim.o.columns * 0.6)
|
local height = 20
|
||||||
local height = 15
|
|
||||||
local row = math.floor((vim.o.lines - height) / 2)
|
local row = math.floor((vim.o.lines - height) / 2)
|
||||||
local col = math.floor((vim.o.columns - width) / 2)
|
local col = math.floor((vim.o.columns - width) / 2)
|
||||||
|
|
||||||
-- Input window (top)
|
local input_win = vim.api.nvim_open_win(input_buf, true, {
|
||||||
Fuzzy.input_win = vim.api.nvim_open_win(Fuzzy.input_buf, true, {
|
|
||||||
relative = 'editor',
|
relative = 'editor',
|
||||||
row = row,
|
row = row,
|
||||||
col = col,
|
col = col,
|
||||||
@ -45,11 +39,10 @@ function Fuzzy.open()
|
|||||||
style = 'minimal',
|
style = 'minimal',
|
||||||
border = 'rounded',
|
border = 'rounded',
|
||||||
})
|
})
|
||||||
vim.api.nvim_buf_set_option(Fuzzy.input_buf, 'buftype', 'prompt')
|
vim.api.nvim_buf_set_option(input_buf, 'buftype', 'prompt')
|
||||||
vim.fn.prompt_setprompt(Fuzzy.input_buf, 'Search: ')
|
vim.fn.prompt_setprompt(input_buf, prompt)
|
||||||
|
|
||||||
-- Results window (below)
|
local result_win = vim.api.nvim_open_win(result_buf, false, {
|
||||||
Fuzzy.result_win = vim.api.nvim_open_win(Fuzzy.result_buf, false, {
|
|
||||||
relative = 'editor',
|
relative = 'editor',
|
||||||
row = row + 2,
|
row = row + 2,
|
||||||
col = col,
|
col = col,
|
||||||
@ -59,36 +52,87 @@ function Fuzzy.open()
|
|||||||
border = 'single',
|
border = 'single',
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Draw initial result list
|
return input_buf, result_buf, input_win, result_win
|
||||||
Fuzzy:update_results('')
|
end
|
||||||
|
|
||||||
------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
-- 🟢 Real-time fuzzy filtering
|
-- 🔵 Highlight selection
|
||||||
------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
|
function Fuzzy:highlight_selection()
|
||||||
|
if not self.result_buf then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.api.nvim_buf_clear_namespace(self.result_buf, 0, 0, -1)
|
||||||
|
if self.matches and self.matches[self.cursor] then
|
||||||
|
vim.api.nvim_buf_add_highlight(self.result_buf, 0, 'Visual', self.cursor - 1, 0, -1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
-- 🔴 Close all floating windows
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
function Fuzzy.close()
|
||||||
|
if Fuzzy.input_win and vim.api.nvim_win_is_valid(Fuzzy.input_win) then
|
||||||
|
vim.api.nvim_win_close(Fuzzy.input_win, true)
|
||||||
|
end
|
||||||
|
if Fuzzy.result_win and vim.api.nvim_win_is_valid(Fuzzy.result_win) then
|
||||||
|
vim.api.nvim_win_close(Fuzzy.result_win, true)
|
||||||
|
end
|
||||||
|
Fuzzy.active = false
|
||||||
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
-- 🟢 File finder (fdfind + fuzzy match)
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
function Fuzzy.open()
|
||||||
|
if Fuzzy.active then
|
||||||
|
Fuzzy.close()
|
||||||
|
end
|
||||||
|
Fuzzy.active = true
|
||||||
|
|
||||||
|
Fuzzy.files = get_file_list()
|
||||||
|
Fuzzy.matches = Fuzzy.files
|
||||||
|
Fuzzy.cursor = 1
|
||||||
|
|
||||||
|
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Search: ')
|
||||||
|
|
||||||
|
local function update_results(text)
|
||||||
|
if text == '' then
|
||||||
|
Fuzzy.matches = Fuzzy.files
|
||||||
|
else
|
||||||
|
Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text)
|
||||||
|
end
|
||||||
|
if #Fuzzy.matches == 0 then
|
||||||
|
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
|
||||||
|
Fuzzy.cursor = 1
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local display = {}
|
||||||
|
for i = 1, math.min(#Fuzzy.matches, 50) do
|
||||||
|
table.insert(display, Fuzzy.matches[i])
|
||||||
|
end
|
||||||
|
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
|
||||||
|
Fuzzy.cursor = 1
|
||||||
|
Fuzzy:highlight_selection()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Live updates
|
||||||
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
|
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
|
||||||
buffer = Fuzzy.input_buf,
|
buffer = Fuzzy.input_buf,
|
||||||
callback = function()
|
callback = function()
|
||||||
local text = vim.fn.getline('.')
|
local text = vim.fn.getline('.')
|
||||||
text = text:gsub('^Search:%s*', '')
|
text = text:gsub('^Search:%s*', '')
|
||||||
Fuzzy:update_results(text)
|
update_results(text)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
------------------------------------------------------------------
|
-- Keymaps
|
||||||
-- 🟢 Keybindings in input window
|
|
||||||
------------------------------------------------------------------
|
|
||||||
vim.keymap.set('i', '<C-n>', function()
|
vim.keymap.set('i', '<C-n>', function()
|
||||||
if not Fuzzy.matches or #Fuzzy.matches == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches)
|
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches)
|
||||||
Fuzzy:highlight_selection()
|
Fuzzy:highlight_selection()
|
||||||
end, { buffer = Fuzzy.input_buf })
|
end, { buffer = Fuzzy.input_buf })
|
||||||
|
|
||||||
vim.keymap.set('i', '<C-p>', function()
|
vim.keymap.set('i', '<C-p>', function()
|
||||||
if not Fuzzy.matches or #Fuzzy.matches == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
Fuzzy.cursor = math.max(Fuzzy.cursor - 1, 1)
|
Fuzzy.cursor = math.max(Fuzzy.cursor - 1, 1)
|
||||||
Fuzzy:highlight_selection()
|
Fuzzy:highlight_selection()
|
||||||
end, { buffer = Fuzzy.input_buf })
|
end, { buffer = Fuzzy.input_buf })
|
||||||
@ -109,63 +153,97 @@ function Fuzzy.open()
|
|||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
-- 🔵 Core update + display logic
|
-- 🟣 Ripgrep-powered content search
|
||||||
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
function Fuzzy:update_results(text)
|
function Fuzzy.open_grep()
|
||||||
if text == '' then
|
if Fuzzy.active then
|
||||||
Fuzzy.matches = Fuzzy.files
|
Fuzzy.close()
|
||||||
else
|
|
||||||
Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text)
|
|
||||||
end
|
end
|
||||||
|
Fuzzy.active = true
|
||||||
|
|
||||||
if #Fuzzy.matches == 0 then
|
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Grep: ')
|
||||||
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
|
Fuzzy.matches = {}
|
||||||
Fuzzy.cursor = 1
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Limit to first 50 results for performance
|
|
||||||
local display = {}
|
|
||||||
for i = 1, math.min(#Fuzzy.matches, 50) do
|
|
||||||
table.insert(display, Fuzzy.matches[i])
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
|
|
||||||
Fuzzy.cursor = 1
|
Fuzzy.cursor = 1
|
||||||
Fuzzy:highlight_selection()
|
|
||||||
|
local function run_grep(query)
|
||||||
|
if query == '' then
|
||||||
|
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- type to search --' })
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local handle = io.popen('rg --vimgrep --hidden --smart-case ' .. vim.fn.shellescape(query))
|
||||||
|
if not handle then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local result = handle:read('*a')
|
||||||
|
handle:close()
|
||||||
|
Fuzzy.matches = vim.split(result, '\n', { trimempty = true })
|
||||||
|
if #Fuzzy.matches == 0 then
|
||||||
|
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local display = {}
|
||||||
|
for i = 1, math.min(#Fuzzy.matches, 50) do
|
||||||
|
table.insert(display, Fuzzy.matches[i])
|
||||||
|
end
|
||||||
|
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
|
||||||
|
Fuzzy.cursor = 1
|
||||||
|
Fuzzy:highlight_selection()
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
|
||||||
|
buffer = Fuzzy.input_buf,
|
||||||
|
callback = function()
|
||||||
|
local text = vim.fn.getline('.')
|
||||||
|
text = text:gsub('^Grep:%s*', '')
|
||||||
|
run_grep(text)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Navigation
|
||||||
|
vim.keymap.set('i', '<C-n>', function()
|
||||||
|
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches)
|
||||||
|
Fuzzy:highlight_selection()
|
||||||
|
end, { buffer = Fuzzy.input_buf })
|
||||||
|
|
||||||
|
vim.keymap.set('i', '<C-p>', function()
|
||||||
|
Fuzzy.cursor = math.max(Fuzzy.cursor - 1, 1)
|
||||||
|
Fuzzy:highlight_selection()
|
||||||
|
end, { buffer = Fuzzy.input_buf })
|
||||||
|
|
||||||
|
vim.keymap.set('i', '<CR>', function()
|
||||||
|
local line = Fuzzy.matches[Fuzzy.cursor]
|
||||||
|
if line then
|
||||||
|
local parts = vim.split(line, ':')
|
||||||
|
local file = parts[1]
|
||||||
|
local lnum = tonumber(parts[2]) or 1
|
||||||
|
Fuzzy.close()
|
||||||
|
vim.cmd('edit ' .. vim.fn.fnameescape(file))
|
||||||
|
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
|
||||||
|
end
|
||||||
|
end, { buffer = Fuzzy.input_buf })
|
||||||
|
|
||||||
|
vim.keymap.set('i', '<Esc>', function()
|
||||||
|
Fuzzy.close()
|
||||||
|
end, { buffer = Fuzzy.input_buf })
|
||||||
|
|
||||||
|
vim.cmd('startinsert')
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
-- 🔵 Highlight the selected file
|
-- 🧩 Commands & Keymaps
|
||||||
--------------------------------------------------------------------
|
|
||||||
function Fuzzy:highlight_selection()
|
|
||||||
if not Fuzzy.result_buf then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
vim.api.nvim_buf_clear_namespace(Fuzzy.result_buf, 0, 0, -1)
|
|
||||||
if Fuzzy.matches and Fuzzy.matches[Fuzzy.cursor] then
|
|
||||||
vim.api.nvim_buf_add_highlight(Fuzzy.result_buf, 0, 'Visual', Fuzzy.cursor - 1, 0, -1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--------------------------------------------------------------------
|
|
||||||
-- 🔴 Clean up
|
|
||||||
--------------------------------------------------------------------
|
|
||||||
function Fuzzy.close()
|
|
||||||
if Fuzzy.input_win and vim.api.nvim_win_is_valid(Fuzzy.input_win) then
|
|
||||||
vim.api.nvim_win_close(Fuzzy.input_win, true)
|
|
||||||
end
|
|
||||||
if Fuzzy.result_win and vim.api.nvim_win_is_valid(Fuzzy.result_win) then
|
|
||||||
vim.api.nvim_win_close(Fuzzy.result_win, true)
|
|
||||||
end
|
|
||||||
Fuzzy.active = false
|
|
||||||
end
|
|
||||||
|
|
||||||
--------------------------------------------------------------------
|
|
||||||
-- 🧩 Command to launch
|
|
||||||
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
vim.api.nvim_create_user_command('FuzzyLive', function()
|
vim.api.nvim_create_user_command('FuzzyLive', function()
|
||||||
Fuzzy.open()
|
Fuzzy.open()
|
||||||
end, {})
|
end, {})
|
||||||
|
vim.api.nvim_create_user_command('FuzzyGrep', function()
|
||||||
|
Fuzzy.open_grep()
|
||||||
|
end, {})
|
||||||
|
|
||||||
|
vim.keymap.set('n', '<leader>f', function()
|
||||||
|
vim.cmd('FuzzyLive')
|
||||||
|
end, { desc = 'Open fuzzy file finder' })
|
||||||
|
vim.keymap.set('n', '<leader>g', function()
|
||||||
|
vim.cmd('FuzzyGrep')
|
||||||
|
end, { desc = 'Search file contents with ripgrep' })
|
||||||
|
|
||||||
return Fuzzy
|
return Fuzzy
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user