code-d icon indicating copy to clipboard operation
code-d copied to clipboard

Incorrect syntax highlighting

Open theonenotzero opened this issue 3 years ago • 4 comments

Here is a small sample d file which illustrates the issues.

fn!T();

cast(void function())x;

obj.method();

Screenshot from VSCode: image

In the first line, fn should be highlighted as a function, as it is indeed a template function being called. Also, T should be highlighted either as a type (purple in my theme) or have no highlighting at all.

In the second line, the wrong closing parenthesis is matched with the opening parenthesis for the cast operation.

In the third line, the method should be highlighted as a function while obj. should not.

theonenotzero avatar May 24 '22 13:05 theonenotzero

for 1 and 3 it's not possible to determine whether things are types, functions or variables from the syntax, semantic highlighting is needed there, which is simply not yet implemented. (See #299)

can fix the second one though.

WebFreak001 avatar May 24 '22 14:05 WebFreak001

I don't believe any of my suggestions would require semantic analysis at all, since ! and . are not characters that can appear in an identifier. For the first one, when you find a site that looks like a function call, is might be possible to attempt to backtrack until the last ! token, which would correctly identify the function name. Similarly, for the third case, by splitting tokens along the . character you can easily determine the boundary.

I don't know how this works under the hood though, so I might be missing something.

theonenotzero avatar May 26 '22 00:05 theonenotzero

struct someType {}
int someFunction() { return 0; }
int someVariable;
int ufcsFun(int x) { return x; }
void myTemplate(alias symbol)() {}

void main() {
    int x = someFunction;
    int y = someVariable;
    int z = someFunction.ufcsFun;
    auto w = someType();

    myTemplate!x;
    myTemplate!(ufcsFun)();
    myTemplate!someVariable();
    myTemplate!someType;
}

all valid and without knowing the definition it's impossible to guess at the function calling site what kind each symbol is.

WebFreak001 avatar May 26 '22 07:05 WebFreak001

Okay, maybe I was being unclear with with what I was asking for. I understand that it is not feasible to know the type of a particular token. However, if a token is being called like a function using (), it is probably a function and that assumption should be made. This is already done. However, I was asking that an identifier called via !() should also be highlighted like a function, regardless of what the type actually is. Right now, the highlighting is 100% wrong, as the template paramater is being highlighted as the function being called. Instead, it would be correct in most circumstances for the identifier before the ! to be highlighted as a function when a structure like this occurs: a!b(c).

For your example, myTemplate!someVariable() is being called using !() so myTemplate should be highlighted as a function since it appears to be one. Additionally, someVariable shouldn't be highlighted as a function, and a little more parsing can confidently determine this.

The second instantiation, myTemplate!(ufccsFun)() should also be highlighted as a function as it is possible to determine that it is one. The first and fourth are unclear as to whether or not they are functions based on a parse alone, so they shouldn't be coloured as functions even if they are functions.

Basically, identify functions called with () and !(), and ignore functions that omit parentheses.

theonenotzero avatar May 28 '22 09:05 theonenotzero