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 #2Given 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 #3Given 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.
Perhaps this: Semantic Highlighting in Neovim
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
As you pointed out, the latter does a very different kind of highlighting.
The former is truly semantic, but needs an external compiler (via LSP).
If 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.
Most editors that support language servers (https://langserver.org/) can do this. They use "real compiler's" lexer/parser for this.
Sounds like a job for custom lint rules.
Python lsp in vscode does it. Other lsp-s probably too.