loctree 0.13.0

Structural code intelligence for AI agents. Scan once, query everything.
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Tree-sitter declaration extraction for the C family.
//!
//! Walks the syntax tree once and emits [`SymbolNode`]s (provenance
//! `TreeSitter`) plus a `Definition`/`Declaration` occurrence per node
//! (confidence `Heuristic` — Tier 1 never claims `Precise`).

use crate::symbols::{
    Confidence, LanguageId, OccurrenceRole, SymbolId, SymbolKind, SymbolNode, SymbolOccurrence,
    SymbolProvenance, TextRange,
};
use std::path::PathBuf;
use tree_sitter::{Node, Parser, Tree};

/// Hard cap on extracted definitions per file — degenerate generated sources
/// should not balloon the snapshot.
const MAX_SYMBOLS_PER_FILE: usize = 4000;

/// Identifier-ish node kinds across the four grammars, used for name
/// resolution fallback.
const IDENT_KINDS: &[&str] = &[
    "identifier",
    "simple_identifier",
    "type_identifier",
    "field_identifier",
    "method_identifier",
    "namespace_identifier",
    "operator_name",
    "destructor_name",
];

#[derive(Default)]
pub(super) struct Extraction {
    pub nodes: Vec<SymbolNode>,
    pub occurrences: Vec<SymbolOccurrence>,
    /// Byte ranges of definition name tokens, so the usage pass does not
    /// re-count a definition site as a reference.
    pub name_ranges: Vec<(usize, usize)>,
    /// Parsed tree, reused by the usage pass (parse once per file).
    pub tree: Option<Tree>,
}

pub(super) fn grammar_for(lang: LanguageId) -> tree_sitter::Language {
    match lang {
        LanguageId::Swift => tree_sitter_swift::LANGUAGE.into(),
        LanguageId::ObjC | LanguageId::ObjCpp => tree_sitter_objc::LANGUAGE.into(),
        LanguageId::C => tree_sitter_c::LANGUAGE.into(),
        LanguageId::Cpp => tree_sitter_cpp::LANGUAGE.into(),
    }
}

/// Human/agent-facing label for a [`SymbolKind`] — also the `kind` segment of
/// the Tier-1 [`SymbolId`] descriptor.
pub(super) fn kind_label(kind: &SymbolKind) -> String {
    match kind {
        SymbolKind::Type => "type".into(),
        SymbolKind::Class => "class".into(),
        SymbolKind::Struct => "struct".into(),
        SymbolKind::Protocol => "protocol".into(),
        SymbolKind::Enum => "enum".into(),
        SymbolKind::Func => "func".into(),
        SymbolKind::Method => "method".into(),
        SymbolKind::Property => "property".into(),
        SymbolKind::Field => "field".into(),
        SymbolKind::Var => "var".into(),
        SymbolKind::Macro => "macro".into(),
        SymbolKind::Typedef => "typedef".into(),
        SymbolKind::Selector => "selector".into(),
        SymbolKind::Module => "module".into(),
        SymbolKind::Namespace => "namespace".into(),
        SymbolKind::Other(s) => s.clone(),
    }
}

pub(super) fn extract(content: &str, relative: &str, lang: LanguageId) -> Extraction {
    let mut out = Extraction::default();
    let mut parser = Parser::new();
    if parser.set_language(&grammar_for(lang)).is_err() {
        return out;
    }
    let Some(tree) = parser.parse(content, None) else {
        return out;
    };
    let src = content.as_bytes();

    let mut stack = vec![tree.root_node()];
    while let Some(node) = stack.pop() {
        if out.nodes.len() >= MAX_SYMBOLS_PER_FILE {
            break;
        }
        if let Some((kind, role)) = classify(node, lang) {
            if let Some((name, name_node)) = resolve_name(node, src, lang, &kind) {
                push_symbol(
                    &mut out, relative, lang, kind, role, name, node, name_node, src,
                );
            }
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.is_named() {
                stack.push(child);
            }
        }
    }
    out.tree = Some(tree);
    out
}

#[allow(clippy::too_many_arguments)]
fn push_symbol(
    out: &mut Extraction,
    relative: &str,
    lang: LanguageId,
    kind: SymbolKind,
    role: OccurrenceRole,
    name: String,
    node: Node,
    name_node: Option<Node>,
    src: &[u8],
) {
    let range = text_range(node);
    let label = kind_label(&kind);
    let range_hash = fnv1a(
        format!(
            "{}:{}:{}:{}",
            relative, range.start_byte, range.end_byte, name
        )
        .as_bytes(),
    );
    let id = SymbolId::from_parts(relative, &label, &name, range_hash);

    let signature = node
        .utf8_text(src)
        .ok()
        .and_then(|t| t.lines().next())
        .map(|l| {
            let trimmed = l.trim();
            let mut sig: String = trimmed.chars().take(160).collect();
            if trimmed.chars().count() > 160 {
                sig.push('');
            }
            sig
        });

    if let Some(n) = name_node {
        out.name_ranges.push((n.start_byte(), n.end_byte()));
    }
    out.occurrences.push(SymbolOccurrence {
        symbol_id: id.clone(),
        file: PathBuf::from(relative),
        range,
        role,
        confidence: Confidence::Heuristic,
        engine: SymbolProvenance::TreeSitter,
    });
    out.nodes.push(SymbolNode {
        id,
        language: lang,
        kind,
        name,
        qualified_name: None,
        module: None,
        usr: None,
        file: Some(PathBuf::from(relative)),
        range: Some(range),
        signature,
        visibility: None,
        provenance: SymbolProvenance::TreeSitter,
    });
}

/// Map a grammar node to a declared-symbol kind + occurrence role.
fn classify(node: Node, lang: LanguageId) -> Option<(SymbolKind, OccurrenceRole)> {
    let kind = node.kind();
    match lang {
        LanguageId::Swift => match kind {
            // `class_declaration` covers class/struct/enum/actor/extension —
            // the leading keyword token disambiguates (see `swift_class_kind`).
            "class_declaration" => Some((swift_class_kind(node), OccurrenceRole::Definition)),
            "protocol_declaration" => Some((SymbolKind::Protocol, OccurrenceRole::Definition)),
            "function_declaration" => Some((
                if has_type_body_ancestor(node) {
                    SymbolKind::Method
                } else {
                    SymbolKind::Func
                },
                OccurrenceRole::Definition,
            )),
            "protocol_function_declaration" => {
                Some((SymbolKind::Method, OccurrenceRole::Declaration))
            }
            "init_declaration" => Some((SymbolKind::Method, OccurrenceRole::Definition)),
            "property_declaration" | "protocol_property_declaration" => {
                Some((SymbolKind::Property, OccurrenceRole::Definition))
            }
            "call_expression" => Some((
                SymbolKind::Other("command".to_string()),
                OccurrenceRole::Definition,
            )),
            "typealias_declaration" => Some((SymbolKind::Typedef, OccurrenceRole::Definition)),
            _ => None,
        },
        LanguageId::ObjC | LanguageId::ObjCpp => match kind {
            "class_interface" => Some((SymbolKind::Class, OccurrenceRole::Declaration)),
            "class_implementation" => Some((SymbolKind::Class, OccurrenceRole::Definition)),
            "protocol_declaration" => Some((SymbolKind::Protocol, OccurrenceRole::Definition)),
            "method_declaration" => Some((SymbolKind::Method, OccurrenceRole::Declaration)),
            "method_definition" => Some((SymbolKind::Method, OccurrenceRole::Definition)),
            "property_declaration" => Some((SymbolKind::Property, OccurrenceRole::Declaration)),
            "function_definition" => Some((SymbolKind::Func, OccurrenceRole::Definition)),
            "type_definition" => Some((SymbolKind::Typedef, OccurrenceRole::Definition)),
            _ => None,
        },
        LanguageId::C => match kind {
            "function_definition" => Some((SymbolKind::Func, OccurrenceRole::Definition)),
            "struct_specifier" if node.child_by_field_name("body").is_some() => {
                Some((SymbolKind::Struct, OccurrenceRole::Definition))
            }
            "enum_specifier" if node.child_by_field_name("body").is_some() => {
                Some((SymbolKind::Enum, OccurrenceRole::Definition))
            }
            "union_specifier" if node.child_by_field_name("body").is_some() => Some((
                SymbolKind::Other("union".to_string()),
                OccurrenceRole::Definition,
            )),
            "type_definition" => Some((SymbolKind::Typedef, OccurrenceRole::Definition)),
            _ => None,
        },
        LanguageId::Cpp => match kind {
            "function_definition" => Some((
                if has_type_body_ancestor(node) {
                    SymbolKind::Method
                } else {
                    SymbolKind::Func
                },
                OccurrenceRole::Definition,
            )),
            "class_specifier" if node.child_by_field_name("body").is_some() => {
                Some((SymbolKind::Class, OccurrenceRole::Definition))
            }
            "struct_specifier" if node.child_by_field_name("body").is_some() => {
                Some((SymbolKind::Struct, OccurrenceRole::Definition))
            }
            "enum_specifier" if node.child_by_field_name("body").is_some() => {
                Some((SymbolKind::Enum, OccurrenceRole::Definition))
            }
            "namespace_definition" => Some((SymbolKind::Namespace, OccurrenceRole::Definition)),
            "type_definition" => Some((SymbolKind::Typedef, OccurrenceRole::Definition)),
            "alias_declaration" => Some((SymbolKind::Typedef, OccurrenceRole::Definition)),
            _ => None,
        },
    }
}

/// Disambiguate Swift `class_declaration` into class/struct/enum/extension by
/// scanning the leading keyword tokens.
fn swift_class_kind(node: Node) -> SymbolKind {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "class" => return SymbolKind::Class,
            "struct" => return SymbolKind::Struct,
            "enum" => return SymbolKind::Enum,
            "actor" => return SymbolKind::Class,
            "extension" => return SymbolKind::Other("extension".to_string()),
            _ => {}
        }
    }
    SymbolKind::Type
}

/// True when the node lives inside a type body (class/struct/protocol), which
/// promotes functions to methods.
fn has_type_body_ancestor(node: Node) -> bool {
    let mut cur = node.parent();
    while let Some(p) = cur {
        match p.kind() {
            // Swift bodies
            "class_body" | "protocol_body" | "enum_class_body"
            // C++ member lists
            | "field_declaration_list" => return true,
            _ => {}
        }
        cur = p.parent();
    }
    false
}

/// Resolve the declared name of a node: explicit `name` field first, then the
/// `declarator` chain (C/C++), then a bounded identifier-descendant search.
fn resolve_name<'a>(
    node: Node<'a>,
    src: &[u8],
    lang: LanguageId,
    kind: &SymbolKind,
) -> Option<(String, Option<Node<'a>>)> {
    if lang == LanguageId::Swift && matches!(kind, SymbolKind::Other(label) if label == "command") {
        return swift_command_label(node, src);
    }

    // ObjC method selectors: prefer the dedicated selector node so
    // `- (void)reload` resolves to `reload`, not the return type.
    if matches!(kind, SymbolKind::Method | SymbolKind::Selector)
        && matches!(lang, LanguageId::ObjC | LanguageId::ObjCpp)
        && let Some(found) = objc_method_name(node, src)
    {
        return Some(found);
    }

    if matches!(kind, SymbolKind::Method | SymbolKind::Selector)
        && let Some(n) = find_descendant_of_kind(node, "method_identifier", 6)
    {
        return text_of(n, src).map(|t| (t, Some(n)));
    }

    if matches!(lang, LanguageId::ObjC | LanguageId::ObjCpp)
        && matches!(kind, SymbolKind::Property)
        && let Some(found) = objc_property_name(node, src)
    {
        return Some(found);
    }

    if let Some(field) = node.child_by_field_name("name")
        && let Some(n) = ident_within(field, 4)
    {
        return text_of(n, src).map(|t| (t, Some(n)));
    }

    // Swift `init` has no name token worth resolving.
    if node.kind() == "init_declaration" {
        return Some(("init".to_string(), None));
    }

    let mut cur = node;
    while let Some(d) = cur.child_by_field_name("declarator") {
        cur = d;
    }
    if cur.id() != node.id()
        && let Some(n) = ident_within(cur, 6)
    {
        return text_of(n, src).map(|t| (t, Some(n)));
    }

    let n = ident_within(node, 4)?;
    text_of(n, src).map(|t| (t, Some(n)))
}

fn text_of(node: Node, src: &[u8]) -> Option<String> {
    node.utf8_text(src)
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

fn swift_command_label<'a>(node: Node<'a>, src: &[u8]) -> Option<(String, Option<Node<'a>>)> {
    if node.kind() != "call_expression" {
        return None;
    }
    let callee = first_child_identifier_text(node, src)?;
    let is_command_call = matches!(callee.as_str(), "CommandMenu" | "CommandGroup")
        || (callee == "Button" && swift_has_command_ancestor(node, src));
    if !is_command_call {
        return None;
    }
    let label = find_descendant_of_kind(node, "line_string_literal", 6)
        .or_else(|| find_descendant_of_kind(node, "multi_line_string_literal", 6))
        .or_else(|| find_descendant_of_kind(node, "raw_string_literal", 6))?;
    string_literal_text(label, src).map(|name| (name, Some(label)))
}

fn swift_has_command_ancestor(mut node: Node, src: &[u8]) -> bool {
    for _ in 0..8 {
        let Some(parent) = node.parent() else {
            return false;
        };
        if parent.kind() == "call_expression"
            && let Some(callee) = first_child_identifier_text(parent, src)
            && matches!(callee.as_str(), "CommandMenu" | "CommandGroup")
        {
            return true;
        }
        node = parent;
    }
    false
}

fn first_child_identifier_text(node: Node, src: &[u8]) -> Option<String> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if IDENT_KINDS.contains(&child.kind()) {
            return text_of(child, src);
        }
    }
    None
}

fn string_literal_text(node: Node, src: &[u8]) -> Option<String> {
    find_descendant_of_kind(node, "line_str_text", 4)
        .or_else(|| find_descendant_of_kind(node, "multi_line_str_text", 4))
        .and_then(|n| text_of(n, src))
        .or_else(|| {
            text_of(node, src).map(|t| t.trim_matches('#').trim_matches('"').trim().to_string())
        })
        .filter(|s| !s.is_empty())
}

fn objc_method_name<'a>(node: Node<'a>, src: &[u8]) -> Option<(String, Option<Node<'a>>)> {
    if let Some(keyword) = find_descendant_of_kind(node, "keyword_declarator", 4)
        && let Some(name) = ident_within(keyword, 4)
    {
        return text_of(name, src).map(|t| (t, Some(name)));
    }
    if let Some(method) = find_descendant_of_kind(node, "method_identifier", 6) {
        return text_of(method, src).map(|t| (t, Some(method)));
    }
    None
}

fn objc_property_name<'a>(node: Node<'a>, src: &[u8]) -> Option<(String, Option<Node<'a>>)> {
    if let Some(field) = find_descendant_of_kind(node, "field_identifier", 6) {
        return text_of(field, src).map(|t| (t, Some(field)));
    }
    let declaration = find_descendant_of_kind(node, "struct_declaration", 4).unwrap_or(node);
    let ident = last_ident_within(declaration, 8)?;
    text_of(ident, src).map(|t| (t, Some(ident)))
}

/// Depth-bounded search for the first identifier-ish descendant.
fn ident_within(node: Node, max_depth: usize) -> Option<Node> {
    if IDENT_KINDS.contains(&node.kind()) {
        return Some(node);
    }
    if max_depth == 0 {
        return None;
    }
    let mut cursor = node.walk();
    let children: Vec<Node> = node.children(&mut cursor).collect();
    for child in children {
        if let Some(found) = ident_within(child, max_depth - 1) {
            return Some(found);
        }
    }
    None
}

/// Depth-bounded search for the last identifier-ish descendant.
fn last_ident_within(node: Node, max_depth: usize) -> Option<Node> {
    let mut found = IDENT_KINDS.contains(&node.kind()).then_some(node);
    if max_depth == 0 {
        return found;
    }
    let mut cursor = node.walk();
    let children: Vec<Node> = node.children(&mut cursor).collect();
    for child in children {
        if let Some(candidate) = last_ident_within(child, max_depth - 1) {
            found = Some(candidate);
        }
    }
    found
}

/// Depth-bounded search for the first descendant of an exact kind.
fn find_descendant_of_kind<'a>(node: Node<'a>, kind: &str, max_depth: usize) -> Option<Node<'a>> {
    if node.kind() == kind {
        return Some(node);
    }
    if max_depth == 0 {
        return None;
    }
    let mut cursor = node.walk();
    let children: Vec<Node> = node.children(&mut cursor).collect();
    for child in children {
        if let Some(found) = find_descendant_of_kind(child, kind, max_depth - 1) {
            return Some(found);
        }
    }
    None
}

pub(super) fn text_range(node: Node) -> TextRange {
    let start = node.start_position();
    let end = node.end_position();
    TextRange {
        start_byte: node.start_byte(),
        end_byte: node.end_byte(),
        start_line: start.row + 1,
        start_col: start.column + 1,
        end_line: end.row + 1,
        end_col: end.column + 1,
    }
}

/// Deterministic FNV-1a — the Tier-1 descriptor hash must be stable across
/// runs (std's `DefaultHasher` makes no such guarantee).
pub(super) fn fnv1a(bytes: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for byte in bytes {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    hash
}