Skip to content

Conversation

@pouyakary
Copy link
Contributor

Hey there. Thanks for the great plugin. I'm the author of the Pro Colors themes. What I wanted to achieve with this theme was to have every token colored as they should be. This is an example of that in TypeScript:

Pro Colors on TypeScript

Every token is colorized. Now, I came across coding with Flutter that I realized the grammar has massive problems and that is not something that I liked:

Dart Language before the new patch

So I worked into the grammar and fixed and improved as many things as I could, while at the same time adding first-class support to Pro Colors. the result of that is this:

Dart code after the patch

Changes

  • So there are lots of things that got changed. First of all, I formatted the code for readability with Virtuous

  • ~/ used to be rendered as two separate operators, it's fixed now.

  • class definitions added.

  • function definitions added.

Screenshot 1401-04-31 at 18 38 29

  • punctuations now have grammars, and can be colorized separately, and have the right priority in the rules.

Screenshot 1401-04-31 at 18 42 27

  • every keyword and type now includes a $1 in their definition, so for example, keyword.control.dart becomes keyword.control.if.dart which makes it possible for theme authors like me to customize every single word as we wish.

Screenshot 1401-04-31 at 18 42 54

  • Type Parameters <T> are colorized in the right way:

Screenshot 1401-04-31 at 18 43 28

  • Object Literal Keys are added:

Screenshot 1401-04-31 at 18 44 20

  • fixed the rule name "variable.other.source.dart to comment.block.documentation.dart

  • matching the => and ( after the function identifier

Screenshot 1401-04-31 at 18 45 57

  • moving all the root patterns to the block repository (to use for string interpolation)

  • moving $ into the string interpolation variable

Screenshot 1401-04-31 at 18 46 44

  • better string interpolation grammar

Screenshot 1401-04-31 at 18 47 42

Screenshot 1401-04-31 at 18 47 53

@pouyakary
Copy link
Contributor Author

Added coloring of the object before the dot notation:

Screenshot 1401-05-01 at 20 28 19

@pouyakary
Copy link
Contributor Author

The class definition has some problems, I'm going to work on that...

@DanTup
Copy link
Member

DanTup commented Jul 23, 2022

Thanks for the contribution! I haven't had chance to look at this in detail yet, but I'll try to do so next week (although I may wait until you think you're done first).

I had been planning to look at this a little in the near future with a goal of making the textmate grammar more closely match the semantic tokens output (see #4062 for one example of why). We'll need to make sure that any changes made in this PR don't make that any worse (I suspect if anything they will make it better, but it's something we'll need to verify). I'd really like to have some better (automated) testing for that since right now changes to this file can be risky, but it's not something that exists today.

Once we are happy with this, we should send the PR to https://github.com/dart-lang/dart-syntax-highlight since that's really the official source of truth for this file now.

Comment on lines +33 to +36
"cSpell.words": [
"dartdoc",
"oldschool"
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file may have been included in error, it doesn't appear to contain any relevant changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the C-Spell plugin in vscode to check the spellings and these two words raised errors, so I added them to the workspace dictionary. You can delete it if you find it useless

@pouyakary
Copy link
Contributor Author

So I worked a little bit on the class definitions and now it includes the inherited class as well:

Screenshot 1401-05-02 at 11 01 13

@pouyakary
Copy link
Contributor Author

pouyakary commented Jul 24, 2022

@DanTup thanks for checking it out. I actually used the JavaScript/TypeScript definitions as a reference (since they are the best grammars implemented so far) and then used their naming (thought well, most themes are targeting those so for example if I change entity.other.inherited-class.js to .dart it'll make the most themes work out of the box with Dart too. So hope it was not a bad break.)

However, Thanks a lot for the heads up! I'll be forking that then :) and probably we can work on it together there

@DanTup
Copy link
Member

DanTup commented Jul 25, 2022

@pouyakary I had a quick compare of this versus the current grammar. Some items like get set as identifiers look better now (they're no longer coloured as keywords), however it looks like String has changed now to no longer be coloured like a class (left is before, right is after):

Screenshot 2022-07-25 at 11 51 37

While String is a built-in type, it's coloured the same as a class when using Semantic Tokens and I definitely don't want to introduce any changes that make additional differences to that (the fewer differences between the grammar and the semantic tokens, the less "flicker" there is when opening a new file when you see the grammar briefly and then the semantic tokens). For references, the same code using semantic tokens looks like this:

Screenshot 2022-07-25 at 11 52 03

I've also added pushed a change to master that now dumps the tokens to snapshot files (see c43941e and 65a577d) using the vscode-tmgrammar-test package that will make verifying changes to the grammar a little easier (although the current test files are quite simple).

If you rebase on master you can run npm run update-grammar-snapshots which will re-generate those snapshots with your changes (which will be required for the tests to pass, allowing the changes to be reviewed in the PR). Feel free to add additional code to the files in the grammar_testing project if there are specific things missing you'd like to ensure don't regress.

(Ultimately I'd like to move tests like that to the dart-lang/syntax-highlight repo, but since this is already using TS/npm it was easier to try out here).

@pouyakary
Copy link
Contributor Author

Fixe a problem with nested generics that looked like this:

Screen Shot 2022-09-20 at 12 19 36 AM

Which is now resolved:

Screen Shot 2022-10-05 at 4 32 02 PM

@pouyakary
Copy link
Contributor Author

String is now a class and all classes can be customizable in the themes:

Screen Shot 2022-10-05 at 4 35 12 PM

@pouyakary
Copy link
Contributor Author

The precedence of assignment operators are fixed so += will no longer be colored as + and =

Screen Shot 2022-10-05 at 4 35 58 PM

@pouyakary
Copy link
Contributor Author

I encountered a problem with a dart library that had uppercase function names. And the grammar counted them as classes:

Screen Shot 2022-09-22 at 11 29 42 AM

I have to play with the grammar to fix this but for now, I just fixed it enough to not recognize it as anything:

Screen Shot 2022-10-05 at 4 41 03 PM

@pouyakary
Copy link
Contributor Author

There was a problem with function definition grammar where it counted await functionName() as definition. It's now resolved:

Screen Shot 2022-09-22 at 6 05 40 PM

@pouyakary
Copy link
Contributor Author

The list of problems that I had found is now fully fixed. the next level would be for me to add tests

@pouyakary
Copy link
Contributor Author

It would be cool if you could check the definitions too @DanTup

@pouyakary
Copy link
Contributor Author

Bug fix. The expand_less : is recognized as a property key in this screen shot:

Screen Shot 2022-10-06 at 12 52 54 AM

And is fixed in this one:

Screen Shot 2022-10-06 at 5 57 44 PM

@DanTup
Copy link
Member

DanTup commented Oct 10, 2022

@pouyakary apologies, I haven't been keeping up with this.

It's hard to review currently because of the formatting changes. Can you re-format it with the original settings to reduce the diff just to the actual changes?

It may also be better to send the PR (or perhaps a few smaller PRs to make it easier to review) to https://github.com/dart-lang/dart-syntax-highlight since that's the source-of-truth for this. It would make sense to add some tests for all of the improvements you've made (we can commit the new tests against the original grammar first, and then it will be easier to see the improvements your changes make).

We'll also want to ensure we don't diverge any further from semantic tokens, I'd like to try and reduce the differences in colours between the grammar/semantic tokens (currently there are quite a lot, although I can see from your commits that you've fixed at least some of them 🙂). I don't currently have an automated way to compare these, although it's something I've been thinking about to try and help them converge.

@pouyakary
Copy link
Contributor Author

@DanTup don't be, I delayed as well. The formatting is because it's just really hard for me to work with the json and this way make it readable for me. I'll fix the spacing. And yeah I think it should be like that. however I have no idea how to move all the history of commits and comments to there.

For the automated testing. I got blocked on the look behind regrex features of the testing. and have no idea how to fix the grammars with that. If that could be solved sure.

And for the semantic tokens. I have used the TypeScript/JavaScript grammar of Microsoft/vscode to see how they view a grammar and since I think all major themes on vscode first implement the TS/JS grammar, it will have some solid grounds. The tokens that go over the bounds of textmate spec are all based on the namings of the JS. I don't know if that's a right idea but I had no better one.

@pouyakary
Copy link
Contributor Author

Needless to say that for each token that you have a better scope I'll appreciate. My only wish doing this was to differentiate between the tokens and have all of them colored.

@DanTup
Copy link
Member

DanTup commented Oct 11, 2022

The formatting is because it's just really hard for me to work with the json and this way make it readable for me. I'll fix the spacing. And yeah I think it should be like that. however I have no idea how to move all the history of commits and comments to there.

I don't think it'll be easy to move all of the commits as-is to another repo, although depending on the scale of the overall changes, a single commit of the new file might not be too difficult to review.

However, you should remove any formatting changes, including changes like this that change the order of fields:

Screenshot 2022-10-11 at 14 20 00

Changes like this make the diff much larger and more difficult to review. Changes are likely to be reviewed and merged much sooner if they are easier to review, because they'll take less time for the reviewer.

For the automated testing. I got blocked on the look behind regrex features of the testing. and have no idea how to fix the grammars with that. If that could be solved sure.

I'm not very familiar with the look behind pattern you used. Could you provide an example (with an explanation of why it's needed) and I can see whether I can find an alternative that works with the Dart regex? Thanks!

Needless to say that for each token that you have a better scope I'll appreciate. My only wish doing this was to differentiate between the tokens and have all of them colored.

Yep, anything working towards this is a great change IMO. Our grammar/semantic tokens are currently quite different (many things are not coloured in the grammar) and it's something I've wanted to improve for a while :-)

@DanTup
Copy link
Member

DanTup commented Jan 4, 2023

@pouyakary what's the current status of this? I replied on dart-lang/dart-syntax-highlight#32 about the issue you had - I didn't really understand the special syntax being used but think we should stick to thinks that Dart can parse (since the grammar is used by more than just VS Code, for example Dart DevTools uses it and parses it using Dart).

@pouyakary
Copy link
Contributor Author

@DanTup I'm actually using the grammar everyday and try to fix the bugs as I encounter them. (yesterday I found out dart can have generator getters and my grammar breaks at get something sync* =>, something I have to push a patch for soon) and then the testing remains which unfortunately I still haven't had the time to go through. And moving the commits to the main repository has become of a challenge for me.

Let me make the patch right now and move the commits to the main dart repo. And since now I've written the most of the grammar, I think I have to request access to the repo and maintain it myself. (which I have no idea who to ask for that)

Let me do the patch now and make a PR today to actually ship a version of this.

@DanTup
Copy link
Member

DanTup commented Jan 4, 2023

And moving the commits to the main repository has become of a challenge for me.

I'm happy to help with this if I can, but we'll need to create test cases for the the things being changed so they can be reviewed (and ensure they don't regress). I think a good first step would be to just extend the tests in https://github.com/dart-lang/dart-syntax-highlight to include code that covers all the things you're changing in the golden tests (right now it will produce the wrong results, but this will make it easier to review the differences when committing the grammar afterwards).

And since now I've written the most of the grammar, I think I have to request access to the repo and maintain it myself. (which I have no idea who to ask for that)

Do you mean the https://github.com/dart-lang/dart-syntax-highlight repo? That's maintained by the Dart team and the best way to get changes is by opening pull requests (which is what I do when I have improvements to make). This grammar is used by many tools so its important all changes are carefully reviewed and tests added to prevent regressions.

Let me know if there's anything I can do to help. Thanks!

@pouyakary
Copy link
Contributor Author

@DanTup okay sure. Let me gradually add things there and use this issue as the changelog.

@DanTup
Copy link
Member

DanTup commented Mar 30, 2023

Going to close this in favour of landing changes in https://github.com/dart-lang/dart-syntax-highlight and then pulling them into here.

I added some comments on dart-lang/dart-syntax-highlight#40 and we can follow up there.

@DanTup DanTup closed this Mar 30, 2023
@DanTup DanTup added the upstream in dart / flutter Needs changing in Dart or Flutter label Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

upstream in dart / flutter Needs changing in Dart or Flutter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants