3 ms·
Ask HN: Any editor with semantic, not syntax, highlighting?
I've recently realized that the reason I seem unable to find a satisfactory syntax highlighting strategy, is that I don't really need any help to tell keywords from variables, strings from operators and so on, but rather to catch subtle mistakes like undefined functions or mispelled variable names. These all look like valid syntactic tokens, but are semantically wrong. To recognize such cases, a tool should understand where some word is declared/defined and where it is merely used, which is a task much more complex than what can be accomplished by any regex.
I wonder if any editor, or plugin for editor, but without having to invoke a real compiler, exists that can at least satisfy requirement #1 below. Bonus points if it can also satisfy requirements #2 and #3 (all examples are in C as the archetype of many similar constructs in other languages).
Requirement #1
Given this snippet, it should highlight control and conrtol in lines 1 and 2 with different styles:
foo() {
int control;
control = 1; // line 1
conrtol = 1; // line 2
}
Requirement #2
Given this snippet, it should highlight local ang global with different styles:
int global;
foo() {
int local;
local = 1; // line 3
global = 1; // line 4
}
Requirement #3
Given this snippet, it should highlight Type in a different style than aaa and ppp:
typedef int Type;
int aaa;
int *ppp;
...
return (Type) * (ppp); // line 5
return (aaa) * (aaa); // line 6
...
Note that lines 5 and 6 have exactly the same structure: not even tree-sitter recognizes that the first is a cast + dereference, while the second is a multiplication.
- card_zero 2y agoPerhaps this: Semantic Highlighting in Neovim https://gist.github.com/swarn/fb37d9eefe1bc616c2a7e476c0bc0316 https://gist.github.com/swarn/fb37d9eefe1bc616c2a7e476c0bc03... In the comments there's also a link to a plugin that does a different kind "Where every variable is a different color": https://github.com/jaxbot/semantic-highlight.vim https://github.com/jaxbot/semantic-highlight.vim
- teo_zero 2y agoAs you pointed out, the latter does a very different kind of highlighting. The former is truly semantic, but needs an external compiler (via LSP).
- wruza 2y agoI wouldn’t expect someone to replicate a full compiler as an internal editor plugin ever again only to highlight mistakes. Language servers and their support is one of the best things that happened to editors recently. Everyone moved on from ad-hoc plugins. LSP is what you’re looking for, imo. A regular LSP for semantic errors and a linter LSP[-like] for style, bad practice, unused entity, deprecation, etc warnings. For requirement 2 and 3 I think VSCode can do that. Some themes tint every possible class of token, so casting will look different from multiplication. Afaik, it definitely differentiates functions, locals, globals and members, while they are all identifiers.
- dchest 2y agoMost editors that support language servers (https://langserver.org/ https://langserver.org/) can do this. They use "real compiler's" lexer/parser for this.
- viraptor 2y agoPython lsp in vscode does it. Other lsp-s probably too.
- austin-cheney 2y agoSounds like a job for custom lint rules.
- solardev 2y agoIf I understand you right, I think all the Jetbrains IDEs do this. They actually analyze code (not just syntax) and point out various errors in logic, scope, references, parameters, etc.