lsp: auto-regenerate textmate grammar based on a project config#1757
lsp: auto-regenerate textmate grammar based on a project config#1757adam2am wants to merge 11 commits intoDanielXMoore:mainfrom
Conversation
…run command to trigger the regeneration
| // Check if options have changed by comparing with cache (async) | ||
| cachedOpts .= {} | ||
| try | ||
| await fsPromises.access cacheFile |
There was a problem hiding this comment.
We shouldn't call access before readFile. There's no guarantee that the file remains in the same state after the call to access and readFile will error anyway if the file is unreadable.
STRd6
left a comment
There was a problem hiding this comment.
Thanks, looks like a good overall approach and it's nice to see the grammar getting some enhancements.
I'll play around with it and get it merged/deployed soon.
| Edge-case matrix covered: | ||
| # comment + comment | ||
| code # trailing + comment | ||
| array.# length X not comment |
There was a problem hiding this comment.
I think this now parses as a comment after #1750
|
Hmm... actually looking a little closer this has some issues with multiple projects. Since it rewrites the grammar file and requires a restart of VSCode it won't work well if there are multiple projects with different configuration. I was hoping VSCode had some kind of way of modifying the grammars but this may be overly complex for how many additional edge cases come up. One approach we've been thinking about was using the TypeScript semantic tokens to provide more accurate syntax highlighting. It may be a bit more work but it should work in much broader cases as well. |
Yeah, but isn’t textmate grammar static on vscode load? I’m not sure it’s even possible to conditionally add rules to textmate and handle the parse options from config without a window reload. |
|
Another possibility is that we can provide an alternative grammar that can be toggled in the file type setting. This way someone can choose the grammar based on their setting and we can add more as needed. So for now We may even be able to reuse large parts of the grammar if they have some kind of include/import capability. |
Agree, it might be an angle. But my main concern is scalability, for now we’d ship 2 options ( |
|
(Sorry, wrote this earlier, but just sending now.) I did a little research on the possibilities here. One potentially useful workaround is Alternatively, perhaps we could maintain multiple rewritten versions of the grammar, like in this PR, only writing versions lazily as needed, which would enable a mixed codebase. We could even select which grammar based on global config and opening pragmas. And by being lazy, we wouldn't need to have all combinations, only those used. Some more related discussion here: microsoft/vscode#68647 |
|
I'm fine with generating up to |
I'm okay with any of the approaches. Whatever it's a My main question here is about the file type approach: can an extension easily change the language association for all relevant files in a workspace? Or would this require users to manually configure files.associations in their settings, which might be a usability hurdle? |
I think the extension can set the language association for each file it's applied to, via |
|
@STRd6 @edemaine Hey, I managed to get this working dynamically via the Here's a repo where you can see it in action. A quick heads-up: the branch is a bit messy because I've bundled in other features I was testing (like a https://github.com/adam2am/Civet-lspcheck/tree/battleground-renameHandler I'll clean it up and split it into bite-sized PRs a little later |
what sort of syntax highlighting differences are there with the config changes?
TextMate grammars are only loaded when needed
sounds good |
parseOptions : coffeeComment is bringing coffeescript type comments some llms when generating civet sometimes would confuse it for a coffeescript and would generate this type of comments instead of javascript type |
The Motivation:
Previously, enabling a feature like coffeeComment in civetconfig.json would not visually update the syntax highlighting, linter error is gone, but syntax highlighting is static, it would still highlight different words with color inside of the # comment instead of it being a comment
The Solution:
This PR implements a regeneration of a civet.json syntax file based on config (currently only for coffeeComment parse option), and a file watcher that monitors Civet config files. On any change, it automatically regenerates the TextMate grammar (if needed) while keeping previous behavior of restarting a language-server.
The only thing user need to do -> reload the window. The toast appears for that, where you can click the
Reload WindowKey Benefits
How It Works: