dotfiles/config/linux-dev/nvim/lua/custom/navigation.lua

172 lines
5.0 KiB
Lua

vim.keymap.set('n', '<leader>f', function()
vim.cmd('FuzzyLive')
end, { desc = 'Open fuzzy file finder' })
-- Minimal realtime fuzzy finder in pure Neovim Lua (no plugins)
local Fuzzy = {}
function Fuzzy.open()
if Fuzzy.active then
Fuzzy.close()
end
Fuzzy.active = true
-- 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
local result = handle:read('*a')
handle:close()
Fuzzy.files = vim.split(result, '\n', { trimempty = true })
else
Fuzzy.files = {}
end
Fuzzy.matches = Fuzzy.files
Fuzzy.cursor = 1
-- Create input and result buffers
Fuzzy.input_buf = vim.api.nvim_create_buf(false, true)
Fuzzy.result_buf = vim.api.nvim_create_buf(false, true)
-- Floating window sizes
local width = math.floor(vim.o.columns * 0.6)
local height = 15
local row = math.floor((vim.o.lines - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
-- Input window (top)
Fuzzy.input_win = vim.api.nvim_open_win(Fuzzy.input_buf, true, {
relative = 'editor',
row = row,
col = col,
width = width,
height = 1,
style = 'minimal',
border = 'rounded',
})
vim.api.nvim_buf_set_option(Fuzzy.input_buf, 'buftype', 'prompt')
vim.fn.prompt_setprompt(Fuzzy.input_buf, 'Search: ')
-- Results window (below)
Fuzzy.result_win = vim.api.nvim_open_win(Fuzzy.result_buf, false, {
relative = 'editor',
row = row + 2,
col = col,
width = width,
height = height - 2,
style = 'minimal',
border = 'single',
})
-- Draw initial result list
Fuzzy:update_results('')
------------------------------------------------------------------
-- 🟢 Real-time fuzzy filtering
------------------------------------------------------------------
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
buffer = Fuzzy.input_buf,
callback = function()
local text = vim.fn.getline('.')
text = text:gsub('^Search:%s*', '')
Fuzzy:update_results(text)
end,
})
------------------------------------------------------------------
-- 🟢 Keybindings in input window
------------------------------------------------------------------
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:highlight_selection()
end, { buffer = Fuzzy.input_buf })
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:highlight_selection()
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<CR>', function()
local choice = Fuzzy.matches[Fuzzy.cursor]
if choice then
Fuzzy.close()
vim.cmd('edit ' .. vim.fn.fnameescape(choice))
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<Esc>', function()
Fuzzy.close()
end, { buffer = Fuzzy.input_buf })
vim.cmd('startinsert')
end
--------------------------------------------------------------------
-- 🔵 Core update + display logic
--------------------------------------------------------------------
function Fuzzy: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
-- 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:highlight_selection()
end
--------------------------------------------------------------------
-- 🔵 Highlight the selected file
--------------------------------------------------------------------
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()
Fuzzy.open()
end, {})
return Fuzzy