Skip to content

Refactor: modernize plugin, deduplicate code, improve robustness#10

Merged
sigfriedCub1990 merged 4 commits into
mainfrom
refactor/improvements
Mar 5, 2026
Merged

Refactor: modernize plugin, deduplicate code, improve robustness#10
sigfriedCub1990 merged 4 commits into
mainfrom
refactor/improvements

Conversation

@sigfriedCub1990
Copy link
Copy Markdown
Owner

Summary

Comprehensive refactoring of the nvim-pytrize.lua codebase addressing code duplication, API modernization, performance, and robustness. All 77 tests pass.

Commits (4 steps)

1. Critical fixes (5a6078c)

  • Extract shared helpers — Duplicated get_param_name_node, find_body_references, and find_fixture_references from rename.lua and usages.lua into new lua/pytrize/ts_helpers.lua. Reduces usages.lua by ~70% and rename.lua by ~40%.
  • Modernize commands — Replace vim.cmd('command ... lua ...') with vim.api.nvim_create_user_command() with desc fields.
  • Error handling — Add pcall to treesitter parser in call_spec.lua; add file/JSON error handling in nodeids.lua.
  • Fragile tree access — Replace arguments:child(N) with named children iteration in call_spec.lua.

2. API modernization (b7bee1d)

  • Simplify marks.lua — Replace manual extmark ID tracking with nvim_buf_clear_namespace().
  • Simplify warn.lua — Remove vestigial vim.fn.escape() and \n handling; just call vim.notify().
  • Remove custom min/max — Use math.min/math.max directly.
  • Modernize utils.with_buf — Use nvim_buf_is_loaded(), vim.bo[], vim.fn.bufload(), pcall safety.
  • Modernize jump/util.lua — Use vim.cmd.edit().

3. Performance & robustness (0df39da)

  • Cache treesitter query — Parse parametrize query once at module level in call_spec.lua.
  • Settings type validation — Reject values with wrong type in settings.update().
  • Cache auto-invalidation — Add BufWritePost/BufDelete autocommand to clear stale fixture scan cache entries.

4. Polish (f75d777)

  • Health check — Add lua/pytrize/health.lua for :checkhealth pytrize.
  • LuaCATS annotations — Add @param/@return/@class to key modules.
  • Drop compat shim — Use vim.treesitter.query.parse directly (requires Neovim >= 0.9).

Files changed

File Action
lua/pytrize/ts_helpers.lua New — shared treesitter helpers
lua/pytrize/health.lua New:checkhealth support
lua/pytrize/rename.lua Rewritten to use shared helpers
lua/pytrize/usages.lua Rewritten to use shared helpers
lua/pytrize/marks.lua Simplified extmark management
lua/pytrize/warn.lua Simplified
lua/pytrize/utils.lua Removed min/max, modernized with_buf
lua/pytrize/call_spec.lua Error handling, query caching, robust access
lua/pytrize/nodeids.lua Error handling
lua/pytrize/init.lua Modern commands, annotations
lua/pytrize/api.lua Annotations, idiomatic patterns
lua/pytrize/ts.lua Cache auto-invalidation
lua/pytrize/settings.lua Type validation, annotations
lua/pytrize/jump/util.lua Modernized
lua/pytrize/jump/param.lua Uses math.min/max
tests/pytrize/utils_spec.lua Updated for new API
tests/pytrize/usages_spec.lua Updated export name
tests/pytrize/settings_spec.lua Added type validation test

…, add error handling

- Extract duplicated get_param_name_node, find_body_references, and
  find_fixture_references into new lua/pytrize/ts_helpers.lua module.
  Both rename.lua and usages.lua now use the shared helpers, reducing
  usages.lua by ~70% and rename.lua by ~40%.
- Also extract find_python_files (grep wrapper) into ts_helpers.
- Modernize command registration in init.lua: replace vim.cmd('command ...')
  with vim.api.nvim_create_user_command() including desc fields.
- Add pcall error handling to treesitter parser in call_spec.lua (get_root)
  so missing Python parser gives a clean warning instead of a crash.
- Add file existence and JSON parse error handling in nodeids.lua.
- Replace fragile arguments:child(N) with named children iteration in
  call_spec.lua (get_second_arg_node and first-arg access in get_calls).
- Update usages_spec.lua for renamed internal export.
@sigfriedCub1990 sigfriedCub1990 force-pushed the refactor/improvements branch from f75d777 to 34a7fdb Compare March 5, 2026 21:35
… Neovim APIs

- Rewrite marks.lua: replace manual extmark ID tracking (ext_ids,
  next_ext_ids tables) with nvim_buf_clear_namespace(). Use
  nvim_get_current_buf() instead of vim.fn.bufnr().
- Simplify warn.lua: remove vestigial vim.fn.escape() and manual \\n
  handling left over from old vim.cmd('echohl...') approach. Now just
  calls vim.notify() directly.
- Remove custom min/max from utils.lua, use math.min/math.max in
  jump/param.lua instead.
- Modernize utils.with_buf: use vim.api.nvim_buf_is_loaded(),
  vim.bo[bufnr].filetype, vim.fn.bufload(), and pcall safety for the
  callback. Errors are now re-raised after cleanup.
- Modernize jump/util.lua: use vim.cmd.edit() instead of
  vim.fn.execute('edit ...').
- Use idiomatic 'opts = opts or {}' and 'bufnr or 0' patterns in
  api.lua and init.lua.
- Cache the treesitter parametrize query at module level in call_spec.lua
  instead of re-parsing it on every call to get_param_call_nodes.
- Add type validation to settings.update(): reject values whose type
  doesn't match the default, with a warning message.
- Add BufWritePost/BufDelete autocommand in ts.lua to automatically
  invalidate fixture scan cache entries when Python files are saved or
  deleted. clear_scan_cache() now accepts an optional filepath argument.
- Add LuaCATS annotations to settings.lua (@Class PytrizeSettings).
- Add lua/pytrize/health.lua for :checkhealth pytrize support. Checks
  Neovim version (>= 0.9), Python treesitter parser, grep availability,
  and configured highlight group existence.
- Add LuaCATS @param/@return doc annotations to api.lua, marks.lua, and
  init.lua for better lua-language-server discoverability.
- Remove deprecated ts_query.parse_query compatibility shim in
  call_spec.lua; use vim.treesitter.query.parse directly (requires
  Neovim >= 0.9).
@sigfriedCub1990 sigfriedCub1990 force-pushed the refactor/improvements branch from 34a7fdb to 821e274 Compare March 5, 2026 21:39
@sigfriedCub1990 sigfriedCub1990 merged commit 6f76b5e into main Mar 5, 2026
2 checks passed
@sigfriedCub1990 sigfriedCub1990 deleted the refactor/improvements branch March 5, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant