M0: compilable skeleton — Kigi 0.1.0 fork surgery
Hard fork of xai-org/grok-build (Apache-2.0) re-targeted as Kigi, an
unofficial Kimi Code CLI community build.
Rename & identity
- 72 xai-*/xai-grok-* crates -> kigi-* (explicit: xai-grok-pager-bin ->
kigi-bin [binary `kigi`], xai-grok-pager -> kigi-tui; rest mechanical);
ptyctl, ptyctl-cli, third_party/ unchanged; proto package
xai.grok.tools.v1 -> kigi.tools.v1
- Config home ~/.kigi (KIGI_SHARE_DIR override), env prefix GROK_* ->
KIGI_*, `kigi --version` carries the unofficial-community-build notice
- clap identity, help text, startup banner, prompt templates rebranded
(templates re-encrypted)
Deletions (PRD removal list #5/#6/#7/#9/#10)
- voice input (xai-grok-voice) and all TUI wiring
- telemetry: Mixpanel client, external OTel stream, Sentry, OTLP layers,
trace/GCS/S3 upload queues (kigi-file-utils halved), workspace upload
module & dc_log, heap-profile uploader, auth-diagnostics uploader,
session-analytics halves of feedback; local zero-egress observability
preserved in new kigi-log crate (unified log, --debug firehose,
subsystem file logs, opt-in instrumentation)
- announcements (crate, remote-settings fields, TUI surfaces)
- plugin marketplace (crate, sources/browse/CTA/extensions-modal tab);
direct plugin install/uninstall/update via kigi-agent git_install kept
- relay/gateway/assets endpoints and features (agent relay, headless
relay transport, gateway bridge, LeaderEnvUrls); leader IPC socket now
~/.kigi/leader.sock + KIGI_LEADER_SOCKET, no ws-url derivation
- functional types rehomed instead of deleted: PermissionMode ->
kigi-config-types, McpInitStrategy -> kigi-mcp, PrCreationSource ->
session signals, TerminalDiagnostics -> kigi-pager-render, agent_id ->
shell util
Endpoints
- kigi-env rewritten: single production KigiEndpoints {coding_api_base_url
https://api.kimi.com/coding/v1 (KIGI_CODE_BASE_URL), oauth_host
https://auth.kimi.com (KIGI_OAUTH_HOST), update_base_url (GitHub
Releases API), upgrade_page_url}; GrokBuildEnvironment enum deleted
Toolchain & workspace hygiene
- Rust 1.97.0 pinned; edition 2024; full cargo update; git2 hoisted to
workspace at 0.21 (Option->Result API migration), quick-xml 0.41
- Root Cargo.toml hand-maintained (PRD §8.1): version 0.1.0 inherited by
all members, members sorted, unused deps pruned
- cargo-deny advisories gate (deny.toml with documented transitive
exceptions); CI workflow (check/clippy/fmt/deny/test, macOS+Linux)
- cross-crate test seams re-gated behind `test-support` cargo feature;
insta snapshot baselines renamed to the kigi_tui prefix
- clippy --workspace --all-targets: zero warnings; fmt clean
Fixes surfaced by the port
- updater probe/installer divergence (bin/kigi vs bin/grok symlink set)
- idle model-metadata refresh dead under KIGI_CODE_BASE_URL override
(new is_effective_coding_endpoint_url, loopback+override aware)
- macOS symlinked-TMPDIR fixture canonicalization (foreign_sessions,
fast-worktree); RSS measurement tests serialized via serial_test
Docs & legal (Apache §4)
- NOTICE added (upstream attribution + change statement); THIRD-PARTY
notices sustained; kigi-tools ported-code notices extended; README,
CONTRIBUTING, SECURITY, AGENTS.md rewritten
Out of scope for M0 (tracked): Kimi auth/inference (M1), search/fetch,
command parity, config import (M2), Computer Hub excision & final
brand-token sweep (M2), distribution & self-update rewrite (M3).
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
use crate::languages::types::TSLanguageConfig;
|
||||
|
||||
pub fn golang() -> TSLanguageConfig {
|
||||
TSLanguageConfig::new(
|
||||
vec!["Go".to_owned(), "go".to_owned()],
|
||||
vec!["go".to_owned()],
|
||||
vec![vec![
|
||||
"function".to_owned(),
|
||||
"type".to_owned(),
|
||||
"struct".to_owned(),
|
||||
"interface".to_owned(),
|
||||
"const".to_owned(),
|
||||
"var".to_owned(),
|
||||
"package".to_owned(),
|
||||
]],
|
||||
r#"
|
||||
; Function definitions
|
||||
(function_declaration
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
; Method definitions
|
||||
(method_declaration
|
||||
name: (field_identifier) @name.definition.method) @definition.method
|
||||
|
||||
; Type definitions (struct, interface, etc.)
|
||||
(type_declaration
|
||||
(type_spec
|
||||
name: (type_identifier) @name.definition.type)) @definition.type
|
||||
|
||||
; Const declarations
|
||||
(const_declaration
|
||||
(const_spec
|
||||
name: (identifier) @name.definition.const)) @definition.const
|
||||
|
||||
; Var declarations
|
||||
(var_declaration
|
||||
(var_spec
|
||||
name: (identifier) @name.definition.var)) @definition.var
|
||||
|
||||
; ============ REFERENCES ============
|
||||
|
||||
; Function calls
|
||||
(call_expression
|
||||
function: (identifier) @name.reference.call) @reference.call
|
||||
|
||||
; Method calls
|
||||
(call_expression
|
||||
function: (selector_expression
|
||||
field: (field_identifier) @name.reference.call)) @reference.call
|
||||
|
||||
; Type references
|
||||
(type_identifier) @name.reference.type
|
||||
|
||||
; Package references in qualified names
|
||||
(qualified_type
|
||||
package: (package_identifier) @name.reference.package
|
||||
name: (type_identifier) @name.reference.type)
|
||||
|
||||
; ============ IMPORTS ============
|
||||
|
||||
; import "package"
|
||||
(import_spec
|
||||
path: (interpreted_string_literal) @name.reference.import)
|
||||
|
||||
; import alias "package"
|
||||
(import_spec
|
||||
name: (package_identifier) @alias.name
|
||||
path: (interpreted_string_literal) @alias.original)
|
||||
"#
|
||||
.to_owned(),
|
||||
|| tree_sitter_go::LANGUAGE.into(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
//! JavaScript/JSX language configuration.
|
||||
|
||||
use crate::languages::types::TSLanguageConfig;
|
||||
|
||||
pub fn js_lang() -> TSLanguageConfig {
|
||||
TSLanguageConfig::new(
|
||||
vec![
|
||||
"JavaScript".to_owned(),
|
||||
"javascript".to_owned(),
|
||||
"js".to_owned(),
|
||||
"jsx".to_owned(),
|
||||
],
|
||||
vec!["js".to_owned(), "jsx".to_owned()],
|
||||
vec![vec![
|
||||
"function".to_owned(),
|
||||
"class".to_owned(),
|
||||
"variable".to_owned(),
|
||||
"const".to_owned(),
|
||||
"let".to_owned(),
|
||||
]],
|
||||
r#"
|
||||
; Class definitions
|
||||
(class_declaration
|
||||
name: (identifier) @name.definition.class) @definition.class
|
||||
|
||||
; Function definitions
|
||||
(function_declaration
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
; Arrow function with variable
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.function
|
||||
value: (arrow_function))) @definition.function
|
||||
|
||||
; Method definitions
|
||||
(method_definition
|
||||
name: (property_identifier) @name.definition.method) @definition.method
|
||||
|
||||
; Variable declarations
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.variable)) @definition.variable
|
||||
|
||||
; Var declarations
|
||||
(variable_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.variable)) @definition.variable
|
||||
|
||||
; ============ REFERENCES ============
|
||||
|
||||
; Function calls
|
||||
(call_expression
|
||||
function: (identifier) @name.reference.call) @reference.call
|
||||
|
||||
; Method calls
|
||||
(call_expression
|
||||
function: (member_expression
|
||||
property: (property_identifier) @name.reference.call)) @reference.call
|
||||
|
||||
; JSX element names
|
||||
(jsx_opening_element
|
||||
name: (identifier) @name.reference.jsx)
|
||||
|
||||
(jsx_self_closing_element
|
||||
name: (identifier) @name.reference.jsx)
|
||||
|
||||
; ============ IMPORTS ============
|
||||
|
||||
; Named imports: import { Foo } from 'bar'
|
||||
(import_specifier
|
||||
name: (identifier) @name.reference.import)
|
||||
|
||||
; Default import: import Foo from 'bar'
|
||||
(import_clause
|
||||
(identifier) @name.reference.import)
|
||||
|
||||
; Import alias: import { Foo as Bar } from 'bar'
|
||||
(import_specifier
|
||||
name: (identifier) @alias.original
|
||||
alias: (identifier) @alias.name)
|
||||
|
||||
; Named exports: export { Foo }
|
||||
(export_specifier
|
||||
name: (identifier) @name.reference.export)
|
||||
|
||||
; Array element identifiers: [foo, bar] (e.g., React useCallback/useEffect dependency arrays)
|
||||
(array
|
||||
(identifier) @name.reference.variable)
|
||||
"#
|
||||
.to_owned(),
|
||||
|| tree_sitter_javascript::LANGUAGE.into(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
mod golang;
|
||||
mod javascript;
|
||||
mod python;
|
||||
mod rust;
|
||||
mod ts;
|
||||
pub mod types;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use golang::golang;
|
||||
pub use javascript::js_lang;
|
||||
pub use python::python_lang;
|
||||
pub use rust::rust_lang;
|
||||
pub use ts::ts_lang;
|
||||
pub use types::TSLanguageConfig;
|
||||
|
||||
/// Registry of all supported languages.
|
||||
///
|
||||
/// Provides lookup by extension and language ID, and can check if two
|
||||
/// extensions belong to the same language family.
|
||||
pub struct LanguageRegistry {
|
||||
/// All registered language configs.
|
||||
configs: Vec<Arc<TSLanguageConfig>>,
|
||||
/// Mapping from file extension to language config.
|
||||
by_extension: HashMap<String, Arc<TSLanguageConfig>>,
|
||||
/// Mapping from language ID to language config.
|
||||
by_id: HashMap<String, Arc<TSLanguageConfig>>,
|
||||
}
|
||||
|
||||
impl LanguageRegistry {
|
||||
/// Create a new language registry with all supported languages.
|
||||
pub fn new() -> Self {
|
||||
let configs: Vec<Arc<TSLanguageConfig>> = vec![
|
||||
Arc::new(rust_lang()),
|
||||
Arc::new(ts_lang()),
|
||||
Arc::new(js_lang()),
|
||||
Arc::new(golang()),
|
||||
Arc::new(python_lang()),
|
||||
];
|
||||
|
||||
let mut by_extension = HashMap::new();
|
||||
let mut by_id = HashMap::new();
|
||||
|
||||
for config in &configs {
|
||||
for ext in config.file_extensions() {
|
||||
by_extension.insert(ext.clone(), Arc::clone(config));
|
||||
}
|
||||
for id in config.language_ids() {
|
||||
by_id.insert(id.clone(), Arc::clone(config));
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
configs,
|
||||
by_extension,
|
||||
by_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a language config by file extension.
|
||||
pub fn for_extension(&self, ext: &str) -> Option<Arc<TSLanguageConfig>> {
|
||||
self.by_extension.get(ext).cloned()
|
||||
}
|
||||
|
||||
/// Get a language config by language ID.
|
||||
pub fn for_id(&self, id: &str) -> Option<Arc<TSLanguageConfig>> {
|
||||
self.by_id.get(id).cloned()
|
||||
}
|
||||
|
||||
/// Get a language config for a file path.
|
||||
///
|
||||
/// Extracts the extension from the path and looks up the config.
|
||||
pub fn for_file_path(&self, path: impl AsRef<Path>) -> Option<Arc<TSLanguageConfig>> {
|
||||
let path = path.as_ref();
|
||||
let ext = path.extension()?.to_str()?;
|
||||
self.for_extension(ext)
|
||||
}
|
||||
|
||||
/// Check if a file path is supported (has a supported extension).
|
||||
pub fn is_supported(&self, path: impl AsRef<Path>) -> bool {
|
||||
self.for_file_path(path).is_some()
|
||||
}
|
||||
|
||||
/// Get all supported file extensions.
|
||||
pub fn supported_extensions(&self) -> Vec<&str> {
|
||||
self.by_extension.keys().map(|s| s.as_str()).collect()
|
||||
}
|
||||
|
||||
/// Get all language configs.
|
||||
pub fn all_configs(&self) -> &[Arc<TSLanguageConfig>] {
|
||||
&self.configs
|
||||
}
|
||||
|
||||
/// Check if two file extensions belong to the same language.
|
||||
///
|
||||
/// Returns true if both extensions are registered under the same language config.
|
||||
pub fn extensions_same_language(&self, ext1: &str, ext2: &str) -> bool {
|
||||
if ext1 == ext2 {
|
||||
return true;
|
||||
}
|
||||
|
||||
match (self.by_extension.get(ext1), self.by_extension.get(ext2)) {
|
||||
(Some(config1), Some(config2)) => {
|
||||
// Compare by primary language ID (they point to the same config)
|
||||
config1.primary_language_id() == config2.primary_language_id()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute a hash of all tree-sitter queries across all languages.
|
||||
///
|
||||
/// This is used to detect when queries change, which should trigger
|
||||
/// a rebuild of the index even if file contents haven't changed.
|
||||
///
|
||||
/// The hash is computed by:
|
||||
/// 1. Sorting languages by their primary ID for deterministic ordering
|
||||
/// 2. Hashing each language's query string in order
|
||||
/// 3. Combining into a single u64 hash
|
||||
pub fn compute_query_hash(&self) -> u64 {
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
|
||||
// Sort configs by primary language ID for deterministic ordering
|
||||
let mut sorted_configs: Vec<_> = self.configs.iter().collect();
|
||||
sorted_configs.sort_by_key(|c| c.primary_language_id());
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
|
||||
for config in sorted_configs {
|
||||
// Hash the language ID and query together
|
||||
config.primary_language_id().hash(&mut hasher);
|
||||
config.file_definition_queries().hash(&mut hasher);
|
||||
}
|
||||
|
||||
hasher.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LanguageRegistry {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//! Python language configuration.
|
||||
|
||||
use crate::languages::types::TSLanguageConfig;
|
||||
|
||||
pub fn python_lang() -> TSLanguageConfig {
|
||||
TSLanguageConfig::new(
|
||||
vec!["Python".to_owned(), "python".to_owned(), "py".to_owned()],
|
||||
vec!["py".to_owned()],
|
||||
vec![vec![
|
||||
"function".to_owned(),
|
||||
"class".to_owned(),
|
||||
"variable".to_owned(),
|
||||
"module".to_owned(),
|
||||
]],
|
||||
// Python definitions query
|
||||
r#"
|
||||
; Class definitions
|
||||
(class_definition
|
||||
name: (identifier) @name.definition.class) @definition.class
|
||||
|
||||
; Function definitions
|
||||
(function_definition
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
; ============ REFERENCES ============
|
||||
|
||||
; Function calls (direct and method calls)
|
||||
(call
|
||||
function: [
|
||||
(identifier) @name.reference.call
|
||||
(attribute
|
||||
attribute: (identifier) @name.reference.call)
|
||||
]) @reference.call
|
||||
"#
|
||||
.to_owned(),
|
||||
|| tree_sitter_python::LANGUAGE.into(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
use crate::languages::types::TSLanguageConfig;
|
||||
|
||||
pub fn rust_lang() -> TSLanguageConfig {
|
||||
TSLanguageConfig::new(
|
||||
vec!["Rust".to_owned(), "rust".to_owned(), "rs".to_owned()],
|
||||
vec!["rs".to_owned()],
|
||||
vec![vec![
|
||||
"const".to_owned(),
|
||||
"function".to_owned(),
|
||||
"variable".to_owned(),
|
||||
"struct".to_owned(),
|
||||
"enum".to_owned(),
|
||||
"union".to_owned(),
|
||||
"typedef".to_owned(),
|
||||
"interface".to_owned(),
|
||||
"field".to_owned(),
|
||||
"enumerator".to_owned(),
|
||||
"module".to_owned(),
|
||||
"label".to_owned(),
|
||||
"lifetime".to_owned(),
|
||||
]],
|
||||
r#"; ADT definitions
|
||||
|
||||
(struct_item
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
(enum_item
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
(union_item
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
; type aliases
|
||||
|
||||
(type_item
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
; method definitions
|
||||
|
||||
(declaration_list
|
||||
(function_item
|
||||
name: (identifier) @name.definition.method)) @definition.method
|
||||
|
||||
; function definitions
|
||||
|
||||
(function_item
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
; trait definitions
|
||||
(trait_item
|
||||
name: (type_identifier) @name.definition.interface) @definition.interface
|
||||
|
||||
; module definitions
|
||||
(mod_item
|
||||
name: (identifier) @name.definition.module) @definition.module
|
||||
|
||||
; macro definitions
|
||||
|
||||
(macro_definition
|
||||
name: (identifier) @name.definition.macro) @definition.macro
|
||||
|
||||
; const and static definitions
|
||||
(const_item
|
||||
name: (identifier) @name.definition.variable) @definition.variable
|
||||
|
||||
(static_item
|
||||
name: (identifier) @name.definition.variable) @definition.variable
|
||||
|
||||
; ============ REFERENCES ============
|
||||
|
||||
; Function and method calls
|
||||
(call_expression
|
||||
function: (identifier) @name.reference.call) @reference.call
|
||||
|
||||
(call_expression
|
||||
function: (field_expression
|
||||
field: (field_identifier) @name.reference.call)) @reference.call
|
||||
|
||||
(macro_invocation
|
||||
macro: (identifier) @name.reference.call) @reference.call
|
||||
|
||||
; implementations
|
||||
|
||||
(impl_item
|
||||
trait: (type_identifier) @name.reference.implementation) @reference.implementation
|
||||
|
||||
(impl_item
|
||||
type: (type_identifier) @name.reference.implementation
|
||||
!trait) @reference.implementation
|
||||
|
||||
; ============ USE/IMPORT REFERENCES ============
|
||||
|
||||
; Simple use: use Foo;
|
||||
(use_declaration
|
||||
argument: (identifier) @name.reference.import) @reference.import
|
||||
|
||||
; Scoped use: use foo::Bar;
|
||||
(use_declaration
|
||||
argument: (scoped_identifier
|
||||
name: (identifier) @name.reference.import)) @reference.import
|
||||
|
||||
; Use list: use foo::{Bar, Baz};
|
||||
(use_declaration
|
||||
argument: (scoped_use_list
|
||||
list: (use_list
|
||||
(identifier) @name.reference.import)))
|
||||
|
||||
; Nested scoped use: use foo::bar::{Baz, Qux};
|
||||
(use_declaration
|
||||
argument: (scoped_use_list
|
||||
list: (use_list
|
||||
(scoped_identifier
|
||||
name: (identifier) @name.reference.import))))
|
||||
|
||||
; Use with alias: use Foo as Bar;
|
||||
(use_declaration
|
||||
argument: (use_as_clause
|
||||
path: (identifier) @name.reference.import))
|
||||
|
||||
(use_declaration
|
||||
argument: (use_as_clause
|
||||
path: (scoped_identifier
|
||||
name: (identifier) @name.reference.import)))
|
||||
|
||||
; ============ ALIAS TRACKING ============
|
||||
; These patterns capture alias relationships for unified lookups
|
||||
|
||||
; use Foo as Bar - captures original and alias
|
||||
(use_declaration
|
||||
argument: (use_as_clause
|
||||
path: (identifier) @alias.original
|
||||
alias: (identifier) @alias.name))
|
||||
|
||||
; use foo::Bar as Baz - scoped version
|
||||
(use_declaration
|
||||
argument: (use_as_clause
|
||||
path: (scoped_identifier
|
||||
name: (identifier) @alias.original)
|
||||
alias: (identifier) @alias.name))
|
||||
|
||||
; ============ TYPE REFERENCES ============
|
||||
|
||||
; Type identifiers in function parameters
|
||||
(parameter
|
||||
type: (type_identifier) @name.reference.type)
|
||||
|
||||
; Return types
|
||||
(function_item
|
||||
return_type: (type_identifier) @name.reference.type)
|
||||
|
||||
; Struct fields
|
||||
(field_declaration
|
||||
type: (type_identifier) @name.reference.type)
|
||||
|
||||
; Let bindings with type annotation
|
||||
(let_declaration
|
||||
type: (type_identifier) @name.reference.type)
|
||||
|
||||
; Generic type arguments: Vec<Foo>
|
||||
(type_arguments
|
||||
(type_identifier) @name.reference.type)
|
||||
|
||||
; Scoped type identifier: foo::Bar
|
||||
(scoped_type_identifier
|
||||
name: (type_identifier) @name.reference.type)
|
||||
|
||||
; Reference types: &Foo
|
||||
(reference_type
|
||||
type: (type_identifier) @name.reference.type)
|
||||
|
||||
; Tuple struct patterns
|
||||
(tuple_struct_pattern
|
||||
type: (identifier) @name.reference.type)
|
||||
|
||||
; Struct expressions: Foo { ... }
|
||||
(struct_expression
|
||||
name: (type_identifier) @name.reference.type)
|
||||
|
||||
; Tuple struct expressions: Foo(...)
|
||||
(call_expression
|
||||
function: (scoped_identifier
|
||||
name: (identifier) @name.reference.call))
|
||||
|
||||
; Path segments in scoped identifiers: foo::Bar::baz()
|
||||
; This captures types used in paths like SomeType::method()
|
||||
(scoped_identifier
|
||||
path: (scoped_identifier
|
||||
name: (identifier) @name.reference.type))
|
||||
|
||||
; Direct scoped calls with type in path: Foo::bar()
|
||||
(scoped_identifier
|
||||
path: (identifier) @name.reference.type
|
||||
name: (identifier))
|
||||
"#
|
||||
.to_owned(),
|
||||
|| tree_sitter_rust::LANGUAGE.into(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
use crate::languages::types::TSLanguageConfig;
|
||||
|
||||
pub fn ts_lang() -> TSLanguageConfig {
|
||||
TSLanguageConfig::new(
|
||||
vec![
|
||||
"Typescript".to_owned(),
|
||||
"TSX".to_owned(),
|
||||
"typescript".to_owned(),
|
||||
"tsx".to_owned(),
|
||||
],
|
||||
vec!["ts".to_owned(), "tsx".to_owned()],
|
||||
vec![vec![
|
||||
"function".to_owned(),
|
||||
"class".to_owned(),
|
||||
"interface".to_owned(),
|
||||
"type".to_owned(),
|
||||
"enum".to_owned(),
|
||||
"variable".to_owned(),
|
||||
"const".to_owned(),
|
||||
"let".to_owned(),
|
||||
]],
|
||||
// Comprehensive TypeScript query with full type coverage
|
||||
r#"
|
||||
;; === DEFINITIONS ===
|
||||
|
||||
(function_signature
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
(method_signature
|
||||
name: (property_identifier) @name.definition.method) @definition.method
|
||||
|
||||
(abstract_method_signature
|
||||
name: (property_identifier) @name.definition.method) @definition.method
|
||||
|
||||
(abstract_class_declaration
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
(module
|
||||
name: (identifier) @name.definition.module) @definition.module
|
||||
|
||||
(interface_declaration
|
||||
name: (type_identifier) @name.definition.interface) @definition.interface
|
||||
|
||||
(function_declaration
|
||||
name: (identifier) @name.definition.function) @definition.function
|
||||
|
||||
(method_definition
|
||||
name: (property_identifier) @name.definition.method) @definition.method
|
||||
|
||||
(class_declaration
|
||||
name: (type_identifier) @name.definition.class) @definition.class
|
||||
|
||||
(type_alias_declaration
|
||||
name: (type_identifier) @name.definition.type) @definition.type
|
||||
|
||||
(enum_declaration
|
||||
name: (identifier) @name.definition.enum) @definition.enum
|
||||
|
||||
;; Arrow function assigned to variable: const foo = () => {}
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.function
|
||||
value: (arrow_function))) @definition.function
|
||||
|
||||
;; React component patterns: const Foo = React.forwardRef(...), React.memo(...)
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.function
|
||||
value: (call_expression))) @definition.function
|
||||
|
||||
;; Variable declarations (const/let)
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.variable)) @definition.variable
|
||||
|
||||
;; Var declarations
|
||||
(variable_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.variable)) @definition.variable
|
||||
|
||||
;; Exported variable declarations: export const foo = ...
|
||||
(export_statement
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (identifier) @name.definition.variable))) @definition.variable
|
||||
|
||||
;; === DESTRUCTURING DEFINITIONS ===
|
||||
|
||||
;; For-of/for-in loop with array destructuring: for (const [a, b] of items)
|
||||
(for_in_statement
|
||||
left: (array_pattern
|
||||
(identifier) @name.definition.variable))
|
||||
|
||||
;; For-of/for-in loop with object destructuring: for (const { a, b } of items)
|
||||
(for_in_statement
|
||||
left: (object_pattern
|
||||
(shorthand_property_identifier_pattern) @name.definition.variable))
|
||||
|
||||
;; For-of/for-in loop with object destructuring (aliased): for (const { a: b } of items)
|
||||
(for_in_statement
|
||||
left: (object_pattern
|
||||
(pair_pattern
|
||||
value: (identifier) @name.definition.variable)))
|
||||
|
||||
;; Regular array destructuring: const [a, b] = someArray
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (array_pattern
|
||||
(identifier) @name.definition.variable)))
|
||||
|
||||
;; Regular object destructuring (shorthand): const { a, b } = someObject
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (object_pattern
|
||||
(shorthand_property_identifier_pattern) @name.definition.variable)))
|
||||
|
||||
;; Regular object destructuring (aliased): const { a: b } = someObject
|
||||
(lexical_declaration
|
||||
(variable_declarator
|
||||
name: (object_pattern
|
||||
(pair_pattern
|
||||
value: (identifier) @name.definition.variable))))
|
||||
|
||||
;; Var array destructuring: var [a, b] = someArray
|
||||
(variable_declaration
|
||||
(variable_declarator
|
||||
name: (array_pattern
|
||||
(identifier) @name.definition.variable)))
|
||||
|
||||
;; Var object destructuring (shorthand): var { a, b } = someObject
|
||||
(variable_declaration
|
||||
(variable_declarator
|
||||
name: (object_pattern
|
||||
(shorthand_property_identifier_pattern) @name.definition.variable)))
|
||||
|
||||
;; Var object destructuring (aliased): var { a: b } = someObject
|
||||
(variable_declaration
|
||||
(variable_declarator
|
||||
name: (object_pattern
|
||||
(pair_pattern
|
||||
value: (identifier) @name.definition.variable))))
|
||||
|
||||
;; Function parameters with array destructuring: function foo([a, b]) {}
|
||||
(formal_parameters
|
||||
(required_parameter
|
||||
pattern: (array_pattern
|
||||
(identifier) @name.definition.variable)))
|
||||
|
||||
;; Function parameters with object destructuring (shorthand): function foo({ a, b }) {}
|
||||
(formal_parameters
|
||||
(required_parameter
|
||||
pattern: (object_pattern
|
||||
(shorthand_property_identifier_pattern) @name.definition.variable)))
|
||||
|
||||
;; Function parameters with object destructuring (aliased): function foo({ a: b }) {}
|
||||
(formal_parameters
|
||||
(required_parameter
|
||||
pattern: (object_pattern
|
||||
(pair_pattern
|
||||
value: (identifier) @name.definition.variable))))
|
||||
|
||||
;; Function parameters (simple): function foo(a, b) {}
|
||||
(formal_parameters
|
||||
(required_parameter
|
||||
pattern: (identifier) @name.definition.variable))
|
||||
|
||||
;; === REFERENCES ===
|
||||
|
||||
;; Member expression object: foo.bar (capture foo as reference)
|
||||
(member_expression
|
||||
object: (identifier) @name.reference.variable)
|
||||
|
||||
;; Capture ALL type identifiers as references (comprehensive)
|
||||
(type_identifier) @name.reference.type
|
||||
|
||||
;; new expressions: new SomeClass()
|
||||
(new_expression
|
||||
constructor: (identifier) @name.reference.class) @reference.class
|
||||
|
||||
;; Named imports (simple): import { DiffViewer } from './code-viewer'
|
||||
(import_specifier
|
||||
name: (identifier) @name.reference.variable
|
||||
!alias) @reference.import
|
||||
|
||||
;; Named imports with alias: import { Foo as Bar } from './module'
|
||||
(import_specifier
|
||||
name: (identifier) @alias.original
|
||||
alias: (identifier) @alias.name) @reference.import.alias
|
||||
|
||||
;; Default imports: import Foo from 'bar'
|
||||
(import_clause
|
||||
(identifier) @name.reference.import)
|
||||
|
||||
;; JSX opening element: <DiffViewer ...>
|
||||
(jsx_opening_element
|
||||
name: (identifier) @name.reference.class)
|
||||
|
||||
;; JSX self-closing element: <DiffViewer ... />
|
||||
(jsx_self_closing_element
|
||||
name: (identifier) @name.reference.class)
|
||||
|
||||
;; JSX member expression element: <Foo.Bar />
|
||||
(jsx_opening_element
|
||||
name: (member_expression
|
||||
object: (identifier) @name.reference.variable))
|
||||
|
||||
(jsx_self_closing_element
|
||||
name: (member_expression
|
||||
object: (identifier) @name.reference.variable))
|
||||
|
||||
;; Function calls: someFunction()
|
||||
(call_expression
|
||||
function: (identifier) @name.reference.call)
|
||||
|
||||
;; Method calls on objects: object.method()
|
||||
(call_expression
|
||||
function: (member_expression
|
||||
object: (identifier) @name.reference.variable))
|
||||
|
||||
;; Extends clause in class: class Foo extends Bar
|
||||
(class_heritage
|
||||
(extends_clause
|
||||
value: (identifier) @name.reference.class))
|
||||
|
||||
;; Implements clause: class Foo implements Bar
|
||||
(class_heritage
|
||||
(implements_clause
|
||||
(type_identifier) @name.reference.interface))
|
||||
|
||||
;; Named exports: export { Foo }
|
||||
(export_specifier
|
||||
name: (identifier) @name.reference.export)
|
||||
|
||||
;; Array element identifiers: [foo, bar] (e.g., React useCallback/useEffect dependency arrays)
|
||||
(array
|
||||
(identifier) @name.reference.variable)
|
||||
"#
|
||||
.to_owned(),
|
||||
|| tree_sitter_typescript::LANGUAGE_TSX.into(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
use crate::scope_graph::nodes::SymbolId;
|
||||
|
||||
/// Function type for getting a tree-sitter language grammar.
|
||||
pub type GrammarFn = fn() -> tree_sitter::Language;
|
||||
|
||||
/// Contains information about the language and extra information which we would need
|
||||
/// per language
|
||||
pub struct TSLanguageConfig {
|
||||
language_ids: Vec<String>,
|
||||
file_extensions: Vec<String>,
|
||||
namespaces: Vec<Vec<String>>,
|
||||
file_definition_queries: String,
|
||||
grammar: GrammarFn,
|
||||
}
|
||||
|
||||
impl TSLanguageConfig {
|
||||
pub fn new(
|
||||
language_ids: Vec<String>,
|
||||
file_extensions: Vec<String>,
|
||||
namespaces: Vec<Vec<String>>,
|
||||
file_definition_queries: String,
|
||||
grammar: GrammarFn,
|
||||
) -> Self {
|
||||
Self {
|
||||
language_ids,
|
||||
file_extensions,
|
||||
namespaces,
|
||||
file_definition_queries,
|
||||
grammar,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the language IDs.
|
||||
pub fn language_ids(&self) -> &[String] {
|
||||
&self.language_ids
|
||||
}
|
||||
|
||||
/// Get the first language ID, or "unknown" if none.
|
||||
pub fn primary_language_id(&self) -> &str {
|
||||
self.language_ids
|
||||
.first()
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("unknown")
|
||||
}
|
||||
|
||||
/// Get the file extensions.
|
||||
pub fn file_extensions(&self) -> &[String] {
|
||||
&self.file_extensions
|
||||
}
|
||||
|
||||
/// Get the namespaces.
|
||||
pub fn namespaces(&self) -> &[Vec<String>] {
|
||||
&self.namespaces
|
||||
}
|
||||
|
||||
/// Get the file definition queries.
|
||||
pub fn file_definition_queries(&self) -> &str {
|
||||
&self.file_definition_queries
|
||||
}
|
||||
|
||||
/// Get the tree-sitter language.
|
||||
pub fn language(&self) -> tree_sitter::Language {
|
||||
(self.grammar)()
|
||||
}
|
||||
|
||||
/// Compile the definitions query for this language.
|
||||
pub fn compile_query(&self) -> Result<tree_sitter::Query, tree_sitter::QueryError> {
|
||||
tree_sitter::Query::new(&self.language(), &self.file_definition_queries)
|
||||
}
|
||||
|
||||
/// Find a SymbolId for a given symbol type name.
|
||||
pub fn symbol_id_of(&self, symbol_type: &str) -> Option<SymbolId> {
|
||||
for (ns_idx, namespace) in self.namespaces.iter().enumerate() {
|
||||
for (sym_idx, sym) in namespace.iter().enumerate() {
|
||||
if sym == symbol_type {
|
||||
return Some(SymbolId::new(ns_idx, sym_idx));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user