tcomment_vim icon indicating copy to clipboard operation
tcomment_vim copied to clipboard

React jsx/tsx

Open zelphir opened this issue 4 years ago • 9 comments

Comments using the default ft in .tsx files doesn't work, it still adds // . The only way I got it working was setting the ft to javascript.jsx, then comments were working fine for both jsx and normal js. I also tried setting the ft to javascriptreact but it didn't work either.

Screenshot 2021-02-10 at 16 42 32 Screenshot 2021-02-10 at 16 42 05

zelphir avatar Feb 10 '21 15:02 zelphir

Which version do you use? Could you please provide me an code example? I assume it uses typescriptreact (//) instead of tsx ({* ... *}). What's the value of g:tcomment#filetype#guess? Does it help to set it to 1? Does let g:tcomment#filetype#guess_typescriptreact = 1 help?

tomtom avatar Feb 11 '21 17:02 tomtom

I assume jsx works properly because the filetype is javascript.jsx and g:tcomment#filetype#guess_javascript = 1.

tomtom avatar Feb 11 '21 17:02 tomtom

I'm of latest master (2de3885).

As code example it's just a basic react jsx but using typescript, so something like:

export default function MyComp() {  
  const data = 1

  return (
    <div>
      <span>{data}</span>
    </div>
  )
}

Running:

set ft ==> filetype=typescriptreact
let g:tcomment#filetype#guess ==> g:tcomment#filetype#guess #0 

let g:tcomment#filetype#guess_typescriptreact = 1 doesn't make any difference 😢

Forcing the filetype to typescript.tsx doesn't work either.

zelphir avatar Feb 11 '21 23:02 zelphir

Hi. I just encounter this issue too but setting

let g:tcomment#filetype#guess_typescriptreact = 1

solve the problem. I hope tcomment can do this automatically, the extension always *.tsx

Cheers.

kuntau avatar Feb 15 '21 16:02 kuntau

I'm having the same problem, setting let g:tcomment#filetype#guess_typescriptreact = 1 only works for commenting not uncommenting. I've collected some debug info on the exact same cursor position, hope this helps fixing the issue:

With let g:tcomment#filetype#guess = 0

TCOMMENT: &ft = typescriptreact => typescriptreact
TCOMMENT: stx = tsxAttrib => tsxAttrib
TCOMMENT: ct  = {'_args': {'beg': 27, 'fallbackFiletype': '', 'end': 27, 'filetype': 'typescriptreact', 'comment_mode': ''}, 'commentstring': '{/* %s */}', 'mode': '', 'filetype': 'tsx'}

With let g:tcomment#filetype#guess = 1 or let g:tcomment#filetype#guess_typescriptreact = 1 (same results), commenting works, but uncommenting doesn't:

TCOMMENT: &ft = typescriptreact => typescriptreact
TCOMMENT: stx = tsxAttrib => tsxAttrib
TCOMMENT: ct  = {'_args': {'beg': 27, 'fallbackFiletype': '', 'end': 27, 'filetype': 'typescriptreact', 'comment_mode': ''}, 'commentstring': '{/* %s */}', 'mode': '', 'filetype': 'tsx'}

With the cursor in the commented JSX code (on an attribute):

TCOMMENT: &ft = typescriptreact => typescriptreact
TCOMMENT: stx = tsxBlockComment => tsxBlockComment
TCOMMENT: ct  = {'rxmid': '', 'rxend': '', '_args': {'beg': 27, 'fallbackFiletype': '', 'end': 27, 'filetype': 'typescriptreact', 'comment_mode': ''}, 'commentstring': '// %s', 'commentstring_rx': '\%%(// %s\|/* %s */\)', 'mode': '', 'filetype': 'typescript', 'replacements': {'*/': {'subst': '|)}>#', 'guard_rx': '^\s*/\?\*'}, '/*': {'subst': '#<{(|', 'guard_rx': '^\s*/\?\*'}}, 'rxbeg': '\*\+'}

I noticed that the comment string changes to the wrong format when the cursor is in the commented block.

s0meone avatar Feb 16 '21 08:02 s0meone

In Vue files it also just comments every block with <!-- -->.

gegoune avatar Mar 19 '21 11:03 gegoune

In Vue files it also just comments every block with <!-- -->.

@cloggier Posted a solution for vue here https://github.com/tomtom/tcomment_vim/issues/284

eduardoarandah avatar Mar 22 '21 18:03 eduardoarandah

@eduardoarandah Thanks, I would rather keep on using treesitter.

gegoune avatar Mar 22 '21 20:03 gegoune

@eduardoarandah You can now use treesitter with https://github.com/JoosepAlviste/nvim-ts-context-commentstring and any commentstring based commenting plugin (I use https://github.com/terrortylor/nvim-comment).

gegoune avatar Mar 26 '21 08:03 gegoune