loctree 0.13.1

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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Single File Component (SFC) script and template extraction.
//!
//! This module handles extraction of script and template content from
//! Svelte (.svelte), Vue (.vue), and Astro (.astro) Single File Components.
//!
//! 𝚅𝚒𝚋𝚎𝚌𝚛𝚊𝚏𝚝𝚎𝚍. with AI Agents ⓒ 2025-2026 Loctree Team

use regex::Regex;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum RuneKind {
    State,
    Derived,
    Props,
    Bindable,
    Effect,
    Inspect,
    Host,
}

impl RuneKind {
    pub(super) fn export_kind(&self) -> &'static str {
        match self {
            Self::State => "rune_state",
            Self::Derived => "rune_derived",
            Self::Props => "rune_props",
            Self::Bindable => "rune_bindable",
            Self::Effect => "rune_effect",
            Self::Inspect => "rune_inspect_debug",
            Self::Host => "rune_host",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct RuneDeclaration {
    pub(super) name: String,
    pub(super) kind: RuneKind,
    pub(super) line: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct SnippetDeclaration {
    pub(super) name: String,
    pub(super) line: usize,
    pub(super) params: Vec<String>,
}

/// Extract script content from a Svelte file.
///
/// Handles both `<script>` and `<script lang="ts">` variants.
pub(super) fn extract_svelte_script(content: &str) -> String {
    extract_sfc_script(content)
}

/// Extract script content from a Vue Single File Component (SFC).
///
/// Handles `<script>`, `<script setup>`, `<script lang="ts">` variants.
pub(super) fn extract_vue_script(content: &str) -> String {
    extract_sfc_script(content)
}

/// Extract Astro frontmatter delimited by leading `---` fence lines.
pub(super) fn extract_astro_frontmatter(content: &str) -> String {
    find_astro_frontmatter_bounds(content)
        .map(|(start, end, _)| content[start..end].to_string())
        .unwrap_or_default()
}

/// Extract Astro template content after frontmatter, or the whole file when no frontmatter exists.
pub(super) fn extract_astro_template(content: &str) -> String {
    find_astro_frontmatter_bounds(content)
        .map(|(_, _, after)| content[after..].to_string())
        .unwrap_or_else(|| content.to_string())
}

/// Extract inline Astro `<script>` blocks from the template body.
pub(super) fn extract_astro_scripts(content: &str) -> String {
    extract_tag_blocks(&extract_astro_template(content), "script")
}

/// Extract inline Astro `<style>` blocks from the template body.
pub(super) fn extract_astro_styles(content: &str) -> String {
    extract_tag_blocks(&extract_astro_template(content), "style")
}

/// Common SFC script extraction used by both Svelte and Vue.
fn extract_sfc_script(content: &str) -> String {
    // Match <script> or <script lang="ts"> or <script module> etc.
    // Use lazy matching to capture all script blocks
    let script_regex = Regex::new(r#"<script[^>]*>([\s\S]*?)</script>"#).ok();

    if let Some(re) = script_regex {
        let mut scripts = Vec::new();
        for caps in re.captures_iter(content) {
            if let Some(script_content) = caps.get(1) {
                scripts.push(script_content.as_str().to_string());
            }
        }
        scripts.join("\n")
    } else {
        String::new()
    }
}

fn find_astro_frontmatter_bounds(content: &str) -> Option<(usize, usize, usize)> {
    let mut offset = 0;
    let mut lines = content.split_inclusive('\n');
    let first = lines.next()?;
    if first.trim_end_matches(['\r', '\n']) != "---" {
        return None;
    }

    let frontmatter_start = first.len();
    offset += first.len();

    for line in lines {
        let trimmed = line.trim_end_matches(['\r', '\n']);
        if trimmed == "---" {
            return Some((frontmatter_start, offset, offset + line.len()));
        }
        offset += line.len();
    }

    None
}

fn extract_tag_blocks(content: &str, tag: &str) -> String {
    let pattern = format!(r#"<{tag}[^>]*>([\s\S]*?)</{tag}>"#);
    let Ok(re) = Regex::new(&pattern) else {
        return String::new();
    };

    re.captures_iter(content)
        .filter_map(|caps| caps.get(1).map(|m| m.as_str().to_string()))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Extract template content from a Svelte file (everything outside <script> and <style>).
pub(super) fn extract_svelte_template(content: &str) -> String {
    let mut result = content.to_string();
    if let Ok(script_re) = Regex::new(r#"<script[^>]*>[\s\S]*?</script>"#) {
        result = script_re.replace_all(&result, "").to_string();
    }
    if let Ok(style_re) = Regex::new(r#"<style[^>]*>[\s\S]*?</style>"#) {
        result = style_re.replace_all(&result, "").to_string();
    }
    result
}

/// Extract template content from a Vue file (everything inside <template> tags).
pub(super) fn extract_vue_template(content: &str) -> String {
    let template_regex = Regex::new(r#"<template[^>]*>([\s\S]*?)</template>"#).ok();

    if let Some(re) = template_regex {
        let mut templates = Vec::new();
        for caps in re.captures_iter(content) {
            if let Some(template_content) = caps.get(1) {
                templates.push(template_content.as_str().to_string());
            }
        }
        templates.join("\n")
    } else {
        String::new()
    }
}

pub(super) fn extract_svelte5_runes(script_content: &str) -> Vec<RuneDeclaration> {
    let mut declarations = Vec::new();

    if let Ok(re) = Regex::new(
        r#"(?m)\b(?:export\s+)?(?:let|const|var)\s+([A-Za-z_$][\w$]*)\s*=\s*\$(state|derived|bindable)(?:\.(raw|snapshot|by))?\s*\("#,
    ) {
        for caps in re.captures_iter(script_content) {
            let Some(name) = caps.get(1) else {
                continue;
            };
            let Some(kind_match) = caps.get(2) else {
                continue;
            };
            let kind = match kind_match.as_str() {
                "state" => RuneKind::State,
                "derived" => RuneKind::Derived,
                "bindable" => RuneKind::Bindable,
                _ => continue,
            };
            declarations.push(RuneDeclaration {
                name: name.as_str().to_string(),
                kind,
                line: line_for_offset(script_content, name.start()),
            });
        }
    }

    if let Ok(re) = Regex::new(
        r#"(?m)\b(?:export\s+)?(?:let|const|var)\s+([A-Za-z_$][\w$]*)\s*=\s*\$props(?:\.\w+)?\s*\("#,
    ) {
        for caps in re.captures_iter(script_content) {
            if let Some(name) = caps.get(1) {
                declarations.push(RuneDeclaration {
                    name: name.as_str().to_string(),
                    kind: RuneKind::Props,
                    line: line_for_offset(script_content, name.start()),
                });
            }
        }
    }

    if let Ok(re) = Regex::new(
        r#"(?m)\b(?:export\s+)?(?:let|const|var)\s*\{([^}]*)\}\s*=\s*\$props(?:\.\w+)?\s*\("#,
    ) {
        for caps in re.captures_iter(script_content) {
            let Some(bindings) = caps.get(1) else {
                continue;
            };
            let line = line_for_offset(script_content, bindings.start());
            for name in extract_destructured_names(bindings.as_str()) {
                declarations.push(RuneDeclaration {
                    name,
                    kind: RuneKind::Props,
                    line,
                });
            }
        }
    }

    for (rune, kind) in [
        ("effect", RuneKind::Effect),
        ("inspect", RuneKind::Inspect),
        ("host", RuneKind::Host),
    ] {
        let pattern = format!(r#"(?m)\${rune}(?:\.\w+)?\s*\("#);
        let Ok(re) = Regex::new(&pattern) else {
            continue;
        };
        for mat in re.find_iter(script_content) {
            declarations.push(RuneDeclaration {
                name: format!("${rune}"),
                kind: kind.clone(),
                line: line_for_offset(script_content, mat.start()),
            });
        }
    }

    declarations
}

pub(super) fn extract_svelte_snippets(template: &str) -> Vec<SnippetDeclaration> {
    let Ok(re) = Regex::new(r#"\{#snippet\s+([A-Za-z_$][\w$]*)\s*\(([^)]*)\)\}"#) else {
        return Vec::new();
    };

    re.captures_iter(template)
        .filter_map(|caps| {
            let name = caps.get(1)?;
            let params = caps
                .get(2)
                .map(|m| {
                    m.as_str()
                        .split(',')
                        .filter_map(|param| {
                            let name = param
                                .trim()
                                .split([':', '=', ' '])
                                .next()
                                .unwrap_or("")
                                .trim();
                            if name.is_empty() {
                                None
                            } else {
                                Some(name.to_string())
                            }
                        })
                        .collect()
                })
                .unwrap_or_default();

            Some(SnippetDeclaration {
                name: name.as_str().to_string(),
                line: line_for_offset(template, name.start()),
                params,
            })
        })
        .collect()
}

fn extract_destructured_names(bindings: &str) -> Vec<String> {
    bindings
        .split(',')
        .filter_map(|part| {
            let mut raw = part.trim();
            if raw.is_empty() {
                return None;
            }
            raw = raw.trim_start_matches("...");
            let raw = raw.split('=').next().unwrap_or(raw).trim();
            let name = raw.rsplit(':').next().unwrap_or(raw).trim();
            let name = name.trim_start_matches("...");
            if is_identifier(name) {
                Some(name.to_string())
            } else {
                None
            }
        })
        .collect()
}

fn is_identifier(name: &str) -> bool {
    let mut chars = name.chars();
    matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_' || c == '$')
        && chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
}

fn line_for_offset(content: &str, offset: usize) -> usize {
    content[..offset].bytes().filter(|b| *b == b'\n').count() + 1
}

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

    #[test]
    fn test_vue_script_extraction_basic() {
        let vue_content = r#"
<script>
const message = 'Hello'
export const greeting = message + ' World'
</script>

<template>
  <div>{{ greeting }}</div>
</template>
        "#;

        let extracted = extract_vue_script(vue_content);
        assert!(extracted.contains("const message = 'Hello'"));
        assert!(extracted.contains("export const greeting"));
        assert!(!extracted.contains("<template>"));
    }

    #[test]
    fn test_svelte_template_extraction() {
        let content = r#"
<script>
    let count = 0;
</script>

<button on:click={() => count++}>
    {count}
</button>

<style>
    button { color: red; }
</style>
        "#;

        let template = extract_svelte_template(content);
        assert!(!template.contains("let count = 0"));
        assert!(!template.contains("button { color: red; }"));
        assert!(template.contains("on:click"));
        assert!(template.contains("{count}"));
    }

    #[test]
    fn test_vue_template_extraction() {
        let content = r#"
<script>
    const count = 0;
</script>

<template>
    <button @click="increment">
        {{ count }}
    </button>
</template>

<style scoped>
    button { color: red; }
</style>
        "#;

        let template = extract_vue_template(content);
        assert!(!template.contains("const count = 0"));
        assert!(!template.contains("button { color: red; }"));
        assert!(template.contains("@click"));
        assert!(template.contains("{{ count }}"));
    }

    #[test]
    fn test_astro_frontmatter_extraction() {
        let content = r#"---
import Card from "../components/Card.astro";
export interface Props { title: string; }
const { title } = Astro.props;
---
<Card title={title} />
"#;

        let frontmatter = extract_astro_frontmatter(content);
        assert!(frontmatter.contains("import Card"));
        assert!(frontmatter.contains("export interface Props"));
        assert!(!frontmatter.contains("<Card"));
    }

    #[test]
    fn test_astro_no_frontmatter_is_empty() {
        let content = "<html><body>No frontmatter</body></html>";

        assert_eq!(extract_astro_frontmatter(content), "");
        assert_eq!(extract_astro_template(content), content);
    }

    #[test]
    fn test_astro_scripts_and_styles_extraction() {
        let content = r#"---
const title = "Demo";
---
<script>
import boot from "../boot";
boot();
</script>
<style>
.card { color: red; }
</style>
"#;

        assert!(extract_astro_scripts(content).contains("import boot"));
        assert!(extract_astro_styles(content).contains(".card"));
    }

    #[test]
    fn test_svelte5_rune_extraction() {
        let script = r#"
let count = $state(0);
const doubled = $derived(count * 2);
let raw = $state.raw([]);
let snapshot = $state.snapshot(count);
let computed = $derived.by(() => count + 1);
let name = $bindable("Ada");
$effect(() => console.log(count));
$inspect(count);
$host();
"#;

        let runes = extract_svelte5_runes(script);
        assert!(
            runes
                .iter()
                .any(|r| r.name == "count" && r.kind == RuneKind::State)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "doubled" && r.kind == RuneKind::Derived)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "raw" && r.kind == RuneKind::State)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "snapshot" && r.kind == RuneKind::State)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "computed" && r.kind == RuneKind::Derived)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "name" && r.kind == RuneKind::Bindable)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "$effect" && r.kind == RuneKind::Effect)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "$inspect" && r.kind == RuneKind::Inspect)
        );
        assert!(
            runes
                .iter()
                .any(|r| r.name == "$host" && r.kind == RuneKind::Host)
        );
    }

    #[test]
    fn test_svelte5_props_destructuring_extraction() {
        let script = r#"
let { title, count = 0, value: alias, ...rest } = $props();
"#;

        let names: Vec<_> = extract_svelte5_runes(script)
            .into_iter()
            .map(|r| r.name)
            .collect();
        assert!(names.contains(&"title".to_string()));
        assert!(names.contains(&"count".to_string()));
        assert!(names.contains(&"alias".to_string()));
        assert!(names.contains(&"rest".to_string()));
    }

    #[test]
    fn test_svelte_snippet_extraction() {
        let template = r#"
{#snippet row(item: Item, index)}
  <li>{index}: {item.name}</li>
{/snippet}
"#;

        let snippets = extract_svelte_snippets(template);
        assert_eq!(snippets.len(), 1);
        assert_eq!(snippets[0].name, "row");
        assert_eq!(snippets[0].params, vec!["item", "index"]);
    }
}