navigation-file: fix lint issues

This commit is contained in:
Tomas Mirchev 2025-10-15 22:22:44 +03:00
parent 9b8bd80cde
commit 01ae211cfc

View File

@ -1,37 +1,36 @@
-- Minimal fuzzy finder + content search in pure Neovim Lua
-- Requires: optional `fdfind` or `fd`, optional `rg` (ripgrep)
-- Minimal fuzzy finder + content search for Neovim 0.11+
-- Optional: `fdfind` or `fd` for file listing, `rg` for text search
local Fuzzy = {}
--------------------------------------------------------------------
-- 🧩 Helper: collect file list (fd > globpath)
-- 🧩 Helpers
--------------------------------------------------------------------
-- Collect all files (try fdfind/fd first, then globpath)
local function get_file_list()
local handle = io.popen('fdfind --type f 2>/dev/null || fd --type f 2>/dev/null')
if handle then
local result = handle:read('*a')
handle:close()
if result ~= '' then
if result and result ~= '' then
return vim.split(result, '\n', { trimempty = true })
end
end
return vim.fn.globpath('.', '**/*', 0, 1)
return vim.fn.globpath('.', '**/*', false, true)
end
--------------------------------------------------------------------
-- 🧩 Helper: open a floating pair of windows (input + results)
--------------------------------------------------------------------
-- Create the input/results floating windows
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)
-- 🧩 Mark both buffers as temporary
-- mark both buffers as scratch/unlisted
for _, b in ipairs({ input_buf, result_buf }) do
vim.bo[b].bufhidden = 'wipe'
vim.bo[b].buflisted = false
vim.bo[b].swapfile = false
end
vim.bo[input_buf].buftype = 'prompt'
vim.bo[result_buf].buftype = 'nofile'
@ -65,15 +64,22 @@ local function open_float(prompt)
end
--------------------------------------------------------------------
-- 🔵 Highlight selection
-- 🔵 Highlight current selection (using extmark, not deprecated API)
--------------------------------------------------------------------
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 not self.ns_id then
self.ns_id = vim.api.nvim_create_namespace('FuzzyHighlight')
end
vim.api.nvim_buf_clear_namespace(self.result_buf, self.ns_id, 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)
vim.api.nvim_buf_set_extmark(self.result_buf, self.ns_id, self.cursor - 1, 0, {
end_line = self.cursor,
hl_group = 'Visual',
hl_eol = true,
})
end
end
@ -81,17 +87,17 @@ 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)
local wins = { Fuzzy.input_win, Fuzzy.result_win }
for _, win in ipairs(wins) do
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end
Fuzzy.active = false
end
--------------------------------------------------------------------
-- 🟢 File finder (fdfind + fuzzy match)
-- 🟢 Fuzzy file finder (using fd or globpath)
--------------------------------------------------------------------
function Fuzzy.open()
if Fuzzy.active then
@ -118,24 +124,21 @@ function Fuzzy.open()
end
local display = {}
for i = 1, math.min(#Fuzzy.matches, 50) do
table.insert(display, Fuzzy.matches[i])
display[#display + 1] = 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' }, {
buffer = Fuzzy.input_buf,
callback = function()
local text = vim.fn.getline('.')
text = text:gsub('^Search:%s*', '')
local text = vim.fn.getline('.'):gsub('^Search:%s*', '')
update_results(text)
end,
})
-- Keymaps
vim.keymap.set('i', '<C-n>', function()
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches)
Fuzzy:highlight_selection()
@ -150,7 +153,7 @@ function Fuzzy.open()
local choice = Fuzzy.matches[Fuzzy.cursor]
if choice then
Fuzzy.close()
vim.cmd('edit ' .. vim.fn.fnameescape(choice))
vim.cmd.edit(vim.fn.fnameescape(choice))
end
end, { buffer = Fuzzy.input_buf })
@ -158,11 +161,11 @@ function Fuzzy.open()
Fuzzy.close()
end, { buffer = Fuzzy.input_buf })
vim.cmd('startinsert')
vim.cmd.startinsert()
end
--------------------------------------------------------------------
-- 🟣 Ripgrep-powered content search
-- 🟣 Ripgrep-based content search
--------------------------------------------------------------------
function Fuzzy.open_grep()
if Fuzzy.active then
@ -192,7 +195,7 @@ function Fuzzy.open_grep()
end
local display = {}
for i = 1, math.min(#Fuzzy.matches, 50) do
table.insert(display, Fuzzy.matches[i])
display[#display + 1] = Fuzzy.matches[i]
end
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
Fuzzy.cursor = 1
@ -202,13 +205,11 @@ function Fuzzy.open_grep()
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
buffer = Fuzzy.input_buf,
callback = function()
local text = vim.fn.getline('.')
text = text:gsub('^Grep:%s*', '')
local text = vim.fn.getline('.'):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()
@ -223,10 +224,9 @@ function Fuzzy.open_grep()
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
local file, lnum = parts[1], tonumber(parts[2]) or 1
Fuzzy.close()
vim.cmd('edit ' .. vim.fn.fnameescape(file))
vim.cmd.edit(vim.fn.fnameescape(file))
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
end
end, { buffer = Fuzzy.input_buf })
@ -235,7 +235,7 @@ function Fuzzy.open_grep()
Fuzzy.close()
end, { buffer = Fuzzy.input_buf })
vim.cmd('startinsert')
vim.cmd.startinsert()
end
--------------------------------------------------------------------
@ -249,10 +249,10 @@ vim.api.nvim_create_user_command('FuzzyGrep', function()
end, {})
vim.keymap.set('n', '<leader>f', function()
vim.cmd('FuzzyLive')
vim.cmd.FuzzyLive()
end, { desc = 'Open fuzzy file finder' })
vim.keymap.set('n', '<leader>g', function()
vim.cmd('FuzzyGrep')
vim.cmd.FuzzyGrep()
end, { desc = 'Search file contents with ripgrep' })
return Fuzzy