JoosepAlviste / nvim-ts-context-commentstring

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

Invalid comment string in jsx, multiline exprassion inside `{}` #22

Open MuhammadSawalhy opened 3 years ago

MuhammadSawalhy commented 3 years ago

Here is the current commentsting:

image

Here is what I expected:

image

JoosepAlviste commented 3 years ago

Hey @MuhammadSawalhy, thanks for the bug report!

I added an extra rule for this exact use-case, but in general, it's a bit tricky to detect similar multi-line expressions inside {} with the current logic. I think that it would require thinking through the problem a bit more.

Together with the {}, the treesitter node is called jsx_expression but we can't really configure jsx_expression: '// %s', because then commenting a single-line expression would be incorrect. The first idea I had was that we could detect if the parent of a node is jsx_expression. We could have a hypothetical configuration like so:

javascript = {
  __default = '// %s',
  jsx_element = '{/* %s */}',
  jsx_fragment = '{/* %s */}',
  jsx_attribute = '// %s',
  comment = '// %s',
  __parent = {
    -- if a node has this as the parent, use the `//` commentstring
    jsx_expression = '// %s',
  },
}

Anyways, I'm not sure if I'll have time to implement this soon. Hopefully the current fix is good enough for now 😄

tbo commented 2 years ago

I've got a similar issue with html string literals:

Screen Shot 2022-04-03 at 11 57 44