diff options
author | Aleksey Kladov <[email protected]> | 2021-01-07 22:39:02 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-01-08 20:47:35 +0000 |
commit | e30c1c3fbf8f70336d985b2b73e5b0f45f3b95f5 (patch) | |
tree | a3cdc2d2f667ab5a122758152eb338a654d387cd /crates/ide/src/syntax_highlighting | |
parent | 981a0d708ec352969f9ca075a3e0e50c6da48197 (diff) |
Simplify highlighting infra
This also fixes the killer whale bug
Diffstat (limited to 'crates/ide/src/syntax_highlighting')
-rw-r--r-- | crates/ide/src/syntax_highlighting/format.rs | 12 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/highlights.rs | 109 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/html.rs | 18 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/injection.rs | 85 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/injector.rs | 83 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tags.rs | 5 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html | 31 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 5 |
8 files changed, 260 insertions, 88 deletions
diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index 26416022b..ab66b406c 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs | |||
@@ -4,9 +4,9 @@ use syntax::{ | |||
4 | AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, | 4 | AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{HighlightTag, HighlightedRange, SymbolKind}; |
8 | syntax_highlighting::HighlightedRangeStack, HighlightTag, HighlightedRange, SymbolKind, | 8 | |
9 | }; | 9 | use super::highlights::Highlights; |
10 | 10 | ||
11 | #[derive(Default)] | 11 | #[derive(Default)] |
12 | pub(super) struct FormatStringHighlighter { | 12 | pub(super) struct FormatStringHighlighter { |
@@ -39,22 +39,20 @@ impl FormatStringHighlighter { | |||
39 | } | 39 | } |
40 | pub(super) fn highlight_format_string( | 40 | pub(super) fn highlight_format_string( |
41 | &self, | 41 | &self, |
42 | range_stack: &mut HighlightedRangeStack, | 42 | stack: &mut Highlights, |
43 | string: &impl HasFormatSpecifier, | 43 | string: &impl HasFormatSpecifier, |
44 | range: TextRange, | 44 | range: TextRange, |
45 | ) { | 45 | ) { |
46 | if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) { | 46 | if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) { |
47 | range_stack.push(); | ||
48 | string.lex_format_specifier(|piece_range, kind| { | 47 | string.lex_format_specifier(|piece_range, kind| { |
49 | if let Some(highlight) = highlight_format_specifier(kind) { | 48 | if let Some(highlight) = highlight_format_specifier(kind) { |
50 | range_stack.add(HighlightedRange { | 49 | stack.add(HighlightedRange { |
51 | range: piece_range + range.start(), | 50 | range: piece_range + range.start(), |
52 | highlight: highlight.into(), | 51 | highlight: highlight.into(), |
53 | binding_hash: None, | 52 | binding_hash: None, |
54 | }); | 53 | }); |
55 | } | 54 | } |
56 | }); | 55 | }); |
57 | range_stack.pop(); | ||
58 | } | 56 | } |
59 | } | 57 | } |
60 | } | 58 | } |
diff --git a/crates/ide/src/syntax_highlighting/highlights.rs b/crates/ide/src/syntax_highlighting/highlights.rs new file mode 100644 index 000000000..3e733c87c --- /dev/null +++ b/crates/ide/src/syntax_highlighting/highlights.rs | |||
@@ -0,0 +1,109 @@ | |||
1 | //! Collects a tree of highlighted ranges and flattens it. | ||
2 | use std::{cmp::Ordering, iter}; | ||
3 | |||
4 | use stdx::equal_range_by; | ||
5 | use syntax::TextRange; | ||
6 | |||
7 | use crate::{HighlightTag, HighlightedRange}; | ||
8 | |||
9 | pub(super) struct Highlights { | ||
10 | root: Node, | ||
11 | } | ||
12 | |||
13 | struct Node { | ||
14 | highlighted_range: HighlightedRange, | ||
15 | nested: Vec<Node>, | ||
16 | } | ||
17 | |||
18 | impl Highlights { | ||
19 | pub(super) fn new(range: TextRange) -> Highlights { | ||
20 | Highlights { | ||
21 | root: Node::new(HighlightedRange { | ||
22 | range, | ||
23 | highlight: HighlightTag::Dummy.into(), | ||
24 | binding_hash: None, | ||
25 | }), | ||
26 | } | ||
27 | } | ||
28 | |||
29 | pub(super) fn add(&mut self, highlighted_range: HighlightedRange) { | ||
30 | self.root.add(highlighted_range); | ||
31 | } | ||
32 | |||
33 | pub(super) fn to_vec(self) -> Vec<HighlightedRange> { | ||
34 | let mut res = Vec::new(); | ||
35 | self.root.flatten(&mut res); | ||
36 | res | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl Node { | ||
41 | fn new(highlighted_range: HighlightedRange) -> Node { | ||
42 | Node { highlighted_range, nested: Vec::new() } | ||
43 | } | ||
44 | |||
45 | fn add(&mut self, highlighted_range: HighlightedRange) { | ||
46 | assert!(self.highlighted_range.range.contains_range(highlighted_range.range)); | ||
47 | |||
48 | // Fast path | ||
49 | if let Some(last) = self.nested.last_mut() { | ||
50 | if last.highlighted_range.range.contains_range(highlighted_range.range) { | ||
51 | return last.add(highlighted_range); | ||
52 | } | ||
53 | if last.highlighted_range.range.end() <= highlighted_range.range.start() { | ||
54 | return self.nested.push(Node::new(highlighted_range)); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | let (start, len) = equal_range_by(&self.nested, |n| { | ||
59 | ordering(n.highlighted_range.range, highlighted_range.range) | ||
60 | }); | ||
61 | |||
62 | if len == 1 | ||
63 | && self.nested[start].highlighted_range.range.contains_range(highlighted_range.range) | ||
64 | { | ||
65 | return self.nested[start].add(highlighted_range); | ||
66 | } | ||
67 | |||
68 | let nested = self | ||
69 | .nested | ||
70 | .splice(start..start + len, iter::once(Node::new(highlighted_range))) | ||
71 | .collect::<Vec<_>>(); | ||
72 | self.nested[start].nested = nested; | ||
73 | } | ||
74 | |||
75 | fn flatten(&self, acc: &mut Vec<HighlightedRange>) { | ||
76 | let mut start = self.highlighted_range.range.start(); | ||
77 | let mut nested = self.nested.iter(); | ||
78 | loop { | ||
79 | let next = nested.next(); | ||
80 | let end = next.map_or(self.highlighted_range.range.end(), |it| { | ||
81 | it.highlighted_range.range.start() | ||
82 | }); | ||
83 | if start < end { | ||
84 | acc.push(HighlightedRange { | ||
85 | range: TextRange::new(start, end), | ||
86 | highlight: self.highlighted_range.highlight, | ||
87 | binding_hash: self.highlighted_range.binding_hash, | ||
88 | }); | ||
89 | } | ||
90 | start = match next { | ||
91 | Some(child) => { | ||
92 | child.flatten(acc); | ||
93 | child.highlighted_range.range.end() | ||
94 | } | ||
95 | None => break, | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | pub(super) fn ordering(r1: TextRange, r2: TextRange) -> Ordering { | ||
102 | if r1.end() <= r2.start() { | ||
103 | Ordering::Less | ||
104 | } else if r2.end() <= r1.start() { | ||
105 | Ordering::Greater | ||
106 | } else { | ||
107 | Ordering::Equal | ||
108 | } | ||
109 | } | ||
diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs index 99ba3a59d..44f611b25 100644 --- a/crates/ide/src/syntax_highlighting/html.rs +++ b/crates/ide/src/syntax_highlighting/html.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | use ide_db::base_db::SourceDatabase; | 3 | use ide_db::base_db::SourceDatabase; |
4 | use oorandom::Rand32; | 4 | use oorandom::Rand32; |
5 | use stdx::format_to; | 5 | use stdx::format_to; |
6 | use syntax::{AstNode, TextRange, TextSize}; | 6 | use syntax::AstNode; |
7 | 7 | ||
8 | use crate::{syntax_highlighting::highlight, FileId, RootDatabase}; | 8 | use crate::{syntax_highlighting::highlight, FileId, RootDatabase}; |
9 | 9 | ||
@@ -22,17 +22,15 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
22 | 22 | ||
23 | let ranges = highlight(db, file_id, None, false); | 23 | let ranges = highlight(db, file_id, None, false); |
24 | let text = parse.tree().syntax().to_string(); | 24 | let text = parse.tree().syntax().to_string(); |
25 | let mut prev_pos = TextSize::from(0); | ||
26 | let mut buf = String::new(); | 25 | let mut buf = String::new(); |
27 | buf.push_str(&STYLE); | 26 | buf.push_str(&STYLE); |
28 | buf.push_str("<pre><code>"); | 27 | buf.push_str("<pre><code>"); |
29 | for range in &ranges { | 28 | for range in &ranges { |
30 | if range.range.start() > prev_pos { | 29 | let curr = &text[range.range]; |
31 | let curr = &text[TextRange::new(prev_pos, range.range.start())]; | 30 | if range.highlight.is_empty() { |
32 | let text = html_escape(curr); | 31 | format_to!(buf, "{}", html_escape(curr)); |
33 | buf.push_str(&text); | 32 | continue; |
34 | } | 33 | } |
35 | let curr = &text[TextRange::new(range.range.start(), range.range.end())]; | ||
36 | 34 | ||
37 | let class = range.highlight.to_string().replace('.', " "); | 35 | let class = range.highlight.to_string().replace('.', " "); |
38 | let color = match (rainbow, range.binding_hash) { | 36 | let color = match (rainbow, range.binding_hash) { |
@@ -42,13 +40,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
42 | _ => "".into(), | 40 | _ => "".into(), |
43 | }; | 41 | }; |
44 | format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, html_escape(curr)); | 42 | format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, html_escape(curr)); |
45 | |||
46 | prev_pos = range.range.end(); | ||
47 | } | 43 | } |
48 | // Add the remaining (non-highlighted) text | ||
49 | let curr = &text[TextRange::new(prev_pos, TextSize::of(&text))]; | ||
50 | let text = html_escape(curr); | ||
51 | buf.push_str(&text); | ||
52 | buf.push_str("</code></pre>"); | 44 | buf.push_str("</code></pre>"); |
53 | buf | 45 | buf |
54 | } | 46 | } |
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index d6be9708d..98ee03e0d 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs | |||
@@ -1,18 +1,18 @@ | |||
1 | //! Syntax highlighting injections such as highlighting of documentation tests. | 1 | //! Syntax highlighting injections such as highlighting of documentation tests. |
2 | 2 | ||
3 | use std::{collections::BTreeMap, convert::TryFrom}; | 3 | use std::convert::TryFrom; |
4 | 4 | ||
5 | use hir::Semantics; | 5 | use hir::Semantics; |
6 | use ide_db::call_info::ActiveParameter; | 6 | use ide_db::call_info::ActiveParameter; |
7 | use itertools::Itertools; | 7 | use itertools::Itertools; |
8 | use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; | 8 | use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; |
9 | 9 | ||
10 | use crate::{Analysis, Highlight, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase}; | 10 | use crate::{Analysis, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase}; |
11 | 11 | ||
12 | use super::HighlightedRangeStack; | 12 | use super::{highlights::Highlights, injector::Injector}; |
13 | 13 | ||
14 | pub(super) fn highlight_injection( | 14 | pub(super) fn highlight_injection( |
15 | acc: &mut HighlightedRangeStack, | 15 | acc: &mut Highlights, |
16 | sema: &Semantics<RootDatabase>, | 16 | sema: &Semantics<RootDatabase>, |
17 | literal: ast::String, | 17 | literal: ast::String, |
18 | expanded: SyntaxToken, | 18 | expanded: SyntaxToken, |
@@ -98,9 +98,6 @@ impl MarkerInfo { | |||
98 | } | 98 | } |
99 | } | 99 | } |
100 | 100 | ||
101 | /// Mapping from extracted documentation code to original code | ||
102 | type RangesMap = BTreeMap<TextSize, TextSize>; | ||
103 | |||
104 | const RUSTDOC_FENCE: &'static str = "```"; | 101 | const RUSTDOC_FENCE: &'static str = "```"; |
105 | const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ | 102 | const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ |
106 | "", | 103 | "", |
@@ -119,20 +116,20 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ | |||
119 | /// Lastly, a vector of new comment highlight ranges (spanning only the | 116 | /// Lastly, a vector of new comment highlight ranges (spanning only the |
120 | /// comment prefix) is returned which is used in the syntax highlighting | 117 | /// comment prefix) is returned which is used in the syntax highlighting |
121 | /// injection to replace the previous (line-spanning) comment ranges. | 118 | /// injection to replace the previous (line-spanning) comment ranges. |
122 | pub(super) fn extract_doc_comments( | 119 | pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec<HighlightedRange>, Injector)> { |
123 | node: &SyntaxNode, | 120 | let mut inj = Injector::default(); |
124 | ) -> Option<(String, RangesMap, Vec<HighlightedRange>)> { | ||
125 | // wrap the doctest into function body to get correct syntax highlighting | 121 | // wrap the doctest into function body to get correct syntax highlighting |
126 | let prefix = "fn doctest() {\n"; | 122 | let prefix = "fn doctest() {\n"; |
127 | let suffix = "}\n"; | 123 | let suffix = "}\n"; |
128 | // Mapping from extracted documentation code to original code | 124 | |
129 | let mut range_mapping: RangesMap = BTreeMap::new(); | 125 | let mut line_start = TextSize::of(prefix); |
130 | let mut line_start = TextSize::try_from(prefix.len()).unwrap(); | ||
131 | let mut is_codeblock = false; | 126 | let mut is_codeblock = false; |
132 | let mut is_doctest = false; | 127 | let mut is_doctest = false; |
133 | // Replace the original, line-spanning comment ranges by new, only comment-prefix | 128 | // Replace the original, line-spanning comment ranges by new, only comment-prefix |
134 | // spanning comment ranges. | 129 | // spanning comment ranges. |
135 | let mut new_comments = Vec::new(); | 130 | let mut new_comments = Vec::new(); |
131 | |||
132 | inj.add_unmapped(prefix); | ||
136 | let doctest = node | 133 | let doctest = node |
137 | .children_with_tokens() | 134 | .children_with_tokens() |
138 | .filter_map(|el| el.into_token().and_then(ast::Comment::cast)) | 135 | .filter_map(|el| el.into_token().and_then(ast::Comment::cast)) |
@@ -169,7 +166,6 @@ pub(super) fn extract_doc_comments( | |||
169 | pos | 166 | pos |
170 | }; | 167 | }; |
171 | 168 | ||
172 | range_mapping.insert(line_start, range.start() + TextSize::try_from(pos).unwrap()); | ||
173 | new_comments.push(HighlightedRange { | 169 | new_comments.push(HighlightedRange { |
174 | range: TextRange::new( | 170 | range: TextRange::new( |
175 | range.start(), | 171 | range.start(), |
@@ -179,62 +175,43 @@ pub(super) fn extract_doc_comments( | |||
179 | binding_hash: None, | 175 | binding_hash: None, |
180 | }); | 176 | }); |
181 | line_start += range.len() - TextSize::try_from(pos).unwrap(); | 177 | line_start += range.len() - TextSize::try_from(pos).unwrap(); |
182 | line_start += TextSize::try_from('\n'.len_utf8()).unwrap(); | 178 | line_start += TextSize::of("\n"); |
183 | 179 | ||
180 | inj.add( | ||
181 | &line[pos..], | ||
182 | TextRange::new(range.start() + TextSize::try_from(pos).unwrap(), range.end()), | ||
183 | ); | ||
184 | inj.add_unmapped("\n"); | ||
184 | line[pos..].to_owned() | 185 | line[pos..].to_owned() |
185 | }) | 186 | }) |
186 | .join("\n"); | 187 | .join("\n"); |
188 | inj.add_unmapped(suffix); | ||
187 | 189 | ||
188 | if doctest.is_empty() { | 190 | if doctest.is_empty() { |
189 | return None; | 191 | return None; |
190 | } | 192 | } |
191 | 193 | ||
192 | let doctest = format!("{}{}{}", prefix, doctest, suffix); | 194 | Some((new_comments, inj)) |
193 | Some((doctest, range_mapping, new_comments)) | ||
194 | } | 195 | } |
195 | 196 | ||
196 | /// Injection of syntax highlighting of doctests. | 197 | /// Injection of syntax highlighting of doctests. |
197 | pub(super) fn highlight_doc_comment( | 198 | pub(super) fn highlight_doc_comment( |
198 | text: String, | ||
199 | range_mapping: RangesMap, | ||
200 | new_comments: Vec<HighlightedRange>, | 199 | new_comments: Vec<HighlightedRange>, |
201 | stack: &mut HighlightedRangeStack, | 200 | inj: Injector, |
201 | stack: &mut Highlights, | ||
202 | ) { | 202 | ) { |
203 | let (analysis, tmp_file_id) = Analysis::from_single_file(text); | 203 | let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string()); |
204 | |||
205 | stack.push(); | ||
206 | for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { | ||
207 | // Determine start offset and end offset in case of multi-line ranges | ||
208 | let mut start_offset = None; | ||
209 | let mut end_offset = None; | ||
210 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { | ||
211 | // It's possible for orig_line_start - line_start to be negative. Add h.range.start() | ||
212 | // here and remove it from the end range after the loop below so that the values are | ||
213 | // always non-negative. | ||
214 | let offset = h.range.start() + orig_line_start - line_start; | ||
215 | if line_start <= &h.range.start() { | ||
216 | start_offset.get_or_insert(offset); | ||
217 | break; | ||
218 | } else { | ||
219 | end_offset.get_or_insert(offset); | ||
220 | } | ||
221 | } | ||
222 | if let Some(start_offset) = start_offset { | ||
223 | h.range = TextRange::new( | ||
224 | start_offset, | ||
225 | h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), | ||
226 | ); | ||
227 | |||
228 | h.highlight |= HighlightModifier::Injected; | ||
229 | stack.add(h); | ||
230 | } | ||
231 | } | ||
232 | |||
233 | // Inject the comment prefix highlight ranges | ||
234 | stack.push(); | ||
235 | for comment in new_comments { | 204 | for comment in new_comments { |
236 | stack.add(comment); | 205 | stack.add(comment); |
237 | } | 206 | } |
238 | stack.pop_and_inject(None); | 207 | |
239 | stack.pop_and_inject(Some(Highlight::from(HighlightTag::Dummy) | HighlightModifier::Injected)); | 208 | for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { |
209 | for r in inj.map_range_up(h.range) { | ||
210 | stack.add(HighlightedRange { | ||
211 | range: r, | ||
212 | highlight: h.highlight | HighlightModifier::Injected, | ||
213 | binding_hash: h.binding_hash, | ||
214 | }); | ||
215 | } | ||
216 | } | ||
240 | } | 217 | } |
diff --git a/crates/ide/src/syntax_highlighting/injector.rs b/crates/ide/src/syntax_highlighting/injector.rs new file mode 100644 index 000000000..0513a9fd6 --- /dev/null +++ b/crates/ide/src/syntax_highlighting/injector.rs | |||
@@ -0,0 +1,83 @@ | |||
1 | //! Extracts a subsequence of a text document, remembering the mapping of ranges | ||
2 | //! between original and extracted texts. | ||
3 | use std::ops::{self, Sub}; | ||
4 | |||
5 | use stdx::equal_range_by; | ||
6 | use syntax::{TextRange, TextSize}; | ||
7 | |||
8 | use super::highlights::ordering; | ||
9 | |||
10 | #[derive(Default)] | ||
11 | pub(super) struct Injector { | ||
12 | buf: String, | ||
13 | ranges: Vec<(TextRange, Option<Delta<TextSize>>)>, | ||
14 | } | ||
15 | |||
16 | impl Injector { | ||
17 | pub(super) fn add(&mut self, text: &str, source_range: TextRange) { | ||
18 | let len = TextSize::of(text); | ||
19 | assert_eq!(len, source_range.len()); | ||
20 | |||
21 | let target_range = TextRange::at(TextSize::of(&self.buf), len); | ||
22 | self.ranges | ||
23 | .push((target_range, Some(Delta::new(target_range.start(), source_range.start())))); | ||
24 | self.buf.push_str(text); | ||
25 | } | ||
26 | pub(super) fn add_unmapped(&mut self, text: &str) { | ||
27 | let len = TextSize::of(text); | ||
28 | |||
29 | let target_range = TextRange::at(TextSize::of(&self.buf), len); | ||
30 | self.ranges.push((target_range, None)); | ||
31 | self.buf.push_str(text); | ||
32 | } | ||
33 | |||
34 | pub(super) fn text(&self) -> &str { | ||
35 | &self.buf | ||
36 | } | ||
37 | pub(super) fn map_range_up(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { | ||
38 | let (start, len) = equal_range_by(&self.ranges, |&(r, _)| ordering(r, range)); | ||
39 | (start..start + len).filter_map(move |i| { | ||
40 | let (target_range, delta) = self.ranges[i]; | ||
41 | let intersection = target_range.intersect(range).unwrap(); | ||
42 | Some(intersection + delta?) | ||
43 | }) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | #[derive(Clone, Copy)] | ||
48 | enum Delta<T> { | ||
49 | Add(T), | ||
50 | Sub(T), | ||
51 | } | ||
52 | |||
53 | impl<T> Delta<T> { | ||
54 | fn new(from: T, to: T) -> Delta<T> | ||
55 | where | ||
56 | T: Ord + Sub<Output = T>, | ||
57 | { | ||
58 | if to >= from { | ||
59 | Delta::Add(to - from) | ||
60 | } else { | ||
61 | Delta::Sub(from - to) | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | impl ops::Add<Delta<TextSize>> for TextSize { | ||
67 | type Output = TextSize; | ||
68 | |||
69 | fn add(self, rhs: Delta<TextSize>) -> TextSize { | ||
70 | match rhs { | ||
71 | Delta::Add(it) => self + it, | ||
72 | Delta::Sub(it) => self - it, | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | impl ops::Add<Delta<TextSize>> for TextRange { | ||
78 | type Output = TextRange; | ||
79 | |||
80 | fn add(self, rhs: Delta<TextSize>) -> TextRange { | ||
81 | TextRange::at(self.start() + rhs, self.len()) | ||
82 | } | ||
83 | } | ||
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 8b8867079..a0286b72d 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs | |||
@@ -94,13 +94,13 @@ impl HighlightTag { | |||
94 | HighlightTag::Comment => "comment", | 94 | HighlightTag::Comment => "comment", |
95 | HighlightTag::EscapeSequence => "escape_sequence", | 95 | HighlightTag::EscapeSequence => "escape_sequence", |
96 | HighlightTag::FormatSpecifier => "format_specifier", | 96 | HighlightTag::FormatSpecifier => "format_specifier", |
97 | HighlightTag::Dummy => "dummy", | ||
98 | HighlightTag::Keyword => "keyword", | 97 | HighlightTag::Keyword => "keyword", |
99 | HighlightTag::Punctuation => "punctuation", | 98 | HighlightTag::Punctuation => "punctuation", |
100 | HighlightTag::NumericLiteral => "numeric_literal", | 99 | HighlightTag::NumericLiteral => "numeric_literal", |
101 | HighlightTag::Operator => "operator", | 100 | HighlightTag::Operator => "operator", |
102 | HighlightTag::StringLiteral => "string_literal", | 101 | HighlightTag::StringLiteral => "string_literal", |
103 | HighlightTag::UnresolvedReference => "unresolved_reference", | 102 | HighlightTag::UnresolvedReference => "unresolved_reference", |
103 | HighlightTag::Dummy => "dummy", | ||
104 | } | 104 | } |
105 | } | 105 | } |
106 | } | 106 | } |
@@ -173,6 +173,9 @@ impl Highlight { | |||
173 | pub(crate) fn new(tag: HighlightTag) -> Highlight { | 173 | pub(crate) fn new(tag: HighlightTag) -> Highlight { |
174 | Highlight { tag, modifiers: HighlightModifiers::default() } | 174 | Highlight { tag, modifiers: HighlightModifiers::default() } |
175 | } | 175 | } |
176 | pub fn is_empty(&self) -> bool { | ||
177 | self.tag == HighlightTag::Dummy && self.modifiers == HighlightModifiers::default() | ||
178 | } | ||
176 | } | 179 | } |
177 | 180 | ||
178 | impl ops::BitOr<HighlightModifier> for HighlightTag { | 181 | impl ops::BitOr<HighlightModifier> for HighlightTag { |
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 4dd7413ba..9d42b11c1 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html | |||
@@ -37,13 +37,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
37 | .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } | 37 | .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } |
38 | </style> | 38 | </style> |
39 | <pre><code><span class="comment documentation">/// ```</span> | 39 | <pre><code><span class="comment documentation">/// ```</span> |
40 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="punctuation injected">_</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="punctuation injected">;</span><span class="punctuation injected"> | 40 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="punctuation injected">_</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="punctuation injected">;</span> |
41 | </span><span class="comment documentation">/// ```</span> | 41 | <span class="comment documentation">/// ```</span> |
42 | <span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="punctuation">{</span> | 42 | <span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="punctuation">{</span> |
43 | <span class="field declaration">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span><span class="punctuation">,</span> | 43 | <span class="field declaration">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span><span class="punctuation">,</span> |
44 | <span class="punctuation">}</span> | 44 | <span class="punctuation">}</span> |
45 | 45 | ||
46 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> | 46 | <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> |
47 | <span class="comment documentation">/// ```</span> | ||
48 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="punctuation injected">_</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"Call me</span> | ||
49 | <span class="comment">// KILLER WHALE</span> | ||
50 | <span class="comment documentation">/// </span><span class="string_literal injected"> Ishmael."</span><span class="punctuation injected">;</span> | ||
51 | <span class="comment documentation">/// ```</span> | ||
47 | <span class="keyword">pub</span> <span class="keyword">const</span> <span class="constant declaration associated">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span> <span class="operator">=</span> <span class="bool_literal">true</span><span class="punctuation">;</span> | 52 | <span class="keyword">pub</span> <span class="keyword">const</span> <span class="constant declaration associated">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span> <span class="operator">=</span> <span class="bool_literal">true</span><span class="punctuation">;</span> |
48 | 53 | ||
49 | <span class="comment documentation">/// Constructs a new `Foo`.</span> | 54 | <span class="comment documentation">/// Constructs a new `Foo`.</span> |
@@ -52,8 +57,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
52 | <span class="comment documentation">///</span> | 57 | <span class="comment documentation">///</span> |
53 | <span class="comment documentation">/// ```</span> | 58 | <span class="comment documentation">/// ```</span> |
54 | <span class="comment documentation">/// #</span><span class="dummy injected"> </span><span class="attribute attribute injected">#</span><span class="attribute attribute injected">!</span><span class="attribute attribute injected">[</span><span class="function attribute injected">allow</span><span class="punctuation attribute injected">(</span><span class="attribute attribute injected">unused_mut</span><span class="punctuation attribute injected">)</span><span class="attribute attribute injected">]</span> | 59 | <span class="comment documentation">/// #</span><span class="dummy injected"> </span><span class="attribute attribute injected">#</span><span class="attribute attribute injected">!</span><span class="attribute attribute injected">[</span><span class="function attribute injected">allow</span><span class="punctuation attribute injected">(</span><span class="attribute attribute injected">unused_mut</span><span class="punctuation attribute injected">)</span><span class="attribute attribute injected">]</span> |
55 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="keyword injected">mut</span><span class="dummy injected"> </span><span class="variable declaration injected mutable">foo</span><span class="punctuation injected">:</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span><span class="punctuation injected"> | 60 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="keyword injected">mut</span><span class="dummy injected"> </span><span class="variable declaration injected mutable">foo</span><span class="punctuation injected">:</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> |
56 | </span> <span class="comment documentation">/// ```</span> | 61 | <span class="comment documentation">/// ```</span> |
57 | <span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration static associated">new</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="struct">Foo</span> <span class="punctuation">{</span> | 62 | <span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration static associated">new</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-></span> <span class="struct">Foo</span> <span class="punctuation">{</span> |
58 | <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">bar</span><span class="punctuation">:</span> <span class="bool_literal">true</span> <span class="punctuation">}</span> | 63 | <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">bar</span><span class="punctuation">:</span> <span class="bool_literal">true</span> <span class="punctuation">}</span> |
59 | <span class="punctuation">}</span> | 64 | <span class="punctuation">}</span> |
@@ -72,18 +77,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
72 | <span class="comment documentation">///</span> | 77 | <span class="comment documentation">///</span> |
73 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">bar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="variable injected">foo</span><span class="operator injected">.</span><span class="field injected">bar</span><span class="dummy injected"> </span><span class="operator injected">||</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="constant injected">bar</span><span class="punctuation injected">;</span> | 78 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">bar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="variable injected">foo</span><span class="operator injected">.</span><span class="field injected">bar</span><span class="dummy injected"> </span><span class="operator injected">||</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="constant injected">bar</span><span class="punctuation injected">;</span> |
74 | <span class="comment documentation">///</span> | 79 | <span class="comment documentation">///</span> |
75 | <span class="comment documentation">/// </span><span class="comment injected">/* multi-line | 80 | <span class="comment documentation">/// </span><span class="comment injected">/* multi-line</span> |
76 | </span><span class="comment documentation">/// </span><span class="comment injected"> comment */</span> | 81 | <span class="comment documentation">/// </span><span class="comment injected"> comment */</span> |
77 | <span class="comment documentation">///</span> | 82 | <span class="comment documentation">///</span> |
78 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"Foo | 83 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"Foo</span> |
79 | </span><span class="comment documentation">/// </span><span class="string_literal injected"> bar | 84 | <span class="comment documentation">/// </span><span class="string_literal injected"> bar</span> |
80 | </span><span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="punctuation injected">;</span> | 85 | <span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="punctuation injected">;</span> |
81 | <span class="comment documentation">///</span> | 86 | <span class="comment documentation">///</span> |
82 | <span class="comment documentation">/// ```</span> | 87 | <span class="comment documentation">/// ```</span> |
83 | <span class="comment documentation">///</span> | 88 | <span class="comment documentation">///</span> |
84 | <span class="comment documentation">/// ```rust,no_run</span> | 89 | <span class="comment documentation">/// ```rust,no_run</span> |
85 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">foobar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span><span class="punctuation injected"> | 90 | <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">foobar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> |
86 | </span> <span class="comment documentation">/// ```</span> | 91 | <span class="comment documentation">/// ```</span> |
87 | <span class="comment documentation">///</span> | 92 | <span class="comment documentation">///</span> |
88 | <span class="comment documentation">/// ```sh</span> | 93 | <span class="comment documentation">/// ```sh</span> |
89 | <span class="comment documentation">/// echo 1</span> | 94 | <span class="comment documentation">/// echo 1</span> |
@@ -94,8 +99,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
94 | <span class="punctuation">}</span> | 99 | <span class="punctuation">}</span> |
95 | 100 | ||
96 | <span class="comment documentation">/// ```</span> | 101 | <span class="comment documentation">/// ```</span> |
97 | <span class="comment documentation">/// </span><span class="macro injected">noop!</span><span class="punctuation injected">(</span><span class="numeric_literal injected">1</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span><span class="punctuation injected"> | 102 | <span class="comment documentation">/// </span><span class="macro injected">noop!</span><span class="punctuation injected">(</span><span class="numeric_literal injected">1</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> |
98 | </span><span class="comment documentation">/// ```</span> | 103 | <span class="comment documentation">/// ```</span> |
99 | <span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span> | 104 | <span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span> |
100 | <span class="punctuation">(</span><span class="punctuation">$</span>expr<span class="punctuation">:</span>expr<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">></span> <span class="punctuation">{</span> | 105 | <span class="punctuation">(</span><span class="punctuation">$</span>expr<span class="punctuation">:</span>expr<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">></span> <span class="punctuation">{</span> |
101 | <span class="punctuation">$</span>expr | 106 | <span class="punctuation">$</span>expr |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 9e1a3974c..a62704c39 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -446,6 +446,11 @@ struct Foo { | |||
446 | } | 446 | } |
447 | 447 | ||
448 | impl Foo { | 448 | impl Foo { |
449 | /// ``` | ||
450 | /// let _ = "Call me | ||
451 | // KILLER WHALE | ||
452 | /// Ishmael."; | ||
453 | /// ``` | ||
449 | pub const bar: bool = true; | 454 | pub const bar: bool = true; |
450 | 455 | ||
451 | /// Constructs a new `Foo`. | 456 | /// Constructs a new `Foo`. |