sem-core 0.9.0

Entity-level semantic diff engine. Extracts functions, classes, and methods from 28 languages via tree-sitter and diffs at the entity level.
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
use regex::Regex;
use std::collections::HashMap;

use crate::model::entity::{build_entity_id, build_entity_id_disambiguated, SemanticEntity};
use crate::parser::plugin::SemanticParserPlugin;
use crate::utils::hash::content_hash;

pub struct LatexParserPlugin;

const SIGNIFICANT_ENVIRONMENTS: &[&str] = &[
    "theorem",
    "lemma",
    "corollary",
    "proposition",
    "definition",
    "proof",
    "example",
    "remark",
    "figure",
    "table",
    "listing",
    "algorithm",
    "abstract",
    "appendix",
];

/// Map LaTeX sectioning commands to hierarchy levels.
fn section_level(cmd: &str) -> Option<usize> {
    match cmd {
        "part" => Some(0),
        "chapter" => Some(1),
        "section" => Some(2),
        "subsection" => Some(3),
        "subsubsection" => Some(4),
        "paragraph" => Some(5),
        _ => None,
    }
}

/// Extract content inside balanced braces starting at byte position `pos` (the `{`).
/// Uses char iteration to handle UTF-8 correctly.
fn extract_braced(s: &str, pos: usize) -> Option<String> {
    let substr = &s[pos..];
    let mut chars = substr.chars();

    if chars.next() != Some('{') {
        return None;
    }

    let mut depth = 1i32;
    let mut result = String::new();

    for ch in chars {
        match ch {
            '{' => {
                depth += 1;
                result.push(ch);
            }
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(result);
                }
                result.push(ch);
            }
            _ => result.push(ch),
        }
    }
    None
}

/// A line is a comment if the first non-whitespace character is `%`.
fn is_comment_line(line: &str) -> bool {
    line.trim_start().starts_with('%')
}

impl SemanticParserPlugin for LatexParserPlugin {
    fn id(&self) -> &str {
        "latex"
    }

    fn extensions(&self) -> &[&str] {
        &[".tex", ".latex", ".cls", ".sty"]
    }

    fn extract_entities(&self, content: &str, file_path: &str) -> Vec<SemanticEntity> {
        let mut entities = Vec::new();
        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return entities;
        }

        let section_re = Regex::new(
            r"\\(part|chapter|section|subsection|subsubsection|paragraph)\*?\{",
        )
        .unwrap();
        let begin_env_re = Regex::new(r"\\begin\{(\w+)\}").unwrap();
        let end_env_re = Regex::new(r"\\end\{(\w+)\}").unwrap();
        let label_re = Regex::new(r"\\label\{([^}]+)\}").unwrap();
        let cmd_def_re =
            Regex::new(r"\\(newcommand|renewcommand|DeclareMathOperator)\*?\{?\\(\w+)").unwrap();

        // --- Locate \begin{document} and \end{document} ---
        let mut doc_start: Option<usize> = None;
        let mut doc_end: Option<usize> = None;
        for (i, &line) in lines.iter().enumerate() {
            if !is_comment_line(line) {
                if doc_start.is_none() && line.contains(r"\begin{document}") {
                    doc_start = Some(i);
                } else if doc_start.is_some()
                    && doc_end.is_none()
                    && line.contains(r"\end{document}")
                {
                    doc_end = Some(i);
                }
            }
        }

        let body_start = doc_start.map_or(0, |s| s + 1);
        let body_end = doc_end.unwrap_or(lines.len());

        // --- Preamble ---
        // For files with \begin{document}: everything before it is preamble.
        // For .sty/.cls without \begin{document}: entire file is preamble.
        let preamble_range: Option<(usize, usize)> = if let Some(ds) = doc_start {
            if ds > 0 {
                Some((0, ds))
            } else {
                None
            }
        } else if file_path.ends_with(".sty") || file_path.ends_with(".cls") {
            Some((0, lines.len()))
        } else {
            None
        };

        if let Some((p_start, p_end)) = preamble_range {
            let preamble_content = lines[p_start..p_end].join("\n").trim().to_string();
            if !preamble_content.is_empty() {
                let pid = build_entity_id(file_path, "preamble", "(preamble)", None);
                entities.push(SemanticEntity {
                    id: pid.clone(),
                    file_path: file_path.to_string(),
                    entity_type: "preamble".to_string(),
                    name: "(preamble)".to_string(),
                    parent_id: None,
                    content_hash: content_hash(&preamble_content),
                    structural_hash: None,
                    content: preamble_content,
                    start_line: p_start + 1,
                    end_line: p_end,
                    metadata: None,
                });

                // Extract command definitions from preamble
                let preamble_lines = &lines[p_start..p_end];
                let mut i = 0;
                while i < preamble_lines.len() {
                    let line = preamble_lines[i];
                    if !is_comment_line(line) {
                        if let Some(caps) = cmd_def_re.captures(line) {
                            let cmd_name = format!("\\{}", &caps[2]);
                            let def_start = i;
                            let mut def_end = i;

                            // Count braces to find multi-line definitions
                            let mut depth: i32 = 0;
                            for j in i..preamble_lines.len() {
                                for ch in preamble_lines[j].chars() {
                                    if ch == '{' {
                                        depth += 1;
                                    } else if ch == '}' {
                                        depth -= 1;
                                    }
                                }
                                def_end = j;
                                if depth <= 0 {
                                    break;
                                }
                            }

                            let def_content = preamble_lines[def_start..=def_end]
                                .join("\n")
                                .trim()
                                .to_string();
                            let cmd_id = build_entity_id(
                                file_path,
                                "command_definition",
                                &cmd_name,
                                Some(&pid),
                            );

                            entities.push(SemanticEntity {
                                id: cmd_id,
                                file_path: file_path.to_string(),
                                entity_type: "command_definition".to_string(),
                                name: cmd_name,
                                parent_id: Some(pid.clone()),
                                content_hash: content_hash(&def_content),
                                structural_hash: None,
                                content: def_content,
                                start_line: p_start + def_start + 1,
                                end_line: p_start + def_end + 1,
                                metadata: None,
                            });

                            i = def_end + 1;
                            continue;
                        }
                    }
                    i += 1;
                }

            }
        }

        // If entire file was treated as preamble (.sty/.cls), skip body parsing
        if preamble_range.map_or(false, |(_, end)| end == lines.len()) {
            return entities;
        }

        // --- Body: Pass 1 – Parse sections (like markdown headings) ---
        struct Section {
            level: usize,
            name: String,
            start_line: usize, // 1-based
            lines: Vec<String>,
            base_id: String,
            parent_index: Option<usize>,
        }

        let mut sections: Vec<Section> = Vec::new();
        let mut current_section: Option<usize> = None;
        let mut section_stack: Vec<(usize, usize)> = Vec::new(); // (level, section index)

        for i in body_start..body_end {
            let line = lines[i];
            let line_num = i + 1; // 1-based

            if is_comment_line(line) {
                if let Some(idx) = current_section {
                    sections[idx].lines.push(line.to_string());
                }
                continue;
            }

            if let Some(m) = section_re.find(line) {
                if let Some(caps) = section_re.captures(line) {
                    let cmd = &caps[1];
                    if let Some(level) = section_level(cmd) {
                        // Use brace-counting to extract title (handles nested braces)
                        let brace_pos = m.end() - 1; // byte offset of the `{`
                        let name = extract_braced(line, brace_pos)
                            .unwrap_or_else(|| cmd.to_string());

                        // Pop stack until we find an ancestor with a strictly lower level
                        while section_stack.last().map_or(false, |(l, _)| *l >= level) {
                            section_stack.pop();
                        }
                        let parent_index = section_stack.last().map(|(_, idx)| *idx);

                        sections.push(Section {
                            level,
                            name: name.clone(),
                            start_line: line_num,
                            lines: vec![line.to_string()],
                            base_id: build_entity_id(file_path, "section", &name, None),
                            parent_index,
                        });
                        let section_index = sections.len() - 1;
                        current_section = Some(section_index);
                        section_stack.push((level, section_index));
                        continue;
                    }
                }
            }

            // Regular line: append to current section
            if let Some(idx) = current_section {
                sections[idx].lines.push(line.to_string());
            }
        }

        // Disambiguate section IDs
        let mut id_counts: HashMap<&str, usize> = HashMap::new();
        for section in &sections {
            *id_counts.entry(section.base_id.as_str()).or_default() += 1;
        }

        let section_ids: Vec<String> = sections
            .iter()
            .map(|section| {
                if id_counts[section.base_id.as_str()] > 1 {
                    build_entity_id_disambiguated(
                        file_path,
                        "section",
                        &section.name,
                        None,
                        section.start_line,
                    )
                } else {
                    section.base_id.clone()
                }
            })
            .collect();

        // Store section ranges for environment parent lookup
        let section_ranges: Vec<(usize, usize, usize)> = sections
            .iter()
            .map(|s| (s.start_line, s.start_line + s.lines.len() - 1, s.level))
            .collect();

        for (index, section) in sections.iter().enumerate() {
            let section_content = section.lines.join("\n").trim().to_string();
            if section_content.is_empty() {
                continue;
            }

            entities.push(SemanticEntity {
                id: section_ids[index].clone(),
                file_path: file_path.to_string(),
                entity_type: "section".to_string(),
                name: section.name.clone(),
                parent_id: section
                    .parent_index
                    .map(|pi| section_ids[pi].clone()),
                content_hash: content_hash(&section_content),
                structural_hash: None,
                content: section_content,
                start_line: section.start_line,
                end_line: section.start_line + section.lines.len() - 1,
                metadata: None,
            });
        }

        // --- Body: Pass 2 – Parse significant environments ---
        struct EnvInfo {
            env_type: String,
            name: String,
            start_line: usize, // 1-based
            end_line: usize,   // 1-based
            content: String,
            base_id: String,
        }

        let mut env_entities: Vec<EnvInfo> = Vec::new();
        // Stack: (env_type, start_line_1based, accumulated_lines)
        let mut env_stack: Vec<(String, usize, Vec<String>)> = Vec::new();

        for i in body_start..body_end {
            let line = lines[i];
            let line_num = i + 1;

            if is_comment_line(line) {
                if let Some((_, _, ref mut env_lines)) = env_stack.last_mut() {
                    env_lines.push(line.to_string());
                }
                continue;
            }

            // Check for \begin{env}
            if let Some(caps) = begin_env_re.captures(line) {
                let env_name = caps[1].to_string();
                if env_name != "document"
                    && SIGNIFICANT_ENVIRONMENTS.contains(&env_name.as_str())
                {
                    env_stack.push((env_name, line_num, vec![line.to_string()]));
                    continue;
                }
            }

            // Check for \end{env}
            if let Some(caps) = end_env_re.captures(line) {
                let env_name = caps[1].to_string();
                if let Some(pos) =
                    env_stack.iter().rposition(|(name, _, _)| *name == env_name)
                {
                    let (env_type, start_line, mut env_lines) = env_stack.remove(pos);
                    env_lines.push(line.to_string());

                    // Try to find a \label inside the environment
                    let label = env_lines
                        .iter()
                        .find_map(|l| label_re.captures(l).map(|c| c[1].to_string()));

                    let name = label.unwrap_or_else(|| env_type.clone());
                    let env_content = env_lines.join("\n").trim().to_string();

                    env_entities.push(EnvInfo {
                        env_type,
                        name: name.clone(),
                        start_line,
                        end_line: line_num,
                        content: env_content,
                        base_id: build_entity_id(file_path, "environment", &name, None),
                    });
                    continue;
                }
            }

            // Accumulate lines inside open environments
            if let Some((_, _, ref mut env_lines)) = env_stack.last_mut() {
                env_lines.push(line.to_string());
            }
        }

        // Disambiguate environment IDs
        let mut env_id_counts: HashMap<&str, usize> = HashMap::new();
        for env in &env_entities {
            *env_id_counts.entry(env.base_id.as_str()).or_default() += 1;
        }

        let env_ids: Vec<String> = env_entities
            .iter()
            .map(|env| {
                if env_id_counts[env.base_id.as_str()] > 1 {
                    build_entity_id_disambiguated(
                        file_path,
                        "environment",
                        &env.name,
                        None,
                        env.start_line,
                    )
                } else {
                    env.base_id.clone()
                }
            })
            .collect();

        for (index, env) in env_entities.iter().enumerate() {
            // Find the deepest (highest-level number) section containing this environment
            let parent_id = find_parent_section_id(
                env.start_line,
                &section_ranges,
                &section_ids,
            );

            let mut metadata = HashMap::new();
            metadata.insert("environment_type".to_string(), env.env_type.clone());

            entities.push(SemanticEntity {
                id: env_ids[index].clone(),
                file_path: file_path.to_string(),
                entity_type: "environment".to_string(),
                name: env.name.clone(),
                parent_id,
                content_hash: content_hash(&env.content),
                structural_hash: None,
                content: env.content.clone(),
                start_line: env.start_line,
                end_line: env.end_line,
                metadata: Some(metadata),
            });
        }

        entities
    }
}

/// Find the ID of the deepest section that contains the given line.
fn find_parent_section_id(
    line: usize,
    section_ranges: &[(usize, usize, usize)], // (start, end, level)
    section_ids: &[String],
) -> Option<String> {
    let mut best: Option<(usize, usize)> = None; // (index, level)
    for (i, &(start, end, level)) in section_ranges.iter().enumerate() {
        if start <= line && line <= end {
            if best.map_or(true, |(_, best_level)| level > best_level) {
                best = Some((i, level));
            }
        }
    }
    best.map(|(idx, _)| section_ids[idx].clone())
}

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

    fn extract(content: &str) -> Vec<SemanticEntity> {
        let plugin = LatexParserPlugin;
        plugin.extract_entities(content, "paper.tex")
    }

    #[test]
    fn basic_section_hierarchy() {
        let content = r"\begin{document}
\section{Introduction}
Some intro text.
\subsection{Background}
Background material.
\section{Methods}
Method details.
\end{document}
";
        let entities = extract(content);

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();

        assert_eq!(sections.len(), 3);
        assert_eq!(sections[0].name, "Introduction");
        assert_eq!(sections[0].parent_id, None);
        assert_eq!(sections[1].name, "Background");
        assert_eq!(
            sections[1].parent_id.as_deref(),
            Some("paper.tex::section::Introduction")
        );
        assert_eq!(sections[2].name, "Methods");
        assert_eq!(sections[2].parent_id, None);
    }

    #[test]
    fn preamble_with_command_definitions() {
        let content = r"\documentclass{article}
\usepackage{amsmath}
\newcommand{\R}{\mathbb{R}}
\renewcommand{\vec}[1]{\mathbf{#1}}
\begin{document}
\section{Body}
Text.
\end{document}
";
        let entities = extract(content);

        let preamble: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "preamble")
            .collect();
        assert_eq!(preamble.len(), 1);
        assert!(preamble[0].content.contains(r"\documentclass{article}"));
        assert!(preamble[0].content.contains(r"\usepackage{amsmath}"));

        let cmds: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "command_definition")
            .collect();
        assert_eq!(cmds.len(), 2);
        assert_eq!(cmds[0].name, r"\R");
        assert_eq!(cmds[1].name, r"\vec");
        assert_eq!(
            cmds[0].parent_id.as_deref(),
            Some("paper.tex::preamble::(preamble)")
        );
    }

    #[test]
    fn environment_with_label() {
        let content = r"\begin{document}
\section{Results}
\begin{theorem}\label{thm:main}
Every even number greater than 2 is the sum of two primes.
\end{theorem}
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 1);
        assert_eq!(envs[0].name, "thm:main");
        assert_eq!(
            envs[0].metadata.as_ref().unwrap().get("environment_type"),
            Some(&"theorem".to_string())
        );
        assert_eq!(
            envs[0].parent_id.as_deref(),
            Some("paper.tex::section::Results")
        );
    }

    #[test]
    fn environment_without_label() {
        let content = r"\begin{document}
\section{Proofs}
\begin{proof}
Trivial.
\end{proof}
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 1);
        // Without a label, the name is the environment type
        assert_eq!(envs[0].name, "proof");
        assert_eq!(envs[0].id, "paper.tex::environment::proof");
    }

    #[test]
    fn starred_sections() {
        let content = r"\begin{document}
\section*{Acknowledgments}
Thanks to everyone.
\end{document}
";
        let entities = extract(content);

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].name, "Acknowledgments");
    }

    #[test]
    fn nested_braces_in_title() {
        let content = r"\begin{document}
\section{The $O(n^{2})$ Algorithm}
Details here.
\end{document}
";
        let entities = extract(content);

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].name, "The $O(n^{2})$ Algorithm");
    }

    #[test]
    fn comments_skipped_for_sections() {
        let content = r"\begin{document}
% \section{Commented Out}
\section{Real Section}
Content here.
\end{document}
";
        let entities = extract(content);

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].name, "Real Section");
    }

    #[test]
    fn empty_document_only_preamble() {
        let content = r"\documentclass{article}
\usepackage{amsmath}
\newcommand{\N}{\mathbb{N}}
\begin{document}
\end{document}
";
        let entities = extract(content);

        let preamble: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "preamble")
            .collect();
        assert_eq!(preamble.len(), 1);

        let cmds: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "command_definition")
            .collect();
        assert_eq!(cmds.len(), 1);
        assert_eq!(cmds[0].name, r"\N");

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();
        assert_eq!(sections.len(), 0);
    }

    #[test]
    fn duplicate_section_names_disambiguated() {
        let content = r"\begin{document}
\section{Results}
First results.
\section{Results}
Second results.
\end{document}
";
        let entities = extract(content);

        let sections: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "section")
            .collect();
        assert_eq!(sections.len(), 2);
        assert_eq!(sections[0].id, "paper.tex::section::Results@L2");
        assert_eq!(sections[1].id, "paper.tex::section::Results@L4");
    }

    #[test]
    fn figure_environment() {
        let content = r"\begin{document}
\section{Experiments}
\begin{figure}
\centering
\includegraphics{plot.png}
\caption{Results}
\label{fig:results}
\end{figure}
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 1);
        assert_eq!(envs[0].name, "fig:results");
        assert_eq!(
            envs[0].metadata.as_ref().unwrap().get("environment_type"),
            Some(&"figure".to_string())
        );
    }

    #[test]
    fn nonsignificant_environments_not_extracted() {
        let content = r"\begin{document}
\section{List}
\begin{itemize}
\item One
\item Two
\end{itemize}
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 0);
    }

    #[test]
    fn sty_file_treated_as_preamble() {
        let content = r"\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}
\newcommand{\foo}{bar}
";
        let plugin = LatexParserPlugin;
        let entities = plugin.extract_entities(content, "mypackage.sty");

        let preamble: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "preamble")
            .collect();
        assert_eq!(preamble.len(), 1);

        let cmds: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "command_definition")
            .collect();
        assert_eq!(cmds.len(), 1);
        assert_eq!(cmds[0].name, r"\foo");
    }

    #[test]
    fn multiline_command_definition() {
        let content = r"\newcommand{\mybox}[1]{%
  \fbox{%
    \parbox{0.9\textwidth}{#1}%
  }%
}
\begin{document}
\section{Body}
Text.
\end{document}
";
        let entities = extract(content);

        let cmds: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "command_definition")
            .collect();
        assert_eq!(cmds.len(), 1);
        assert_eq!(cmds[0].name, r"\mybox");
        assert!(cmds[0].content.contains(r"\parbox"));
        assert_eq!(cmds[0].start_line, 1);
        assert_eq!(cmds[0].end_line, 5);
    }

    #[test]
    fn multiple_environments_disambiguated() {
        let content = r"\begin{document}
\section{Theorems}
\begin{theorem}
First theorem.
\end{theorem}
\begin{theorem}
Second theorem.
\end{theorem}
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 2);
        // Both have name "theorem" (no labels), so they get disambiguated
        assert_eq!(envs[0].id, "paper.tex::environment::theorem@L3");
        assert_eq!(envs[1].id, "paper.tex::environment::theorem@L6");
    }

    #[test]
    fn abstract_before_sections() {
        let content = r"\begin{document}
\begin{abstract}
This paper presents results.
\end{abstract}
\section{Introduction}
Intro text.
\end{document}
";
        let entities = extract(content);

        let envs: Vec<&SemanticEntity> = entities
            .iter()
            .filter(|e| e.entity_type == "environment")
            .collect();
        assert_eq!(envs.len(), 1);
        assert_eq!(envs[0].name, "abstract");
        // abstract is before any section, so no parent
        assert_eq!(envs[0].parent_id, None);
    }
}