dirge-agent 0.12.3

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
use std::path::Path;

use tree_sitter::{Node, Parser};

use crate::semantic::adapter::LanguageAdapter;
use crate::semantic::common::{node_text, signature_first_line};
use crate::semantic::types::{ByteRange, ExtractedFile, Import, ImportKind, Symbol, SymbolKind};

/// Tree-sitter adapter for Java.
///
/// Visibility: explicit `public` modifier → exported. Anything else
/// (no modifier = package-private, plus `private` / `protected`)
/// stays non-exported. The package + import system is implicit so
/// we don't try to model package-private visibility separately.
///
/// Class methods get `parent_class = <containing class name>`;
/// interface methods get the interface name. Nested classes are
/// walked recursively.
pub struct JavaAdapter;

impl JavaAdapter {
    fn signature(&self, n: Node, s: &[u8]) -> String {
        // Prefer the body field (set by tree-sitter-java for
        // class/interface/method bodies); fall back to walking
        // children for any *_body node (covers enum_body which
        // doesn't always expose the field); finally a first-line
        // cap as the universal fallback.
        if let Some(body) = n.child_by_field_name("body") {
            return String::from_utf8_lossy(&s[n.start_byte()..body.start_byte()])
                .trim()
                .to_string();
        }
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && (c.kind().ends_with("_body") || c.kind() == "block")
            {
                return String::from_utf8_lossy(&s[n.start_byte()..c.start_byte()])
                    .trim()
                    .to_string();
            }
        }
        signature_first_line(n, s)
    }

    /// Examines the `modifiers` child (if any) for the literal token
    /// `public`. Java tree-sitter exposes modifiers as a sibling node
    /// whose CHILDREN are the modifier tokens, so we use the raw
    /// text of the modifiers node and substring-match.
    fn is_public(&self, n: Node, s: &[u8]) -> bool {
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && c.kind() == "modifiers"
            {
                let text = node_text(c, s);
                return text.split_whitespace().any(|t| t == "public");
            }
        }
        false
    }

    fn ident_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
        for i in 0..n.named_child_count() {
            let c = n.named_child(i)?;
            if c.kind() == "identifier" {
                return Some(node_text(c, s).to_string());
            }
        }
        None
    }

    /// Walk a `class_body` / `interface_body` / `enum_body` and
    /// emit child symbols (methods, fields, nested classes).
    fn walk_class_body(&self, body: Node, s: &[u8], symbols: &mut Vec<Symbol>, parent: &str) {
        for i in 0..body.named_child_count() {
            let Some(c) = body.named_child(i) else {
                continue;
            };
            match c.kind() {
                "method_declaration" => {
                    if let Some(name) = self.ident_name(c, s) {
                        symbols.push(Symbol {
                            kind: SymbolKind::Method,
                            is_exported: self.is_public(c, s),
                            name,
                            range: ByteRange::from(c),
                            signature: self.signature(c, s),
                            parent_class: Some(parent.to_string()),
                        });
                    }
                }
                "constructor_declaration" => {
                    // Constructor name matches the class; emit as a
                    // Method so users can find it via list_symbols
                    // --kind method --parent <class>.
                    if let Some(name) = self.ident_name(c, s) {
                        symbols.push(Symbol {
                            kind: SymbolKind::Method,
                            is_exported: self.is_public(c, s),
                            name,
                            range: ByteRange::from(c),
                            signature: self.signature(c, s),
                            parent_class: Some(parent.to_string()),
                        });
                    }
                }
                "field_declaration" => {
                    // `field_declaration > variable_declarator >
                    // identifier`. There can be multiple vars per
                    // declaration (`int a, b, c;`); emit each.
                    let is_pub = self.is_public(c, s);
                    for j in 0..c.named_child_count() {
                        if let Some(decl) = c.named_child(j)
                            && decl.kind() == "variable_declarator"
                            && let Some(name_n) = decl.named_child(0)
                            && name_n.kind() == "identifier"
                        {
                            let name = node_text(name_n, s).to_string();
                            symbols.push(Symbol {
                                kind: SymbolKind::Variable,
                                is_exported: is_pub,
                                name,
                                range: ByteRange::from(c),
                                signature: node_text(c, s).lines().next().unwrap_or("").to_string(),
                                parent_class: Some(parent.to_string()),
                            });
                        }
                    }
                }
                "class_declaration"
                | "interface_declaration"
                | "enum_declaration"
                | "record_declaration" => {
                    // Nested class — recurse from the top of the
                    // walker so the new class gets its own
                    // symbol + body walk.
                    self.walk_top(c, s, symbols);
                }
                _ => {}
            }
        }
    }

    fn walk_top(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
        match n.kind() {
            "class_declaration" => {
                let Some(name) = self.ident_name(n, s) else {
                    return;
                };
                let is_pub = self.is_public(n, s);
                symbols.push(Symbol {
                    kind: SymbolKind::Class,
                    is_exported: is_pub,
                    name: name.clone(),
                    range: ByteRange::from(n),
                    signature: self.signature(n, s),
                    parent_class: None,
                });
                if let Some(body) = n.child_by_field_name("body") {
                    self.walk_class_body(body, s, symbols, &name);
                }
            }
            "interface_declaration" => {
                let Some(name) = self.ident_name(n, s) else {
                    return;
                };
                let is_pub = self.is_public(n, s);
                symbols.push(Symbol {
                    kind: SymbolKind::Interface,
                    is_exported: is_pub,
                    name: name.clone(),
                    range: ByteRange::from(n),
                    signature: self.signature(n, s),
                    parent_class: None,
                });
                if let Some(body) = n.child_by_field_name("body") {
                    self.walk_class_body(body, s, symbols, &name);
                }
            }
            "enum_declaration" | "record_declaration" => {
                // Records (Java 16+) are immutable data carriers
                // that look like \`public record Point(int x, int y) {}\`.
                // tree-sitter exposes them as record_declaration; we
                // surface them as Class so list_symbols finds them,
                // and walk the body for any explicit methods. The
                // auto-generated accessors aren't AST-visible so we
                // can't list them, but the signature line preserves
                // the component list which is the useful info.
                let Some(name) = self.ident_name(n, s) else {
                    return;
                };
                let is_pub = self.is_public(n, s);
                symbols.push(Symbol {
                    kind: SymbolKind::Class,
                    is_exported: is_pub,
                    name: name.clone(),
                    range: ByteRange::from(n),
                    signature: self.signature(n, s),
                    parent_class: None,
                });
                if let Some(body) = n.child_by_field_name("body") {
                    self.walk_class_body(body, s, symbols, &name);
                }
            }
            _ => {}
        }
    }

    fn handle_import(&self, n: Node, s: &[u8], imports: &mut Vec<Import>) {
        // `import_declaration > scoped_identifier`. Static imports
        // and wildcards (`import java.util.*`) have similar shapes;
        // we just record the raw FQN text.
        for i in 0..n.named_child_count() {
            if let Some(c) = n.named_child(i)
                && matches!(c.kind(), "scoped_identifier" | "identifier")
            {
                let path = node_text(c, s).to_string();
                imports.push(Import {
                    names: vec![path.clone()],
                    source: path,
                    kind: ImportKind::Qualified,
                });
                return;
            }
        }
    }
}

impl LanguageAdapter for JavaAdapter {
    fn extensions(&self) -> &[&str] {
        &[".java"]
    }

    fn extract(&self, file_path: &Path, source: &str) -> Result<ExtractedFile, String> {
        let lang: tree_sitter::Language = tree_sitter_java::LANGUAGE.into();
        let mut parser = Parser::new();
        parser
            .set_language(&lang)
            .map_err(|e| format!("Failed to set language: {e}"))?;
        let tree = parser.parse(source, None).ok_or("Failed to parse source")?;
        let root = tree.root_node();
        let bytes = source.as_bytes();

        let mut symbols = Vec::new();
        let mut imports = Vec::new();
        let mut warnings = Vec::new();
        if root.has_error() {
            warnings.push("tree-sitter reported syntax errors".to_string());
        }

        for i in 0..root.named_child_count() {
            let Some(c) = root.named_child(i) else {
                continue;
            };
            match c.kind() {
                "class_declaration"
                | "interface_declaration"
                | "enum_declaration"
                | "record_declaration" => {
                    self.walk_top(c, bytes, &mut symbols);
                }
                "import_declaration" => self.handle_import(c, bytes, &mut imports),
                _ => {}
            }
        }

        let exports: Vec<String> = symbols
            .iter()
            .filter(|s| s.is_exported)
            .map(|s| s.name.clone())
            .collect();

        Ok(ExtractedFile {
            file_path: file_path.to_path_buf(),
            symbols,
            imports,
            exports,
            warnings,
            mtime: std::time::SystemTime::now(),
            size: 0,
            head_hash: 0,
        })
    }

    fn find_callees_in_range(
        &self,
        source: &str,
        _file_path: &Path,
        range: ByteRange,
    ) -> Result<Vec<String>, String> {
        let lang: tree_sitter::Language = tree_sitter_java::LANGUAGE.into();
        // `method_invocation` covers both bare `foo()` and `obj.foo()`.
        // The method's NAME is the `name` field child of type
        // `identifier`. Object_creation_expression handles
        // `new Foo()` constructor calls.
        let query_str = r#"
            (method_invocation name: (identifier) @callee)
            (object_creation_expression type: (type_identifier) @callee)
        "#;
        crate::semantic::common::run_callee_query(&lang, query_str, source, range)
    }
}

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

    fn pb(n: &str) -> std::path::PathBuf {
        std::path::PathBuf::from(n)
    }

    #[test]
    fn extracts_class_with_method_and_field_visibility() {
        let src = "public class App {\n  private int n;\n  public String name;\n  public void hello() {}\n  private void helper() {}\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        let app = f.symbols.iter().find(|s| s.name == "App").unwrap();
        assert!(matches!(app.kind, SymbolKind::Class));
        assert!(app.is_exported);
        let hello = f.symbols.iter().find(|s| s.name == "hello").unwrap();
        assert!(hello.is_exported);
        assert_eq!(hello.parent_class.as_deref(), Some("App"));
        let helper = f.symbols.iter().find(|s| s.name == "helper").unwrap();
        assert!(!helper.is_exported);
        let n_field = f.symbols.iter().find(|s| s.name == "n").unwrap();
        assert!(matches!(n_field.kind, SymbolKind::Variable));
        assert!(!n_field.is_exported);
        let name_field = f.symbols.iter().find(|s| s.name == "name").unwrap();
        assert!(name_field.is_exported);
    }

    #[test]
    fn extracts_interface_with_methods() {
        let src = "public interface Greeter {\n  String greet(String n);\n  default String wave() { return \"o/\"; }\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        let g = f.symbols.iter().find(|s| s.name == "Greeter").unwrap();
        assert!(matches!(g.kind, SymbolKind::Interface));
        let greet = f.symbols.iter().find(|s| s.name == "greet").unwrap();
        assert!(matches!(greet.kind, SymbolKind::Method));
        assert_eq!(greet.parent_class.as_deref(), Some("Greeter"));
        assert!(f.symbols.iter().any(|s| s.name == "wave"));
    }

    #[test]
    fn extracts_constructor_as_method() {
        let src = "public class App {\n  public App(String p) {}\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        // Two `App` symbols: the class + the constructor. The
        // constructor's kind is Method.
        let methods: Vec<_> = f
            .symbols
            .iter()
            .filter(|s| s.name == "App" && matches!(s.kind, SymbolKind::Method))
            .collect();
        assert_eq!(methods.len(), 1);
        assert_eq!(methods[0].parent_class.as_deref(), Some("App"));
    }

    #[test]
    fn extracts_enum_as_class() {
        let src = "enum Color { RED, GREEN }\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        let c = f.symbols.iter().find(|s| s.name == "Color").unwrap();
        assert!(matches!(c.kind, SymbolKind::Class));
    }

    #[test]
    fn extracts_imports() {
        let src = "import java.util.List;\nimport java.util.Map;\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        assert!(f.imports.iter().any(|i| i.source == "java.util.List"));
        assert!(f.imports.iter().any(|i| i.source == "java.util.Map"));
    }

    #[test]
    fn find_callees_method_and_constructor() {
        let src = "public class App {\n  public void run() {\n    helper();\n    System.out.println(\"x\");\n    Foo f = new Foo();\n  }\n  private void helper() {}\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        let run = f.symbols.iter().find(|s| s.name == "run").unwrap();
        let callees = JavaAdapter
            .find_callees_in_range(src, &pb("a.java"), run.range)
            .unwrap();
        assert!(callees.contains(&"helper".to_string()));
        assert!(callees.contains(&"println".to_string()));
        assert!(callees.contains(&"Foo".to_string()));
    }

    #[test]
    fn nested_class_is_recursively_walked() {
        let src =
            "public class Outer {\n  public class Inner {\n    public void deep() {}\n  }\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        assert!(
            f.symbols
                .iter()
                .any(|s| s.name == "Inner" && matches!(s.kind, SymbolKind::Class))
        );
        let deep = f.symbols.iter().find(|s| s.name == "deep").unwrap();
        assert_eq!(deep.parent_class.as_deref(), Some("Inner"));
    }

    /// Records (Java 16+) — previously silently dropped.
    #[test]
    fn extracts_record_declaration_as_class() {
        let src = "public record Point(int x, int y) {\n  public boolean isOrigin() { return x == 0 && y == 0; }\n}\n";
        let f = JavaAdapter.extract(&pb("a.java"), src).unwrap();
        let point = f.symbols.iter().find(|s| s.name == "Point").unwrap();
        assert!(matches!(point.kind, SymbolKind::Class));
        assert!(point.is_exported);
        let m = f.symbols.iter().find(|s| s.name == "isOrigin").unwrap();
        assert_eq!(m.parent_class.as_deref(), Some("Point"));
    }
}