Skip to content

fix: update nvim-treesitter API#1

Open
rtuszik wants to merge 1 commit intoKashifKhn:mainfrom
rtuszik:treesitter-compat
Open

fix: update nvim-treesitter API#1
rtuszik wants to merge 1 commit intoKashifKhn:mainfrom
rtuszik:treesitter-compat

Conversation

@rtuszik
Copy link

@rtuszik rtuszik commented Oct 9, 2025

Was running into errors on latest neovim versions.

Error executing lua: ...y/nvim-remove-comments/lua/nvim-remove-comments/core.lua:11: attempt to call field 'has_parser' (a nil value)

Implements:

  • Replace deprecated parsers.has_parser() with pcall(vim.treesitter.language.add)
  • Use vim.treesitter.get_parser() instead of parsers.get_parser()
  • improved notifications
  • remove unused edits variable

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 edits variable declaration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +10 to +11
local has_parser = pcall(vim.treesitter.language.add, ft)
if not has_parser then
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
end

This approach directly checks if we can get a parser for the filetype, which is the actual requirement.

Copilot uses AI. Check for mistakes.
Comment on lines 17 to 20
if not parser then
vim.notify("Failed to get parser for " .. ft, vim.log.levels.ERROR)
return
end
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

local root = parser:parse()[1]:root()
local trees = parser:parse()
if not trees or #trees == 0 then
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Suggested change
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)

Copilot uses AI. Check for mistakes.
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 ]])
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
-- local query = ts.query.parse(ft, [[ (comment) @comment ]])

Copilot uses AI. Check for mistakes.
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.

2 participants