nvim-ts-context-commentstring icon indicating copy to clipboard operation
nvim-ts-context-commentstring copied to clipboard

fix(jsx): correctly comment first line of JSX

Open JoosepAlviste opened this issue 2 years ago • 1 comments

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

JoosepAlviste avatar Dec 31 '22 21:12 JoosepAlviste

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.

Slotos avatar Nov 15 '23 21:11 Slotos