code_moniker_core/lang/
document.rs1use std::ops::Range;
2
3use tree_sitter::Tree;
4
5pub struct ParsedDocument {
10 primary: Tree,
11 injections: Vec<SyntaxInjection>,
12}
13
14impl ParsedDocument {
15 pub fn new(primary: Tree) -> Self {
16 Self {
17 primary,
18 injections: Vec::new(),
19 }
20 }
21
22 pub fn with_injections(primary: Tree, injections: Vec<SyntaxInjection>) -> Self {
23 Self {
24 primary,
25 injections,
26 }
27 }
28
29 pub fn primary(&self) -> &Tree {
30 &self.primary
31 }
32
33 pub fn injections(&self) -> &[SyntaxInjection] {
34 &self.injections
35 }
36
37 pub fn injection_for_host(&self, host_byte_range: Range<usize>) -> Option<&SyntaxInjection> {
38 self.injections
39 .iter()
40 .find(|injection| injection.host_byte_range == host_byte_range)
41 }
42
43 pub fn injection_within(&self, container_byte_range: Range<usize>) -> Option<&SyntaxInjection> {
44 self.injections.iter().find(|injection| {
45 injection.host_byte_range.start >= container_byte_range.start
46 && injection.host_byte_range.end <= container_byte_range.end
47 })
48 }
49}
50
51pub struct SyntaxInjection {
52 language: &'static str,
53 host_byte_range: Range<usize>,
54 content_byte_range: Range<usize>,
55 tree: Tree,
56}
57
58impl SyntaxInjection {
59 pub fn new(
60 language: &'static str,
61 host_byte_range: Range<usize>,
62 content_byte_range: Range<usize>,
63 tree: Tree,
64 ) -> Self {
65 Self {
66 language,
67 host_byte_range,
68 content_byte_range,
69 tree,
70 }
71 }
72
73 pub fn language(&self) -> &'static str {
74 self.language
75 }
76
77 pub fn host_byte_range(&self) -> Range<usize> {
78 self.host_byte_range.clone()
79 }
80
81 pub fn content_byte_range(&self) -> Range<usize> {
82 self.content_byte_range.clone()
83 }
84
85 pub fn tree(&self) -> &Tree {
86 &self.tree
87 }
88}