refactor(config): enhance ignore patterns and remove terminals.lua

This commit is contained in:
Prad Nukala committed 2026-07-11 18:46:47 -04:00
1 parent d8bf571fdc
commit f80b18544a
2 files changed
+99 -137

No files matched your search

+99 -49
View File
@@ -1,12 +1,20 @@
--- Shared ignore patterns for file explorers and pickers --- Shared ignore patterns for file explorers and pickers
--- This module provides a centralized list of patterns to ignore across --- This module provides a centralized list of patterns to ignore across
--- neotree, snacks.picker, and other file browsing tools. --- snacks.picker and other file browsing tools. (The never_show lists were
--- consumed by neo-tree, now replaced by yazi.nvim; kept as reference data.)
---
--- Everything here is fast-event-context safe (pure Lua + libuv only):
--- snacks picker transforms run inside async/uv callbacks where vimL
--- functions (vim.fn.*) raise E5560.
--- @module config.ignore_patterns --- @module config.ignore_patterns
local uv = vim.uv or vim.loop
local M = {} local M = {}
--- Core ignore patterns used across the configuration --- Core ignore patterns used across the configuration
--- These patterns use glob syntax (* for wildcards) --- These patterns use glob syntax (* and ? wildcards), matched against
--- file basenames and individual path segments.
M.patterns = { M.patterns = {
-- Build artifacts and generated files -- Build artifacts and generated files
".beads", ".beads",
@@ -33,14 +41,11 @@ M.patterns = {
"AGENTS.md", "AGENTS.md",
"test", "test",
"tests", "tests",
".omc",
-- Configuration files -- Configuration files
".editorconfig", ".editorconfig",
".gitpod.*", ".gitpod.*",
-- Lock files and dependencies -- Lock files and dependencies
"*.lock", "*.lock",
"*.lockb", "*.lockb",
@@ -176,7 +181,6 @@ M.never_show_patterns = {
"Icon?", "Icon?",
"iCloud~", "iCloud~",
"com~", "com~",
"*.icns",
".conform*", ".conform*",
".null-ls_*", ".null-ls_*",
} }
@@ -198,66 +202,112 @@ M.ignored_directories = {
".worktrees", ".worktrees",
} }
--- Read additional ignore patterns from .rgignore file in git root local EMPTY = {}
--- @return table patterns List of patterns from .rgignore
function M.read_rgignore()
local patterns = {}
local current_file = vim.fn.expand "%:p"
local current_dir = vim.fn.fnamemodify(current_file, ":h")
if current_dir == "" then current_dir = vim.fn.getcwd() end --- Convert a glob (* and ? wildcards) to an anchored Lua pattern
--- @param glob string
--- @return string
local function glob_to_lua(glob)
return "^" .. glob:gsub("[%%%.%(%)%+%-%^%$%[%]]", "%%%0"):gsub("%*", ".*"):gsub("%?", ".") .. "$"
end
local git_root = --- @param globs string[]
vim.fn.systemlist("cd " .. vim.fn.shellescape(current_dir) .. " && git rev-parse --show-toplevel 2>/dev/null")[1] --- @return string[]
local function compile_globs(globs)
if git_root and git_root ~= "" and not git_root:match "^fatal:" then local out = {}
local rgignore_path = git_root .. "/.rgignore" for _, g in ipairs(globs) do
local file = io.open(rgignore_path, "r") out[#out + 1] = glob_to_lua(g)
if file then
for line in file:lines() do
if line ~= "" and not line:match "^#" then
line = line:gsub("^%./", "")
table.insert(patterns, line)
end
end
file:close()
end
end end
return out
end
--- Find the git root for a directory by walking up (pure libuv, cached)
local root_cache = {} ---@type table<string, string|false>
--- @param dir string
--- @return string?
local function git_root(dir)
local cached = root_cache[dir]
if cached ~= nil then return cached or nil end
local cur = dir
while cur and cur ~= "" do
if uv.fs_stat(cur .. "/.git") then
root_cache[dir] = cur
return cur
end
local parent = cur:match "(.+)/[^/]+$"
if not parent or parent == cur then break end
cur = parent
end
root_cache[dir] = false
return nil
end
--- Read additional ignore patterns from the .rgignore file in the git root.
--- Cached per file and invalidated on mtime change. Safe in fast event contexts.
--- @param dir? string directory to resolve the git root from (defaults to cwd)
--- @return string[] patterns List of glob patterns from .rgignore
local rgignore_cache = {} ---@type table<string, {mtime: number, patterns: string[]}>
function M.read_rgignore(dir)
local root = git_root(dir or uv.cwd())
if not root then return EMPTY end
local path = root .. "/.rgignore"
local stat = uv.fs_stat(path)
if not stat then return EMPTY end
local cached = rgignore_cache[path]
if cached and cached.mtime == stat.mtime.sec then return cached.patterns end
local patterns = {}
local file = io.open(path, "r")
if file then
for line in file:lines() do
if line ~= "" and not line:match "^#" then
patterns[#patterns + 1] = (line:gsub("^%./", ""))
end
end
file:close()
end
rgignore_cache[path] = { mtime = stat.mtime.sec, patterns = patterns }
return patterns return patterns
end end
--- Get all hide patterns including rgignore --- Get all hide patterns including rgignore
--- @param dir? string directory to resolve the git root from (defaults to cwd)
--- @return table patterns Combined list of all patterns to hide --- @return table patterns Combined list of all patterns to hide
function M.get_all_patterns() function M.get_all_patterns(dir)
local rgignore_patterns = M.read_rgignore() return vim.list_extend(vim.list_extend({}, M.patterns), M.read_rgignore(dir))
return vim.list_extend(vim.list_extend({}, M.patterns), rgignore_patterns)
end end
--- Create a file filter function for pickers --- Create a filter for snacks picker `transform` options.
--- @return function filter Function that filters files based on ignore patterns --- Snacks contract: return `false` to DROP an item, `true`/`nil` to keep it
--- (returning nil keeps the item!). Runs per item in a fast event context,
--- so only pure Lua and libuv are used here.
--- @return fun(item: snacks.picker.finder.Item): boolean
function M.create_picker_filter() function M.create_picker_filter()
local static ---@type string[]? compiled static globs (lazy)
local last_raw, last_rg = nil, EMPTY ---@type string[]?, string[]
return function(item) return function(item)
if not item or not item.file then return item end local file = item and item.file
if not file then return true end
local file = item.file static = static or compile_globs(M.patterns)
local basename = vim.fn.fnamemodify(file, ":t") local raw = M.read_rgignore(item.cwd)
local dir = vim.fn.fnamemodify(file, ":h:t") if raw ~= last_raw then
last_raw, last_rg = raw, compile_globs(raw)
-- Check against all patterns
local all_patterns = M.get_all_patterns()
for _, pattern in ipairs(all_patterns) do
local lua_pattern = pattern:gsub("%.", "%%."):gsub("%*", ".*"):gsub("%?", ".")
if basename:match("^" .. lua_pattern .. "$") or file:match(lua_pattern) then return nil end
end end
-- Check ignored directories -- Match every path segment (directories and basename) anchored,
for _, ignored in ipairs(M.ignored_directories) do -- so `test` drops `test/foo.lua` but never `latest.md`.
if dir == ignored or file:match("/" .. ignored .. "/") then return nil end for seg in file:gmatch "[^/\\]+" do
for _, pat in ipairs(static) do
if seg:match(pat) then return false end
end
for _, pat in ipairs(last_rg) do
if seg:match(pat) then return false end
end
for _, dirname in ipairs(M.ignored_directories) do
if seg == dirname then return false end
end
end end
return true
return item
end end
end end
-88
View File
@@ -1,88 +0,0 @@
--- Terminal configurations using toggleterm
--- This module provides terminal toggle functions for various CLI tools
--- @module config.terminals
local M = {}
--- Check if toggleterm is available
--- @return boolean available Whether toggleterm is loaded
local function is_toggleterm_available()
local ok, _ = pcall(require, "toggleterm.terminal")
return ok
end
--- Initialize all terminal configurations
--- Only runs if toggleterm is available
function M.setup()
if not is_toggleterm_available() then
vim.notify("toggleterm not available, skipping terminal setup", vim.log.levels.WARN)
return
end
local Terminal = require("toggleterm.terminal").Terminal
-- Scooter find and replace terminal
local smartCommit = Terminal:new {
cmd = "smartcommit",
hidden = true,
direction = "vertical",
close_on_exit = true,
on_open = function(term)
vim.cmd "startinsert!"
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
}
-- Scooter find and replace terminal
local scooter = Terminal:new {
cmd = "scooter",
hidden = true,
direction = "vertical",
close_on_exit = true,
on_open = function(term)
vim.cmd "startinsert!"
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
}
-- Mk (make) terminal
local lazygit = Terminal:new {
cmd = "lazygit",
hidden = true,
direction = "float",
on_open = function(term)
vim.cmd "startinsert!"
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
}
-- Mk (make) terminal
local lazydocker = Terminal:new {
cmd = "lazydocker",
hidden = true,
direction = "float",
on_open = function(term)
vim.cmd "startinsert!"
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
}
-- yazi terminal
local yazi = Terminal:new {
cmd = "yazi",
hidden = true,
direction = "float",
on_open = function(term)
vim.cmd "startinsert!"
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
}
-- Export toggle functions to module instead of global namespace
M.scooter_toggle = function() scooter:toggle() end
M.mk_toggle = function() mk:toggle() end
M.lazygit_toggle = function() lazygit:toggle() end
M.lazydocker_toggle = function() lazydocker:toggle() end
M.smartCommit_toggle = function() smartCommit:toggle() end
M.yazi_toggle = function() yazi:toggle() end
end
return M