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