symforge 11.20.5

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Adapter between SymForge's `LanguageId` and `ast_grep_core` types.
//!
//! Implements `ast_grep_core::Language` and `LanguageExt` so we can use
//! ast-grep's structural pattern matching on indexed source files.
//!
//! Key detail: many languages don't accept `$` as an identifier character.
//! We must replace `$` with a Unicode expando char before parsing so
//! tree-sitter treats metavariables as valid identifiers. The expando chars
//! match the official `ast-grep-language` crate exactly.

use crate::domain::index::LanguageId;
use ast_grep_core::language::Language;
use ast_grep_core::matcher::PatternBuilder;
use ast_grep_core::meta_var::MetaVariable;
use ast_grep_core::tree_sitter::{LanguageExt, StrDoc, TSLanguage};
use ast_grep_core::{AstGrep, Pattern, PatternError};
use std::borrow::Cow;
use std::time::Duration;
use tree_sitter::Parser;

/// Wrapper that implements ast-grep's `Language` trait for a SymForge `LanguageId`.
#[derive(Clone)]
pub struct SgLang {
    ts_lang: TSLanguage,
    /// The character used to replace `$` before parsing. Languages that accept
    /// `$` in identifiers (JavaScript, TypeScript, Java, Dart) use `$` directly.
    /// Others use a Unicode letter that the grammar accepts as an identifier.
    expando: char,
}

/// Pre-process a pattern string by replacing `$` metavariable sigils with the
/// language-specific expando character. This is the same algorithm used by the
/// official `ast-grep-language` crate.
fn pre_process_pattern(expando: char, query: &str) -> Cow<'_, str> {
    if expando == '$' {
        return Cow::Borrowed(query);
    }
    let mut ret = Vec::with_capacity(query.len());
    let mut dollar_count = 0usize;
    for c in query.chars() {
        if c == '$' {
            dollar_count += 1;
            continue;
        }
        let need_replace = matches!(c, 'A'..='Z' | '_') || dollar_count == 3;
        let sigil = if need_replace { expando } else { '$' };
        ret.extend(std::iter::repeat_n(sigil, dollar_count));
        dollar_count = 0;
        ret.push(c);
    }
    // Trailing anonymous multiple ($$$)
    let sigil = if dollar_count == 3 { expando } else { '$' };
    ret.extend(std::iter::repeat_n(sigil, dollar_count));
    Cow::Owned(ret.into_iter().collect())
}

impl SgLang {
    /// Returns `None` for config-only languages (JSON, TOML, YAML, Markdown, Env)
    /// that have no meaningful AST patterns.
    pub fn from_language_id(lang: &LanguageId, is_tsx: bool) -> Option<Self> {
        // Expando chars sourced from the official ast-grep-language crate.
        // Languages that accept `$` as an identifier char use '$' (no replacement).
        // Languages that don't accept `$` use a Unicode letter the grammar allows.
        let (ts_lang, expando): (TSLanguage, char) = match lang {
            // Expando languages (don't accept $ in identifiers)
            LanguageId::Rust => (tree_sitter_rust::LANGUAGE.into(), 'µ'),
            LanguageId::Python => (tree_sitter_python::LANGUAGE.into(), 'µ'),
            LanguageId::Go => (tree_sitter_go::LANGUAGE.into(), 'µ'),
            LanguageId::C => (tree_sitter_c::LANGUAGE.into(), '𐀀'),
            LanguageId::Cpp => (tree_sitter_cpp::LANGUAGE.into(), '𐀀'),
            LanguageId::CSharp => (tree_sitter_c_sharp::LANGUAGE.into(), 'µ'),
            LanguageId::Ruby => (tree_sitter_ruby::LANGUAGE.into(), 'µ'),
            LanguageId::Php => (tree_sitter_php::LANGUAGE_PHP.into(), 'µ'),
            LanguageId::Swift => (tree_sitter_swift::LANGUAGE.into(), 'µ'),
            LanguageId::Kotlin => (tree_sitter_kotlin_sg::LANGUAGE.into(), 'µ'),
            LanguageId::Elixir => (tree_sitter_elixir::LANGUAGE.into(), 'µ'),
            LanguageId::Css => (tree_sitter_css::LANGUAGE.into(), '_'),
            LanguageId::Scss => (tree_sitter_scss::language(), '_'),
            // Stub languages (accept $ in identifiers — no expando needed)
            LanguageId::JavaScript => (tree_sitter_javascript::LANGUAGE.into(), '$'),
            // `.tsx` uses the JSX-aware TSX grammar so structural patterns can
            // match inside JSX; `.ts` stays on the plain TypeScript grammar.
            LanguageId::TypeScript if is_tsx => (tree_sitter_typescript::LANGUAGE_TSX.into(), '$'),
            LanguageId::TypeScript => (tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(), '$'),
            LanguageId::Java => (tree_sitter_java::LANGUAGE.into(), '$'),
            LanguageId::Dart => (tree_sitter_dart::LANGUAGE.into(), '$'),
            LanguageId::Perl => (tree_sitter_perl::LANGUAGE.into(), '$'),
            LanguageId::Html => (tree_sitter_html::LANGUAGE.into(), '$'),
            // Config-only languages — no structural patterns
            LanguageId::Json
            | LanguageId::Toml
            | LanguageId::Yaml
            | LanguageId::Markdown
            | LanguageId::Text
            | LanguageId::Env => return None,
        };
        Some(Self { ts_lang, expando })
    }
}

impl Language for SgLang {
    fn kind_to_id(&self, kind: &str) -> u16 {
        self.ts_lang.id_for_node_kind(kind, true)
    }

    fn field_to_id(&self, field: &str) -> Option<u16> {
        self.ts_lang.field_id_for_name(field).map(|f| f.get())
    }

    fn expando_char(&self) -> char {
        self.expando
    }

    fn pre_process_pattern<'q>(&self, query: &'q str) -> Cow<'q, str> {
        pre_process_pattern(self.expando, query)
    }

    fn build_pattern(&self, builder: &PatternBuilder) -> Result<Pattern, PatternError> {
        builder.build(|src| StrDoc::try_new(src, self.clone()))
    }
}

impl LanguageExt for SgLang {
    fn get_ts_language(&self) -> TSLanguage {
        self.ts_lang.clone()
    }
}

/// A single structural match result.
pub struct StructuralMatch {
    /// Byte offset of the match start in the source.
    pub start_byte: usize,
    /// Byte offset of the match end in the source.
    pub end_byte: usize,
    /// Zero-based start line.
    pub start_line: usize,
    /// Zero-based start column.
    pub start_col: usize,
    /// The matched text.
    pub text: String,
    /// Captured metavariables: (name, text).
    pub captures: Vec<(String, String)>,
}

/// A structural pattern compiled for one SymForge language.
pub struct CompiledStructuralPattern {
    lang: SgLang,
    pattern: Pattern,
}

/// Compile an ast-grep pattern for the given language.
///
/// Returns an error string if the language is unsupported or the pattern cannot be compiled.
pub fn compile_structural_pattern(
    pattern_str: &str,
    lang: &LanguageId,
    is_tsx: bool,
) -> Result<CompiledStructuralPattern, String> {
    let sg_lang = SgLang::from_language_id(lang, is_tsx)
        .ok_or_else(|| format!("structural search not supported for {:?}", lang))?;

    let pattern = Pattern::try_new(pattern_str, sg_lang.clone())
        .map_err(|e| format!("invalid structural pattern: {e}"))?;

    Ok(CompiledStructuralPattern {
        lang: sg_lang,
        pattern,
    })
}

/// Search `source` using an already compiled ast-grep pattern. The parse is
/// bounded by the indexer's deadline; a cancelled parse is an error, not an
/// empty match list.
pub fn structural_search_with_compiled(
    source: &str,
    compiled: &CompiledStructuralPattern,
) -> Result<Vec<StructuralMatch>, String> {
    structural_search_within(source, compiled, super::PARSE_DEADLINE)
}

fn structural_search_within(
    source: &str,
    compiled: &CompiledStructuralPattern,
    deadline: Duration,
) -> Result<Vec<StructuralMatch>, String> {
    let mut parser = Parser::new();
    parser
        .set_language(&compiled.lang.get_ts_language())
        .map_err(|e| format!("failed to set tree-sitter language: {e}"))?;
    let tree = super::parse_within(&mut parser, source, deadline)?;
    let root = AstGrep::doc(StrDoc {
        src: source.to_string(),
        lang: compiled.lang.clone(),
        tree,
    });

    Ok(root
        .root()
        .find_all(&compiled.pattern)
        .map(|node_match| {
            let start = node_match.start_pos();
            let text = node_match.text().to_string();

            // Extract metavariable captures
            let env = node_match.get_env();
            let captures: Vec<(String, String)> = env
                .get_matched_variables()
                .filter_map(|var| match var {
                    MetaVariable::Capture(name, _) => {
                        env.get_match(&name).map(|n| (name, n.text().to_string()))
                    }
                    MetaVariable::MultiCapture(name) => {
                        let nodes = env.get_multiple_matches(&name);
                        let combined: String = nodes
                            .iter()
                            .map(|n| n.text().to_string())
                            .collect::<Vec<_>>()
                            .join(", ");
                        if combined.is_empty() {
                            None
                        } else {
                            Some((name, combined))
                        }
                    }
                    _ => None,
                })
                .collect();

            StructuralMatch {
                start_byte: node_match.range().start,
                end_byte: node_match.range().end,
                start_line: start.line(),
                start_col: start.byte_point().1,
                text,
                captures,
            }
        })
        .collect())
}

/// Search `source` for occurrences of an ast-grep `pattern` in the given language.
///
/// Returns an error string if the pattern cannot be compiled (e.g., syntax error).
pub fn structural_search(
    source: &str,
    pattern_str: &str,
    lang: &LanguageId,
    is_tsx: bool,
) -> Result<Vec<StructuralMatch>, String> {
    let compiled = compile_structural_pattern(pattern_str, lang, is_tsx)?;
    structural_search_with_compiled(source, &compiled)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn structural_search_past_its_deadline_is_an_error() {
        let source = "fn f() { let x = 1; }\n".repeat(20_000);
        let compiled = compile_structural_pattern("fn $NAME() { $$$ }", &LanguageId::Rust, false)
            .expect("pattern should compile");
        let Err(err) = structural_search_within(&source, &compiled, Duration::ZERO) else {
            panic!("a zero deadline must cancel the parse");
        };
        assert!(err.contains("cancelled"), "unexpected error: {err}");
    }

    #[test]
    fn test_structural_search_rust_function() {
        let source = r#"
fn hello() {
    println!("hello");
}
fn world(x: i32) {
    println!("{}", x);
}
"#;
        let matches = structural_search(source, "fn $NAME($$$) { $$$ }", &LanguageId::Rust, false)
            .expect("pattern should compile");
        assert_eq!(matches.len(), 2);
        assert!(matches[0].text.contains("hello"));
        assert!(matches[1].text.contains("world"));
    }

    #[test]
    fn test_structural_search_captures() {
        let source = "let x = 42;\nlet y = 100;";
        let matches = structural_search(source, "let $NAME = $VALUE", &LanguageId::Rust, false)
            .expect("pattern should compile");
        assert_eq!(matches.len(), 2);
        assert!(!matches[0].captures.is_empty());
    }

    #[test]
    fn test_structural_search_no_match() {
        let source = "fn main() {}";
        let matches = structural_search(
            source,
            "struct $NAME { $$$FIELDS }",
            &LanguageId::Rust,
            false,
        )
        .expect("pattern should compile");
        assert!(matches.is_empty());
    }

    #[test]
    fn test_structural_search_config_language_rejected() {
        let result = structural_search("{}", "{ $$$BODY }", &LanguageId::Json, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_structural_search_javascript() {
        let source = "const x = 42;\nconst y = 100;";
        let matches = structural_search(
            source,
            "const $NAME = $VALUE",
            &LanguageId::JavaScript,
            false,
        )
        .expect("pattern should compile");
        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn test_structural_search_rust_struct() {
        let source = r#"
pub struct Foo {
    pub name: String,
    pub age: u32,
}
struct Bar {
    x: i32,
}
"#;
        // `struct $NAME` matches bare structs; `pub struct` needs explicit `pub` in pattern
        let bare = structural_search(
            source,
            "struct $NAME { $$$FIELDS }",
            &LanguageId::Rust,
            false,
        )
        .expect("pattern should compile");
        assert_eq!(bare.len(), 1, "bare struct pattern matches Bar");
        assert!(bare[0].text.contains("Bar"));

        let pub_matches = structural_search(
            source,
            "pub struct $NAME { $$$FIELDS }",
            &LanguageId::Rust,
            false,
        )
        .expect("pattern should compile");
        assert_eq!(pub_matches.len(), 1, "pub struct pattern matches Foo");
        assert!(pub_matches[0].text.contains("Foo"));
    }

    #[test]
    fn test_structural_search_rust_impl() {
        let source = "impl Foo { fn bar(&self) {} }";
        let matches = structural_search(source, "impl $TYPE { $$$ }", &LanguageId::Rust, false)
            .expect("pattern should compile");
        assert_eq!(matches.len(), 1);
        assert!(matches[0].text.contains("Foo"));
    }

    #[test]
    fn test_structural_search_python_function() {
        let source = "def greet(name):\n    print(name)\n";
        let matches = structural_search(
            source,
            "def $FNAME($$$):\n    $$$",
            &LanguageId::Python,
            false,
        )
        .expect("pattern should compile");
        assert!(!matches.is_empty(), "should match Python function def");
    }

    #[test]
    fn test_structural_search_go_function() {
        let source = "func hello() { fmt.Println(\"hello\") }";
        let matches = structural_search(source, "func $NAME() { $$$ }", &LanguageId::Go, false)
            .expect("pattern should compile");
        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn test_structural_search_tsx_jsx_component() {
        // A JSX component body only parses under the TSX grammar. With the plain
        // TypeScript grammar this yields a partial parse and the structural
        // pattern matches nothing; with the TSX grammar (is_tsx=true) the
        // function declaration is matched.
        let source = r#"
export function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <RouterProvider router={router} />
    </QueryClientProvider>
  );
}
"#;
        let matches = structural_search(
            source,
            "function $NAME() { $$$ }",
            &LanguageId::TypeScript,
            true,
        )
        .expect("pattern should compile against the TSX grammar");
        assert_eq!(
            matches.len(),
            1,
            "TSX grammar must match the JSX component function"
        );
        assert!(matches[0].text.contains("App"));
    }
}