Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the nvim-remove-comments plugin to use the modern Neovim treesitter API, replacing deprecated nvim-treesitter functions. The changes address errors that occurred on recent Neovim versions where parsers.has_parser() was no longer available.
Changes:
- Migrated from deprecated nvim-treesitter parser API to native vim.treesitter API
- Added user-facing notifications for parser-related errors
- Improved error handling for parser operations and tree parsing
- Removed unused
editsvariable declaration
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local has_parser = pcall(vim.treesitter.language.add, ft) | ||
| if not has_parser then |
There was a problem hiding this comment.
The usage of vim.treesitter.language.add is incorrect for checking parser availability. This function is for registering/adding language parsers, not for checking if they exist.
The correct approach would be to wrap the vim.treesitter.get_parser call in a pcall and check if it succeeds. Consider replacing lines 10-20 with:
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, ft)
if not ok or not parser then
vim.notify("No treesitter parser found for " .. ft, vim.log.levels.WARN)
return
endThis approach directly checks if we can get a parser for the filetype, which is the actual requirement.
| if not parser then | ||
| vim.notify("Failed to get parser for " .. ft, vim.log.levels.ERROR) | ||
| return | ||
| end |
There was a problem hiding this comment.
If the parser check is moved to be combined with vim.treesitter.get_parser (as suggested in the previous comment), then this redundant check on lines 17-20 can be removed entirely, as the parser existence would already be validated by the pcall wrapper.
|
|
||
| local root = parser:parse()[1]:root() | ||
| local trees = parser:parse() | ||
| if not trees or #trees == 0 then |
There was a problem hiding this comment.
Consider adding a notification when no trees are parsed (line 23-25), similar to the other error conditions. This would help users understand why the operation failed silently. For example:
if not trees or #trees == 0 then
vim.notify("Failed to parse syntax tree for " .. ft, vim.log.levels.ERROR)
return
end| if not trees or #trees == 0 then | |
| if not trees or #trees == 0 then | |
| vim.notify("Failed to parse syntax tree for " .. ft, vim.log.levels.ERROR) |
| local root = trees[1]:root() | ||
| local query_str = config.queries[ft] | ||
| local query = ts.query.parse(ft, query_str or [[ (comment) @comment ]]) | ||
| -- local query = ts.query.parse(ft, [[ (comment) @comment ]]) |
There was a problem hiding this comment.
This commented-out line appears to be leftover debug or development code. Consider removing it to keep the codebase clean, as it serves no purpose and the same logic is implemented on line 29.
| -- local query = ts.query.parse(ft, [[ (comment) @comment ]]) |
Was running into errors on latest neovim versions.
Implements:
parsers.has_parser()withpcall(vim.treesitter.language.add)vim.treesitter.get_parser()instead ofparsers.get_parser()editsvariable