Ask HN: Any editor with semantic, not syntax, highlighting?

6 points by teo_zero 2 days ago

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.
solardev 20 hours ago

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.

dchest 2 days ago

Most editors that support language servers (https://langserver.org/) can do this. They use "real compiler's" lexer/parser for this.

austin-cheney a day ago

Sounds like a job for custom lint rules.

viraptor 2 days ago

Python lsp in vscode does it. Other lsp-s probably too.