Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable To Multiline Comment typescriptreact Through Comment.nvim #51

Closed
DennisTheMenace780 opened this issue Dec 19, 2022 · 2 comments
Closed

Comments

@DennisTheMenace780
Copy link

DennisTheMenace780 commented Dec 19, 2022

I have followed the setup guide using Comment.nvim

Comment.nvim

local status_ok, comment = pcall(require, "Comment")
if not status_ok then
  return
end

comment.setup {
    pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook()
}

Treesitter.nvim

local status_ok, configs = pcall(require, "nvim-treesitter.configs")
if not status_ok then
	return
end

configs.setup({
	ensure_installed = "all", -- one of "all", "maintained", or list of languages.
	sync_install = false, -- install languages synchronously
	ignore_install = { "" }, -- List of parsers to ignore installing
	highlight = {
		enable = true, --  false will disable whole extension
		disable = { "" },
		additional_vim_regex_highlighting = true,
	},
	indent = { enable = true, disable = { "yaml" } },
	autopairs = {
		enable = true,
	},
	context_commentstring = {
		enable = true,
		enable_autocmd = false,
	},
})

From everything I have been reading, this is really all that is needed to get going with the plugin. However when I try to do any Visual Line comments i'll get // and Visual Block comments give me a /* */, so it appears to be missing the curly braces. I have also configured Tree Sitter to pick up the context_commentstring, but still not working.

It seems like it won't accept the tsx nodes at all, even if I modify the config option of context_commentstring to include tsx.

I am not sure what I can provide to help diagnose the issue, but happy to upon request.

@DennisTheMenace780
Copy link
Author

Alright so this sucked to figure out, but I figured it out for anyone who comes across this.

Tl;DR the reason I could not get this plugin to work was because my cursor was not on an appropriate Tree Sitter node.

It felt like the plugin was just not hitting the appropriate nodes no matter what I tried, so I installed NvimTreesitter Playground to actually see what was happening behind the scenes. My problem was that I had my cursor at the start of whitespace but on the same line a JSX Element would be, and I thought that would allow me to just comment out what I wanted.

What needed to happen is that the cursor must be placed, literally, on the JSX tag or element for the commenting to work. Once I discovered that, everything worked beautifully.

@JoosepAlviste
Copy link
Owner

Hey @DennisTheMenace780! This issue seems a bit odd since this plugin should definitely handle the case where your cursor is not exactly on the JSX node. For example, in this case:

(
  <div>
    <h1>Hello</h1>
^ -- cursor
  </div>
)

The comment character should be calculated based on the first non-whitespace character in the line:

(
  <div>
    <h1>Hello</h1>
    ^ -- this position is used to calculate the commentstring
  </div>
)

This should result in the {/* %s */} commentstring.

Could you maybe give a specific example of an unexpected use case?

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

No branches or pull requests

2 participants