Refactor: modernize plugin, deduplicate code, improve robustness#10
Merged
Conversation
…, 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.
f75d777 to
34a7fdb
Compare
… 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).
34a7fdb to
821e274
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)get_param_name_node,find_body_references, andfind_fixture_referencesfromrename.luaandusages.luainto newlua/pytrize/ts_helpers.lua. Reducesusages.luaby ~70% andrename.luaby ~40%.vim.cmd('command ... lua ...')withvim.api.nvim_create_user_command()withdescfields.pcallto treesitter parser incall_spec.lua; add file/JSON error handling innodeids.lua.arguments:child(N)with named children iteration incall_spec.lua.2. API modernization (
b7bee1d)marks.lua— Replace manual extmark ID tracking withnvim_buf_clear_namespace().warn.lua— Remove vestigialvim.fn.escape()and\nhandling; just callvim.notify().min/max— Usemath.min/math.maxdirectly.utils.with_buf— Usenvim_buf_is_loaded(),vim.bo[],vim.fn.bufload(),pcallsafety.jump/util.lua— Usevim.cmd.edit().3. Performance & robustness (
0df39da)call_spec.lua.settings.update().BufWritePost/BufDeleteautocommand to clear stale fixture scan cache entries.4. Polish (
f75d777)lua/pytrize/health.luafor:checkhealth pytrize.@param/@return/@classto key modules.vim.treesitter.query.parsedirectly (requires Neovim >= 0.9).Files changed
lua/pytrize/ts_helpers.lualua/pytrize/health.lua:checkhealthsupportlua/pytrize/rename.lualua/pytrize/usages.lualua/pytrize/marks.lualua/pytrize/warn.lualua/pytrize/utils.lualua/pytrize/call_spec.lualua/pytrize/nodeids.lualua/pytrize/init.lualua/pytrize/api.lualua/pytrize/ts.lualua/pytrize/settings.lualua/pytrize/jump/util.lualua/pytrize/jump/param.luatests/pytrize/utils_spec.luatests/pytrize/usages_spec.luatests/pytrize/settings_spec.lua