vscode-scala-syntax icon indicating copy to clipboard operation
vscode-scala-syntax copied to clipboard

Add support for meta.function-call

Open AdrianRaFo opened this issue 4 years ago • 6 comments

I have noticed that the function/method calls don't get any color when I configure it using the meta.function-call scope:

Settings.json:

{
  "scope": "meta.function-call",
  "settings": {
    "foreground": "#82AAFF"
  }
}

I tested this in other languages and it works as expected using that scope. Am I using the wrong scope? Could this be added? Thanks in advance

AdrianRaFo avatar Apr 08 '21 20:04 AdrianRaFo

Could you provide some examples where you would expect this coloring? And some example of the other languages as a guide.

nicolasstucki avatar Apr 09 '21 06:04 nicolasstucki

Here you can see a little example on scala: scala

and the same in python: python

and this is how I see the same scala code on IntelliJ: intellij scala

Notice that If I try to color the source.scala scope it colors all the code.

AdrianRaFo avatar Apr 12 '21 15:04 AdrianRaFo

I think we can implement something that highlights some function calls as meta.function-call, but this would only be an approximation of coloring all function calls. The simplest heuristic we can use is to assign meta.function-call for any identifier followed by ( or {.

There are some cases where we cannot highlight correctly:

  • Arity-0 function calls that omit parentheses (e.g. reply instead of reply()) — though this is (mostly) dropped in Scala 3
  • Calls to functions whose names starts with an uppercase letter. These are ambiguous with calls to the apply method of an object (e.g. MyObject(1, 2, 3) vs MyFunction(1, 2, 3)) — though in this case, I think we can just go with the convention that uppercased identifiers are names of types, and lowercased identifiers are for methods and variables. So function calls starting with an uppercase would not be assigned meta.function-call.

In general, coloring all function calls correctly would require information from the compiler about which identifiers correspond to function names. In VS Code terminology, we would need to write a semantic highlighter, but this project is a syntax highlighter. So I think some approximation definitely is acceptable for the scope of this project.

MaximeKjaer avatar Apr 12 '21 16:04 MaximeKjaer

Adding a color for the a . following a name and optionally ( or { looks awesome. I don't care to wait for scala 3 to make mandatory the parenthesis or brackets

AdrianRaFo avatar Apr 12 '21 22:04 AdrianRaFo

I do not really see how we could get this information syntactically. This is in the domain of semantic highlighting.

Consider the following cases

val x1 = ???
def x2 = ???
def x3() = ???
val x4: () => () = ???

x1 // not a function call
x2 // function call
x3() // function call
x4() // not a function call

p.x1 // not a function call
p.x2 // function call
p.x3() // function call
p.x4() // not a function call

nicolasstucki avatar Apr 13 '21 08:04 nicolasstucki

Semantic highlighting is part of LSP, so we might make it work in Metals. We should already have most information needed in semanticdb and we added the project as part of GSoC, but I am not sure if anyone applied.

tgodzik avatar Apr 13 '21 09:04 tgodzik