gleam-lang / tree-sitter-gleam

🌳 A tree-sitter grammar for the Gleam programming language
Apache License 2.0
66 stars 13 forks source link

Functions sometimes matched as properties #88

Closed ghivert closed 2 months ago

ghivert commented 2 months ago

Hi ! I'm trying tree-sitter parser with Zed, and I'm wondering why some functions are not matched as functions.

And because a picture is worth thousand words, here's an illustration.

Capture d’écran 2024-06-03 à 17 27 48

Here, compute_index is correctly seen as a function, but cache.cache_search_results and dict.insert are seen as properties, while semantically, they are functions.

Is there any way to change the parser to match those cases as functions?

J3RN commented 2 months ago

If I recall correctly, this is an ambiguity that can only be resolved with a more powerful parser. The reason being that foo.bar could be accessing the field bar on record foo or it could be referencing function bar on module foo. The only way to disambiguate is to track what the local variables are, and if foo is one, then we can reasonably assume that foo.bar is property access, and otherwise assume that foo.bar is a remote function invocation.

In fact, we do do this (disambiguation example), but it's reliant on tree-sitter's tagging mechanism which isn't widely supported (e.g. likely not by Zed, but you can look into it).

ghivert commented 2 months ago

OK, got it. But I was wondering, isn't the fact that it has parentheses indicating a function call enough to classify it as a function?

lpil commented 2 months ago

Record fields can also be functions. There's no way to tell if something is a record or a module without performing type analysis.

ghivert commented 2 months ago

Yup, I understand this, my question was more about the fact that in the expression dict.insert() (explicitly with the parens) insert could be considered as a function and not a property, no? Visually, it would make sense for the syntax highlighting to display it in the same colour as functions. 🙂 I totally get that in an expression like key |> dict.get, get can't really be highlighted as function though.

Anyway, it's more about curiosity than anything else, the code is already readable ha ha, thanks for taking time to answer

lpil commented 2 months ago

The property/function distinction doesn't make much sense as properties can be functions. They're not mutually exclusive groups as one is a type and one is a structural matter.

ghivert commented 2 months ago

Hmm, I see what you mean. Thanks a lot for the explanation. Let’s wait for semantic highlighting then 😁