-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Wrong comment format in JSX blocks #1075
Comments
The main obstacle to this is that comment tokens (and language-data in general) are currently per-language, but the JavaScript language, with JSX, includes two different contexts with their own commenting style. We could use the Maybe we can extend the language-data interface to make this kind of thing better to express, I'll have to think about it a bit more. |
FEATURE: Syntax-driven language data queries now support sublanguages, which make it possible to return different data for specific parts of the tree produced by a single language. Issue codemirror/dev#1075
FIX: Make sure code in JSX context can be commented correctly. Issue codemirror/dev#1075
Attached patches add support for overriding language data within a language to @codemirror/language and use it in @codemirror/lang-javascript to improve commenting of JSX. Let me know if this works for you. |
I think that required another fix which wasn't released yet. Try with @codemirror/commands 6.2.1 |
hmm just tried and it seems that regressed the initial fix 😕 from above Repl: |
In that context, JavaScript-style comments are valid, aren't they? The start of the line isn't in JSX context yet, so you can comment it using |
No if it's within the parenthesis, it's not valid since that indicates the bounds of the JSX context. If it were inline (e.g. |
Other than that case though, it seems to be working well! |
The parentheses aren't part of JSX as far as I can see—people for some reason put them around JSX expressions, but those are just regular JS parentheses, no? |
No when returning JSX inside a functional component in React, everything in the parenthesis is parsed as JSX which means Here's an example of what I mean, including the inline case when Easy to repro if you fork this Repl (assuming you have a Replit account 😄) |
How would that work, syntactically speaking? The parser doesn't know you're in a React component, so parentheses are parsed as normal. The reason that becomes invalid is that it produces an empty pair of parentheses. There's no guarantee that commenting out lines will leave your code syntactically valid. |
Hmm fair enough. I guess the only way to differentiate is if you have JSX on the same line inside the parenthesis. I guess I was confused bc my local editor (Neovim) handles that case as I described (although un-toggling still inserts |
as an aside, do you think it's worth exporting the |
I think in that case you're responsible for configuring it as well. The
Linewise commenting looks up the language context at the start of the line, because that's where it is going to be inserting comment tokens. This can still go wrong of course (say, if you end up inserting block comment tokens on a line that's a language boundary), but having custom intelligent commenting routines for every kind of context is more than I'm willing to commit to—and more generally, a 'comment this line' feature that never introduces syntax errors seems tricky to design. |
Looks like this breaks commenting in large selections that include both JSX and regular JS. I've opened a separate issue here: #1105 |
Describe the issue
It's nice that you can add JSX support for JS files with
javascript({jsx: true})
but one papercut that we've been running into when using it is that the wrong comment format is used with the toggle comment command/shortcut in JSX blocks. In this context, it still uses the default//
line comment in js, but it should be using the block comment format wrapped in braces like so:{/* ... */}
. Otherwise, there will be a compiler error as having//
in JSX blocks is not valid but block comments wrapped in braces is.See demo:
Also happy to take a stab at it myself and open a PR if you're busy! I think the solution here involves using an extension that sets the
languageDataAt
while the cursor is in a JSX block, the hard part is figuring out when you're in that context (still a bit of a Lezer noob and JSX blocks can get quite complex) and also to know when the line is already commented with that format, so any tips would be appreciated!Browser and platform
Firefox 109.0.1, MacOS Ventura 13.0.1 (but should repro on all browsers/devices)
Reproduction link
https://cm-jsx-comment.sergeichestakov.repl.co/
The text was updated successfully, but these errors were encountered: