tree-sitter-go
tree-sitter-go copied to clipboard
`package_identifier` is missing in some cases
For the following snippet
package main
import "context"
func foo(ctx context.Context) {
ctx := context.WithCancel(ctx)
}
the tree-sitter AST is
package_clause:
package_identifier:
import_declaration:
import_spec:
interpreted_string_literal:
function_declaration:
identifier:
parameter_list:
parameter_declaration:
identifier:
qualified_type:
package_identifier:
type_identifier:
block:
short_var_declaration:
expression_list:
identifier:
expression_list:
call_expression:
selector_expression:
identifier:
field_identifier:
argument_list:
identifier:
For the function call context.WithCancel
, package_identifier
is not emitted but its emitted for parameter.
I am using tree-sitter via https://github.com/emacs-tree-sitter, please let me know if you want me to test this in some other way or if you require some other info.
Yeah, context.Context
is intentionally treated differently than context.WithCancel
, because one is a type and one is an expression. We parse context.WithCancel
as a selector_expression
, because there's no way to know whether context
is a package name or the name of a local variable (without maintaining a symbol table).
I don't think this is a bug.
There's a question as to whether it's worth Tree Sitter trying to distinguish ambiguous terms like identifier.identifier
based on context (for example, if you know it's a parameter or field type then it must be a qualified reference to a type imported from another package) even though in the general case you can't tell. For example, context.WithCancel
could refer to a func/var/const/type imported by import "context"
, but it's possible that that package has a different name, and context actually refers to a top-level var in an adjacent file in this package. Attempting this disambiguation gives better results with partial information, but it may require clients to handle more cases.
Any progress on this? I'm using nvim-treesitter and I'd like to see package names highlighted like in Goland.