-
Notifications
You must be signed in to change notification settings - Fork 78
Add TextMate grammar support back to VSCode extension #410
base: master
Are you sure you want to change the base?
Conversation
I've done similar research and come to similar conclusions, and I have started work on this in the In particular, I've been writing a new textmate grammar based on the Typst lexer/parser. This is because any incompatibility between the grammar and Typst could cause worse bugs than we have without textmate. For example, if a region of markup was incorrectly highlighted by TM as code, it would be hard for users to see why only certain brackets are wrongly highlighted (or not highlighted). Moreover, because users can't opt out of TM highlighting, a bug like this would affect all users. While writing this grammar, I've come to believe that it's likely impossible, or at least very difficult, for TM to correctly give a complete highlighting of Typst code (in summary, it has very poor support for syntactic structures which run across multiple lines, and Typst has these). This has led me to a few other ideas:
I also have a few relevant thoughts on the scope of this project. In particular, I think #252, #260 and #375 are strictly out of scope. I view this project as primarily a language server with associated extensions for language clients when needed, so VS Code's behavior when it does not use the LSP, and the reliance of other extensions on TM scopes, isn't a core part of our requirements. So, even if it would resolve these issues, if TM causes problems, I would rather not add it. That said, I'd like to resolve those issues if possible, since it would improve the user experience with Typst. Similarly, I don't think of it as the responsibility of this project to provide a TextMate grammar for Typst. It's also possible for other VS Code extensions to provide a TM grammar for Typst in case users would like non-LSP highlighting. If we provide a very simple grammar, other extensions (or even a second extension sourced from this repo) could provide injection grammars, and could easily be disabled if they cause problems for users. Again, I'd like to provide a grammar because it would be good to, but not if it comes at a high cost. (On the topic of linked issues, #264 and #401 are the result of incorrect handling of multiline semantic tokens. It may be superficially hidden by TM, but the fundamental issue will remain. I'm aware of this and understand the underlying problem, but haven't gotten to fixing it yet.) Ultimately, I think we shouldn't, and that we don't need to, ship an incorrect TM grammar. If we do include one, I'd prefer it be simple so that we have a lowered maintenance overhead.
|
@nvarner, my consideration is following: You position is totally correct and your comment brings me many new points. This repository is all about typst LSP, rather than typst language support. If we maintainers find a bug in TextMate grammar (doesn't ship in LSP extension), we have no responsibility to resolve them, but close them as unplanned. However, as a part of community, if we don't have a way to redirecting them, it would be frustrated. for regular document writers, who don't quite know coding, it is hard to let them distinguish a LSP and a fully language support. When they find a syntax highlighting error, they consider a bug in LSP, come here, and open an issue, for example, Brackets are colorized inside strings and comments #314. Considering that, imo the most correct decision is removing language-configuration.json and create a new vscode extension repository named "Typst Language Support" containing the language-configuration file and the tmLanguage file.
It brings more complexity to users but more correct to me. As for writing a new tm grammar, I'm happy that you have restarted it, we can move discussion then. The create a new "Typst Language Support", there are two options:
Experiment, we extension can contribute a language without https://github.com/nvarner/typst-lsp/blob/0d5b9330a1d515fa47456e755f751ea4056282b8/editors/vscode/language-configuration.json Result with only LSP extension: Result with only Language Support extension (TextMate grammar to add): Result with only Both extension: |
If we consider removing the language-configuration file, we can change this PR and still close/transfer these 7 issues. |
I like your suggested approach. I think it's best to keep the Typst Language Support extension within this repository, since it will likely be closely related to the LSP extension. I'll do some more experimentation and find the best way to split the extension and make users aware that they'll need to install two things. |
This PR adds TextMate grammar support back to VSCode extension, using grammar from michidk, @michidk.
There is a problem drop at the begin of PR: what's the relationship between tm grammars and semantic tokens providers? The short answer is we need both of them.
From some comment and commit, a language support would like to provide both of them to gain a best syntax and semantic language support.
Dark ships syntax definitions in their extension.
VSCode maintains a TextMate grammar for rust and enables it even if a semantic tokens provider is added.
In Rainbow brackets on type generics rust-lang/rust-analyzer#14985 (comment)
This is why we don't see "grammar" contribution in package.json of rust-analyzer's extension.
In #252, @nvarner commented:
It is correct that "the TextMate grammar is out of date", but there are two direction rather than avoid using TextMate grammar.
I prefer to point 2, since we community also needs a nice TextMate grammar for syntax highlighting on GitHub. It would be nice if we can contribute one.
What does it fix?
Close No highlighting with typst-lsp.semanticTokens set to disable #252
Fix by providing highlighting by TextMate grammar in default.
Close Port some changes from the linguist pull request? #256
Fix by reintroducing it.
Close Highlighting not working for unsaved snippets on VS Code #260
Fix by providing highlighting by TextMate grammar for memory files.
Close Only the First Line of Multiline Comments Is Highlighted #264 and close multiline string not colored correctly #401
Fix by correct scope from TextMate grammar. But I doubt there is another bug in semantic highlighting.
Close Brackets are colorized inside strings and comments #314
Fix by allowing unbalanced bracket in scopes. See VSCode Update April 2022 (version 1.67) and issue development
Close Still no language field exist in tokens/scopes when inspect in vscode #375
Fix by providing tm scopes by TextMate grammar in default.
Results are following: