1 +#
Plan 001: Simplify and Optimize Neovim Config 3 +> Bugs have been fixed. This plan covers remaining performance, perceived performance, and maintainability improvements.
7 +> Removed: `set_makeprg_for_just_projects` autocmd (1.2) — deemed useless.
8 +> Done: 1.3 SHA256 → concat (17,329x faster per benchmark).
9 +> Done: 1.4 Pre-build default_markers table (avoids join allocation on every call).
10 +> Done: 1.5 LSP global keymaps moved out of LspAttach.
11 +> Done: 2.1 Defer devicons loading (mini.icons via puk.defer).
12 +> Done: 3.1-3.6 (simplification — diagnostic icons, cheatsheet categories, lsp_info extraction, LspClick rename, prequire cache, patches/string removal).
13 +> Fixed: LSP servers not loading (vim.lsp.enable(servers) was deleted in 9c4290c).
19 +###
1.1 Cache `os_background` result to file 21 +**File:** `lua/std/const.lua`
23 +`os_background()` calls `vim.system(...):wait()` with a 2-second timeout, blocking startup.
26 +- Cache result to `stdpath('cache')/os_background`
27 +- On load, read cache file (fast filesystem read)
28 +- Only re-query when cache is stale (e.g. > 1 hour old) or missing
29 +- Listen for OS theme change signals to invalidate
31 +###
1.3 Replace SHA256 cache hashing with string concatenation ✅ 33 +**File:** `lua/std/project.lua`
35 +`vim.fn.sha256` is called 3 times per cache lookup. Overkill for an in-memory cache key.
38 +- Replace `create_hash` with simple string concatenation: `path .. '|' .. markers_str .. '|' .. stop`
40 +###
1.4 Pre-build default_markers table once 42 +**File:** `lua/std/project.lua`
44 +`find_project_root` calls `join(default_markers, markers)` on every invocation, allocating a new 16+ element table each time.
47 +- Build the default markers table once at module load time
48 +- Only merge custom markers when actually provided
50 +###
1.5 Move LSP global keymaps out of LspAttach callback 52 +**File:** `lua/bugabinga/lsp.lua`
54 +Lines 53-130 re-create the same global keymaps each time an LSP client attaches. Only the inlay hint toggle (buffer-local) needs per-attach creation.
57 +- Register global `gr*` keymaps once at module load (outside the autocmd)
58 +- Keep only buffer-local keymaps (inlay hint toggle) inside `lsp_attach`
60 +###
1.6 Speed up `is_ignored` with O(1) lookup first 62 +**File:** `lua/bugabinga/treesitter.lua`
64 +Linear scan with pattern matching on every `FileType` event.
66 +**Benchmark result (TDD with `vim.uv.hrtime()`):**
68 +| Scenario | Linear (current) | Optimized (kv+patterns) | Verdict |
70 +| Exact match "help" | ~0.3 ns/op | ~0.2 ns/op | neutral |
71 +| Pattern match "gitcommit" | ~0.2 ns/op | ~0.2 ns/op | neutral |
72 +| No match "lua" | ~1539 ns/op | ~31 ns/op | 50x faster |
74 +The only meaningful win is the worst-case (no match → full scan). Exact-match and pattern-match cases — likely the common cases — show no improvement. JIT inlining of small functions dominates the early-return cases, making the benchmark unreliable for those.
76 +**Resolution:** Closed — not worth the complexity. The worst-case at ~1.5 µs is already fast enough. Added correctness tests instead.
79 +- `tests/bugabinga/treesitter_spec.lua` — correctness tests for `is_ignored` (10 cases)
80 +- `lua/std/ignored.lua` — added `filetypes_patterns` (pre-built list of regex entries)
81 +- `lua/bugabinga/treesitter.lua` — added `return { is_ignored = is_ignored }` for testability
83 +###
1.7 Cache cwd in statusline git component 85 +**File:** `lua/bugabinga/statusline.lua`
87 +`vim.uv.cwd()` called on every statusline render.
89 +**Done.** `cached_cwd` module-level variable updated on `DirChanged` event.
92 +- `lua/bugabinga/statusline.lua` — added `cached_cwd = vim.uv.cwd()` at module level; `git_component()` now uses it instead of calling `vim.uv.cwd()` on every render; `DirChanged` autocmd updates `cached_cwd` alongside clearing the git cache
93 +- `tests/bugabinga/statusline_spec.lua` — 3 tests for git component caching behavior
97 +##
2. PERCEIVED PERFORMANCE (Deferred Loading) 99 +###
2.1 Defer `devicons` loading ✅ 101 +**File:** `lua/bugabinga/devicons.lua`
103 +Uses `puk()` (synchronous). Icons aren't needed until first render.
105 +**Done.** Changed `puk 'mini.icons'` to `puk.defer('mini.icons')`. Icons load on next event loop tick.
107 +###
2.2 Defer `oil` (file explorer) loading 109 +**Skipped — not worth the complexity.** Oil's keymap, autocmd, and global function reference `oil` at registration time. Deferring requires restructuring to register those inside the defer callback, which is invasive for marginal gain.
111 +###
2.3 Defer `nvim-treesitter` setup 113 +**Skipped — not worth the complexity.** The `FileType` autocmd calls `require 'nvim-treesitter.parsers'` which would fail if treesitter hasn't loaded yet. Deferred setup risks breaking parser auto-detection on early file opens.
115 +###
2.4 Consider deferring more option modules 117 +**Skipped.** `core` (mapleader) must be early. `filetypes` is already minimal. Not worth the risk.
121 +##
3. SIMPLIFICATION & MAINTAINABILITY 123 +###
3.1 Simplify `diagnostic.lua` data structure ✅ 125 +**Done.** Replaced dual array+named-lookup with a simple named table. `prefix_format` now converts numeric severity to name via `vim.diagnostic.severity[context.severity]`.
127 +###
3.2 Deduplicate cheatsheet category definitions ✅ 129 +**Done.** Extracted shared `categories` table and `category_patterns` array. Both functions now reference the same data.
131 +###
3.3 Extract LspInfo popup to separate module ✅ 133 +**Done.** Extracted 80-line inline popup from `lsp.lua` into `bugabinga/lsp_info.lua`. `lsp.lua` now calls `require('bugabinga.lsp_info').show(clients)`.
135 +###
3.4 Rename `_G.LspClick` to avoid namespace collision ✅ 137 +**Done.** Renamed to `_G.__bugabinga_lsp_click` in both definition and statusline reference.
139 +###
3.5 Cache prequire proxy for missing modules ✅ 141 +**Done.** Added `proxy_cache` table keyed by module name. Proxies are now reused on repeated failed requires.
143 +###
3.6 Delete `patches/string.lua` ✅ 145 +**Done.** `string.strip` was defined but never used. Removed the file and the `require 'patches.string'` from `patches/init.lua`.
149 +##
4. ARCHITECTURE CONSISTENCY 151 +###
4.1 Standardize sync vs defer for all plugins 153 +**Pattern:** All plugins that don't affect initial render should use `puk.defer`.
155 +**Candidates for deferral:**
156 +- `devicons` -> `puk.defer`
157 +- `oil` -> `puk.defer`
158 +- `nvim-treesitter` setup -> `puk.defer`
159 +- `mini.icons` -> `puk.defer`
161 +**Already correctly deferred:**
162 +- `mini.pick`, `mini.extra`, `mini.clue`, `mini.diff`, `mini.jump2d`
163 +- `mini.move`, `mini.hipatterns`, `mini.splitjoin`, `mini.bracketed`
164 +- `conform`, `grug-far`, `nvim-surround`, `nvim-autopairs`
165 +- `colorizer`, `specs`, `rulebook`, `incline`, `nvim-ts-autotag`
167 +###
4.2 Consider shared augroup naming convention 169 +**File:** `lua/std/auto.lua`
171 +Each `auto('name')` creates a separate augroup. Fine for isolation, but no way to group related autocmds.
174 +- Consider a naming convention: `auto('lsp.attach')`, `auto('lsp.completion')`, etc.
175 +- Or add a second-level grouping API (low priority)
179 +##
Implementation Order 181 +1. Cache `os_background` (biggest startup win)
182 +2. Move LSP global keymaps out of LspAttach
183 +3. Defer `devicons` (done); oil/treesitter skipped — not worth complexity
184 +4. Pre-build default_markers, simplify SHA256 caching
185 +5. Speed up `is_ignored` with kv lookup first
186 +6. Cache cwd in statusline
187 +7. Extract LspInfo, dedupe cheatsheet categories, simplify diagnostics
188 +8. Rename `_G.LspClick`, cache prequire proxy
189 +9. Evaluate `patches/string.lua` extraction