Compare commits

...

7 Commits

12 changed files with 555 additions and 317 deletions

15
docs/README.md Normal file
View File

@@ -0,0 +1,15 @@
# New Spec
For new LSP, add in `lua/modules/language-specs.lua` at `lsp = <name>`.
Name should match the mason registry one at: `https://github.com/mason-org/mason-registry/tree/main/packages`
1. Run `nvim --headless +InstallAll +qa` (it invalidates cache automatically)
2. Run ` nvim --headless +FetchLspConfigs +qa` -> It will download the nvim-lspconfig variant in `lsp/`
You may need to run: `pkill prettierd` (as it is running in background)
# Other commands
```
nvim --headless +Sync +qa # For packages/plugins
```

View File

@@ -1,13 +1,3 @@
TODO:
- wrap up invero theme in separate repo and proper colors?
- check plugins logins
- cache / create final result
- simplify coding: ts, lsp, lint, format (check other repos)
- how to download parsers and plugins alternative
- telescope alternative
- keymaps
- wrap up everything
```lua ```lua
--[[ --[[
Neovim Lua config: ways to set things Neovim Lua config: ways to set things
@@ -31,6 +21,7 @@ TODO:
## check macos fileS: https://github.com/dsully/dotfiles/blob/main/.data/macos-defaults/globals.yaml ## check macos fileS: https://github.com/dsully/dotfiles/blob/main/.data/macos-defaults/globals.yaml
# Used pacakges: # Used pacakges:
- rockspaces Metadata files describing how to build and install a Lua package. - rockspaces Metadata files describing how to build and install a Lua package.
- luarocks Package manager for Lua modules. (optional) - luarocks Package manager for Lua modules. (optional)
- tree-sitter Parser generator. Not needed except for using CLI. (optional) - tree-sitter Parser generator. Not needed except for using CLI. (optional)
@@ -66,8 +57,8 @@ TODO:
- ncurses ncurses-dev ncurses-libs ncurses-terminfo \ - ncurses ncurses-dev ncurses-libs ncurses-terminfo \
- check: https://github.com/glepnir/nvim/blob/main/Dockerfile - check: https://github.com/glepnir/nvim/blob/main/Dockerfile
# Currently installed # Currently installed
- plenary.nvim - plenary.nvim
- lazy.nvim - lazy.nvim
@@ -93,11 +84,12 @@ TODO:
- plenary.nvim - plenary.nvim
- harpoon # tags - harpoon # tags
# Notes: # Notes:
- in lsp change tsserver to vtsls - in lsp change tsserver to vtsls
# New package definition # New package definition
- Plugin and Package managers - Plugin and Package managers
- folke/lazy.nvim - folke/lazy.nvim
- mason-org/mason.nvim - mason-org/mason.nvim
@@ -106,7 +98,6 @@ TODO:
- nvim-treesitter-textobjects - nvim-treesitter-textobjects
- LSP - LSP
- neovim/nvim-lspconfig - neovim/nvim-lspconfig
- nvim-ts-autotag tag elements (`</>`) - nvim-ts-autotag tag elements (`</>`)
- windwp/nvim-autopairs auto pairs - windwp/nvim-autopairs auto pairs
- blink.cmp autocompletion - blink.cmp autocompletion
@@ -123,11 +114,13 @@ TODO:
- mini.indentscope - mini.indentscope
## Deps: ## Deps:
- SchemaStore.nvim - SchemaStore.nvim
- mason-lspconfig.nvim - mason-lspconfig.nvim
- mason.nvim - mason.nvim
## Maybe: ## Maybe:
- folke/ts-comments.nvim better comments - folke/ts-comments.nvim better comments
- grug-far.nvim find and replace - grug-far.nvim find and replace
- markdown-preview.nvim side by side md (disabled in folke) - markdown-preview.nvim side by side md (disabled in folke)
@@ -146,6 +139,7 @@ TODO:
- undo tree (find a plugin) - undo tree (find a plugin)
## AI help ## AI help
- jackMort/ChatGPT.nvim - jackMort/ChatGPT.nvim
- MunifTanjim/nui.nvim (dep) - MunifTanjim/nui.nvim (dep)
- nvim-lua/plenary.nvim (dep) - nvim-lua/plenary.nvim (dep)
@@ -155,6 +149,7 @@ TODO:
- milanglacier/minuet-ai.nvim (folke) - milanglacier/minuet-ai.nvim (folke)
## Options ## Options
``` ```
opt.backup = true opt.backup = true
@@ -178,6 +173,7 @@ vim.keymap.set("n", "<C-c>", "ciw")
``` ```
folke cmd folke cmd
```lua ```lua
-- show cursor line only in active window -- show cursor line only in active window
vim.api.nvim_create_autocmd({ "InsertLeave", "WinEnter" }, { vim.api.nvim_create_autocmd({ "InsertLeave", "WinEnter" }, {
@@ -210,6 +206,7 @@ vim.api.nvim_create_autocmd("BufWritePre", {
``` ```
Enable folding with TS: Enable folding with TS:
``` ```
vim.opt.foldmethod = "expr" vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()" vim.opt.foldexpr = "nvim_treesitter#foldexpr()"

View File

@@ -5,6 +5,12 @@ end
vim.env.PATH = vim.fn.stdpath('data') .. '/mason/bin:' .. vim.env.PATH vim.env.PATH = vim.fn.stdpath('data') .. '/mason/bin:' .. vim.env.PATH
vim.filetype.add({
pattern = {
['.*/templates/.*%.ya?ml'] = 'yaml.helm-values',
},
})
require('core.options') require('core.options')
require('core.keymaps') require('core.keymaps')
require('core.events') require('core.events')

25
lsp/helm_ls.lua Normal file
View File

@@ -0,0 +1,25 @@
---@brief
---
--- https://github.com/mrjosh/helm-ls
---
--- Helm Language server. (This LSP is in early development)
---
--- `helm Language server` can be installed by following the instructions [here](https://github.com/mrjosh/helm-ls).
---
--- The default `cmd` assumes that the `helm_ls` binary can be found in `$PATH`.
---
--- If need Helm file highlight use [vim-helm](https://github.com/towolf/vim-helm) plugin.
---@type vim.lsp.Config
return {
cmd = { 'helm_ls', 'serve' },
filetypes = { 'helm', 'yaml.helm-values' },
root_markers = { 'Chart.yaml' },
capabilities = {
workspace = {
didChangeWatchedFiles = {
dynamicRegistration = true,
},
},
},
}

80
lsp/yamlls.lua Normal file
View File

@@ -0,0 +1,80 @@
---@brief
---
--- https://github.com/redhat-developer/yaml-language-server
---
--- `yaml-language-server` can be installed via `yarn`:
--- ```sh
--- yarn global add yaml-language-server
--- ```
---
--- To use a schema for validation, there are two options:
---
--- 1. Add a modeline to the file. A modeline is a comment of the form:
---
--- ```
--- # yaml-language-server: $schema=<urlToTheSchema|relativeFilePath|absoluteFilePath}>
--- ```
---
--- where the relative filepath is the path relative to the open yaml file, and the absolute filepath
--- is the filepath relative to the filesystem root ('/' on unix systems)
---
--- 2. Associated a schema url, relative , or absolute (to root of project, not to filesystem root) path to
--- the a glob pattern relative to the detected project root. Check `:checkhealth vim.lsp` to determine the resolved project
--- root.
---
--- ```lua
--- vim.lsp.config('yamlls', {
--- ...
--- settings = {
--- yaml = {
--- ... -- other settings. note this overrides the lspconfig defaults.
--- schemas = {
--- ["https://json.schemastore.org/github-workflow.json"] = "/.github/workflows/*",
--- ["../path/relative/to/file.yml"] = "/.github/workflows/*",
--- ["/path/from/root/of/project"] = "/.github/workflows/*",
--- },
--- },
--- }
--- })
--- ```
---
--- Currently, kubernetes is special-cased in yammls, see the following upstream issues:
--- * [#211](https://github.com/redhat-developer/yaml-language-server/issues/211).
--- * [#307](https://github.com/redhat-developer/yaml-language-server/issues/307).
---
--- To override a schema to use a specific k8s schema version (for example, to use 1.18):
---
--- ```lua
--- vim.lsp.config('yamlls', {
--- ...
--- settings = {
--- yaml = {
--- ... -- other settings. note this overrides the lspconfig defaults.
--- schemas = {
--- ["https://raw.githubusercontent.com/yannh/kubernetes-json-schema/refs/heads/master/v1.32.1-standalone-strict/all.json"] = "/*.k8s.yaml",
--- ... -- other schemas
--- },
--- },
--- }
--- })
--- ```
---@type vim.lsp.Config
return {
cmd = { 'yaml-language-server', '--stdio' },
filetypes = { 'helm', 'yaml', 'yaml.docker-compose', 'yaml.gitlab', 'yaml.helm-values' },
root_markers = { '.git' },
settings = {
-- https://github.com/redhat-developer/vscode-redhat-telemetry#how-to-disable-telemetry-reporting
redhat = { telemetry = { enabled = false } },
-- formatting disabled by default in yaml-language-server; enable it
yaml = { format = { enable = true } },
},
on_init = function(client)
--- https://github.com/neovim/nvim-lspconfig/pull/4016
--- Since formatting is disabled by default if you check `client:supports_method('textDocument/formatting')`
--- during `LspAttach` it will return `false`. This hack sets the capability to `true` to facilitate
--- autocmd's which check this capability
client.server_capabilities.documentFormattingProvider = true
end,
}

View File

@@ -31,12 +31,14 @@ vim.opt.textwidth = 100
vim.opt.colorcolumn = '+0' vim.opt.colorcolumn = '+0'
vim.opt.signcolumn = 'no' vim.opt.signcolumn = 'no'
vim.opt.number = false vim.opt.number = true
vim.opt.relativenumber = false vim.opt.relativenumber = true
vim.opt.cursorline = false vim.opt.cursorline = false
vim.opt.ruler = false vim.opt.ruler = false
vim.opt.winborder = 'rounded' vim.opt.winborder = 'rounded'
vim.opt.guicursor = 'n-v-i-c:block' vim.opt.guicursor = 'n-v-i-c:block'
vim.opt.list = true
vim.opt.listchars = { leadmultispace = '', tab = '', trail = '·' }
vim.opt.laststatus = 3 vim.opt.laststatus = 3
vim.opt.statusline = '── %f %h%w%m%r %= [%l,%c-%L] ──' vim.opt.statusline = '── %f %h%w%m%r %= [%l,%c-%L] ──'

View File

@@ -1,7 +1,32 @@
local M = {} local M = {}
-- vim.filetype.add({
-- pattern = {
-- ['.*/templates/.*%.ya?ml'] = 'yaml.helm-values',
-- ['.*/templates/.*%.tpl'] = 'yaml.helm-values',
-- },
-- })
-- vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, {
-- pattern = '**/templates/**/*.y?ml',
-- callback = function()
-- vim.bo.filetype = 'yaml.helm-values'
-- end,
-- })
--
function M.get() function M.get()
return { return {
{
ft = 'yaml.helm-values',
ts = 'helm',
lsp = 'helm-ls',
},
{
ft = 'yaml',
ts = 'yaml',
lsp = 'yaml-language-server',
format = { 'prettierd', 'prettier' },
},
{ ts = { 'yaml', 'toml', 'sql', 'diff', 'dockerfile', 'gitcommit', 'gitignore' } }, { ts = { 'yaml', 'toml', 'sql', 'diff', 'dockerfile', 'gitcommit', 'gitignore' } },
{ ts = { 'c', 'cpp', 'go', 'rust', 'python' } }, { ts = { 'c', 'cpp', 'go', 'rust', 'python' } },

View File

@@ -1,13 +1,12 @@
require('plugins.filetree') require('plugins.filetree')
require('plugins.finder').setup({ local finder = require('plugins.finder')
finder.setup({
exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo', '*-lock.json' }, exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo', '*-lock.json' },
use_disk_cache = true, -- optional use_disk_cache = true,
follow_symlinks = true,
}) })
vim.keymap.set('n', '<leader>f', function() vim.keymap.set('n', '<leader>f', finder.files)
require('plugins.finder').files() vim.keymap.set('n', '<leader>g', finder.grep)
end) -- vim.keymap.set('n', '<leader>fc', finder.clear_cache)
-- vim.keymap.set('n', '<leader>fD', finder.diagnose)
vim.keymap.set('n', '<leader>g', function()
require('plugins.finder').grep()
end)

View File

@@ -4,6 +4,8 @@ function M.load_theme()
highlights = function(c, tool) highlights = function(c, tool)
c.bg_float = tool(152) c.bg_float = tool(152)
return { return {
FinderPath = { fg = c.muted },
Whitespace = { fg = c.outline_light },
ModeMsg = { fg = c.yellow, bg = c.none, bold = true }, ModeMsg = { fg = c.yellow, bg = c.none, bold = true },
WinSeparator = { fg = c.outline, bg = c.base }, WinSeparator = { fg = c.outline, bg = c.base },
StatusLine = { fg = c.outline, bg = c.base, bold = false }, StatusLine = { fg = c.outline, bg = c.base, bold = false },

View File

@@ -99,7 +99,7 @@ require('nvim-tree').setup({
modified = { enable = true, show_on_dirs = true, show_on_open_dirs = true }, modified = { enable = true, show_on_dirs = true, show_on_open_dirs = true },
filters = { filters = {
enable = true, enable = true,
git_ignored = true, git_ignored = false,
dotfiles = false, dotfiles = false,
git_clean = false, git_clean = false,
no_buffer = false, no_buffer = false,

View File

@@ -6,7 +6,7 @@ local M = {}
-- Config -- Config
--------------------------------------------------------------------- ---------------------------------------------------------------------
M.config = { M.config = {
file_cmd = nil, -- "fd" | "fdfind" | nil (auto) file_cmd = nil, -- "fd" | "fdfind" | nil (auto-detect)
grep_cmd = 'rg', -- ripgrep binary grep_cmd = 'rg', -- ripgrep binary
page_size = 60, -- soft cap; real viewport height is measured page_size = 60, -- soft cap; real viewport height is measured
debounce_ms = 80, debounce_ms = 80,
@@ -15,8 +15,10 @@ M.config = {
cache_ttl_sec = 20, cache_ttl_sec = 20,
max_items = 5000, -- safety cap for massive outputs max_items = 5000, -- safety cap for massive outputs
debug = false, debug = false,
follow_symlinks = true, -- pass -L to fd
grep_path_width = 30,
-- Exclusion controls (added to tool flags and used in glob fallback) -- Exclusion patterns (used by fd/rg in addition to their gitignore handling)
exclude_patterns = { 'node_modules', 'dist', 'build', '.git' }, exclude_patterns = { 'node_modules', 'dist', 'build', '.git' },
-- Optional on-disk filelist cache at $ROOT/.devflow/finder_cache.json -- Optional on-disk filelist cache at $ROOT/.devflow/finder_cache.json
@@ -77,16 +79,18 @@ local function L(msg, data)
end end
local function now_sec() local function now_sec()
return vim.loop.now() / 1000 return os.time()
end end
local function clamp(v, lo, hi) local function clamp(v, lo, hi)
return (v < lo) and lo or ((v > hi) and hi or v) return (v < lo) and lo or ((v > hi) and hi or v)
end end
local function cmd_exists(bin) local function cmd_exists(bin)
return vim.fn.executable(bin) == 1 return vim.fn.executable(bin) == 1
end end
-- single reusable timer to avoid churn; caller controls ms -- single reusable timer to avoid churn
local function debounce(fn, ms) local function debounce(fn, ms)
if not S.timer then if not S.timer then
S.timer = vim.loop.new_timer() S.timer = vim.loop.new_timer()
@@ -98,30 +102,12 @@ local function debounce(fn, ms)
end) end)
end end
local function read_gitignore(root)
local p = root .. '/.gitignore'
local f = io.open(p, 'r')
if not f then
return {}
end
local lines = {}
for line in f:lines() do
local l = vim.trim(line)
if #l > 0 and not l:match('^#') and not l:match('^!') then
l = l:gsub('/+$', '')
table.insert(lines, l)
end
end
f:close()
return lines
end
local function ensure_cache_dir(root) local function ensure_cache_dir(root)
local dir = root .. '/.devflow' local dir = root .. '/.devflow'
local ok = vim.loop.fs_stat(dir) local ok = vim.loop.fs_stat(dir)
if not ok then if not ok then
pcall(vim.loop.fs_mkdir, dir, 448) pcall(vim.loop.fs_mkdir, dir, 448) -- 0700
end -- 0700 end
return dir return dir
end end
@@ -141,7 +127,7 @@ local function load_cache(root)
return nil return nil
end end
if now_sec() - (data.timestamp or 0) > M.config.cache_ttl_sec then if now_sec() - (data.timestamp or 0) > M.config.cache_ttl_sec then
pcall(os.remove, file) -- proactively clear stale pcall(os.remove, file)
return nil return nil
end end
return data.files return data.files
@@ -173,7 +159,6 @@ local function project_root()
return vim.fn.getcwd(0, 0) return vim.fn.getcwd(0, 0)
end end
-- Robust relative-to-root
local function normalize_rel(root, p) local function normalize_rel(root, p)
if not p or p == '' then if not p or p == '' then
return '' return ''
@@ -189,7 +174,6 @@ local function normalize_rel(root, p)
return abs:sub(#root_norm + 2) return abs:sub(#root_norm + 2)
end end
end end
-- strip leading ./ as last resort
return (p:gsub('^%./', '')) return (p:gsub('^%./', ''))
end end
@@ -201,7 +185,6 @@ local function to_abs_path(root, rel)
end end
local function to_display_path(abs) local function to_display_path(abs)
-- relative to current working dir if possible, else absolute
return vim.fn.fnamemodify(abs, ':.') return vim.fn.fnamemodify(abs, ':.')
end end
@@ -225,11 +208,11 @@ local function effective_debounce_ms()
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- Render helpers -- Render
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function ensure_visible() local function ensure_visible()
local page = page_rows() local page = page_rows()
local sel = clamp(S.select, 1, #S.filtered) local sel = clamp(S.select, 1, math.max(1, #S.filtered))
local top = S.scroll + 1 local top = S.scroll + 1
local bot = S.scroll + page local bot = S.scroll + page
if sel < top then if sel < top then
@@ -240,9 +223,6 @@ local function ensure_visible()
S.scroll = clamp(S.scroll, 0, math.max(#S.filtered - page, 0)) S.scroll = clamp(S.scroll, 0, math.max(#S.filtered - page, 0))
end end
---------------------------------------------------------------------
-- Render
---------------------------------------------------------------------
local function render() local function render()
if not (S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then if not (S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then
return return
@@ -253,31 +233,107 @@ local function render()
local total = #S.filtered local total = #S.filtered
local view = {} local view = {}
if total == 0 then if total == 0 then
view = { '-- no matches --' } view = { ' (no matches)' }
else else
local start_idx = S.scroll + 1 local start_idx = S.scroll + 1
local end_idx = math.min(start_idx + page_rows() - 1, total) local end_idx = math.min(start_idx + page_rows() - 1, total)
for i = start_idx, end_idx do for i = start_idx, end_idx do
local line = S.filtered[i] view[#view + 1] = tostring(S.filtered[i] or '')
view[#view + 1] = tostring(line or '') end
end
-- For grep mode, reformat lines with fixed-width path column
local offset_map = {} -- maps display line -> character offset for highlighting
if S.mode == 'grep' and total > 0 then
local path_width = M.config.grep_path_width or 40
for i = 1, #view do
local line = view[i]
if line then
-- Parse original vimgrep format: file:line:col:content
local file, lnum, col = line:match('^(.-):(%d+):(%d+):')
if file and lnum and col then
-- Pad line number to 3 chars with underscores
local padded_lnum = lnum
while #padded_lnum < 3 do
padded_lnum = ' ' .. padded_lnum
end
local lineinfo = ':' .. padded_lnum .. '|'
local content_start = #file + 1 + #lnum + 1 + #col + 2 -- file + :lnum: + col:
local content_part = line:sub(content_start)
-- Calculate how much space we need for file + lineinfo
local prefix_len = #file + #lineinfo
local formatted_line
if prefix_len > path_width then
-- Need to truncate the filepath part only
local available_for_file = path_width - #lineinfo
if available_for_file > 1 then
local truncated_file = '' .. file:sub(-(available_for_file - 1))
formatted_line = truncated_file .. lineinfo .. content_part
-- Offset is: original_file_length - truncated_file_length
offset_map[i] = #file - #truncated_file
else
-- Extreme case: line info itself is too long, just show what we can
formatted_line = ('' .. file):sub(1, path_width) .. lineinfo .. content_part
offset_map[i] = #file - (path_width - #lineinfo)
end
elseif prefix_len < path_width then
-- Right-align by padding before the filepath
local padding = string.rep(' ', path_width - prefix_len)
formatted_line = padding .. file .. lineinfo .. content_part
offset_map[i] = -(path_width - prefix_len) -- negative for padding added
else
formatted_line = file .. lineinfo .. content_part
offset_map[i] = 0
end
view[i] = formatted_line
end
end
end end
end end
vim.bo[S.buf_res].modifiable = true vim.bo[S.buf_res].modifiable = true
vim.bo[S.buf_res].readonly = false
vim.api.nvim_buf_set_lines(S.buf_res, 0, -1, false, view) vim.api.nvim_buf_set_lines(S.buf_res, 0, -1, false, view)
vim.api.nvim_buf_clear_namespace(S.buf_res, S.ns, 0, -1) vim.api.nvim_buf_clear_namespace(S.buf_res, S.ns, 0, -1)
-- match highlights (visible window only) -- highlight groups
vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true }) vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true })
vim.api.nvim_set_hl(0, 'FinderPath', { fg = '#888888', default = true })
for i = 1, #view do for i = 1, #view do
local idx = S.scroll + i local idx = S.scroll + i
local line = view[i]
-- In grep mode, highlight the entire "file:line|" prefix in gray
if S.mode == 'grep' and line then
-- Find the end of the full prefix including the pipe
local _, prefix_end = line:find(':[ 0-9]+|')
if prefix_end then
pcall(vim.api.nvim_buf_set_extmark, S.buf_res, S.ns, i - 1, 0, {
end_col = prefix_end,
hl_group = 'FinderPath',
})
end
end
-- match highlights (query matches in orange)
local spans = S.positions[idx] local spans = S.positions[idx]
if spans then if spans then
for _, se in ipairs(spans) do for _, se in ipairs(spans) do
local scol, ecol = se[1], se[2] local scol, ecol = se[1], se[2]
if ecol > scol then
vim.api.nvim_buf_set_extmark(S.buf_res, S.ns, i - 1, scol, { -- Adjust positions for grep mode formatting
if S.mode == 'grep' and offset_map[i] then
scol = scol - offset_map[i]
ecol = ecol - offset_map[i]
end
if ecol > scol and scol >= 0 then
pcall(vim.api.nvim_buf_set_extmark, S.buf_res, S.ns, i - 1, scol, {
end_col = ecol, end_col = ecol,
hl_group = 'FinderMatch', hl_group = 'FinderMatch',
}) })
@@ -287,10 +343,10 @@ local function render()
end end
-- selection highlight -- selection highlight
if total > 0 and #view > 0 then if total > 0 then
vim.api.nvim_set_hl(0, 'FinderSelection', { link = 'CursorLine', default = true }) vim.api.nvim_set_hl(0, 'FinderSelection', { link = 'CursorLine', default = true })
local rel = clamp(S.select - S.scroll, 1, #view) local rel = clamp(S.select - S.scroll, 1, #view)
vim.api.nvim_buf_set_extmark(S.buf_res, S.ns, rel - 1, 0, { pcall(vim.api.nvim_buf_set_extmark, S.buf_res, S.ns, rel - 1, 0, {
end_line = rel, end_line = rel,
hl_group = 'FinderSelection', hl_group = 'FinderSelection',
hl_eol = true, hl_eol = true,
@@ -298,11 +354,11 @@ local function render()
end end
vim.bo[S.buf_res].modifiable = false vim.bo[S.buf_res].modifiable = false
L('render', { select = S.select, scroll = S.scroll, total = total, lines = #view }) L('render', { select = S.select, scroll = S.scroll, total = total })
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- Querying / Filtering -- Filtering
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function compute_positions_files(items, q) local function compute_positions_files(items, q)
local ok, res = pcall(vim.fn.matchfuzzypos, items, q) local ok, res = pcall(vim.fn.matchfuzzypos, items, q)
@@ -320,14 +376,13 @@ local function compute_positions_files(items, q)
local spans = {} local spans = {}
local start_col, last_col = nil, nil local start_col, last_col = nil, nil
for _, c in ipairs(cols) do for _, c in ipairs(cols) do
local c0 = c
if not start_col then if not start_col then
start_col, last_col = c0, c0 start_col, last_col = c, c
elseif c0 == last_col + 1 then elseif c == last_col + 1 then
last_col = c0 last_col = c
else else
table.insert(spans, { start_col, last_col + 1 }) -- end exclusive table.insert(spans, { start_col, last_col + 1 })
start_col, last_col = c0, c0 start_col, last_col = c, c
end end
end end
if start_col then if start_col then
@@ -342,14 +397,16 @@ local function compute_positions_grep(lines, q)
if q == '' then if q == '' then
return lines, {} return lines, {}
end end
local qlow = q:lower() -- Match rg's --smart-case: case-insensitive unless query has uppercase
local case_insensitive = not q:match('[A-Z]')
local qmatch = case_insensitive and q:lower() or q
local positions = {} local positions = {}
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
local llow = tostring(line or ''):lower() local lmatch = case_insensitive and tostring(line or ''):lower() or tostring(line or '')
local spans = {} local spans = {}
local sidx = 1 local sidx = 1
while true do while true do
local s, e = llow:find(qlow, sidx, true) local s, e = lmatch:find(qmatch, sidx, true)
if not s then if not s then
break break
end end
@@ -365,7 +422,7 @@ local function compute_positions_grep(lines, q)
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- set_items -- set_items / set_query
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function set_items(list) local function set_items(list)
list = list or {} list = list or {}
@@ -377,11 +434,12 @@ local function set_items(list)
list = tmp list = tmp
end end
-- sanitize to strings
for i, v in ipairs(list) do for i, v in ipairs(list) do
list[i] = tostring(v or '') list[i] = tostring(v or '')
end end
table.sort(list)
S.items = list S.items = list
S.filtered = list S.filtered = list
S.positions = {} S.positions = {}
@@ -390,9 +448,6 @@ local function set_items(list)
render() render()
end end
---------------------------------------------------------------------
-- set_query
---------------------------------------------------------------------
local function set_query(q) local function set_query(q)
local prev_val = S.filtered[S.select] local prev_val = S.filtered[S.select]
S.query = q S.query = q
@@ -410,7 +465,6 @@ local function set_query(q)
end end
end end
-- selection policy:
if not S.user_moved then if not S.user_moved then
S.select = 1 S.select = 1
else else
@@ -428,11 +482,11 @@ local function set_query(q)
ensure_visible() ensure_visible()
render() render()
L('set_query', { query = q, filtered = #S.filtered, user_moved = S.user_moved }) L('set_query', { query = q, filtered = #S.filtered })
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- Move / Accept -- Navigation
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function move_down() local function move_down()
if #S.filtered == 0 then if #S.filtered == 0 then
@@ -454,23 +508,59 @@ local function move_up()
render() render()
end end
-- Correct parser for rg --vimgrep (file:line:col:match) local function page_down()
if #S.filtered == 0 then
return
end
S.user_moved = true
S.select = clamp(S.select + page_rows(), 1, #S.filtered)
ensure_visible()
render()
end
local function page_up()
if #S.filtered == 0 then
return
end
S.user_moved = true
S.select = clamp(S.select - page_rows(), 1, #S.filtered)
ensure_visible()
render()
end
---------------------------------------------------------------------
-- Accept selection
---------------------------------------------------------------------
local function parse_vimgrep(line) local function parse_vimgrep(line)
if type(line) ~= 'string' then if type(line) ~= 'string' then
return nil return nil
end end
local file, lnum = line:match('^(.-):(%d+):%d+:') local file, lnum, col = line:match('^(.-):(%d+):(%d+):')
if not file then if not file then
return nil return nil
end end
return file, tonumber(lnum) or 1 return file, tonumber(lnum) or 1, tonumber(col) or 1
end end
local function accept_selection_files() local function accept_selection()
local pick = S.filtered[S.select] local pick = S.filtered[S.select]
if type(pick) ~= 'string' or pick == '' then if type(pick) ~= 'string' or pick == '' or pick:match('^%s*%(') then
return -- ignore "(no matches)" placeholder
end
if S.mode == 'grep' then
local file, lnum, col = parse_vimgrep(pick)
if not file then
return return
end end
local abs = to_abs_path(S.root, file)
local path = to_display_path(abs)
M.close()
vim.schedule(function()
vim.cmd.edit(vim.fn.fnameescape(path))
pcall(vim.api.nvim_win_set_cursor, 0, { lnum, col - 1 })
end)
else
local abs = to_abs_path(S.root, pick) local abs = to_abs_path(S.root, pick)
local path = to_display_path(abs) local path = to_display_path(abs)
M.close() M.close()
@@ -478,28 +568,10 @@ local function accept_selection_files()
vim.cmd.edit(vim.fn.fnameescape(path)) vim.cmd.edit(vim.fn.fnameescape(path))
end) end)
end end
local function accept_selection_grep()
local pick = S.filtered[S.select]
if type(pick) ~= 'string' or pick == '' then
return
end
local file, lnum = parse_vimgrep(pick)
if not file then
return
end
local abs = to_abs_path(S.root, file)
local path = to_display_path(abs)
local ln = tonumber(lnum) or 1
M.close()
vim.schedule(function()
vim.cmd.edit(vim.fn.fnameescape(path))
pcall(vim.api.nvim_win_set_cursor, 0, { ln, 0 })
end)
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- Backends -- Async backends
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function cancel_job(job) local function cancel_job(job)
if not job then if not job then
@@ -507,7 +579,7 @@ local function cancel_job(job)
end end
pcall(function() pcall(function()
job:kill(15) job:kill(15)
end) -- SIGTERM if available end)
end end
local function collect_files_async(cb) local function collect_files_async(cb)
@@ -531,24 +603,19 @@ local function collect_files_async(cb)
end end
end end
local excludes = {} if file_cmd and cmd_exists(file_cmd) then
for _, p in ipairs(M.config.exclude_patterns or {}) do
table.insert(excludes, p)
end
local gi = read_gitignore(root)
for _, p in ipairs(gi) do
table.insert(excludes, p)
end
if file_cmd then
local args = { file_cmd, '--type', 'f', '--hidden', '--color', 'never' } local args = { file_cmd, '--type', 'f', '--hidden', '--color', 'never' }
if file_cmd == 'fd' or file_cmd == 'fdfind' then if file_cmd == 'fd' or file_cmd == 'fdfind' then
table.insert(args, '--strip-cwd-prefix') table.insert(args, '--strip-cwd-prefix')
if M.config.follow_symlinks then
table.insert(args, '-L')
end end
for _, ex in ipairs(excludes) do end
for _, ex in ipairs(M.config.exclude_patterns or {}) do
table.insert(args, '--exclude') table.insert(args, '--exclude')
table.insert(args, ex) table.insert(args, ex)
end end
cancel_job(S.job_files) cancel_job(S.job_files)
S.job_files = vim.system(args, { text = true, cwd = root }, function(obj) S.job_files = vim.system(args, { text = true, cwd = root }, function(obj)
if not (S.active and gen == S.gen) then if not (S.active and gen == S.gen) then
@@ -556,9 +623,11 @@ local function collect_files_async(cb)
end end
local list = {} local list = {}
if obj.stdout then if obj.stdout then
local raw = vim.split(obj.stdout, '\n', { trimempty = true }) for p in obj.stdout:gmatch('[^\n]+') do
for _, p in ipairs(raw) do local rel = normalize_rel(root, p)
list[#list + 1] = normalize_rel(root, p) if rel ~= '' then
list[#list + 1] = rel
end
end end
end end
if #list > M.config.max_items then if #list > M.config.max_items then
@@ -578,20 +647,23 @@ local function collect_files_async(cb)
return return
end end
-- Try git ls-files as async fallback -- Fallback: git ls-files
if cmd_exists('git') then if cmd_exists('git') then
cancel_job(S.job_files) cancel_job(S.job_files)
S.job_files = vim.system( S.job_files = vim.system(
{ 'git', 'ls-files', '-co', '--exclude-standard', '-z' }, { 'git', 'ls-files', '-co', '--exclude-standard' },
{ text = true, cwd = root }, { text = true, cwd = root },
function(o2) function(obj)
if not (S.active and gen == S.gen) then if not (S.active and gen == S.gen) then
return return
end end
local list = {} local list = {}
if o2.stdout then if obj.stdout then
for p in o2.stdout:gmatch('([^%z]+)') do for p in obj.stdout:gmatch('[^\n]+') do
list[#list + 1] = normalize_rel(root, p) local rel = normalize_rel(root, p)
if rel ~= '' then
list[#list + 1] = rel
end
end end
end end
if #list > M.config.max_items then if #list > M.config.max_items then
@@ -612,7 +684,7 @@ local function collect_files_async(cb)
return return
end end
-- Last resort omitted: blocking glob removed to keep async-only behavior L('no file lister available')
cb({}) cb({})
end end
@@ -624,6 +696,7 @@ local function grep_async(query, cb)
local gen = S.gen local gen = S.gen
local rg = M.config.grep_cmd local rg = M.config.grep_cmd
if not cmd_exists(rg) then if not cmd_exists(rg) then
L('grep_cmd not found', rg)
cb({}) cb({})
return return
end end
@@ -640,28 +713,27 @@ local function grep_async(query, cb)
'never', 'never',
'--path-separator', '--path-separator',
'/', '/',
'--',
query,
} }
-- Apply excludes as negative globs
-- Add excludes before the pattern
for _, p in ipairs(M.config.exclude_patterns or {}) do for _, p in ipairs(M.config.exclude_patterns or {}) do
table.insert(args, 2, '--glob') table.insert(args, '--glob')
table.insert(args, 3, '!' .. p) table.insert(args, '!' .. p)
end
for _, p in ipairs(read_gitignore(root)) do
table.insert(args, 2, '--glob')
table.insert(args, 3, '!' .. p)
end end
table.insert(args, '--')
table.insert(args, query)
cancel_job(S.job_rg) cancel_job(S.job_rg)
S.job_rg = vim.system(args, { text = true, cwd = root }, function(obj) S.job_rg = vim.system(args, { text = true, cwd = root }, function(obj)
if not (S.active and gen == S.gen) then if not (S.active and gen == S.gen) then
return return
end end
local list = {} local list = {}
-- Accept output even if exit code != 0 (no matches or partial errors)
if obj.stdout and #obj.stdout > 0 then if obj.stdout and #obj.stdout > 0 then
list = vim.split(obj.stdout, '\n', { trimempty = true }) for line in obj.stdout:gmatch('[^\n]+') do
list[#list + 1] = line
end
end end
if #list > M.config.max_items then if #list > M.config.max_items then
local tmp = {} local tmp = {}
@@ -692,19 +764,14 @@ local function open_layout(prompt)
vim.bo[b].swapfile = false vim.bo[b].swapfile = false
end end
end end
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
vim.bo[S.buf_inp].buftype = 'prompt' vim.bo[S.buf_inp].buftype = 'prompt'
end
if S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res) then
vim.bo[S.buf_res].buftype = 'nofile' vim.bo[S.buf_res].buftype = 'nofile'
vim.bo[S.buf_res].modifiable = false vim.bo[S.buf_res].modifiable = false
vim.bo[S.buf_res].readonly = false
end
local width = math.floor(vim.o.columns * 0.8) local width = math.floor(vim.o.columns * 0.8)
local height = math.min(math.floor(vim.o.lines * 0.5), M.config.page_size + 2) local height = math.min(math.floor(vim.o.lines * 0.5), M.config.page_size + 2)
local col = math.floor((vim.o.columns - width) / 2) local col = math.floor((vim.o.columns - width) / 2)
local row = math.floor((vim.o.lines - height) * 0.7) local row = math.floor((vim.o.lines - height) * 0.4)
S.win_inp = vim.api.nvim_open_win(S.buf_inp, true, { S.win_inp = vim.api.nvim_open_win(S.buf_inp, true, {
relative = 'editor', relative = 'editor',
@@ -717,32 +784,25 @@ local function open_layout(prompt)
focusable = true, focusable = true,
zindex = 200, zindex = 200,
}) })
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
vim.fn.prompt_setprompt(S.buf_inp, prompt or '') vim.fn.prompt_setprompt(S.buf_inp, prompt or '')
end
S.win_res = vim.api.nvim_open_win(S.buf_res, false, { S.win_res = vim.api.nvim_open_win(S.buf_res, false, {
relative = 'editor', relative = 'editor',
style = 'minimal', style = 'minimal',
border = 'single', border = 'rounded',
width = width, width = width,
height = math.max(1, height - 2), height = math.max(1, height - 2),
col = col, col = col,
row = row + 2, row = row + 3,
focusable = false, focusable = false,
zindex = 199, zindex = 199,
}) })
if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then
vim.wo[S.win_res].cursorline = false
vim.wo[S.win_res].cursorlineopt = 'line'
end
if S.aug then if S.aug then
pcall(vim.api.nvim_del_augroup_by_id, S.aug) pcall(vim.api.nvim_del_augroup_by_id, S.aug)
end end
S.aug = vim.api.nvim_create_augroup('finder_session', { clear = true }) S.aug = vim.api.nvim_create_augroup('finder_session', { clear = true })
-- Close if focus enters a non-floating window that isn't ours
vim.api.nvim_create_autocmd('WinEnter', { vim.api.nvim_create_autocmd('WinEnter', {
group = S.aug, group = S.aug,
callback = function() callback = function()
@@ -760,8 +820,6 @@ local function open_layout(prompt)
end, end,
}) })
-- Close if prompt buffer hides or leaves
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, { vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, {
group = S.aug, group = S.aug,
buffer = S.buf_inp, buffer = S.buf_inp,
@@ -771,9 +829,7 @@ local function open_layout(prompt)
end end
end, end,
}) })
end
-- Re-render on resize to respect new viewport height
vim.api.nvim_create_autocmd('VimResized', { vim.api.nvim_create_autocmd('VimResized', {
group = S.aug, group = S.aug,
callback = function() callback = function()
@@ -784,7 +840,7 @@ local function open_layout(prompt)
}) })
vim.cmd.startinsert() vim.cmd.startinsert()
L('open_layout', { win_inp = S.win_inp, win_res = S.win_res }) L('open_layout')
end end
local function close_layout() local function close_layout()
@@ -802,7 +858,6 @@ local function close_layout()
pcall(vim.api.nvim_del_augroup_by_id, S.aug) pcall(vim.api.nvim_del_augroup_by_id, S.aug)
S.aug = nil S.aug = nil
end end
L('close_layout')
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
@@ -810,25 +865,17 @@ end
--------------------------------------------------------------------- ---------------------------------------------------------------------
local function attach_handlers() local function attach_handlers()
local opts = { buffer = S.buf_inp, nowait = true, silent = true, noremap = true } local opts = { buffer = S.buf_inp, nowait = true, silent = true, noremap = true }
vim.keymap.set('i', '<C-n>', move_down, opts) vim.keymap.set('i', '<C-n>', move_down, opts)
vim.keymap.set('i', '<C-p>', move_up, opts) vim.keymap.set('i', '<C-p>', move_up, opts)
vim.keymap.set('i', '<Down>', move_down, opts) vim.keymap.set('i', '<Down>', move_down, opts)
vim.keymap.set('i', '<Up>', move_up, opts) vim.keymap.set('i', '<Up>', move_up, opts)
vim.keymap.set('i', '<CR>', function() vim.keymap.set('i', '<C-d>', page_down, opts)
if S.mode == 'grep' then vim.keymap.set('i', '<C-u>', page_up, opts)
accept_selection_grep() vim.keymap.set('i', '<CR>', accept_selection, opts)
else vim.keymap.set('i', '<Esc>', M.close, opts)
accept_selection_files() vim.keymap.set('i', '<C-c>', M.close, opts)
end
end, opts)
vim.keymap.set('i', '<Esc>', function()
M.close()
end, opts)
vim.keymap.set('i', '<C-c>', function()
M.close()
end, opts)
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
group = S.aug, group = S.aug,
buffer = S.buf_inp, buffer = S.buf_inp,
@@ -838,10 +885,10 @@ local function attach_handlers()
if not (S.active and S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp)) then if not (S.active and S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp)) then
return return
end end
local raw = vim.fn.getline('.') local raw = vim.fn.getline('.') or ''
raw = type(raw) == 'string' and raw or '' local prompt_len = S.mode == 'files' and 8 or 6 -- "Search: " or "Grep: "
local prompt_pat = (S.mode == 'files') and '^Search:%s*' or '^Grep:%s*' local q = raw:sub(prompt_len + 1)
local q = raw:gsub(prompt_pat, '')
if S.mode == 'grep' then if S.mode == 'grep' then
grep_async(q, function(list) grep_async(q, function(list)
if not S.active then if not S.active then
@@ -856,13 +903,12 @@ local function attach_handlers()
end, delay) end, delay)
end, end,
}) })
end
L('keymaps attached', opts) L('handlers attached')
end end
--------------------------------------------------------------------- ---------------------------------------------------------------------
-- Public -- Public API
--------------------------------------------------------------------- ---------------------------------------------------------------------
function M.files() function M.files()
if S.active then if S.active then
@@ -879,7 +925,7 @@ function M.files()
if not S.active then if not S.active then
return return
end end
set_items(list) -- render initial list (relative to root) set_items(list)
end) end)
end end
@@ -902,7 +948,6 @@ function M.close()
if not S.active then if not S.active then
return return
end end
-- stop timers and jobs first
if S.timer then if S.timer then
pcall(function() pcall(function()
S.timer:stop() S.timer:stop()
@@ -922,26 +967,67 @@ function M.close()
S.query, S.select, S.scroll = '', 1, 0 S.query, S.select, S.scroll = '', 1, 0
S.user_moved = false S.user_moved = false
S.files_cached = false S.files_cached = false
L('session closed') L('closed')
end
function M.clear_cache()
local root = project_root()
local file = root .. '/.devflow/finder_cache.json'
local ok = pcall(os.remove, file)
vim.notify(ok and 'Finder cache cleared' or 'No cache to clear')
end
function M.diagnose()
local root = project_root()
local lines = { '=== Finder Diagnostics ===' }
local fd_cmd = M.config.file_cmd
or (cmd_exists('fd') and 'fd')
or (cmd_exists('fdfind') and 'fdfind')
or nil
table.insert(lines, 'fd command: ' .. tostring(fd_cmd))
table.insert(
lines,
'rg command: '
.. tostring(M.config.grep_cmd)
.. ' (exists: '
.. tostring(cmd_exists(M.config.grep_cmd))
.. ')'
)
local cache_file = root .. '/.devflow/finder_cache.json'
local cache_stat = vim.loop.fs_stat(cache_file)
if cache_stat then
table.insert(lines, 'Cache file: ' .. cache_file)
table.insert(lines, 'Cache size: ' .. cache_stat.size .. ' bytes')
local cached = load_cache(root)
if cached then
table.insert(lines, 'Cache valid: true (' .. #cached .. ' files)')
else
table.insert(lines, 'Cache valid: false (expired or corrupt)')
end
else
table.insert(lines, 'Cache: none')
end
table.insert(lines, 'Exclude patterns: ' .. vim.inspect(M.config.exclude_patterns))
table.insert(lines, 'Follow symlinks: ' .. tostring(M.config.follow_symlinks))
vim.notify(table.concat(lines, '\n'), vim.log.levels.INFO)
end end
function M.setup(opts) function M.setup(opts)
M.config = vim.tbl_deep_extend('force', M.config, opts or {}) M.config = vim.tbl_deep_extend('force', M.config, opts or {})
if M.config.file_cmd and not cmd_exists(M.config.file_cmd) then
pcall( local fd_cmd = M.config.file_cmd
vim.notify, if fd_cmd and not cmd_exists(fd_cmd) then
("finder: file_cmd '%s' not found"):format(M.config.file_cmd), vim.notify('[finder] file_cmd "' .. fd_cmd .. '" not found', vim.log.levels.WARN)
vim.log.levels.WARN
)
end end
if M.config.grep_cmd and not cmd_exists(M.config.grep_cmd) then if M.config.grep_cmd and not cmd_exists(M.config.grep_cmd) then
pcall( vim.notify('[finder] grep_cmd "' .. M.config.grep_cmd .. '" not found', vim.log.levels.WARN)
vim.notify,
("finder: grep_cmd '%s' not found, will try 'rg'"):format(M.config.grep_cmd),
vim.log.levels.WARN
)
end end
L('setup', M.config)
L('setup complete', M.config)
end end
return M return M

View File

@@ -73,7 +73,8 @@ vim.api.nvim_create_user_command('Sync', function()
end, {}) end, {})
vim.api.nvim_create_user_command('FetchLspConfigs', function() vim.api.nvim_create_user_command('FetchLspConfigs', function()
local base_url = 'https://raw.githubusercontent.com/neovim/nvim-lspconfig/master/lsp/' -- local base_url = 'https://raw.githubusercontent.com/neovim/nvim-lspconfig/master/lsp/'
local base_url = 'https://raw.githubusercontent.com/neovim/nvim-lspconfig/refs/heads/master/lsp/'
local lm = require('plugins.language-manager') local lm = require('plugins.language-manager')
lm.invalidate_cache() lm.invalidate_cache()