Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

89.6 KB
Raw Blame
// Copyright 2026 Ix Infrastructure Inc.

import { SupportedLanguages } from './languages.js';

/* 
 * Tree-sitter queries for extracting code definitions.
 * 
 * Note: Different grammars (typescript vs tsx vs javascript) may have
 * slightly different node types. These queries are designed to be 
 * compatible with the standard tree-sitter grammars.
 */

// TypeScript queries - works with tree-sitter-typescript
export const TYPESCRIPT_QUERIES = `
(class_declaration
  name: (type_identifier) @name) @definition.class

(abstract_class_declaration
  name: (type_identifier) @name) @definition.class

(interface_declaration
  name: (type_identifier) @name) @definition.interface

; Type aliases and enums are often a TypeScript codebase's central vocabulary.
(type_alias_declaration
  name: (type_identifier) @name) @definition.type

(enum_declaration
  name: (identifier) @name) @definition.enum

; namespace NS { ... }
(internal_module
  name: (identifier) @name) @definition.namespace

(abstract_method_signature
  name: (property_identifier) @name) @definition.method

; A class field holding a function is a method in all but syntax: draw = () => ...
(public_field_definition
  name: (property_identifier) @name
  value: [(arrow_function) (function_expression)]) @definition.method

; Overload signatures: \`function f(a: string): string;\` above the implementation.
(function_signature
  name: (identifier) @name) @definition.function

(function_declaration
  name: (identifier) @name) @definition.function

(method_definition
  name: (property_identifier) @name) @definition.method

(lexical_declaration
  (variable_declarator
    name: (identifier) @name
    value: (arrow_function))) @definition.function

(lexical_declaration
  (variable_declarator
    name: (identifier) @name
    value: (function_expression))) @definition.function

(export_statement
  declaration: (lexical_declaration
    (variable_declarator
      name: (identifier) @name
      value: (arrow_function)))) @definition.function

(export_statement
  declaration: (lexical_declaration
    (variable_declarator
      name: (identifier) @name
      value: (function_expression)))) @definition.function

; Module-level constants (Ix#679). Anchored under (program ...) so the 5,085
; const declarations in ix-cli/src stay out and the 259 module-scope ones come
; in: a blanket variable_declarator rule multiplies a package's entity count
; ~20x with function-local bindings, which costs every ranking and every bundle
; budget. The literal "const" keeps \`let\` out — mutable module state is not
; the thing an agent searches for by name.
;
; Export is deliberately not required: BUDGETS in commands/context.ts is
; module-scope and unexported, and an export-only rule would still miss it.
; Destructuring patterns do not match either — \`name: (identifier)\` skips
; \`const { a, b } = obj\`, which binds no single name worth indexing.
;
; The value is captured so index.ts can drop the arrow-function and
; function-expression cases, which the four rules above have already claimed as
; definition.function. Without that, an exported arrow function arrives twice.
(program
  (lexical_declaration
    "const"
    (variable_declarator
      name: (identifier) @name
      value: (_) @const.value)) @definition.const)

(program
  (export_statement
    declaration: (lexical_declaration
      "const"
      (variable_declarator
        name: (identifier) @name
        value: (_) @const.value))) @definition.const)

(import_statement
  source: (string) @import.source) @import

; Re-export statements: export { X } from './y'
(export_statement
  source: (string) @import.source) @import

; Dynamic import with string literal: import('./foo') or await import('./foo')
(call_expression
  function: (import)
  arguments: (arguments (string) @import.source)) @import

(call_expression
  function: (identifier) @call.name) @call

(call_expression
  function: (member_expression
    property: (property_identifier) @call.name)) @call

; this.method() — capture this as qualifier so index.ts can substitute the enclosing class.
; Analogous to Python's self/cls substitution.
(call_expression
  function: (member_expression
    object: (this) @_qualifier
    property: (property_identifier) @call.name)) @call

; Constructor calls: new Foo()
(new_expression
  constructor: (identifier) @call.name) @call

; Heritage queries - class extends, plain or abstract, a bare name or ns.Base
(class_declaration
  name: (type_identifier) @heritage.class
  (class_heritage
    (extends_clause
      value: [(identifier) (member_expression)] @heritage.extends))) @heritage

(abstract_class_declaration
  name: (type_identifier) @heritage.class
  (class_heritage
    (extends_clause
      value: [(identifier) (member_expression)] @heritage.extends))) @heritage

(abstract_class_declaration
  name: (type_identifier) @heritage.class
  (class_heritage
    (implements_clause
      (type_identifier) @heritage.implements))) @heritage.impl

; Heritage queries - class implements interface
(class_declaration
  name: (type_identifier) @heritage.class
  (class_heritage
    (implements_clause
      (type_identifier) @heritage.implements))) @heritage.impl

; Heritage queries - interface extends interface
(interface_declaration
  name: (type_identifier) @heritage.class
  (extends_type_clause
    (type_identifier) @heritage.extends)) @heritage

; Type references — captures types used in annotations/parameters/return types,
; and inside the type forms they compose: Promise<Foo>, Foo[], Foo | null,
; Foo & Bar, \`x as Foo\`, \`extends Base<Foo>\`, \`type T = Foo\`. Duplicates
; per enclosing definition are dropped in index.ts.
(type_annotation (type_identifier) @reference.type)
(generic_type name: (type_identifier) @reference.type)
(type_arguments (type_identifier) @reference.type)
(array_type (type_identifier) @reference.type)
(union_type (type_identifier) @reference.type)
(intersection_type (type_identifier) @reference.type)
(as_expression (type_identifier) @reference.type)
(type_alias_declaration value: (type_identifier) @reference.type)
`;

// JavaScript queries - works with tree-sitter-javascript
export const JAVASCRIPT_QUERIES = `
(class_declaration
  name: (identifier) @name) @definition.class

(function_declaration
  name: (identifier) @name) @definition.function

(method_definition
  name: (property_identifier) @name) @definition.method

(lexical_declaration
  (variable_declarator
    name: (identifier) @name
    value: (arrow_function))) @definition.function

(lexical_declaration
  (variable_declarator
    name: (identifier) @name
    value: (function_expression))) @definition.function

; var foo = function() {} (CommonJS var declarations)
(variable_declaration
  (variable_declarator
    name: (identifier) @name
    value: (function_expression))) @definition.function

; var foo = () => {} (CommonJS var arrow functions)
(variable_declaration
  (variable_declarator
    name: (identifier) @name
    value: (arrow_function))) @definition.function

; obj.method = function name() {} (prototype/mixin method assignments)
(expression_statement
  (assignment_expression
    left: (member_expression
      property: (property_identifier) @name)
    right: (function_expression))) @definition.function

(export_statement
  declaration: (lexical_declaration
    (variable_declarator
      name: (identifier) @name
      value: (arrow_function)))) @definition.function

(export_statement
  declaration: (lexical_declaration
    (variable_declarator
      name: (identifier) @name
      value: (function_expression)))) @definition.function

; Module-level constants (Ix#679). Anchored under (program ...) so the 5,085
; const declarations in ix-cli/src stay out and the 259 module-scope ones come
; in: a blanket variable_declarator rule multiplies a package's entity count
; ~20x with function-local bindings, which costs every ranking and every bundle
; budget. The literal "const" keeps \`let\` out — mutable module state is not
; the thing an agent searches for by name.
;
; Export is deliberately not required: BUDGETS in commands/context.ts is
; module-scope and unexported, and an export-only rule would still miss it.
; Destructuring patterns do not match either — \`name: (identifier)\` skips
; \`const { a, b } = obj\`, which binds no single name worth indexing.
;
; The value is captured so index.ts can drop the arrow-function and
; function-expression cases, which the four rules above have already claimed as
; definition.function. Without that, an exported arrow function arrives twice.
(program
  (lexical_declaration
    "const"
    (variable_declarator
      name: (identifier) @name
      value: (_) @const.value)) @definition.const)

(program
  (export_statement
    declaration: (lexical_declaration
      "const"
      (variable_declarator
        name: (identifier) @name
        value: (_) @const.value))) @definition.const)

(import_statement
  source: (string) @import.source) @import

; Re-export statements: export { X } from './y'
(export_statement
  source: (string) @import.source) @import

; Dynamic import with string literal: import('./foo') or await import('./foo')
(call_expression
  function: (import)
  arguments: (arguments (string) @import.source)) @import

; CommonJS require() → IMPORTS
(call_expression
  function: (identifier) @_req
  arguments: (arguments (string) @import.source)
  (#eq? @_req "require")) @import

(call_expression
  function: (identifier) @call.name) @call

(call_expression
  function: (member_expression
    property: (property_identifier) @call.name)) @call

; this.method() — capture this as qualifier so index.ts can substitute the enclosing class.
; Analogous to Python's self/cls substitution.
(call_expression
  function: (member_expression
    object: (this) @_qualifier
    property: (property_identifier) @call.name)) @call

; Constructor calls: new Foo()
(new_expression
  constructor: (identifier) @call.name) @call

; Heritage queries - class extends (JavaScript uses different AST than TypeScript)
; In tree-sitter-javascript, class_heritage directly contains the parent identifier
(class_declaration
  name: (identifier) @heritage.class
  (class_heritage
    (identifier) @heritage.extends)) @heritage
`;

// Python queries - works with tree-sitter-python
export const PYTHON_QUERIES = `
(class_definition
  name: (identifier) @name) @definition.class

(function_definition
  name: (identifier) @name) @definition.function

(import_statement
  name: (dotted_name) @import.source) @import

(import_from_statement
  module_name: (dotted_name) @import.source) @import

(import_from_statement
  module_name: (relative_import) @import.source) @import

; from . import utils / from .models import User — capture imported symbol names (relative imports)
(import_from_statement
  module_name: (relative_import) @import.module
  name: (dotted_name (identifier) @import.name)) @import.names

(import_from_statement
  module_name: (relative_import) @import.module
  name: (aliased_import
    name: (dotted_name (identifier) @import.name)
    alias: (identifier) @import.local)) @import.names

; from module import Class — capture imported symbol names (absolute imports)
; e.g. from sqlalchemy.sql.schema import Column → import.name = Column
(import_from_statement
  module_name: (dotted_name) @import.module
  name: (dotted_name (identifier) @import.name)) @import.names

(import_from_statement
  module_name: (dotted_name) @import.module
  name: (aliased_import
    name: (dotted_name (identifier) @import.name)
    alias: (identifier) @import.local)) @import.names

(call
  function: (identifier) @call.name) @call

(call
  function: (attribute
    attribute: (identifier) @call.name)) @call

; Attribute calls with object captured as qualifier so Tier-1b resolution can use
; class/module name to break ties (e.g. Session.execute → resolves to Session in
; the importing file).  Emitted alongside the bare-name pattern above so that
; bare-name global fallback still fires when qualifier resolution fails.
(call
  function: (attribute
    object: (identifier) @_qualifier
    attribute: (identifier) @call.name)) @call

; Django ORM manager pattern: Model.objects.method(...)
; Unwinds the intermediate accessor (objects/related_manager/etc.) and captures
; the model class as qualifier, e.g. User.objects.filter(...) → CALLS User.filter.
; This creates cross-file edges between view/form code and model definitions.
(call
  function: (attribute
    object: (attribute
      object: (identifier) @_qualifier
      attribute: (identifier))
    attribute: (identifier) @call.name)) @call

; Chained call pattern: qualifier.method1(...).method2(...)
; Propagates the qualifier from the inner call to the outer, e.g.
; User.filter(...).order_by(...) → CALLS User.order_by.
(call
  function: (attribute
    object: (call
      function: (attribute
        object: (identifier) @_qualifier
        attribute: (identifier)))
    attribute: (identifier) @call.name)) @call

; Heritage queries - Python class inheritance
(class_definition
  name: (identifier) @heritage.class
  superclasses: (argument_list
    (identifier) @heritage.extends)) @heritage

; Type references — captures types used in type annotations / hints
(typed_parameter type: (type (identifier) @reference.type))
(typed_default_parameter type: (type (identifier) @reference.type))

; Class/function references passed as keyword argument values
; e.g. LoginView.as_view(authentication_form=AdminAuthenticationForm, ...)
; Emits a CALLS edge from the enclosing function to the referenced identifier.
(call
  arguments: (argument_list
    (keyword_argument
      value: (identifier) @call.name)))

; Typed parameters: capture name + type together so qualifier substitution can
; map a local variable name to its declared class (e.g. query: Query → 'query' → 'Query').
(function_definition
  parameters: (parameters
    (typed_parameter
      (identifier) @_typed_param_name
      type: (type (identifier) @_typed_param_type)))) @_typed_param_scope

(function_definition
  parameters: (parameters
    (typed_default_parameter
      (identifier) @_typed_param_name
      type: (type (identifier) @_typed_param_type)))) @_typed_param_scope

; Assignment tracking: x = SomeClass() or x = SomeClass(args)
; Maps local variable name → constructor type for untyped-parameter qualifier substitution.
; Only PascalCase RHS names are tracked in TypeScript (constructor convention).
(function_definition
  body: (block
    (expression_statement
      (assignment
        left: (identifier) @_assign_lhs
        right: (call
          function: (identifier) @_assign_rhs_type))))) @_assign_scope

; Assignment tracking: x = Model.objects.method()
; Captures the outermost identifier (model class) for ORM manager patterns.
(function_definition
  body: (block
    (expression_statement
      (assignment
        left: (identifier) @_assign_lhs
        right: (call
          function: (attribute
            object: (attribute
              object: (identifier) @_assign_rhs_type
              attribute: (identifier))
            attribute: (identifier))))))) @_assign_scope

; Assignment tracking: x = module.Class (direct attribute reference, no call)
; Handles variable aliasing like: engineclass = base.Engine
; Combined with direct-call resolution in index.ts, this lets engineclass(...) → Engine(...)
(function_definition
  body: (block
    (expression_statement
      (assignment
        left: (identifier) @_assign_lhs
        right: (attribute
          attribute: (identifier) @_assign_rhs_type))))) @_assign_scope
`;

// Java queries - works with tree-sitter-java
export const JAVA_QUERIES = `
; Classes, Interfaces, Enums, Annotations
(class_declaration name: (identifier) @name) @definition.class
(interface_declaration name: (identifier) @name) @definition.interface
(enum_declaration name: (identifier) @name) @definition.enum
(annotation_type_declaration name: (identifier) @name) @definition.annotation

; Methods & Constructors
(method_declaration name: (identifier) @name) @definition.method
(constructor_declaration name: (identifier) @name) @definition.constructor

; Imports - capture any import declaration child as source
(import_declaration (_) @import.source) @import

; Calls
(method_invocation name: (identifier) @call.name) @call
(method_invocation object: (_) name: (identifier) @call.name) @call

; Constructor calls: new Foo()
(object_creation_expression type: (type_identifier) @call.name) @call

; Heritage - extends class (plain: extends Foo)
(class_declaration name: (identifier) @heritage.class
  (superclass (type_identifier) @heritage.extends)) @heritage

; Heritage - extends class with generics (extends Foo<T>)
(class_declaration name: (identifier) @heritage.class
  (superclass (generic_type (type_identifier) @heritage.extends))) @heritage

; Heritage - implements interfaces (plain: implements Foo)
(class_declaration name: (identifier) @heritage.class
  (super_interfaces (type_list (type_identifier) @heritage.implements))) @heritage.impl

; Heritage - implements interfaces with generics (implements Foo<T>)
(class_declaration name: (identifier) @heritage.class
  (super_interfaces (type_list (generic_type (type_identifier) @heritage.implements)))) @heritage.impl

; Heritage - interface extends interfaces (plain: extends Foo)
(interface_declaration name: (identifier) @heritage.class
  (extends_interfaces (type_list (type_identifier) @heritage.extends))) @heritage

; Heritage - interface extends interfaces with generics (extends Foo<T>)
(interface_declaration name: (identifier) @heritage.class
  (extends_interfaces (type_list (generic_type (type_identifier) @heritage.extends)))) @heritage

; Type references — captures types used in field/param/return-type positions
(field_declaration type: (type_identifier) @reference.type)
(formal_parameter type: (type_identifier) @reference.type)
(local_variable_declaration type: (type_identifier) @reference.type)
(method_declaration type: (type_identifier) @reference.type)
`;

// C queries - works with tree-sitter-c
export const C_QUERIES = `
; Functions (direct declarator)
(function_definition declarator: (function_declarator declarator: (identifier) @name)) @definition.function
(declaration declarator: (function_declarator declarator: (identifier) @name)) @definition.function

; Functions returning pointers (pointer_declarator wraps function_declarator)
(function_definition declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function
(declaration declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function

; Functions returning double pointers (nested pointer_declarator)
(function_definition declarator: (pointer_declarator declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name)))) @definition.function

; Structs, Unions, Enums, Typedefs
; Only match struct/union with a body to avoid treating forward declarations as definitions
(struct_specifier name: (type_identifier) @name body: (field_declaration_list)) @definition.struct
(union_specifier name: (type_identifier) @name body: (field_declaration_list)) @definition.union
(enum_specifier name: (type_identifier) @name) @definition.enum
(type_definition declarator: (type_identifier) @name) @definition.typedef

; Macros
(preproc_function_def name: (identifier) @name) @definition.macro
(preproc_def name: (identifier) @name) @definition.macro

; Includes
(preproc_include path: (_) @import.source) @import

; Calls
(call_expression function: (identifier) @call.name) @call
(call_expression function: (field_expression field: (field_identifier) @call.name)) @call

; Type references — captures types used in declaration/parameter positions
(declaration type: (type_identifier) @reference.type)
(parameter_declaration type: (type_identifier) @reference.type)
; struct_specifier forms: e.g. "const struct Curl_protocol foo = {...}"
(declaration type: (struct_specifier name: (type_identifier) @reference.type))
(parameter_declaration type: (struct_specifier name: (type_identifier) @reference.type))
`;

// Go queries - works with tree-sitter-go
export const GO_QUERIES = `
; Package declaration — captures the package name so the parser can
; prefix every Go entity's qualifiedName with it, eliminating the bare-name
; collision problem that makes call resolution non-deterministic when
; multiple packages define a function with the same name.
(package_clause (package_identifier) @package.name)

; Functions
(function_declaration name: (identifier) @name) @definition.function

; Methods — non-pointer receiver: func (r ReceiverType) Name(...)
; The receiver.type capture lets the parser set container = ReceiverType so that
; qualifiedKey becomes "ReceiverType.Name" instead of bare "Name".
(method_declaration
  receiver: (parameter_list (parameter_declaration
    type: (type_identifier) @receiver.type))
  name: (field_identifier) @name) @definition.method

; Methods — pointer receiver: func (r *ReceiverType) Name(...)
(method_declaration
  receiver: (parameter_list (parameter_declaration
    type: (pointer_type (type_identifier) @receiver.type)))
  name: (field_identifier) @name) @definition.method

; Types
(type_declaration (type_spec name: (type_identifier) @name type: (struct_type))) @definition.struct
(type_declaration (type_spec name: (type_identifier) @name type: (interface_type))) @definition.interface

; Interface method signatures — emits each method inside an interface_type as a method entity.
; @heritage.class = interface name (used as container in index.ts via heritageClassCapture).
; node type is method_elem in tree-sitter-go v0.21+.
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (interface_type
      (method_elem
        name: (field_identifier) @name)))) @definition.method

; Imports
(import_declaration (import_spec name: (package_identifier) @import.alias path: (interpreted_string_literal) @import.source)) @import
(import_declaration (import_spec path: (interpreted_string_literal) @import.source)) @import
(import_declaration (import_spec_list (import_spec path: (interpreted_string_literal) @import.source))) @import
(import_declaration (import_spec_list (import_spec name: (package_identifier) @import.alias path: (interpreted_string_literal) @import.source))) @import

; Struct embedding — value: type Foo struct { Bar }
; NOTE: no @definition.* capture here — if it were present, defCapture fires first and
; the heritage block is never reached (early-exit in the first pass).
; The . anchors require the type_identifier to be the FIRST and ONLY named child of
; field_declaration, distinguishing anonymous embedded fields from named fields like:
;   w Writer  (named field — field_identifier_list precedes type, not matched)
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (struct_type
      (field_declaration_list
        (field_declaration .
          (type_identifier) @heritage.extends .)))))

; Struct embedding — pointer: type Foo struct { *Bar }
; The . anchor requires pointer_type to be the first named child (anonymous embedded).
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (struct_type
      (field_declaration_list
        (field_declaration .
          (pointer_type (type_identifier) @heritage.extends) .)))))

; Struct embedding — qualified type: type Foo struct { pkg.Bar }
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (struct_type
      (field_declaration_list
        (field_declaration .
          (qualified_type name: (type_identifier) @heritage.extends) .)))))

; Struct embedding — pointer-to-qualified: type Foo struct { *pkg.Bar }
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (struct_type
      (field_declaration_list
        (field_declaration .
          (pointer_type (qualified_type name: (type_identifier) @heritage.extends)) .)))))

; Interface embedding — bare: type Reader interface { Closer }
; Embedded interfaces are wrapped in type_elem in tree-sitter-go's grammar.
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (interface_type
      (type_elem (type_identifier) @heritage.extends))))

; Interface embedding — qualified: type Reader interface { io.Reader }
(type_declaration
  (type_spec
    name: (type_identifier) @heritage.class
    type: (interface_type
      (type_elem (qualified_type name: (type_identifier) @heritage.extends)))))

; Calls
(call_expression function: (identifier) @call.name) @call
; Qualified calls: pkg.Func() or localVar.Method() where the operand is a simple
; identifier — capture it as @_qualifier so cross-file resolution uses the fully
; qualified name "scrape.NewManager" instead of bare "NewManager", preventing
; ambiguous same-named symbols in different packages from being collapsed.
(call_expression function: (selector_expression
  operand: (identifier) @_qualifier
  field: (field_identifier) @call.name)) @call
; All other method-call shapes — chained off function call, indexed,
; type-asserted, parenthesized, dereferenced, sliced, etc. We omit the
; operand field constraint so this matches any subtree. The bare method
; name (field_identifier) is captured; no @_qualifier means cross-file
; resolution falls back to method-name-only matching for these calls.
(call_expression function: (selector_expression
  field: (field_identifier) @call.name)) @call

; Struct literal construction: User{Name: "Alice"}
(composite_literal type: (type_identifier) @call.name) @call

; ── Type references ─────────────────────────────────────────────────
; Captures types used in field, parameter, variadic, and result positions.
; The bare patterns below catch the common case (Item param / Item return),
; but Go signatures lean heavily on pointer, slice, map, channel,
; qualified-type, and variadic shapes — each needs its own pattern in
; tree-sitter's structural query language.

; --- Bare type_identifier ---
(field_declaration type: (type_identifier) @reference.type)
(parameter_declaration type: (type_identifier) @reference.type)
(function_declaration result: (type_identifier) @reference.type)
(method_declaration result: (type_identifier) @reference.type)
(variadic_parameter_declaration type: (type_identifier) @reference.type)

; --- Pointer types ---
(field_declaration type: (pointer_type (type_identifier) @reference.type))
(parameter_declaration type: (pointer_type (type_identifier) @reference.type))
(function_declaration result: (pointer_type (type_identifier) @reference.type))
(method_declaration result: (pointer_type (type_identifier) @reference.type))
(variadic_parameter_declaration type: (pointer_type (type_identifier) @reference.type))

; --- Qualified types: pkg.Type (capture the bare type_identifier "Type")
(field_declaration type: (qualified_type name: (type_identifier) @reference.type))
(parameter_declaration type: (qualified_type name: (type_identifier) @reference.type))
(function_declaration result: (qualified_type name: (type_identifier) @reference.type))
(method_declaration result: (qualified_type name: (type_identifier) @reference.type))
(variadic_parameter_declaration type: (qualified_type name: (type_identifier) @reference.type))

; --- Pointer-to-qualified: *pkg.Type ---
(field_declaration type: (pointer_type (qualified_type name: (type_identifier) @reference.type)))
(parameter_declaration type: (pointer_type (qualified_type name: (type_identifier) @reference.type)))
(function_declaration result: (pointer_type (qualified_type name: (type_identifier) @reference.type)))
(method_declaration result: (pointer_type (qualified_type name: (type_identifier) @reference.type)))

; --- Slice types: []Item / []*Item / []pkg.Item ---
(field_declaration type: (slice_type element: (type_identifier) @reference.type))
(field_declaration type: (slice_type element: (pointer_type (type_identifier) @reference.type)))
(field_declaration type: (slice_type element: (qualified_type name: (type_identifier) @reference.type)))
(parameter_declaration type: (slice_type element: (type_identifier) @reference.type))
(parameter_declaration type: (slice_type element: (pointer_type (type_identifier) @reference.type)))
(parameter_declaration type: (slice_type element: (qualified_type name: (type_identifier) @reference.type)))
(function_declaration result: (slice_type element: (type_identifier) @reference.type))
(function_declaration result: (slice_type element: (pointer_type (type_identifier) @reference.type)))
(method_declaration result: (slice_type element: (type_identifier) @reference.type))
(method_declaration result: (slice_type element: (pointer_type (type_identifier) @reference.type)))

; --- Map types: map[K]V (capture both K and V type_identifiers) ---
(field_declaration type: (map_type key: (type_identifier) @reference.type))
(field_declaration type: (map_type value: (type_identifier) @reference.type))
(field_declaration type: (map_type value: (pointer_type (type_identifier) @reference.type)))
(field_declaration type: (map_type value: (qualified_type name: (type_identifier) @reference.type)))
(parameter_declaration type: (map_type key: (type_identifier) @reference.type))
(parameter_declaration type: (map_type value: (type_identifier) @reference.type))
(parameter_declaration type: (map_type value: (pointer_type (type_identifier) @reference.type)))
(parameter_declaration type: (map_type value: (qualified_type name: (type_identifier) @reference.type)))

; --- Channel types: chan Item ---
(field_declaration type: (channel_type value: (type_identifier) @reference.type))
(parameter_declaration type: (channel_type value: (type_identifier) @reference.type))

; --- Type assertions: x.(SomeType), x.(*pkg.SomeType) ---
(type_assertion_expression type: (type_identifier) @reference.type)
(type_assertion_expression type: (pointer_type (type_identifier) @reference.type))
(type_assertion_expression type: (qualified_type name: (type_identifier) @reference.type))
(type_assertion_expression type: (pointer_type (qualified_type name: (type_identifier) @reference.type)))

; --- Type switch cases: case Foo, *Bar, pkg.Baz: ... ---
(type_case type: (type_identifier) @reference.type)
(type_case type: (pointer_type (type_identifier) @reference.type))
(type_case type: (qualified_type name: (type_identifier) @reference.type))
(type_case type: (pointer_type (qualified_type name: (type_identifier) @reference.type)))

; --- Var declarations with explicit type: var x SomeType ---
(var_spec type: (type_identifier) @reference.type)
(var_spec type: (pointer_type (type_identifier) @reference.type))
(var_spec type: (qualified_type name: (type_identifier) @reference.type))
(var_spec type: (pointer_type (qualified_type name: (type_identifier) @reference.type)))
(var_spec type: (slice_type element: (type_identifier) @reference.type))
(var_spec type: (slice_type element: (pointer_type (type_identifier) @reference.type)))
(var_spec type: (slice_type element: (qualified_type name: (type_identifier) @reference.type)))
(var_spec type: (map_type key: (type_identifier) @reference.type))
(var_spec type: (map_type value: (type_identifier) @reference.type))
(var_spec type: (map_type value: (qualified_type name: (type_identifier) @reference.type)))
(var_spec type: (channel_type value: (type_identifier) @reference.type))

; --- Const declarations with explicit type: const x SomeType = 0 ---
(const_spec type: (type_identifier) @reference.type)
(const_spec type: (qualified_type name: (type_identifier) @reference.type))

; --- Composite literals with qualified type: &pkg.User{...}
; (bare-type composite literals already captured as @call.name above —
; that emits a CALLS edge to the type name, which the resolver treats as
; a constructor-like call. The pattern below covers the qualified form
; that the bare composite_literal pattern doesn't match.)
(composite_literal type: (qualified_type name: (type_identifier) @reference.type))
(composite_literal type: (slice_type element: (type_identifier) @reference.type))
(composite_literal type: (slice_type element: (qualified_type name: (type_identifier) @reference.type)))
(composite_literal type: (map_type value: (type_identifier) @reference.type))
(composite_literal type: (map_type value: (qualified_type name: (type_identifier) @reference.type)))
(composite_literal type: (array_type element: (type_identifier) @reference.type))

; --- Array types in field/parameter/result/var positions: [N]Item ---
(field_declaration type: (array_type element: (type_identifier) @reference.type))
(field_declaration type: (array_type element: (qualified_type name: (type_identifier) @reference.type)))
(parameter_declaration type: (array_type element: (type_identifier) @reference.type))
(parameter_declaration type: (array_type element: (qualified_type name: (type_identifier) @reference.type)))
(function_declaration result: (array_type element: (type_identifier) @reference.type))
(method_declaration result: (array_type element: (type_identifier) @reference.type))
(var_spec type: (array_type element: (type_identifier) @reference.type))

; --- Generic type constraints: func F[T Ord](...) ---
; T is the type-parameter name, Ord is the constraint (a real type reference).
; type_constraint contains _type children directly (not wrapped in type_elem).
(type_parameter_declaration type: (type_constraint (type_identifier) @reference.type))
(type_parameter_declaration type: (type_constraint (qualified_type name: (type_identifier) @reference.type)))

; --- Type instantiation expressions: Container[Item], Pick[int]
; (rhs of short var decls, function arguments, parametric type uses)
(type_instantiation_expression type: (type_identifier) @reference.type)
(type_instantiation_expression type: (qualified_type name: (type_identifier) @reference.type))

; ── Go method-call receiver-type inference ──────────────────────────
; Track each parameter's declared type so method calls on that parameter
; can be rewritten from "x.Method" to "Type.Method" — eliminating the
; bare-name ambiguity that drops or misroutes 15-25% of CALL edges.
; Mirrors the Python typed-parameter mechanism (_typed_param_scope etc.).

; Function parameter — bare type: func F(x Item) {...}
(function_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (type_identifier) @_go_param_type))) @_go_param_scope

; Function parameter — pointer type: func F(x *Item) {...}
(function_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (pointer_type (type_identifier) @_go_param_type)))) @_go_param_scope

; Function parameter — qualified type: func F(x pkg.Item) {...}
(function_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (qualified_type name: (type_identifier) @_go_param_type)))) @_go_param_scope

; Function parameter — pointer-qualified: func F(x *pkg.Item) {...}
(function_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (pointer_type (qualified_type name: (type_identifier) @_go_param_type))))) @_go_param_scope

; Method parameter — bare type
(method_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (type_identifier) @_go_param_type))) @_go_param_scope

; Method parameter — pointer type
(method_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (pointer_type (type_identifier) @_go_param_type)))) @_go_param_scope

; Method parameter — qualified type
(method_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (qualified_type name: (type_identifier) @_go_param_type)))) @_go_param_scope

; Method parameter — pointer-qualified
(method_declaration
  parameters: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (pointer_type (qualified_type name: (type_identifier) @_go_param_type))))) @_go_param_scope

; Method receiver — bare type: func (r Recv) M() {...}  → r → Recv
(method_declaration
  receiver: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (type_identifier) @_go_param_type))) @_go_param_scope

; Method receiver — pointer type: func (r *Recv) M() {...}
(method_declaration
  receiver: (parameter_list (parameter_declaration
    name: (identifier) @_go_param_name
    type: (pointer_type (type_identifier) @_go_param_type)))) @_go_param_scope
(parameter_declaration type: (pointer_type (type_identifier) @reference.type))
(function_declaration result: (pointer_type (type_identifier) @reference.type))
(method_declaration result: (pointer_type (type_identifier) @reference.type))
; Imported/package-qualified type refs: *scheduler.Scheduler, scheduler.Scheduler
(field_declaration type: (qualified_type (type_identifier) @reference.type))
(parameter_declaration type: (qualified_type (type_identifier) @reference.type))
(function_declaration result: (qualified_type (type_identifier) @reference.type))
(method_declaration result: (qualified_type (type_identifier) @reference.type))
(field_declaration type: (pointer_type (qualified_type (type_identifier) @reference.type)))
(parameter_declaration type: (pointer_type (qualified_type (type_identifier) @reference.type)))
(function_declaration result: (pointer_type (qualified_type (type_identifier) @reference.type)))
(method_declaration result: (pointer_type (qualified_type (type_identifier) @reference.type)))
`;

// C++ queries - works with tree-sitter-cpp
export const CPP_QUERIES = `
; Classes, Structs, Namespaces
(class_specifier name: (type_identifier) @name) @definition.class
(struct_specifier name: (type_identifier) @name) @definition.struct
(namespace_definition name: (namespace_identifier) @name) @definition.namespace
(enum_specifier name: (type_identifier) @name) @definition.enum

; Typedefs and unions (common in C-style headers and mixed C/C++ code)
(type_definition declarator: (type_identifier) @name) @definition.typedef
(union_specifier name: (type_identifier) @name) @definition.union

; Macros
(preproc_function_def name: (identifier) @name) @definition.macro
(preproc_def name: (identifier) @name) @definition.macro

; Functions & Methods (direct declarator)
(function_definition declarator: (function_declarator declarator: (identifier) @name)) @definition.function
(function_definition declarator: (function_declarator declarator: (qualified_identifier scope: (_) @receiver.type name: (identifier) @name))) @definition.method

; Functions/methods returning pointers (pointer_declarator wraps function_declarator)
(function_definition declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function
(function_definition declarator: (pointer_declarator declarator: (function_declarator declarator: (qualified_identifier scope: (_) @receiver.type name: (identifier) @name)))) @definition.method

; Functions/methods returning double pointers (nested pointer_declarator)
(function_definition declarator: (pointer_declarator declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name)))) @definition.function
(function_definition declarator: (pointer_declarator declarator: (pointer_declarator declarator: (function_declarator declarator: (qualified_identifier scope: (_) @receiver.type name: (identifier) @name))))) @definition.method

; Functions/methods returning references (reference_declarator wraps function_declarator)
(function_definition declarator: (reference_declarator (function_declarator declarator: (identifier) @name))) @definition.function
(function_definition declarator: (reference_declarator (function_declarator declarator: (qualified_identifier scope: (_) @receiver.type name: (identifier) @name)))) @definition.method

; Destructors (destructor_name is distinct from identifier in tree-sitter-cpp)
(function_definition declarator: (function_declarator declarator: (qualified_identifier scope: (_) @receiver.type name: (destructor_name) @name))) @definition.method

; Function declarations / prototypes (common in headers)
(declaration declarator: (function_declarator declarator: (identifier) @name)) @definition.function
(declaration declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function

; Inline class method declarations (inside class body, no body: void Foo();)
(field_declaration declarator: (function_declarator declarator: (identifier) @name)) @definition.method

; Inline class method definitions (inside class body, with body: void Foo() { ... })
(field_declaration_list
  (function_definition
    declarator: (function_declarator
      declarator: [(field_identifier) (identifier) (operator_name) (destructor_name)] @name)) @definition.method)

; Templates
(template_declaration (class_specifier name: (type_identifier) @name)) @definition.template
(template_declaration (function_definition declarator: (function_declarator declarator: (identifier) @name))) @definition.template

; Includes
(preproc_include path: (_) @import.source) @import

; Calls
(call_expression function: (identifier) @call.name) @call
(call_expression function: (field_expression field: (field_identifier) @call.name)) @call
(call_expression function: (field_expression argument: (identifier) @_qualifier field: (field_identifier) @call.name)) @call
(call_expression function: (qualified_identifier name: (identifier) @call.name)) @call
(call_expression function: (template_function name: (identifier) @call.name)) @call

; Constructor calls: new User()
(new_expression type: (type_identifier) @call.name) @call

; Heritage
(class_specifier name: (type_identifier) @heritage.class
  (base_class_clause (type_identifier) @heritage.extends)) @heritage
(class_specifier name: (type_identifier) @heritage.class
  (base_class_clause (access_specifier) (type_identifier) @heritage.extends)) @heritage

; Type references — captures types used in declaration/parameter positions
(declaration type: (type_identifier) @reference.type)
(parameter_declaration type: (type_identifier) @reference.type)

; Typed declarations for qualifier substitution: local vars, fields, params
(declaration
  type: (type_identifier) @_typed_var_type
  declarator: (identifier) @_typed_var_name) @_typed_var_scope
(declaration
  type: (type_identifier) @_typed_var_type
  declarator: (pointer_declarator declarator: (identifier) @_typed_var_name)) @_typed_var_scope
(declaration
  type: (type_identifier) @_typed_var_type
  declarator: (init_declarator declarator: (identifier) @_typed_var_name)) @_typed_var_scope
(declaration
  type: (type_identifier) @_typed_var_type
  declarator: (init_declarator declarator: (pointer_declarator declarator: (identifier) @_typed_var_name))) @_typed_var_scope
(declaration
  type: (type_identifier) @_typed_var_type
  declarator: (reference_declarator (identifier) @_typed_var_name)) @_typed_var_scope
(field_declaration
  type: (type_identifier) @_typed_var_type
  declarator: (field_identifier) @_typed_var_name) @_typed_var_scope
(field_declaration
  type: (type_identifier) @_typed_var_type
  declarator: (pointer_declarator declarator: (field_identifier) @_typed_var_name)) @_typed_var_scope
(parameter_declaration
  type: (type_identifier) @_typed_var_type
  declarator: (identifier) @_typed_var_name) @_typed_var_scope
(parameter_declaration
  type: (type_identifier) @_typed_var_type
  declarator: (pointer_declarator declarator: (identifier) @_typed_var_name)) @_typed_var_scope
`;

// C# queries - works with tree-sitter-c-sharp
export const CSHARP_QUERIES = `
; Types
(class_declaration name: (identifier) @name) @definition.class
(interface_declaration name: (identifier) @name) @definition.interface
(struct_declaration name: (identifier) @name) @definition.struct
(enum_declaration name: (identifier) @name) @definition.enum
(record_declaration name: (identifier) @name) @definition.record
(delegate_declaration name: (identifier) @name) @definition.delegate

; Namespaces (block form and C# 10+ file-scoped form)
(namespace_declaration name: (identifier) @name) @definition.namespace
(namespace_declaration name: (qualified_name) @name) @definition.namespace
(file_scoped_namespace_declaration name: (identifier) @name) @definition.namespace
(file_scoped_namespace_declaration name: (qualified_name) @name) @definition.namespace

; Methods & Properties
(method_declaration name: (identifier) @name) @definition.method
(local_function_statement name: (identifier) @name) @definition.function
(constructor_declaration name: (identifier) @name) @definition.constructor
(property_declaration name: (identifier) @name) @definition.property

; Primary constructors (C# 12): class User(string name, int age) { }
(class_declaration name: (identifier) @name (parameter_list) @definition.constructor)
(record_declaration name: (identifier) @name (parameter_list) @definition.constructor)

; Using
(using_directive (qualified_name) @import.source) @import
(using_directive (identifier) @import.source) @import

; Calls
(invocation_expression function: (identifier) @call.name) @call
(invocation_expression function: (member_access_expression name: (identifier) @call.name)) @call

; Null-conditional method calls: user?.Save()
; Parses as: invocation_expression → conditional_access_expression → member_binding_expression → identifier
(invocation_expression
  function: (conditional_access_expression
    (member_binding_expression
      (identifier) @call.name))) @call

; Constructor calls: new Foo() and new Foo { Props }
(object_creation_expression type: (identifier) @call.name) @call

; Target-typed new (C# 9): User u = new("x", 5)
(variable_declaration type: (identifier) @call.name (variable_declarator (implicit_object_creation_expression) @call))

; Heritage
(class_declaration name: (identifier) @heritage.class
  (base_list (identifier) @heritage.extends)) @heritage
(class_declaration name: (identifier) @heritage.class
  (base_list (generic_name (identifier) @heritage.extends))) @heritage

; Type references — captures types used in field/param/return-type positions
(parameter type: (identifier) @reference.type)
(variable_declaration type: (identifier) @reference.type)
(field_declaration (variable_declaration type: (identifier) @reference.type))
(method_declaration returns: (identifier) @reference.type)
`;

// Rust queries - works with tree-sitter-rust
export const RUST_QUERIES = `
; Functions & Items
(function_item name: (identifier) @name) @definition.function
(struct_item name: (type_identifier) @name) @definition.struct
(enum_item name: (type_identifier) @name) @definition.enum
(trait_item name: (type_identifier) @name) @definition.trait
(impl_item type: (type_identifier) @name !trait) @definition.impl
(impl_item type: (generic_type type: (type_identifier) @name) !trait) @definition.impl
(mod_item name: (identifier) @name) @definition.module

; Type aliases, const, static, macros
(type_item name: (type_identifier) @name) @definition.type
(const_item name: (identifier) @name) @definition.const
(static_item name: (identifier) @name) @definition.static
(macro_definition name: (identifier) @name) @definition.macro

; Use statements
(use_declaration argument: (_) @import.source) @import

; Calls
(call_expression function: (identifier) @call.name) @call
(call_expression function: (field_expression field: (field_identifier) @call.name)) @call
(call_expression function: (scoped_identifier
  path: (identifier) @_qualifier
  name: (identifier) @call.name)) @call
(call_expression function: (generic_function function: (identifier) @call.name)) @call

; Struct literal construction: User { name: value }
(struct_expression name: (type_identifier) @call.name) @call

; Type references — captures types used in field/param/return-type positions
(field_declaration type: (type_identifier) @reference.type)
(parameter pattern: (_) type: (type_identifier) @reference.type)
(function_item return_type: (type_identifier) @reference.type)

; Wrapped type references — &Type, &mut Type
(field_declaration type: (reference_type (type_identifier) @reference.type))
(parameter pattern: (_) type: (reference_type (type_identifier) @reference.type))
(function_item return_type: (reference_type (type_identifier) @reference.type))

; Generic wrapper types — Box<Type>, Arc<Type>, Vec<Type>, Option<Type>, Result<Type,_>, etc.
; Capture the type argument (inner type), not the wrapper name.
(field_declaration type: (generic_type type_arguments: (type_arguments (type_identifier) @reference.type)))
(parameter pattern: (_) type: (generic_type type_arguments: (type_arguments (type_identifier) @reference.type)))
(function_item return_type: (generic_type type_arguments: (type_arguments (type_identifier) @reference.type)))

; impl Trait in parameter / return positions
(parameter pattern: (_) type: (abstract_type (type_identifier) @reference.type))
(function_item return_type: (abstract_type (type_identifier) @reference.type))

; dyn Trait — trait objects (direct: fn foo(x: dyn Trait) or field: x: dyn Trait)
(field_declaration type: (dynamic_type (type_identifier) @reference.type))
(parameter pattern: (_) type: (dynamic_type (type_identifier) @reference.type))
(function_item return_type: (dynamic_type (type_identifier) @reference.type))

; dyn Trait nested inside generic wrapper — Box<dyn Trait>, Arc<dyn Trait>, etc.
(field_declaration type: (generic_type type_arguments: (type_arguments (dynamic_type (type_identifier) @reference.type))))
(parameter pattern: (_) type: (generic_type type_arguments: (type_arguments (dynamic_type (type_identifier) @reference.type))))
(function_item return_type: (generic_type type_arguments: (type_arguments (dynamic_type (type_identifier) @reference.type))))

; Heritage (trait implementation) — all combinations of concrete/generic trait × concrete/generic type
(impl_item trait: (type_identifier) @heritage.trait type: (type_identifier) @heritage.class) @heritage
(impl_item trait: (generic_type type: (type_identifier) @heritage.trait) type: (type_identifier) @heritage.class) @heritage
(impl_item trait: (type_identifier) @heritage.trait type: (generic_type type: (type_identifier) @heritage.class)) @heritage
(impl_item trait: (generic_type type: (type_identifier) @heritage.trait) type: (generic_type type: (type_identifier) @heritage.class)) @heritage
`;

// PHP queries - works with tree-sitter-php (php_only grammar)
export const PHP_QUERIES = `
; ── Namespace ────────────────────────────────────────────────────────────────
(namespace_definition
  name: (namespace_name) @name) @definition.namespace

; ── Classes ──────────────────────────────────────────────────────────────────
(class_declaration
  name: (name) @name) @definition.class

; ── Interfaces ───────────────────────────────────────────────────────────────
(interface_declaration
  name: (name) @name) @definition.interface

; ── Traits ───────────────────────────────────────────────────────────────────
(trait_declaration
  name: (name) @name) @definition.trait

; ── Enums (PHP 8.1) ──────────────────────────────────────────────────────────
(enum_declaration
  name: (name) @name) @definition.enum

; ── Top-level functions ───────────────────────────────────────────────────────
(function_definition
  name: (name) @name) @definition.function

; ── Methods (including constructors) ─────────────────────────────────────────
(method_declaration
  name: (name) @name) @definition.method

; ── Class properties (including Eloquent $fillable, $casts, etc.) ────────────
(property_declaration
  (property_element
    (variable_name
      (name) @name))) @definition.property

; ── Imports: use statements ──────────────────────────────────────────────────
; Simple: use App\\Models\\User;
; The leading anchor pins the capture to the clause's FIRST named child. In
; "use A\\B\\C as Alias" the alias is a sibling (name) of the (qualified_name),
; so an unanchored alternation would capture it too and emit a phantom import
; of Alias. "use GlobalThing;" is a lone (name) child and still matches.
(namespace_use_declaration
  (namespace_use_clause
    . [(qualified_name) (name)] @import.source)) @import

; Grouped: use App\\Models\\{User, Repo};
; The shared prefix is a namespace_name; each member is its own
; namespace_use_clause carrying the bare member name (and optionally an
; 'as' alias). The clause is captured whole so the handler can take the
; FIRST name child as the imported symbol and skip the alias.
(namespace_use_declaration
  (namespace_name) @import.prefix
  (namespace_use_group
    (namespace_use_clause) @import.clause)) @import

; ── Function/method calls ────────────────────────────────────────────────────
; Regular function call: foo()
(function_call_expression
  function: (name) @call.name) @call

; Method call: $obj->method()
(member_call_expression
  object: (_) @_qualifier
  name: (name) @call.name) @call

; Nullsafe method call: $obj?->method()
(nullsafe_member_call_expression
  object: (_) @_qualifier
  name: (name) @call.name) @call

; Static call: Foo::bar() (php_only uses scoped_call_expression)
(scoped_call_expression
  name: (name) @call.name) @call

; Constructor call: new User()
(object_creation_expression (name) @call.name) @call

; Typed properties: private Service $s / private ?Service $s
;
; ?Service is NOT a named_type -- tree-sitter-php wraps it as
; (optional_type (named_type (name))). Matching only named_type silently drops
; every nullable dependency back to a bare method name, which is most of them in
; modern PHP. Union types (Service|Repo) are deliberately left out: there is no
; single receiver type to attribute the call to, so a bare name is the honest
; answer there.
(property_declaration
  type: [(named_type (name) @_typed_var_type)
         (optional_type (named_type (name) @_typed_var_type))]
  (property_element
    name: (variable_name (name) @_typed_var_name))) @_typed_var_scope

; Constructor-promoted properties: private ?Service $service
(property_promotion_parameter
  type: [(named_type (name) @_typed_var_type)
         (optional_type (named_type (name) @_typed_var_type))]
  name: (variable_name (name) @_typed_var_name)) @_typed_var_scope

; Typed parameters on methods AND plain functions: function run(?Service $s)
;
; function_definition is a separate node from method_declaration, so rooting
; this at method_declaration alone left every top-level function's parameters
; untyped. The handler needs no change: childForFieldName('name') works on both,
; and with no enclosing class the scope becomes the bare function name — which is
; exactly what findEnclosingFunction returns for a top-level function.
[(method_declaration
   parameters: (formal_parameters
     [(simple_parameter
       type: [(named_type (name) @_typed_param_type)
              (optional_type (named_type (name) @_typed_param_type))]
       name: (variable_name (name) @_typed_param_name))
      (property_promotion_parameter
       type: [(named_type (name) @_typed_param_type)
              (optional_type (named_type (name) @_typed_param_type))]
       name: (variable_name (name) @_typed_param_name))]))
 (function_definition
   parameters: (formal_parameters
     (simple_parameter
       type: [(named_type (name) @_typed_param_type)
              (optional_type (named_type (name) @_typed_param_type))]
       name: (variable_name (name) @_typed_param_name))))] @_typed_param_scope

; ── Heritage: extends ────────────────────────────────────────────────────────
(class_declaration
  name: (name) @heritage.class
  (base_clause
    [(name) (qualified_name)] @heritage.extends)) @heritage

; ── Heritage: implements ─────────────────────────────────────────────────────
(class_declaration
  name: (name) @heritage.class
  (class_interface_clause
    [(name) (qualified_name)] @heritage.implements)) @heritage.impl

; ── Heritage: use trait (must capture enclosing class name) ──────────────────
(class_declaration
  name: (name) @heritage.class
  body: (declaration_list
    (use_declaration
      [(name) (qualified_name)] @heritage.trait))) @heritage

; ── Type references ───────────────────────────────────────────────────────────
(named_type (name) @reference.type)
`;

// Ruby queries - works with tree-sitter-ruby
export const RUBY_QUERIES = `
; ── Modules ──────────────────────────────────────────────────────────────────
(module
  name: (constant) @name) @definition.module

; ── Classes ──────────────────────────────────────────────────────────────────
(class
  name: (constant) @name) @definition.class

; Scoped class name: class Module::ClassName
(class
  name: (scope_resolution) @name) @definition.class

; ── Instance methods ─────────────────────────────────────────────────────────
(method
  name: (identifier) @name) @definition.method

; ── Singleton (class-level) methods ──────────────────────────────────────────
(singleton_method
  name: (identifier) @name) @definition.method

; ── Require / require_relative → IMPORTS ─────────────────────────────────────
; Capture the string argument so the second pass emits an IMPORTS edge.
(call
  method: (identifier) @_req
  arguments: (argument_list
    (string (string_content) @import.source))
  (#match? @_req "^require"))  @import

; ── All other calls ───────────────────────────────────────────────────────────
(call
  method: (identifier) @call.name) @call

; ── Bare calls without parens (identifiers at statement level are method calls) ─
; NOTE: This may over-capture variable reads as calls (e.g. 'result' at
; statement level). Ruby's grammar makes bare identifiers ambiguous — they
; could be local variables or zero-arity method calls. Post-processing via
; the BUILTINS set suppresses most false positives.
(body_statement
  (identifier) @call.name @call)

; ── Heritage: class < SuperClass ─────────────────────────────────────────────
(class
  name: (constant) @heritage.class
  superclass: (superclass
    (constant) @heritage.extends)) @heritage

; Scoped superclass: class Foo < Module::Class
; Captures full "Module::Class" text to avoid ambiguity with bare "Class" names
(class
  name: (constant) @heritage.class
  superclass: (superclass
    (scope_resolution) @heritage.extends)) @heritage

; Scoped class name, bare superclass: class Module::Foo < Bar
(class
  name: (scope_resolution) @heritage.class
  superclass: (superclass
    (constant) @heritage.extends)) @heritage

; Scoped class name, scoped superclass: class Module::Foo < Module::Bar
(class
  name: (scope_resolution) @heritage.class
  superclass: (superclass
    (scope_resolution) @heritage.extends)) @heritage
`;

// Kotlin queries - works with tree-sitter-kotlin (fwcd/tree-sitter-kotlin)
// Based on official tags.scm; functions use simple_identifier, classes use type_identifier
export const KOTLIN_QUERIES = `
; ── Interfaces ─────────────────────────────────────────────────────────────
; tree-sitter-kotlin (fwcd) has no interface_declaration node type.
; Interfaces are class_declaration nodes with an anonymous "interface" keyword child.
(class_declaration
  "interface"
  (type_identifier) @name) @definition.interface

; ── Classes (regular, data, sealed, enum) ────────────────────────────────
; All have the anonymous "class" keyword child. enum class has both
; "enum" and "class" children — the "class" child still matches.
(class_declaration
  "class"
  (type_identifier) @name) @definition.class

; ── Object declarations (Kotlin singletons) ──────────────────────────────
(object_declaration
  (type_identifier) @name) @definition.class

; ── Companion objects (named only) ───────────────────────────────────────
(companion_object
  (type_identifier) @name) @definition.class

; ── Functions (top-level, member, extension) ──────────────────────────────
(function_declaration
  (simple_identifier) @name) @definition.function

; ── Properties ───────────────────────────────────────────────────────────
(property_declaration
  (variable_declaration
    (simple_identifier) @name)) @definition.property

; ── Enum entries ─────────────────────────────────────────────────────────
(enum_entry
  (simple_identifier) @name) @definition.enum

; ── Type aliases ─────────────────────────────────────────────────────────
(type_alias
  (type_identifier) @name) @definition.type

; ── Imports ──────────────────────────────────────────────────────────────
(import_header
  (identifier) @import.source) @import

; ── Function calls (direct) ──────────────────────────────────────────────
(call_expression
  (simple_identifier) @call.name) @call

; ── Method calls (via navigation: obj.method()) ──────────────────────────
(call_expression
  (navigation_expression
    (navigation_suffix
      (simple_identifier) @call.name))) @call

; ── Constructor invocations ──────────────────────────────────────────────
(constructor_invocation
  (user_type
    (type_identifier) @call.name)) @call

; ── Infix function calls (e.g., a to b, x until y) ──────────────────────
(infix_expression
  (simple_identifier) @call.name) @call

; ── Heritage: extends / implements via delegation_specifier ──────────────
; Interface implementation (bare user_type): class Foo : Bar
(class_declaration
  (type_identifier) @heritage.class
  (delegation_specifier
    (user_type (type_identifier) @heritage.extends))) @heritage

; Class extension (constructor_invocation): class Foo : Bar()
(class_declaration
  (type_identifier) @heritage.class
  (delegation_specifier
    (constructor_invocation
      (user_type (type_identifier) @heritage.extends)))) @heritage

; ── Type references ───────────────────────────────────────────────────────────
(type_reference (user_type (type_identifier) @reference.type))
`;

// Swift queries - works with tree-sitter-swift
export const SWIFT_QUERIES = `
; Classes
(class_declaration "class" name: (type_identifier) @name) @definition.class

; Structs
(class_declaration "struct" name: (type_identifier) @name) @definition.struct

; Enums
(class_declaration "enum" name: (type_identifier) @name) @definition.enum

; Extensions (mapped to class — no dedicated label in schema)
(class_declaration "extension" name: (user_type (type_identifier) @name)) @definition.class

; Actors
(class_declaration "actor" name: (type_identifier) @name) @definition.class

; Protocols (mapped to interface)
(protocol_declaration name: (type_identifier) @name) @definition.interface

; Type aliases
(typealias_declaration name: (type_identifier) @name) @definition.type

; Functions (top-level and methods)
(function_declaration name: (simple_identifier) @name) @definition.function

; Protocol method declarations
(protocol_function_declaration name: (simple_identifier) @name) @definition.method

; Initializers — "init" is the canonical name; line offset tracked via definition node
(init_declaration "init" @name) @definition.constructor

; Properties (stored and computed)
(property_declaration (pattern (simple_identifier) @name)) @definition.property

; Imports
(import_declaration (identifier (simple_identifier) @import.source)) @import

; Calls - direct function calls
(call_expression (simple_identifier) @call.name) @call

; Calls - member/navigation calls (obj.method())
(call_expression (navigation_expression (navigation_suffix (simple_identifier) @call.name))) @call

; Heritage - class/struct/enum inheritance and protocol conformance
(class_declaration name: (type_identifier) @heritage.class
  (inheritance_specifier inherits_from: (user_type (type_identifier) @heritage.extends))) @heritage

; Heritage - protocol inheritance
(protocol_declaration name: (type_identifier) @heritage.class
  (inheritance_specifier inherits_from: (user_type (type_identifier) @heritage.extends))) @heritage

; Heritage - extension protocol conformance (e.g. extension Foo: SomeProtocol)
; Extensions wrap the name in user_type unlike class/struct/enum declarations
(class_declaration "extension" name: (user_type (type_identifier) @heritage.class)
  (inheritance_specifier inherits_from: (user_type (type_identifier) @heritage.extends))) @heritage

; Type references — captures types used in annotations/parameters/return types
(type_annotation (user_type (type_identifier) @reference.type))
`;

// Scala queries - works with tree-sitter-scala
export const SCALA_QUERIES = `
; ── Classes (class, case class, abstract class, sealed class) ────────────────
(class_definition
  name: (identifier) @name) @definition.class

; ── Traits (mapped to interface) ─────────────────────────────────────────────
(trait_definition
  name: (identifier) @name) @definition.interface

; ── Object definitions (companion objects, case objects, singletons) ─────────
(object_definition
  name: (identifier) @name) @definition.class

; ── Function / method definitions ────────────────────────────────────────────
(function_definition
  name: (identifier) @name) @definition.function

; ── Abstract method declarations (in traits / abstract classes) ──────────────
(function_declaration
  name: (identifier) @name) @definition.method

; ── Type aliases ──────────────────────────────────────────────────────────────
(type_definition
  name: (type_identifier) @name) @definition.type

; ── Val/var definitions (class-level members only, not method-local vars) ──────
; Restricting to template_body ensures we only capture class/object-level
; declarations (e.g. companion object implicits) and not local val bindings
; inside method bodies, which would add noise entities to the graph.
(template_body
  (val_definition (identifier) @name) @definition.const)

(template_body
  (var_definition (identifier) @name) @definition.const)

; ── Top-level val/var definitions (compilation-unit level) ────────────────────
; Scala 2 forbids these, so ordinary .scala files are unaffected. They matter
; for .sbt build definitions, where every module is a top-level
; \`lazy val core = (project in file("core"))\`, and for Scala 3 top-level
; definitions. Still excludes method-local bindings, which are nested inside a
; block rather than the compilation unit.
(compilation_unit
  (val_definition (identifier) @name) @definition.const)

(compilation_unit
  (var_definition (identifier) @name) @definition.const)

; ── Imports: capture full declaration for dotted-path reconstruction ─────────
; Handles: import ix.memory.model._  (wildcard)
;          import ix.memory.model.{NodeKind, GraphNode}  (selective)
;          import ix.memory.model.NodeKind  (simple)
(import_declaration) @import.stmt

; ── Calls: direct function call ──────────────────────────────────────────────
(call_expression
  function: (identifier) @call.name) @call

; ── Calls: method call (obj.method()) ────────────────────────────────────────
(call_expression
  function: (field_expression
    field: (identifier) @call.name)) @call

; ── Constructor: new Foo() ────────────────────────────────────────────────────
(instance_expression
  (type_identifier) @call.name) @call

; ── Heritage: class / object extends and with ────────────────────────────────
(class_definition
  name: (identifier) @heritage.class
  extend: (extends_clause
    (type_identifier) @heritage.extends)) @heritage

(trait_definition
  name: (identifier) @heritage.class
  extend: (extends_clause
    (type_identifier) @heritage.extends)) @heritage

(object_definition
  name: (identifier) @heritage.class
  extend: (extends_clause
    (type_identifier) @heritage.extends)) @heritage

; ── Type references ───────────────────────────────────────────────────────────
; Case class parameter types: case class Foo(x: NodeKind)
(class_parameter (type_identifier) @reference.type)
; Method parameter types: def foo(x: NodeKind)
(parameter (type_identifier) @reference.type)
; Val/var type annotations: val x: NodeKind = ...
(val_definition (type_identifier) @reference.type)
(var_definition (type_identifier) @reference.type)
; Return type annotations: def foo(): NodeKind = ...
; (type_identifier is a direct child of function_definition, no named field)
(function_definition (type_identifier) @reference.type)
; Generic type arguments: Map[NodeId, NodeKind], Option[NodeKind], Set[NodeKind], etc.
(generic_type (type_arguments (type_identifier) @reference.type))

; ── Qualified singleton / companion-object member access ──────────────────────
; Two grammar shapes arise in practice:
;   expression context:  NodeKind.Class      → field_expression
;   pattern  context:  case Some(NodeKind.Class) => … → stable_identifier
; Both shapes are captured below. The (#match? ^[A-Z]) predicates restrict to
; UpperCamelCase identifiers (Scala convention for case objects / companion
; object members), filtering out lowercase method / field names.

; Expression: val x = NodeKind.Decision
(field_expression
  value: (identifier) @_qualifier
  field: (identifier) @call.name
  (#match? @_qualifier "^[A-Z]")
  (#match? @call.name "^[A-Z]"))

; Pattern:  case Some(NodeKind.Class) | Some(NodeKind.Trait) => …
(stable_identifier
  (identifier) @_qualifier
  "."
  (identifier) @call.name
  (#match? @_qualifier "^[A-Z]")
  (#match? @call.name "^[A-Z]"))
`;
export const MAKEFILE_QUERIES = `
; ── Rule targets ─────────────────────────────────────────────────────────────
(rule
  (targets (word) @name)
  (#not-match? @name "^\\.[A-Z]")) @definition.function

; ── Variable assignments ──────────────────────────────────────────────────────
(variable_assignment
  name: (word) @name) @definition.macro

; ── Include directives ────────────────────────────────────────────────────────
(include_directive
  (list (word) @import.name))
`;


// tree-sitter-elixir: https://github.com/elixir-lang/tree-sitter-elixir
// Most constructs are represented as `call` nodes with a `target` identifier.
export const ELIXIR_QUERIES = `
; ── Module / protocol / impl definitions ─────────────────────────────────────
(call
  target: (identifier) @_defmodule
  (arguments (alias) @name)
  (#match? @_defmodule "^(defmodule|defprotocol)$")) @definition.class

(call
  target: (identifier) @_defimpl
  (arguments (alias) @name)
  (#eq? @_defimpl "defimpl")) @definition.class

; ── Function definitions with parens: def foo(args) do … end ─────────────────
(call
  target: (identifier) @_def
  (arguments
    (call target: (identifier) @name))
  (#match? @_def "^(def|defp)$")) @definition.function

; ── Macro definitions with parens: defmacro my_macro(x) do … end ─────────────
(call
  target: (identifier) @_defmacro
  (arguments
    (call target: (identifier) @name))
  (#match? @_defmacro "^(defmacro|defmacrop)$")) @definition.macro

; ── Zero-arity function definitions without parens: def foo do … end ─────────
(call
  target: (identifier) @_def0
  (arguments
    (identifier) @name)
  (#match? @_def0 "^(def|defp)$")) @definition.function

; ── Zero-arity macro definitions without parens ───────────────────────────────
(call
  target: (identifier) @_defmacro0
  (arguments
    (identifier) @name)
  (#match? @_defmacro0 "^(defmacro|defmacrop)$")) @definition.macro

; ── Module directives → IMPORTS ───────────────────────────────────────────────
(call
  target: (identifier) @_directive
  (arguments (alias) @import.source)
  (#match? @_directive "^(alias|import|use|require)$")) @import

  ; ── Grouped alias: alias MyApp.{User, Repo, Post} → one IMPORTS per member ───
(call
  target: (identifier) @_alias_group
  (arguments
    (dot
      left: (alias) @import.prefix
      right: (tuple (alias) @import.source)))
  (#eq? @_alias_group "alias")) @import

; ── Qualified calls: Foo.bar() — captures qualifier so resolution is possible ─
(call
  target: (dot
    left: (alias) @_qualifier
    right: (identifier) @call.name)) @call

; ── Unqualified calls — keywords excluded to avoid bogus edges ────────────────
(call
  target: (identifier) @call.name
    (#not-match? @call.name "^(def|defp|defmacro|defmacrop|defmodule|defprotocol|defimpl|defstruct|use|alias|import|require|quote|unquote|if|unless|case|cond|with|for|try|receive|raise|throw|fn|do|behaviour|behavior|impl|derive|moduledoc|doc|spec|type|typep|opaque|callback|enforce_keys)$")) @call


; ── Guarded function definitions: def f(x) when guard do … end ──────────────
(call
  target: (identifier) @_defg
  (arguments
    (binary_operator 
      left: (call target: (identifier) @name)))
  (#match? @_defg "^(def|defp)$")) @definition.function

 ; ── Guarded macro definitions ─────────────────────────────────────────────────
(call
  target: (identifier) @_defmacrog
  (arguments
    (binary_operator 
      left: (call target: (identifier) @name)))
  (#match? @_defmacrog "^(defmacro|defmacrop)$")) @definition.macro
 `;

export const SAS_QUERIES = `
; Macro definitions — %MACRO name ... %MEND
(macro_definition
  name: (macro_name) @name) @definition.macro

; Macro call statements — %name(args);
(macro_call_statement
  name: (macro_name) @call.name) @call

; Inline macro calls — %name(args) inside DATA/PROC/etc.
(macro_call
  name: (macro_name) @call.name) @call

; DATA step — capture the whole dataset_name node so two-part names (work.foo)
; stay one entity. Sentinels (_null_) and unexpanded macro vars (&ds) are
; filtered downstream in index.ts (they have no stable identity).
(data_step
  (data_step_header
    (dataset_name) @name)) @definition.module

; PROC step — the procedure name itself is too generic to be a graph node
; (means/sort/freq repeat once per file). index.ts re-keys this on the PROC's
; out= output dataset, or drops it when the step produces no dataset.
(proc_step
  (proc_step_header
    name: (identifier) @name)) @definition.module

; %INCLUDE — string literal form: %include 'path/to/file.sas';
(include_statement
  source: (string_literal) @import.source) @import

; %INCLUDE — fileref form: %include FILEREF; or %include FILEREF(member.sas);
(include_statement
  source: (fileref_source) @import.source) @import

; LIBNAME — library path import
(libname_statement
  (string_literal) @import.source) @import

; PROC SQL — CREATE TABLE AS output
(sql_create_statement
  output: (dataset_name) @name) @definition.module

; PROC SQL — FROM table inputs
(sql_select_statement
  (table_reference
    (dataset_name) @import.source)) @import

; PROC SQL — JOIN table inputs
(sql_select_statement
  (sql_join_clause
    (table_reference
      (dataset_name) @import.source))) @import

; DATA step SET inputs
(data_step
  (set_statement
    (dataset_name) @import.source)) @import

; DATA step MERGE inputs
(data_step
  (merge_statement
    (dataset_name) @import.source)) @import

; DATA step UPDATE inputs
(data_step
  (update_statement
    (dataset_name) @import.source)) @import
`;

// R queries — works with @davisvaughan/tree-sitter-r
export const R_QUERIES = `
; Function definitions — mirrors the official tags.scm bundled with the grammar.
; Covers <- and = operators, identifier and string LHS (S3/R5 method names).
(binary_operator
  lhs: (identifier) @name
  operator: ["<-" "="]
  rhs: (function_definition)) @definition.function

(binary_operator
  lhs: (string) @name
  operator: ["<-" "="]
  rhs: (function_definition)) @definition.function

; Direct calls: foo(args)
(call
  function: (identifier) @call.name) @call

; Package-qualified calls: pkg::func(args) and pkg:::func(args)
; @_namespace (not @_qualifier) marks a real :: namespace call so the dstName
; keeps the :: separator. That lets the patch builder externalize only genuine
; package calls, not base-R dotted names like is.null.
(call
  function: (namespace_operator
    lhs: (identifier) @_namespace
    rhs: (identifier) @call.name)) @call

; Method calls: obj$method(args)
(call
  function: (extract_operator
    rhs: (identifier) @call.name)) @call

; Imports: library(pkg) / require(pkg) — unquoted symbol form
; In the R grammar, each positional argument is wrapped in an (argument) node with a value: field.
(call
  function: (identifier) @_import_fn
  arguments: (arguments (argument value: (identifier) @import.source))
  (#match? @_import_fn "^(library|require)$")) @import

; Imports: library("pkg") / require("pkg") — quoted string form
(call
  function: (identifier) @_import_fn
  arguments: (arguments (argument value: (string) @import.source))
  (#match? @_import_fn "^(library|require)$")) @import

; Source file imports: source("path/to/file.R")
(call
  function: (identifier) @_src
  arguments: (arguments (argument value: (string) @import.source))
  (#eq? @_src "source")) @import
`;

// CSS. @import (string or url(...)) as stylesheet imports; class and id selectors
// as definitions (repeats of the same name fold to one node per file); @keyframes
// animation names as definitions.
export const CSS_QUERIES = `
(import_statement (string_value (string_content) @import.source)) @import
(import_statement
  (call_expression (function_name) @_f (arguments (string_value (string_content) @import.source)))
  (#eq? @_f "url")) @import

(class_selector (class_name) @name) @definition.class
(id_selector (id_name) @name) @definition.class
(keyframes_statement (keyframes_name) @name) @definition.const
`;

// HCL / Terraform. Each labeled block (resource/data/variable/output/module/
// provider) is a definition named by its primary (last) label; a module's
// `source` attribute is an import to the referenced module.
export const HCL_QUERIES = `
((block (identifier) @_t (string_lit (template_literal) @name) . (block_start))
  (#match? @_t "^(resource|data|variable|output|module|provider)$")) @definition.class

((block (identifier) @_t
   (body (attribute (identifier) @_a
     (expression (literal_value (string_lit (template_literal) @import.source))))))
  (#eq? @_t "module") (#eq? @_a "source")) @import
`;

// XML. The useful cross-file signal in XML config is its dependency references,
// so the queries extract IMPORTS from the attributes that name another file or
// package: href (XInclude), resource (Spring), schemaLocation (XSD), src, and
// Include (MSBuild PackageReference/ProjectReference). Quotes are stripped by the
// shared import-specifier normalization.
export const XML_QUERIES = `
((element (STag (Attribute (Name) @_a (AttValue) @import.source)))
  (#match? @_a "^(href|resource|schemaLocation|src|Include)$")) @import

((element (EmptyElemTag (Attribute (Name) @_a (AttValue) @import.source)))
  (#match? @_a "^(href|resource|schemaLocation|src|Include)$")) @import
`;

// HTML. Resource dependencies as imports (<script src>, <link href>, and
// <a/img/iframe/source/use href|src>), and custom elements (hyphenated tag names
// = web components) as definitions.
export const HTML_QUERIES = `
((script_element
  (start_tag
    (attribute (attribute_name) @_a (quoted_attribute_value (attribute_value) @import.source))))
  (#eq? @_a "src")) @import

((element
  (start_tag (tag_name) @_t
    (attribute (attribute_name) @_a (quoted_attribute_value (attribute_value) @import.source))))
  (#match? @_t "^(link|a|img|iframe|source|use)$")
  (#match? @_a "^(href|src)$")) @import

((element (start_tag (tag_name) @name) (#match? @name "-")) @definition.class)
`;

// Zig. Functions (top-level + struct/union methods are all function_declaration),
// const-bound struct/enum/union types, calls (bare + field-access), and the
// @import("path") builtin as an import.
export const ZIG_QUERIES = `
(function_declaration name: (identifier) @name) @definition.function

(variable_declaration (identifier) @name (struct_declaration)) @definition.struct
(variable_declaration (identifier) @name (enum_declaration)) @definition.enum
(variable_declaration (identifier) @name (union_declaration)) @definition.struct

(call_expression function: (identifier) @call.name) @call
(call_expression function: (field_expression member: (identifier) @call.name)) @call

(builtin_function
  (builtin_identifier) @_imp
  (arguments (string) @import.source)
  (#eq? @_imp "@import")) @import
`;

// Haskell. Function definitions (with args) and top-level binds (no args), data
// types / newtypes / type synonyms, type classes, the module header, function
// application as calls (bare and module-qualified), and module imports.
// NOTE: `type_synomym` is the grammar's actual (misspelled) node name.
export const HASKELL_QUERIES = `
(function name: (variable) @name) @definition.function
(bind name: (variable) @name) @definition.function
(data_type name: (name) @name) @definition.type
(newtype name: (name) @name) @definition.type
(type_synomym (name) @name) @definition.type
(class name: (name) @name) @definition.interface
(header module: (module) @name) @definition.module
(apply function: (variable) @call.name) @call
(apply function: (qualified (variable) @call.name)) @call
(import module: (module) @import.source) @import
`;

// Bash / shell. Functions (both `foo() {}` and `function foo {}`), command
// invocations as calls (system commands dangle at resolution; calls to defined
// functions resolve), and `source FILE` / `. FILE` as imports.
// PowerShell. `function`, `filter` and `workflow` all parse as
// function_statement, so one pattern covers the three. Imports cover the three
// ways a script pulls in code: Import-Module, `using module`/`using namespace`,
// and dot-sourcing (`. ./common.ps1`), which is how most scripts actually share
// helpers.
//
// Class properties are deliberately not captured: the `variable` node keeps its
// `$` sigil, so `$Root` would never join to the `.Root` that references it, and
// there is no name-normalisation hook to strip it. Better absent than wrong.
export const POWERSHELL_QUERIES = `
(function_statement (function_name) @name) @definition.function

(class_statement (simple_name) @name) @definition.class

(class_method_definition (simple_name) @name) @definition.method

(enum_statement (simple_name) @name) @definition.enum

(command (command_name) @call.name) @call

(command
  (command_name) @_cmd
  (command_elements . (command_argument_sep) . (generic_token) @import.source)
  (#eq? @_cmd "Import-Module")) @import

(command
  (command_name) @_using
  (command_elements
    (generic_token) @_scope
    (generic_token) @import.source)
  (#eq? @_using "using")
  (#match? @_scope "^(module|namespace)$")) @import

(command
  (command_invokation_operator) @_op
  (command_name_expr) @import.source
  (#eq? @_op ".")) @import
`;

export const BASH_QUERIES = `
(function_definition name: (word) @name) @definition.function

(command name: (command_name (word) @call.name)) @call

(command
  name: (command_name (word) @_src)
  argument: [(word) (string) (concatenation)] @import.source
  (#match? @_src "^(source|\\.)$")) @import
`;

// Lua — mirrors the grammar's bundled tags.scm, mapped to Ix capture names.
// Covers: function declarations (plain / local / table-dot / method-colon),
// function expressions assigned to a name or table field, calls (bare / dotted /
// method), and require("mod") / require "mod" imports.
export const LUA_QUERIES = `
(function_declaration
  name: [
    (identifier) @name
    (dot_index_expression field: (identifier) @name)
  ]) @definition.function

(function_declaration
  name: (method_index_expression method: (identifier) @name)) @definition.method

(assignment_statement
  (variable_list . name: [
    (identifier) @name
    (dot_index_expression field: (identifier) @name)
  ])
  (expression_list . value: (function_definition))) @definition.function

(table_constructor
  (field name: (identifier) @name value: (function_definition))) @definition.function

(function_call
  name: [
    (identifier) @call.name
    (dot_index_expression field: (identifier) @call.name)
    (method_index_expression method: (identifier) @call.name)
  ]) @call

(function_call
  name: (identifier) @_req
  arguments: (arguments (string) @import.source)
  (#eq? @_req "require")) @import
`;

export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
  [SupportedLanguages.TypeScript]: TYPESCRIPT_QUERIES,
  [SupportedLanguages.JavaScript]: JAVASCRIPT_QUERIES,
  [SupportedLanguages.Python]: PYTHON_QUERIES,
  [SupportedLanguages.Java]: JAVA_QUERIES,
  [SupportedLanguages.C]: C_QUERIES,
  [SupportedLanguages.Go]: GO_QUERIES,
  [SupportedLanguages.CPlusPlus]: CPP_QUERIES,
  [SupportedLanguages.CSharp]: CSHARP_QUERIES,
  [SupportedLanguages.Ruby]: RUBY_QUERIES,
  [SupportedLanguages.Rust]: RUST_QUERIES,
  [SupportedLanguages.PHP]: PHP_QUERIES,
  [SupportedLanguages.Kotlin]: KOTLIN_QUERIES,
  [SupportedLanguages.Swift]: SWIFT_QUERIES,
  [SupportedLanguages.Scala]: SCALA_QUERIES,
  [SupportedLanguages.YAML]: '',
  [SupportedLanguages.Dockerfile]: '',
  [SupportedLanguages.SQL]: '',
  [SupportedLanguages.JSON]: '',
  [SupportedLanguages.TOML]: '',
  [SupportedLanguages.Markdown]: '',
  [SupportedLanguages.R]: R_QUERIES,
  [SupportedLanguages.SAS]: SAS_QUERIES,
  [SupportedLanguages.Elixir]: ELIXIR_QUERIES,
  [SupportedLanguages.Makefile]: MAKEFILE_QUERIES,
  [SupportedLanguages.Lua]: LUA_QUERIES,
  [SupportedLanguages.Bash]: BASH_QUERIES,
  [SupportedLanguages.PowerShell]: POWERSHELL_QUERIES,
  [SupportedLanguages.Haskell]: HASKELL_QUERIES,
  [SupportedLanguages.Zig]: ZIG_QUERIES,
  [SupportedLanguages.HTML]: HTML_QUERIES,
  [SupportedLanguages.XML]: XML_QUERIES,
  [SupportedLanguages.HCL]: HCL_QUERIES,
  [SupportedLanguages.CSS]: CSS_QUERIES,
  // LaTeX/TeX has no tree-sitter grammar in this stack; it is parsed by the
  // hand-rolled scanner in parseLatexFile (index.ts), like Markdown/YAML/etc.
  [SupportedLanguages.LaTeX]: '',
  // Protocol Buffers likewise: no grammar in this stack, parsed by
  // parseProtoFile's scanner in index.ts.
  [SupportedLanguages.Proto]: '',
};