aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_highlighting/injection.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/syntax_highlighting/injection.rs')
-rw-r--r--crates/ide/src/syntax_highlighting/injection.rs56
1 files changed, 52 insertions, 4 deletions
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
index 6cbd683c6..d6be9708d 100644
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ b/crates/ide/src/syntax_highlighting/injection.rs
@@ -22,7 +22,8 @@ pub(super) fn highlight_injection(
22 return None; 22 return None;
23 } 23 }
24 let value = literal.value()?; 24 let value = literal.value()?;
25 let (analysis, tmp_file_id) = Analysis::from_single_file(value.into_owned()); 25 let marker_info = MarkerInfo::new(&*value);
26 let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());
26 27
27 if let Some(range) = literal.open_quote_text_range() { 28 if let Some(range) = literal.open_quote_text_range() {
28 acc.add(HighlightedRange { 29 acc.add(HighlightedRange {
@@ -33,9 +34,10 @@ pub(super) fn highlight_injection(
33 } 34 }
34 35
35 for mut h in analysis.highlight(tmp_file_id).unwrap() { 36 for mut h in analysis.highlight(tmp_file_id).unwrap() {
36 if let Some(r) = literal.map_range_up(h.range) { 37 let range = marker_info.map_range_up(h.range);
37 h.range = r; 38 if let Some(range) = literal.map_range_up(range) {
38 acc.add(h) 39 h.range = range;
40 acc.add(h);
39 } 41 }
40 } 42 }
41 43
@@ -50,6 +52,52 @@ pub(super) fn highlight_injection(
50 Some(()) 52 Some(())
51} 53}
52 54
55/// Data to remove `$0` from string and map ranges
56#[derive(Default, Debug)]
57struct MarkerInfo {
58 cleaned_text: String,
59 markers: Vec<TextRange>,
60}
61
62impl MarkerInfo {
63 fn new(mut text: &str) -> Self {
64 let marker = "$0";
65
66 let mut res = MarkerInfo::default();
67 let mut offset: TextSize = 0.into();
68 while !text.is_empty() {
69 let idx = text.find(marker).unwrap_or(text.len());
70 let (chunk, next) = text.split_at(idx);
71 text = next;
72 res.cleaned_text.push_str(chunk);
73 offset += TextSize::of(chunk);
74
75 if let Some(next) = text.strip_prefix(marker) {
76 text = next;
77
78 let marker_len = TextSize::of(marker);
79 res.markers.push(TextRange::at(offset, marker_len));
80 offset += marker_len;
81 }
82 }
83 res
84 }
85 fn map_range_up(&self, range: TextRange) -> TextRange {
86 TextRange::new(
87 self.map_offset_up(range.start(), true),
88 self.map_offset_up(range.end(), false),
89 )
90 }
91 fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize {
92 for r in &self.markers {
93 if r.start() < offset || (start && r.start() == offset) {
94 offset += r.len()
95 }
96 }
97 offset
98 }
99}
100
53/// Mapping from extracted documentation code to original code 101/// Mapping from extracted documentation code to original code
54type RangesMap = BTreeMap<TextSize, TextSize>; 102type RangesMap = BTreeMap<TextSize, TextSize>;
55 103