This commit is contained in:
Tomas Mirchev 2025-10-18 23:59:20 +03:00
parent 01ae211cfc
commit 9f6ba885b5
3 changed files with 142 additions and 63 deletions

View File

@ -25,22 +25,16 @@ vim.g.have_nerd_font = true
vim.opt.termguicolors = false vim.opt.termguicolors = false
vim.opt.textwidth = 100 vim.opt.textwidth = 100
-- vim.opt.colorcolumn = '+0' vim.opt.colorcolumn = '+0'
vim.opt.signcolumn = 'no' vim.opt.signcolumn = 'no'
vim.opt.number = true vim.opt.number = true
vim.opt.relativenumber = true vim.opt.relativenumber = true
vim.opt.cursorline = true vim.opt.cursorline = true
vim.opt.guicursor = 'n-v-i-c:block' vim.opt.guicursor = 'n-v-i-c:block'
-- vim.opt.statusline = ' %f %h%w%m%r%= ' vim.opt.statusline = '> %f %h%w%m%r %= [%l,%c-%L]'
vim.opt.statusline = '> %f %h%w%m%r %= %l,%c - %L '
vim.opt.fillchars:append({ stl = '', stlnc = '' })
vim.opt.winborder = 'rounded' vim.opt.winborder = 'rounded'
vim.opt.laststatus = 3 vim.opt.fillchars:append({ vert = '' })
vim.opt.showcmd = false
-- vim.opt.cmdheight = 0
-- Wrap -- Wrap
vim.opt.wrap = false vim.opt.wrap = false

View File

@ -1,5 +1,5 @@
-- Minimal fuzzy finder + content search for Neovim 0.11+ -- Minimal fuzzy finder + content search for Neovim 0.11+
-- Optional: `fdfind` or `fd` for file listing, `rg` for text search -- Optional: `fdfind` or `fd` for file listing, and `rg` (ripgrep) for text search.
local Fuzzy = {} local Fuzzy = {}
@ -20,7 +20,7 @@ local function get_file_list()
return vim.fn.globpath('.', '**/*', false, true) return vim.fn.globpath('.', '**/*', false, true)
end end
-- Create the input/results floating windows -- Create floating input + result windows
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)
@ -64,7 +64,7 @@ local function open_float(prompt)
end end
-------------------------------------------------------------------- --------------------------------------------------------------------
-- 🔵 Highlight current selection (using extmark, not deprecated API) -- 🔵 Highlight current selection
-------------------------------------------------------------------- --------------------------------------------------------------------
function Fuzzy:highlight_selection() function Fuzzy:highlight_selection()
if not self.result_buf then if not self.result_buf then
@ -75,11 +75,14 @@ function Fuzzy:highlight_selection()
end end
vim.api.nvim_buf_clear_namespace(self.result_buf, self.ns_id, 0, -1) 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_set_extmark(self.result_buf, self.ns_id, self.cursor - 1, 0, { local rel_cursor = self.cursor - (self.scroll or 0)
end_line = self.cursor, if rel_cursor >= 1 and rel_cursor <= self.page_size then
hl_group = 'Visual', vim.api.nvim_buf_set_extmark(self.result_buf, self.ns_id, rel_cursor - 1, 0, {
hl_eol = true, end_line = rel_cursor,
}) hl_group = 'Visual',
hl_eol = true,
})
end
end end
end end
@ -97,7 +100,7 @@ function Fuzzy.close()
end end
-------------------------------------------------------------------- --------------------------------------------------------------------
-- 🟢 Fuzzy file finder (using fd or globpath) -- 🟢 File finder
-------------------------------------------------------------------- --------------------------------------------------------------------
function Fuzzy.open() function Fuzzy.open()
if Fuzzy.active then if Fuzzy.active then
@ -108,27 +111,35 @@ function Fuzzy.open()
Fuzzy.files = get_file_list() Fuzzy.files = get_file_list()
Fuzzy.matches = Fuzzy.files Fuzzy.matches = Fuzzy.files
Fuzzy.cursor = 1 Fuzzy.cursor = 1
Fuzzy.scroll = 0
Fuzzy.page_size = 50
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Search: ') Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Search: ')
local function render_results()
local total = #Fuzzy.matches
if total == 0 then
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
return
end
local start_idx = Fuzzy.scroll + 1
local end_idx = math.min(start_idx + Fuzzy.page_size - 1, total)
local display = {}
for i = start_idx, end_idx do
display[#display + 1] = Fuzzy.matches[i]
end
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
Fuzzy:highlight_selection()
end
local function update_results(text) local function update_results(text)
if text == '' then if text == '' then
Fuzzy.matches = Fuzzy.files Fuzzy.matches = Fuzzy.files
else else
Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text) Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text)
end end
if #Fuzzy.matches == 0 then Fuzzy.cursor, Fuzzy.scroll = 1, 0
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' }) render_results()
Fuzzy.cursor = 1
return
end
local display = {}
for i = 1, math.min(#Fuzzy.matches, 50) do
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 end
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
@ -140,13 +151,23 @@ function Fuzzy.open()
}) })
vim.keymap.set('i', '<C-n>', function() vim.keymap.set('i', '<C-n>', function()
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches) if Fuzzy.cursor < #Fuzzy.matches then
Fuzzy:highlight_selection() Fuzzy.cursor = Fuzzy.cursor + 1
if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then
Fuzzy.scroll = Fuzzy.scroll + 1
end
render_results()
end
end, { buffer = Fuzzy.input_buf }) end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<C-p>', function() vim.keymap.set('i', '<C-p>', function()
Fuzzy.cursor = math.max(Fuzzy.cursor - 1, 1) if Fuzzy.cursor > 1 then
Fuzzy:highlight_selection() Fuzzy.cursor = Fuzzy.cursor - 1
if Fuzzy.cursor <= Fuzzy.scroll then
Fuzzy.scroll = math.max(Fuzzy.scroll - 1, 0)
end
render_results()
end
end, { buffer = Fuzzy.input_buf }) end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<CR>', function() vim.keymap.set('i', '<CR>', function()
@ -157,15 +178,16 @@ function Fuzzy.open()
end end
end, { buffer = Fuzzy.input_buf }) end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<Esc>', function() vim.keymap.set('i', '<Esc>', Fuzzy.close, { buffer = Fuzzy.input_buf })
Fuzzy.close() vim.keymap.set('i', '<C-c>', Fuzzy.close, { buffer = Fuzzy.input_buf })
end, { buffer = Fuzzy.input_buf }) vim.keymap.set('n', '<Esc>', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.keymap.set('n', 'q', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.cmd.startinsert() vim.cmd.startinsert()
end end
-------------------------------------------------------------------- --------------------------------------------------------------------
-- 🟣 Ripgrep-based content search -- 🟣 Ripgrep-based content search (scrolling + match highlighting)
-------------------------------------------------------------------- --------------------------------------------------------------------
function Fuzzy.open_grep() function Fuzzy.open_grep()
if Fuzzy.active then if Fuzzy.active then
@ -174,8 +196,48 @@ function Fuzzy.open_grep()
Fuzzy.active = true Fuzzy.active = true
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Grep: ') Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Grep: ')
Fuzzy.matches = {} Fuzzy.matches, Fuzzy.cursor, Fuzzy.scroll = {}, 1, 0
Fuzzy.cursor = 1 Fuzzy.page_size = 50
Fuzzy.ns_id = vim.api.nvim_create_namespace('FuzzyHighlight')
local function render_results(query)
local total = #Fuzzy.matches
if total == 0 then
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
return
end
local start_idx = Fuzzy.scroll + 1
local end_idx = math.min(start_idx + Fuzzy.page_size - 1, total)
local display = {}
for i = start_idx, end_idx do
display[#display + 1] = Fuzzy.matches[i]
end
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
vim.api.nvim_buf_clear_namespace(Fuzzy.result_buf, Fuzzy.ns_id, 0, -1)
-- highlight selection
local rel_cursor = math.min(Fuzzy.cursor - Fuzzy.scroll, #display)
vim.api.nvim_buf_set_extmark(Fuzzy.result_buf, Fuzzy.ns_id, rel_cursor - 1, 0, {
end_line = rel_cursor,
hl_group = 'Visual',
hl_eol = true,
})
-- highlight query matches
if query and query ~= '' then
local pattern = vim.pesc(query)
for i, line in ipairs(display) do
for s, e in line:gmatch('()' .. pattern .. '()') do
vim.api.nvim_buf_set_extmark(Fuzzy.result_buf, Fuzzy.ns_id, i - 1, s - 1, {
end_col = e - 1,
hl_group = 'Search',
})
end
end
end
end
local function run_grep(query) local function run_grep(query)
if query == '' then if query == '' then
@ -189,17 +251,8 @@ function Fuzzy.open_grep()
local result = handle:read('*a') local result = handle:read('*a')
handle:close() handle:close()
Fuzzy.matches = vim.split(result, '\n', { trimempty = true }) Fuzzy.matches = vim.split(result, '\n', { trimempty = true })
if #Fuzzy.matches == 0 then Fuzzy.cursor, Fuzzy.scroll = 1, 0
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' }) render_results(query)
return
end
local display = {}
for i = 1, math.min(#Fuzzy.matches, 50) do
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 end
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
@ -211,13 +264,25 @@ function Fuzzy.open_grep()
}) })
vim.keymap.set('i', '<C-n>', function() vim.keymap.set('i', '<C-n>', function()
Fuzzy.cursor = math.min(Fuzzy.cursor + 1, #Fuzzy.matches) if Fuzzy.cursor < #Fuzzy.matches then
Fuzzy:highlight_selection() Fuzzy.cursor = Fuzzy.cursor + 1
if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then
Fuzzy.scroll = Fuzzy.scroll + 1
end
local query = vim.fn.getline('.'):gsub('^Grep:%s*', '')
render_results(query)
end
end, { buffer = Fuzzy.input_buf }) end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<C-p>', function() vim.keymap.set('i', '<C-p>', function()
Fuzzy.cursor = math.max(Fuzzy.cursor - 1, 1) if Fuzzy.cursor > 1 then
Fuzzy:highlight_selection() Fuzzy.cursor = Fuzzy.cursor - 1
if Fuzzy.cursor <= Fuzzy.scroll then
Fuzzy.scroll = math.max(Fuzzy.scroll - 1, 0)
end
local query = vim.fn.getline('.'):gsub('^Grep:%s*', '')
render_results(query)
end
end, { buffer = Fuzzy.input_buf }) end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<CR>', function() vim.keymap.set('i', '<CR>', function()

View File

@ -49,12 +49,15 @@ end
return { return {
'nvim-tree/nvim-tree.lua', 'nvim-tree/nvim-tree.lua',
version = '*', version = '*',
dev = true,
opts = { opts = {
on_attach = my_on_attach, on_attach = my_on_attach,
view = { signcolumn = 'no' }, view = { signcolumn = 'no' },
actions = { file_popup = { open_win_config = { border = 'rounded' } } }, actions = { file_popup = { open_win_config = { border = 'rounded' } } },
renderer = { renderer = {
root_folder_label = false, root_folder_label = function(path)
return '-- ' .. vim.fn.fnamemodify(path, ':t') .. ' --'
end,
special_files = {}, special_files = {},
highlight_hidden = 'all', highlight_hidden = 'all',
@ -67,6 +70,7 @@ return {
}, },
icons = { icons = {
bookmarks_placement = 'after', bookmarks_placement = 'after',
git_placement = 'after',
show = { show = {
file = false, file = false,
folder = false, folder = false,
@ -77,16 +81,32 @@ return {
diagnostics = false, diagnostics = false,
bookmarks = true, bookmarks = true,
}, },
git_placement = 'after',
glyphs = { glyphs = {
-- default = '•',
default = ' ',
symlink = '',
bookmark = '󰆤',
modified = '',
hidden = '󰜌',
folder = {
arrow_closed = '',
arrow_open = '',
default = '',
open = '',
empty = '',
empty_open = '',
symlink = '',
symlink_open = '',
},
git = { git = {
unstaged = '', unstaged = '', -- '✗',
staged = '', staged = '',
unmerged = '', unmerged = '',
renamed = '', renamed = '',
untracked = '', untracked = '',
deleted = '', -- '󰧧', deleted = '', -- '󰧧',
ignored = '', ignored = '',
}, },
}, },
}, },