JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.18k stars 35 forks source link

fix(jsx): correctly comment first line of JSX #56

Open JoosepAlviste opened 1 year ago

JoosepAlviste commented 1 year ago

The first and last lines of JSX should be commented with // %s, rather than {/* %s */}. However, the current way of configuring the plugin does not allow us to check if the current node is a jsx_element with a parent of parenthesized_expression. We could allow checking the parent node in the configuration, but that would be a bit tricky and a lot of work.

Since this is (hopefully) a 1-time exception, then it seems okay to add an explicit check for this. If we have similar cases in the future though, then we should think of making this configurable somehow.

Fixes #29

Slotos commented 1 year ago

A maybe viable idea - generalize this to alternates with an optional "only child" configuration directive. E.g.:

object = { '// %s', { '/* %s */', only_child = "comment" }}, -- first line
jsx_expression = {{ '/* %s */', only_child = "comment" }}, -- later lines

Then, when checking the node, alternates can be checked with something like (node:child_count() != 1 or node:child(1):type() != match.only_child). If it matches, use the alternate, use default if present, fallback to parent otherwise.

A similar "parent" directive could be employed too.