Skip to content

Conversation

@Kaiser-Yang
Copy link
Contributor

Using node:type() do not work as expected, sometimes, the type will go with a "chunk" or even empty string, we must iterate the node of current line to check if current line is inside a comment block. Besides, in the code, there is no ctx passed to the default function, just remove the parameter in the doc.

Using node:type() do not work as expected, sometimes, the type will go
with a "chunk" or even empty string, we must iterate the node of current
line to check if current line is inside a comment block. Besides, in the
code, there is no `ctx` passed to the default function, just remove the
parameter in the doc.
@Kaiser-Yang
Copy link
Contributor Author

Kaiser-Yang commented Feb 28, 2025

This works fine.

In non-comment context:

image

After I add --:

image

@saghen
Copy link
Owner

saghen commented Mar 24, 2025

Sorry for the delay! I haven't gotten around to this one yet because it's quite a big recipe, to the point that I think we should include a utility function for it in the core. I'd also like to look into ways we can simplify this, if that's possible

@stefanboca
Copy link
Collaborator

You can replace the large if cases with vim.treesitter.node_contains(node, {row, col, row, col}).

You might also consider searching for an injected comment parser, instead of a specific node:

local function inside_comment_block()
  local ok, parser = pcall(vim.treesitter.get_parser)
  if not ok or not parser then return end
  local cursor = vim.api.nvim_win_get_cursor(0)
  local row, col = cursor[1] - 1, cursor[2]
  return parser:language_for_range({ row, col, row, col }):lang()
end

thought this only works for languages whose queries support comment injections, and only if the user has the comment parser installed.

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.

4 participants