fwcd / kotlin-language-server

Kotlin code completion, diagnostics and more for any editor/IDE using the Language Server Protocol
MIT License
1.62k stars 203 forks source link

Inconsistent Semantic Tokens #525

Open anthony-S93 opened 9 months ago

anthony-S93 commented 9 months ago

The semantic tokens assigned to variables in declaration statements are inconsistent with the tokens assigned to those same variables everywhere they are used.

The following sample code illustrates the issue.

fun main(args: Array<String>) {
    var mutable_variable = 42
    println("The answer to life, the universe, and everything is " + mutable_variable)
}

2023-11-11-21-11-21

Using an inspection tool, I could see that mutable_variable was assigned the token lsp.type.property.kotlin in the declaration statement:

var mutable_variable = 42

However, mutable_variable was assigned lsp.type.variable everywhere else (which is obvious from the different highlighting) .

2023-11-11-21-14-22

I am new to Kotlin, but my intuition tells me that the language server shouldn't treat a variable in a var declaration as a property unless the declaration occurs within the context of a class. Is this the expected behavior? Or am I completely off base?

EDIT: After a few days of experimentation, I discovered other inconsistencies.

1) for loop variables parsed as parameters in the for header.

fun main() {
    val fruits = listOf("apple", "banana", "citrus")
    for (fruit in fruits) {
         println("I love $fruit")
    }
}

Using an inspection tool on the sample code above reveals the following semantic token assignments for various references of the variable fruit:

2023-11-14-22-31-09

2023-11-14-22-31-58

Once again, it appears that the language server relies on syntax rather than semantics when assigning tokens to variables.