-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Feature-Request: Add correct highlighting for React.js #11061
Comments
Have you tried brackets-jsx extension? It should add syntax highlighting for |
@petetnt: Yes, I already have it installed, together with jsxHint and other plugins. The code folding and syntax highlighting are looking the way you see them in the screenshot. The highlighting seems to be related to this plugin (it is a current issue there on automenta/brackets-jsx#2). It seems the plugin itself is deprecated. |
So it's kind of understandable, don't you think? The JS you're showing is all kinds of wrong. The fact that React parses it fine, doesn't make it valid JS. To me it's completely normal that Brackets has no idea how to deal with it. In fact, it's doing a pretty good job as it is. The feature request is not to match the correct bracket, it's to support JSX files in terms of highlighting and such. |
@thany: Yes I do understand fully. The problem is that React encourages users to use the .js instead of the older .jsx ending (a thing that seems kind of stupid to me because of all the problem this causes as seen here). As React gains more attention, more projects will be written with it (and files with the .js extension). |
I agree with @weblogixx, we should be able to support JSX syntax in |
CodeMirror doesn't have a React/JS language mode yet either, and I haven't seen attempts at one other than the deprecated code that the |
@anyone modify you codeMirror file inside your brackets installation so you can get syntax coloration for your nested replace:
by
NB: Still waitng for plugin so we can have calltips :( (Tern-react maybe?) |
@dnbard Did you get a chance to try @abdelouahabb 's advice and see if it makes working with React JS code snippets a little better? |
@nethip Nope. This solution didn't worked for me. |
@dnbard it works here, which file did you changed? Of course the support is not at 100%, it will be colored as JS . (the markup will not be checked...) |
@abdelouahabb But I don't need the support of |
Welp, I started working building proper highlighting for which of course results in:
which was added by (issue: #2948 -> PR: #2971). Can you undef a fileExtension via an extension? |
Yes you can, using var jsLanguage = LanguageManager.getLanguage("javascript");
jsLanguage.removeFileExtension("js"); |
Thanks @marcelgerber! |
Any news about this feature? I'll probably start a project with React soon and this thing is driving me crazy :( |
@petetnt just curious on your approach to a solution. Are you creating a CodeMirror JSX mode first and then pulling it into Brackets, or some other approach? |
@lkcampbell The current idea is to create a Sadly I haven't still had much time to work on this and the |
@petetnt okay let me know if I can help out. I have some free time on my hands recently and it would be really cool to get this functionality finally :). When you mentioned a mode already in the wild, are you referring to this link: https://github.com/antimatter15/codemirror-jsx/blob/master/jsx.js |
Yup, that's the one. There's also this newer fork that that still has at least some major issues. If you want to take a crack at it, by all means do. I'd love to see proper support myself too, as pretty much 99% of things I do right now are React based. My version is in a quite bad shape so I don't have much to share with you right now. My current idea was to use |
Actually the overlay way isn't that bad way to get started now that I tried it out again:
Like I said, the prop-handling is the real issue |
@petetnt, I tweaked your code a tiny bit and used the @marcelgerber suggestion to update the .jsx extension on the fly to jsx (should it be JSX?). Try out this code as an extension and see if it acts how you expect it to act: |
@petetnt also, what theme are you using so I can properly compare colors with you. I'm still seeing some strange colors associated with the quoted strings in the html tags as well as a few curly braces that are one color for the left bracket and another color for the right. But it is getting closer. |
Those strange colors are coming from |
The theme is Tomorrows Night Eigthties. For the another overlay, at least something like this is possible: var fooMode = CodeMirror.defineMode("fooMode", function(config, parserConfig) {
/** define fooMode **/
var xmlMode = CodeMirror.getMode(config, {name: 'xml', htmlMode: true});
return CodeMirror.overlayMode(fooMode, xmlMode);
});
CodeMirror.defineMode("javascriptmixed", function(config, parserConfig) {
var jsMode = CodeMirror.getMode(config, 'fooMode');
return CodeMirror.overlayMode(fooMode, jsMode);
}); I tried this when I started running into the |
@petetnt, yes I understand the problem better now. I will spend some time with the "triple overlay" approach today to see if there is reasonably simple solution. If not, I will look into the new CodeMirror mode approach. |
@petetnt The triple overlay approach does work. I suppose, theoretically, you could keep layering overlays on top of each other until you ran out of memory. Some questions for you. Purely brainstorming right now. Is the statement Would it be possible to have a mode that detects a left curly brace and then flips into Javascript mode until hits the matching right curly brace? Then flips back into HTML mode which eventually flips back into Javascript mode again? |
@lkcampbell Basically valid JavaScript, but you can pass in JSX-elements (or DOM-elements). (If you are feeling adventurous that is, not advocating anyone to do so though) Consider something like https://jsfiddle.net/ckeaLv2x/
So in worst case scenario there needs to be tons of flipflopping around XML and JS mode. Then there's things such as curly braces in middle of strings:
But I think the JSMode parser can already correctly catch those, so "Anything valid javascript from "attr={" to the end "}" is a great start. In best case scenario the overlap mode catches the inside |
@petetnt Ugh!! Okay, send me an email at my same user name at gmail.com. This clearly is not a weekend project. Let's discuss more and not use up anymore issue comments on this. |
@marijhn just added an |
@petetnt just packed it to the extension and uploaded: https://github.com/apla/brackets-jsx So, we can use |
@apla code folding works if you select the block of code, |
@apla You probably already know this, but just in case, issues with the new mode are being tracked at codemirror/codemirror5#3745. The new mode is being updated fairly regularly. That way, you can keep your extension synced up with the changes. @petetnt or @dabbott, the code folding doesn't appear to be working in Brackets. Do either of you know if the CodeMirror JSX mode and the CodeMirror fold addon are working together? |
Pinging @thehogfather as he is the Code Folding expert here. I'd assume that getting the folding working correctly (without selection) might require quite a lot of work. |
@petetnt not necessarily a lot of work. The Brackets code folding is built on top of the CodeMirror fold addon code. So, if the new CodeMirror jsx mode works with the existing CodeMirror fold addon code, it should mostly work on the Brackets side as well with minimal tweaking. If it doesn't work, that's a use case that needs to be considered on the CodeMirror side. |
@petetnt @lkcampbell it should not be a lot of work to get code folding to actually work for jsx. it should be a matter of registering the correct fold helpers for the jsx mode (in this case at least brace fold and xml fold). I'll have a quick look and post any updates here. |
So it seems that the jsx mode was not correctly returning the contextual mode of the document - always defaulting to jsx (instead of switching between javascript and xml). @apla this pr should fix it. But please test it more than I have - (only tested for about 10 minutes). Also flagged it up [in this codemirror commit].(codemirror/codemirror5@b3f9487) |
Tracking to verify after cm integration #12177 |
Now JSX is available. |
JSX formatting works well for |
@timburgess I've been thinking about using the |
@petetnt problem with moving the extension type I think we need to look at the existing "htmlmixed" mode to create something similar for Javascript/JSX. |
@timburgess my languages.json now looks like this: "javascript": {
"name": "JavaScript",
"mode": "jsx",
"fileExtensions": ["js", "jsx", "js.erb", "jsm", "_js"],
"blockComment": ["/*", "*/"],
"lineComment": ["//"]
}, JSX is already a |
@petetnt Nice. That works well. |
If you're using binary version you can achieve this by putting in brackets.json (Debug -> Open Preferences File)
|
Hi there,
it would be nice if brackets could automatically detect jsx-code in .jsx (and possibly .js) files. This would make it easier to develop React-Components in Brackets.
Please look at the attached screenshot to see what I mean.
The text was updated successfully, but these errors were encountered: