I'm writing an extension for Visual Studio Code which features syntax highlighting for a scripting language. I was able to color many important elements via a TextMate grammar file. There are many lines where I would not know if a captured string refers to a user defined class (for a static call, for instance), or an instance of a class/variable until I perform analysis in code.
Is there a way I can dynamically highlight syntax via code in Visual Studio Code or any alternatives for achieving this?
What you are asking for is semantic coloring. It is not supported as of VS Code 1.35. You could try to emulate it using decorators, but the decorators can easily get out of sync with the rest of the highlighting during editing
This VS Code issue tracks adding semantic coloring to VS Code.
Related
This comment syntax highlighting from this package is very nice for making different comments stick out & organizing thoughts.
It looks like the picture below...
Is this possible in VS Code for any and all languages?
Requirement is that this should be possible to integrate with existing themes in VS Code.
Minimally I'm asking for Dart/Flutter, but would prefer a universal comment syntax highlighter so all languages get same comment treatment.
The TODO Highlight Extension does this.
VSCode is built on top of MonacoEditor which doesn't support Textmate grammars and themes. But somehow VSCode made it possible. I am curious how VSCode is able to do this.
I am asking because I am making a code editor (based on Monaco) with TextMate grammar and theme support. But I am unable to understand how I can achieve it.
Though there are packages like monaco-textmate to make TextMate grammars work with Monaco, syntax highlighting is still not working properly.
TextMate grammars depend on a particular regex implementation / library called Oniguruma, which is implemented in C. Monaco however is designed to run in the browser, and the JavaScript regex engine available there is not compatible with Oniguruma. All of this is explained in detail in the "Why doesn't the editor support TextMate grammars?" section of Monaco's FAQ. It also mentions the possibiliy of perhaps eventually compiling Oniguruma to WebAssembly to work around this.
VSCode itself uses vscode-textmate for its TMLanguage handling, which has the Oniguruma library as a native dependency. VSCode can have native dependencies because it doesn't run in a browser environment.
According to monaco-textmate's readme, it is actually heavily based on vscode-textmate:
99% of the code in this repository is extracted straight from vscode-textmate
And it does use the WASM approach mentioned earlier:
monaco-textmate relies on onigasm package to provide oniguruma regex engine in browsers. onigasm itself relies on WebAssembly.
As to why syntax highlighting doesn't always work as expected with monaco-textmate... I have no idea, I expect this is simply a bug in the implementation. Perhaps wait for a response from the maintainer, the issue you linked is fairly new.
At least conceptually there shouldn't be a reason why it couldn't achieve the same syntax highlighting VSCode does, since it uses the same regex flavour.
VS Code has basic parsing support out of the box (e.g. highlighting matching brackets, line comments and block comments). Is it possible to use this parser in an extension or do you know of a good parser that is accessible from TypeScript?
Sam Harwell is working on a TS runtime for the ANTLR tool, which I started using in an vscode extension to parse text. Still in early alpha state but already usable.
I'd like to write a language server to VSCode with semantic highlight support. The language I'm using has very complex rules, so I'd like not to rely on a tokenizer to distinguish between identifiers and keywords.
I already have a language service in VS Community, where I've written my own Classifier. It's possible to write own classifier in VSCode, or the only way to colorize a document is add TextMate language specification file to a VScode package?
Semantic coloring is not supported by the LSP as of VS Code 1.29.
There are two main issues currently tracking this feature:
LSP issue
VS Code issue which tracks adding semantic coloring to the VS Code api
I really enjoyed defining syntax highlighting rules in Notepad++ for the various build logs and artifacts I've worked on in the past to make certain text markers pop out. I was wondering if VSCode has any capabilities to do likewise.
For reference to the unfamiliar here's the Notepad++ documentation and a live demonstration:
https://npp-user-manual.org/docs/user-defined-language-system/
How to Create Custom Language Definitions in Notepad++
Looks like there is support for something in this direction. The terminology is slightly different though and there may not be a GUI interface like Notedpad++
Declarative Language Feature
Syntax Highlight Guide: VS Code uses TextMate grammar for syntax highlighting. This guide will walk you through writing a simple TextMate grammar and converting it into a VS Code extension.
Create Custom Language in Visual Studio Code
https://code.visualstudio.com/api/language-extensions/overview#declarative-language-features
https://code.visualstudio.com/api/language-extensions/language-configuration-guide