nvim-treesitter/runtime/queries/starlark/locals.scm
Jade Lovelace 7815bfb108 feat(starlark): build targets as locals
Problem: I have very big BUILD files I want to navigate quickly.
I have Telescope, so I can use `:Telescope treesitter` for an outline,
but build targets aren't emitted as definitions.

Solution: Add a query that catches build targets.

One thing I'm unclear on is:
- I want to filter to just build targets.
- Build targets are absolutely not any of the mentioned types in the
  CONTRIBUTING file.
- Build targets do not actually create identifiers in buck2/bazel: they
  create objects which are observable to the outside world and which the
  user generally *cares* about, but they aren't in the same namespace as
  normal identifiers.

  This means that they *especially* don't fit into any of the existing
  categories.

I would like guidance as to whether this is the right approach.
2025-10-13 18:21:01 -07:00

107 lines
2.2 KiB
Scheme

; Program structure
(module) @local.scope
; Function with parameters, defines parameters
(parameters
(identifier) @local.definition.parameter)
(default_parameter
(identifier) @local.definition.parameter)
(typed_parameter
(identifier) @local.definition.parameter)
(typed_default_parameter
(identifier) @local.definition.parameter)
; *args parameter
(parameters
(list_splat_pattern
(identifier) @local.definition.parameter))
; **kwargs parameter
(parameters
(dictionary_splat_pattern
(identifier) @local.definition.parameter))
; Function defines function and scope
((function_definition
name: (identifier) @local.definition.function) @local.scope
(#set! definition.function.scope "parent"))
; Loops
; not a scope!
(for_statement
left: (pattern_list
(identifier) @local.definition.var))
(for_statement
left: (tuple_pattern
(identifier) @local.definition.var))
(for_statement
left: (identifier) @local.definition.var)
; for in list comprehension
(for_in_clause
left: (identifier) @local.definition.var)
(for_in_clause
left: (tuple_pattern
(identifier) @local.definition.var))
(for_in_clause
left: (pattern_list
(identifier) @local.definition.var))
(dictionary_comprehension) @local.scope
(list_comprehension) @local.scope
(set_comprehension) @local.scope
; Assignments
(assignment
left: (identifier) @local.definition.var)
(assignment
left: (pattern_list
(identifier) @local.definition.var))
(assignment
left: (tuple_pattern
(identifier) @local.definition.var))
(assignment
left: (attribute
(identifier)
(identifier) @local.definition.field))
; Walrus operator x := 1
(named_expression
(identifier) @local.definition.var)
(as_pattern
alias: (as_pattern_target) @local.definition.var)
; REFERENCES
(identifier) @local.reference
; Starlark-specific
; Loads
((call
function: (identifier) @_fn
arguments: (argument_list
(string) @local.definition.import))
(#eq? @_fn "load"))
; Build target definitions
((module
(expression_statement
(call
arguments: (argument_list
(keyword_argument
name: (identifier) @_name
value: (string
(string_content) @local.definition.target))))))
(#eq? @_name "name"))