aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_highlighting
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/syntax_highlighting')
-rw-r--r--crates/ide/src/syntax_highlighting/format.rs94
-rw-r--r--crates/ide/src/syntax_highlighting/highlight.rs517
-rw-r--r--crates/ide/src/syntax_highlighting/highlights.rs100
-rw-r--r--crates/ide/src/syntax_highlighting/html.rs28
-rw-r--r--crates/ide/src/syntax_highlighting/inject.rs158
-rw-r--r--crates/ide/src/syntax_highlighting/injection.rs240
-rw-r--r--crates/ide/src/syntax_highlighting/injector.rs81
-rw-r--r--crates/ide/src/syntax_highlighting/macro_rules.rs8
-rw-r--r--crates/ide/src/syntax_highlighting/tags.rs139
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html41
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/injection.html4
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs5
12 files changed, 1012 insertions, 403 deletions
diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs
index 26416022b..a74ca844b 100644
--- a/crates/ide/src/syntax_highlighting/format.rs
+++ b/crates/ide/src/syntax_highlighting/format.rs
@@ -1,65 +1,51 @@
1//! Syntax highlighting for format macro strings. 1//! Syntax highlighting for format macro strings.
2use syntax::{ 2use syntax::{
3 ast::{self, FormatSpecifier, HasFormatSpecifier}, 3 ast::{self, FormatSpecifier, HasFormatSpecifier},
4 AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, 4 AstNode, AstToken, TextRange,
5}; 5};
6 6
7use crate::{ 7use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag, SymbolKind};
8 syntax_highlighting::HighlightedRangeStack, HighlightTag, HighlightedRange, SymbolKind,
9};
10
11#[derive(Default)]
12pub(super) struct FormatStringHighlighter {
13 format_string: Option<SyntaxElement>,
14}
15 8
16impl FormatStringHighlighter { 9pub(super) fn highlight_format_string(
17 pub(super) fn check_for_format_string(&mut self, parent: &SyntaxNode) { 10 stack: &mut Highlights,
18 // Check if macro takes a format string and remember it for highlighting later. 11 string: &ast::String,
19 // The macros that accept a format string expand to a compiler builtin macros 12 range: TextRange,
20 // `format_args` and `format_args_nl`. 13) {
21 if let Some(name) = parent 14 if is_format_string(string).is_none() {
22 .parent() 15 return;
23 .and_then(ast::MacroCall::cast)
24 .and_then(|mc| mc.path())
25 .and_then(|p| p.segment())
26 .and_then(|s| s.name_ref())
27 {
28 match name.text().as_str() {
29 "format_args" | "format_args_nl" => {
30 self.format_string = parent
31 .children_with_tokens()
32 .filter(|t| t.kind() != SyntaxKind::WHITESPACE)
33 .nth(1)
34 .filter(|e| ast::String::can_cast(e.kind()))
35 }
36 _ => {}
37 }
38 }
39 } 16 }
40 pub(super) fn highlight_format_string( 17
41 &self, 18 string.lex_format_specifier(|piece_range, kind| {
42 range_stack: &mut HighlightedRangeStack, 19 if let Some(highlight) = highlight_format_specifier(kind) {
43 string: &impl HasFormatSpecifier, 20 stack.add(HlRange {
44 range: TextRange, 21 range: piece_range + range.start(),
45 ) { 22 highlight: highlight.into(),
46 if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) { 23 binding_hash: None,
47 range_stack.push();
48 string.lex_format_specifier(|piece_range, kind| {
49 if let Some(highlight) = highlight_format_specifier(kind) {
50 range_stack.add(HighlightedRange {
51 range: piece_range + range.start(),
52 highlight: highlight.into(),
53 binding_hash: None,
54 });
55 }
56 }); 24 });
57 range_stack.pop();
58 } 25 }
26 });
27}
28
29fn is_format_string(string: &ast::String) -> Option<()> {
30 let parent = string.syntax().parent();
31
32 let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?;
33 if !matches!(name.text().as_str(), "format_args" | "format_args_nl") {
34 return None;
35 }
36
37 let first_literal = parent
38 .children_with_tokens()
39 .filter_map(|it| it.as_token().cloned().and_then(ast::String::cast))
40 .next()?;
41 if &first_literal != string {
42 return None;
59 } 43 }
44
45 Some(())
60} 46}
61 47
62fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> { 48fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
63 Some(match kind { 49 Some(match kind {
64 FormatSpecifier::Open 50 FormatSpecifier::Open
65 | FormatSpecifier::Close 51 | FormatSpecifier::Close
@@ -71,8 +57,10 @@ fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
71 | FormatSpecifier::DollarSign 57 | FormatSpecifier::DollarSign
72 | FormatSpecifier::Dot 58 | FormatSpecifier::Dot
73 | FormatSpecifier::Asterisk 59 | FormatSpecifier::Asterisk
74 | FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier, 60 | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
75 FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral, 61
76 FormatSpecifier::Identifier => HighlightTag::Symbol(SymbolKind::Local), 62 FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
63
64 FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
77 }) 65 })
78} 66}
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs
new file mode 100644
index 000000000..1a88975d2
--- /dev/null
+++ b/crates/ide/src/syntax_highlighting/highlight.rs
@@ -0,0 +1,517 @@
1//! Computes color for a single element.
2
3use hir::{AsAssocItem, Semantics, VariantDef};
4use ide_db::{
5 defs::{Definition, NameClass, NameRefClass},
6 RootDatabase,
7};
8use rustc_hash::FxHashMap;
9use syntax::{
10 ast, AstNode, AstToken, NodeOrToken, SyntaxElement,
11 SyntaxKind::{self, *},
12 SyntaxNode, SyntaxToken, T,
13};
14
15use crate::{Highlight, HlMod, HlTag, SymbolKind};
16
17pub(super) fn element(
18 sema: &Semantics<RootDatabase>,
19 bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
20 syntactic_name_ref_highlighting: bool,
21 element: SyntaxElement,
22) -> Option<(Highlight, Option<u64>)> {
23 let db = sema.db;
24 let mut binding_hash = None;
25 let highlight: Highlight = match element.kind() {
26 FN => {
27 bindings_shadow_count.clear();
28 return None;
29 }
30
31 // Highlight definitions depending on the "type" of the definition.
32 NAME => {
33 let name = element.into_node().and_then(ast::Name::cast).unwrap();
34 let name_kind = NameClass::classify(sema, &name);
35
36 if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
37 if let Some(name) = local.name(db) {
38 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
39 *shadow_count += 1;
40 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
41 }
42 };
43
44 match name_kind {
45 Some(NameClass::ExternCrate(_)) => HlTag::Symbol(SymbolKind::Module).into(),
46 Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
47 Some(NameClass::ConstReference(def)) => highlight_def(db, def),
48 Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
49 let mut h = HlTag::Symbol(SymbolKind::Field).into();
50 if let Definition::Field(field) = field_ref {
51 if let VariantDef::Union(_) = field.parent_def(db) {
52 h |= HlMod::Unsafe;
53 }
54 }
55
56 h
57 }
58 None => highlight_name_by_syntax(name) | HlMod::Definition,
59 }
60 }
61
62 // Highlight references like the definitions they resolve to
63 NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
64 // even though we track whether we are in an attribute or not we still need this special case
65 // as otherwise we would emit unresolved references for name refs inside attributes
66 Highlight::from(HlTag::Symbol(SymbolKind::Function))
67 }
68 NAME_REF => {
69 let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
70 highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| {
71 match NameRefClass::classify(sema, &name_ref) {
72 Some(name_kind) => match name_kind {
73 NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(),
74 NameRefClass::Definition(def) => {
75 if let Definition::Local(local) = &def {
76 if let Some(name) = local.name(db) {
77 let shadow_count =
78 bindings_shadow_count.entry(name.clone()).or_default();
79 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
80 }
81 };
82
83 let mut h = highlight_def(db, def);
84
85 if let Definition::Local(local) = &def {
86 if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) {
87 h |= HlMod::Consuming;
88 }
89 }
90
91 if let Some(parent) = name_ref.syntax().parent() {
92 if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
93 if let Definition::Field(field) = def {
94 if let VariantDef::Union(_) = field.parent_def(db) {
95 h |= HlMod::Unsafe;
96 }
97 }
98 }
99 }
100
101 h
102 }
103 NameRefClass::FieldShorthand { .. } => {
104 HlTag::Symbol(SymbolKind::Field).into()
105 }
106 },
107 None if syntactic_name_ref_highlighting => {
108 highlight_name_ref_by_syntax(name_ref, sema)
109 }
110 None => HlTag::UnresolvedReference.into(),
111 }
112 })
113 }
114
115 // Simple token-based highlighting
116 COMMENT => {
117 let comment = element.into_token().and_then(ast::Comment::cast)?;
118 let h = HlTag::Comment;
119 match comment.kind().doc {
120 Some(_) => h | HlMod::Documentation,
121 None => h.into(),
122 }
123 }
124 STRING | BYTE_STRING => HlTag::StringLiteral.into(),
125 ATTR => HlTag::Attribute.into(),
126 INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
127 BYTE => HlTag::ByteLiteral.into(),
128 CHAR => HlTag::CharLiteral.into(),
129 QUESTION => Highlight::new(HlTag::Operator) | HlMod::ControlFlow,
130 LIFETIME => {
131 let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
132
133 match NameClass::classify_lifetime(sema, &lifetime) {
134 Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
135 None => match NameRefClass::classify_lifetime(sema, &lifetime) {
136 Some(NameRefClass::Definition(def)) => highlight_def(db, def),
137 _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)),
138 },
139 _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) | HlMod::Definition,
140 }
141 }
142 p if p.is_punct() => match p {
143 T![&] => {
144 let h = HlTag::Operator.into();
145 let is_unsafe = element
146 .parent()
147 .and_then(ast::RefExpr::cast)
148 .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr))
149 .unwrap_or(false);
150 if is_unsafe {
151 h | HlMod::Unsafe
152 } else {
153 h
154 }
155 }
156 T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlTag::Operator.into(),
157 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
158 HlTag::Symbol(SymbolKind::Macro).into()
159 }
160 T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
161 HlTag::BuiltinType.into()
162 }
163 T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
164 HlTag::Keyword.into()
165 }
166 T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
167 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
168
169 let expr = prefix_expr.expr()?;
170 let ty = sema.type_of_expr(&expr)?;
171 if ty.is_raw_ptr() {
172 HlTag::Operator | HlMod::Unsafe
173 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
174 HlTag::Operator.into()
175 } else {
176 HlTag::Punctuation.into()
177 }
178 }
179 T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
180 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
181
182 let expr = prefix_expr.expr()?;
183 match expr {
184 ast::Expr::Literal(_) => HlTag::NumericLiteral,
185 _ => HlTag::Operator,
186 }
187 .into()
188 }
189 _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
190 HlTag::Operator.into()
191 }
192 _ if element.parent().and_then(ast::BinExpr::cast).is_some() => HlTag::Operator.into(),
193 _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
194 HlTag::Operator.into()
195 }
196 _ if element.parent().and_then(ast::RangePat::cast).is_some() => HlTag::Operator.into(),
197 _ if element.parent().and_then(ast::RestPat::cast).is_some() => HlTag::Operator.into(),
198 _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
199 _ => HlTag::Punctuation.into(),
200 },
201
202 k if k.is_keyword() => {
203 let h = Highlight::new(HlTag::Keyword);
204 match k {
205 T![break]
206 | T![continue]
207 | T![else]
208 | T![if]
209 | T![loop]
210 | T![match]
211 | T![return]
212 | T![while]
213 | T![in] => h | HlMod::ControlFlow,
214 T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow,
215 T![unsafe] => h | HlMod::Unsafe,
216 T![true] | T![false] => HlTag::BoolLiteral.into(),
217 T![self] => {
218 let self_param_is_mut = element
219 .parent()
220 .and_then(ast::SelfParam::cast)
221 .and_then(|p| p.mut_token())
222 .is_some();
223 let self_path = &element
224 .parent()
225 .as_ref()
226 .and_then(SyntaxNode::parent)
227 .and_then(ast::Path::cast)
228 .and_then(|p| sema.resolve_path(&p));
229 let mut h = HlTag::Symbol(SymbolKind::SelfParam).into();
230 if self_param_is_mut
231 || matches!(self_path,
232 Some(hir::PathResolution::Local(local))
233 if local.is_self(db)
234 && (local.is_mut(db) || local.ty(db).is_mutable_reference())
235 )
236 {
237 h |= HlMod::Mutable
238 }
239
240 if let Some(hir::PathResolution::Local(local)) = self_path {
241 if is_consumed_lvalue(element, &local, db) {
242 h |= HlMod::Consuming;
243 }
244 }
245
246 h
247 }
248 T![ref] => element
249 .parent()
250 .and_then(ast::IdentPat::cast)
251 .and_then(|ident_pat| {
252 if sema.is_unsafe_ident_pat(&ident_pat) {
253 Some(HlMod::Unsafe)
254 } else {
255 None
256 }
257 })
258 .map(|modifier| h | modifier)
259 .unwrap_or(h),
260 _ => h,
261 }
262 }
263
264 _ => return None,
265 };
266
267 return Some((highlight, binding_hash));
268
269 fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
270 fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
271 use std::{collections::hash_map::DefaultHasher, hash::Hasher};
272
273 let mut hasher = DefaultHasher::new();
274 x.hash(&mut hasher);
275 hasher.finish()
276 }
277
278 hash((name, shadow_count))
279 }
280}
281
282fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
283 match def {
284 Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro),
285 Definition::Field(_) => HlTag::Symbol(SymbolKind::Field),
286 Definition::ModuleDef(def) => match def {
287 hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module),
288 hir::ModuleDef::Function(func) => {
289 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
290 if func.as_assoc_item(db).is_some() {
291 h |= HlMod::Associated;
292 if func.self_param(db).is_none() {
293 h |= HlMod::Static
294 }
295 }
296 if func.is_unsafe(db) {
297 h |= HlMod::Unsafe;
298 }
299 return h;
300 }
301 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct),
302 hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HlTag::Symbol(SymbolKind::Enum),
303 hir::ModuleDef::Adt(hir::Adt::Union(_)) => HlTag::Symbol(SymbolKind::Union),
304 hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant),
305 hir::ModuleDef::Const(konst) => {
306 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
307 if konst.as_assoc_item(db).is_some() {
308 h |= HlMod::Associated
309 }
310 return h;
311 }
312 hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait),
313 hir::ModuleDef::TypeAlias(type_) => {
314 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
315 if type_.as_assoc_item(db).is_some() {
316 h |= HlMod::Associated
317 }
318 return h;
319 }
320 hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType,
321 hir::ModuleDef::Static(s) => {
322 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
323 if s.is_mut(db) {
324 h |= HlMod::Mutable;
325 h |= HlMod::Unsafe;
326 }
327 return h;
328 }
329 },
330 Definition::SelfType(_) => HlTag::Symbol(SymbolKind::Impl),
331 Definition::TypeParam(_) => HlTag::Symbol(SymbolKind::TypeParam),
332 Definition::ConstParam(_) => HlTag::Symbol(SymbolKind::ConstParam),
333 Definition::Local(local) => {
334 let tag = if local.is_param(db) {
335 HlTag::Symbol(SymbolKind::ValueParam)
336 } else {
337 HlTag::Symbol(SymbolKind::Local)
338 };
339 let mut h = Highlight::new(tag);
340 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
341 h |= HlMod::Mutable;
342 }
343 if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) {
344 h |= HlMod::Callable;
345 }
346 return h;
347 }
348 Definition::LifetimeParam(_) => HlTag::Symbol(SymbolKind::LifetimeParam),
349 Definition::Label(_) => HlTag::Symbol(SymbolKind::Label),
350 }
351 .into()
352}
353
354fn highlight_func_by_name_ref(
355 sema: &Semantics<RootDatabase>,
356 name_ref: &ast::NameRef,
357) -> Option<Highlight> {
358 let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
359 highlight_method_call(sema, &mc)
360}
361
362fn highlight_method_call(
363 sema: &Semantics<RootDatabase>,
364 method_call: &ast::MethodCallExpr,
365) -> Option<Highlight> {
366 let func = sema.resolve_method_call(&method_call)?;
367 let mut h = HlTag::Symbol(SymbolKind::Function).into();
368 h |= HlMod::Associated;
369 if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
370 h |= HlMod::Unsafe;
371 }
372 if let Some(self_param) = func.self_param(sema.db) {
373 match self_param.access(sema.db) {
374 hir::Access::Shared => (),
375 hir::Access::Exclusive => h |= HlMod::Mutable,
376 hir::Access::Owned => {
377 if let Some(receiver_ty) =
378 method_call.receiver().and_then(|it| sema.type_of_expr(&it))
379 {
380 if !receiver_ty.is_copy(sema.db) {
381 h |= HlMod::Consuming
382 }
383 }
384 }
385 }
386 }
387 Some(h)
388}
389
390fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
391 let default = HlTag::UnresolvedReference;
392
393 let parent = match name.syntax().parent() {
394 Some(it) => it,
395 _ => return default.into(),
396 };
397
398 let tag = match parent.kind() {
399 STRUCT => HlTag::Symbol(SymbolKind::Struct),
400 ENUM => HlTag::Symbol(SymbolKind::Enum),
401 VARIANT => HlTag::Symbol(SymbolKind::Variant),
402 UNION => HlTag::Symbol(SymbolKind::Union),
403 TRAIT => HlTag::Symbol(SymbolKind::Trait),
404 TYPE_ALIAS => HlTag::Symbol(SymbolKind::TypeAlias),
405 TYPE_PARAM => HlTag::Symbol(SymbolKind::TypeParam),
406 RECORD_FIELD => HlTag::Symbol(SymbolKind::Field),
407 MODULE => HlTag::Symbol(SymbolKind::Module),
408 FN => HlTag::Symbol(SymbolKind::Function),
409 CONST => HlTag::Symbol(SymbolKind::Const),
410 STATIC => HlTag::Symbol(SymbolKind::Static),
411 IDENT_PAT => HlTag::Symbol(SymbolKind::Local),
412 _ => default,
413 };
414
415 tag.into()
416}
417
418fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight {
419 let default = HlTag::UnresolvedReference;
420
421 let parent = match name.syntax().parent() {
422 Some(it) => it,
423 _ => return default.into(),
424 };
425
426 match parent.kind() {
427 METHOD_CALL_EXPR => {
428 return ast::MethodCallExpr::cast(parent)
429 .and_then(|it| highlight_method_call(sema, &it))
430 .unwrap_or_else(|| HlTag::Symbol(SymbolKind::Function).into());
431 }
432 FIELD_EXPR => {
433 let h = HlTag::Symbol(SymbolKind::Field);
434 let is_union = ast::FieldExpr::cast(parent)
435 .and_then(|field_expr| {
436 let field = sema.resolve_field(&field_expr)?;
437 Some(if let VariantDef::Union(_) = field.parent_def(sema.db) {
438 true
439 } else {
440 false
441 })
442 })
443 .unwrap_or(false);
444 if is_union {
445 h | HlMod::Unsafe
446 } else {
447 h.into()
448 }
449 }
450 PATH_SEGMENT => {
451 let path = match parent.parent().and_then(ast::Path::cast) {
452 Some(it) => it,
453 _ => return default.into(),
454 };
455 let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
456 Some(it) => it,
457 _ => {
458 // within path, decide whether it is module or adt by checking for uppercase name
459 return if name.text().chars().next().unwrap_or_default().is_uppercase() {
460 HlTag::Symbol(SymbolKind::Struct)
461 } else {
462 HlTag::Symbol(SymbolKind::Module)
463 }
464 .into();
465 }
466 };
467 let parent = match expr.syntax().parent() {
468 Some(it) => it,
469 None => return default.into(),
470 };
471
472 match parent.kind() {
473 CALL_EXPR => HlTag::Symbol(SymbolKind::Function).into(),
474 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
475 HlTag::Symbol(SymbolKind::Struct)
476 } else {
477 HlTag::Symbol(SymbolKind::Const)
478 }
479 .into(),
480 }
481 }
482 _ => default.into(),
483 }
484}
485
486fn is_consumed_lvalue(
487 node: NodeOrToken<SyntaxNode, SyntaxToken>,
488 local: &hir::Local,
489 db: &RootDatabase,
490) -> bool {
491 // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
492 parents_match(node, &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST]) && !local.ty(db).is_copy(db)
493}
494
495/// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
496fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
497 while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
498 if parent.kind() != *kind {
499 return false;
500 }
501
502 // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
503 // in the same pattern is unstable: rust-lang/rust#68354.
504 node = node.parent().unwrap().into();
505 kinds = rest;
506 }
507
508 // Only true if we matched all expected kinds
509 kinds.len() == 0
510}
511
512fn is_child_of_impl(element: &SyntaxElement) -> bool {
513 match element.parent() {
514 Some(e) => e.kind() == IMPL,
515 _ => false,
516 }
517}
diff --git a/crates/ide/src/syntax_highlighting/highlights.rs b/crates/ide/src/syntax_highlighting/highlights.rs
new file mode 100644
index 000000000..11c11ed28
--- /dev/null
+++ b/crates/ide/src/syntax_highlighting/highlights.rs
@@ -0,0 +1,100 @@
1//! Collects a tree of highlighted ranges and flattens it.
2use std::{cmp::Ordering, iter};
3
4use stdx::equal_range_by;
5use syntax::TextRange;
6
7use crate::{HlRange, HlTag};
8
9pub(super) struct Highlights {
10 root: Node,
11}
12
13struct Node {
14 hl_range: HlRange,
15 nested: Vec<Node>,
16}
17
18impl Highlights {
19 pub(super) fn new(range: TextRange) -> Highlights {
20 Highlights {
21 root: Node::new(HlRange { range, highlight: HlTag::None.into(), binding_hash: None }),
22 }
23 }
24
25 pub(super) fn add(&mut self, hl_range: HlRange) {
26 self.root.add(hl_range);
27 }
28
29 pub(super) fn to_vec(self) -> Vec<HlRange> {
30 let mut res = Vec::new();
31 self.root.flatten(&mut res);
32 res
33 }
34}
35
36impl Node {
37 fn new(hl_range: HlRange) -> Node {
38 Node { hl_range, nested: Vec::new() }
39 }
40
41 fn add(&mut self, hl_range: HlRange) {
42 assert!(self.hl_range.range.contains_range(hl_range.range));
43
44 // Fast path
45 if let Some(last) = self.nested.last_mut() {
46 if last.hl_range.range.contains_range(hl_range.range) {
47 return last.add(hl_range);
48 }
49 if last.hl_range.range.end() <= hl_range.range.start() {
50 return self.nested.push(Node::new(hl_range));
51 }
52 }
53
54 let (start, len) =
55 equal_range_by(&self.nested, |n| ordering(n.hl_range.range, hl_range.range));
56
57 if len == 1 && self.nested[start].hl_range.range.contains_range(hl_range.range) {
58 return self.nested[start].add(hl_range);
59 }
60
61 let nested = self
62 .nested
63 .splice(start..start + len, iter::once(Node::new(hl_range)))
64 .collect::<Vec<_>>();
65 self.nested[start].nested = nested;
66 }
67
68 fn flatten(&self, acc: &mut Vec<HlRange>) {
69 let mut start = self.hl_range.range.start();
70 let mut nested = self.nested.iter();
71 loop {
72 let next = nested.next();
73 let end = next.map_or(self.hl_range.range.end(), |it| it.hl_range.range.start());
74 if start < end {
75 acc.push(HlRange {
76 range: TextRange::new(start, end),
77 highlight: self.hl_range.highlight,
78 binding_hash: self.hl_range.binding_hash,
79 });
80 }
81 start = match next {
82 Some(child) => {
83 child.flatten(acc);
84 child.hl_range.range.end()
85 }
86 None => break,
87 }
88 }
89 }
90}
91
92pub(super) fn ordering(r1: TextRange, r2: TextRange) -> Ordering {
93 if r1.end() <= r2.start() {
94 Ordering::Less
95 } else if r2.end() <= r1.start() {
96 Ordering::Greater
97 } else {
98 Ordering::Equal
99 }
100}
diff --git a/crates/ide/src/syntax_highlighting/html.rs b/crates/ide/src/syntax_highlighting/html.rs
index 99ba3a59d..0ee7bc96e 100644
--- a/crates/ide/src/syntax_highlighting/html.rs
+++ b/crates/ide/src/syntax_highlighting/html.rs
@@ -3,7 +3,7 @@
3use ide_db::base_db::SourceDatabase; 3use ide_db::base_db::SourceDatabase;
4use oorandom::Rand32; 4use oorandom::Rand32;
5use stdx::format_to; 5use stdx::format_to;
6use syntax::{AstNode, TextRange, TextSize}; 6use syntax::AstNode;
7 7
8use crate::{syntax_highlighting::highlight, FileId, RootDatabase}; 8use crate::{syntax_highlighting::highlight, FileId, RootDatabase};
9 9
@@ -20,35 +20,27 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
20 ) 20 )
21 } 21 }
22 22
23 let ranges = highlight(db, file_id, None, false); 23 let hl_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 r in &hl_ranges {
30 if range.range.start() > prev_pos { 29 let chunk = html_escape(&text[r.range]);
31 let curr = &text[TextRange::new(prev_pos, range.range.start())]; 30 if r.highlight.is_empty() {
32 let text = html_escape(curr); 31 format_to!(buf, "{}", chunk);
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 = r.highlight.to_string().replace('.', " ");
38 let color = match (rainbow, range.binding_hash) { 36 let color = match (rainbow, r.binding_hash) {
39 (true, Some(hash)) => { 37 (true, Some(hash)) => {
40 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash)) 38 format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
41 } 39 }
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, chunk);
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/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs
new file mode 100644
index 000000000..281461493
--- /dev/null
+++ b/crates/ide/src/syntax_highlighting/inject.rs
@@ -0,0 +1,158 @@
1//! "Recursive" Syntax highlighting for code in doctests and fixtures.
2
3use hir::Semantics;
4use ide_db::call_info::ActiveParameter;
5use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};
6
7use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase};
8
9use super::{highlights::Highlights, injector::Injector};
10
11pub(super) fn ra_fixture(
12 hl: &mut Highlights,
13 sema: &Semantics<RootDatabase>,
14 literal: ast::String,
15 expanded: SyntaxToken,
16) -> Option<()> {
17 let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
18 if !active_parameter.name.starts_with("ra_fixture") {
19 return None;
20 }
21 let value = literal.value()?;
22
23 if let Some(range) = literal.open_quote_text_range() {
24 hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
25 }
26
27 let mut inj = Injector::default();
28
29 let mut text = &*value;
30 let mut offset: TextSize = 0.into();
31
32 while !text.is_empty() {
33 let marker = "$0";
34 let idx = text.find(marker).unwrap_or(text.len());
35 let (chunk, next) = text.split_at(idx);
36 inj.add(chunk, TextRange::at(offset, TextSize::of(chunk)));
37
38 text = next;
39 offset += TextSize::of(chunk);
40
41 if let Some(next) = text.strip_prefix(marker) {
42 if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) {
43 hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None });
44 }
45
46 text = next;
47
48 let marker_len = TextSize::of(marker);
49 offset += marker_len;
50 }
51 }
52
53 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
54
55 for mut hl_range in analysis.highlight(tmp_file_id).unwrap() {
56 for range in inj.map_range_up(hl_range.range) {
57 if let Some(range) = literal.map_range_up(range) {
58 hl_range.range = range;
59 hl.add(hl_range.clone());
60 }
61 }
62 }
63
64 if let Some(range) = literal.close_quote_text_range() {
65 hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
66 }
67
68 Some(())
69}
70
71const RUSTDOC_FENCE: &'static str = "```";
72const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
73 "",
74 "rust",
75 "should_panic",
76 "ignore",
77 "no_run",
78 "compile_fail",
79 "edition2015",
80 "edition2018",
81 "edition2021",
82];
83
84/// Injection of syntax highlighting of doctests.
85pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) {
86 let doc_comments = node
87 .children_with_tokens()
88 .filter_map(|it| it.into_token().and_then(ast::Comment::cast))
89 .filter(|it| it.kind().doc.is_some());
90
91 if !doc_comments.clone().any(|it| it.text().contains(RUSTDOC_FENCE)) {
92 return;
93 }
94
95 let mut inj = Injector::default();
96 inj.add_unmapped("fn doctest() {\n");
97
98 let mut is_codeblock = false;
99 let mut is_doctest = false;
100
101 // Replace the original, line-spanning comment ranges by new, only comment-prefix
102 // spanning comment ranges.
103 let mut new_comments = Vec::new();
104 for comment in doc_comments {
105 match comment.text().find(RUSTDOC_FENCE) {
106 Some(idx) => {
107 is_codeblock = !is_codeblock;
108 // Check whether code is rust by inspecting fence guards
109 let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..];
110 let is_rust =
111 guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
112 is_doctest = is_codeblock && is_rust;
113 continue;
114 }
115 None if !is_doctest => continue,
116 None => (),
117 }
118
119 let line: &str = comment.text().as_str();
120 let range = comment.syntax().text_range();
121
122 let mut pos = TextSize::of(comment.prefix());
123 // whitespace after comment is ignored
124 if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) {
125 pos += TextSize::of(ws);
126 }
127 // lines marked with `#` should be ignored in output, we skip the `#` char
128 if let Some(ws) = line[pos.into()..].chars().next().filter(|&c| c == '#') {
129 pos += TextSize::of(ws);
130 }
131
132 new_comments.push(TextRange::at(range.start(), pos));
133
134 inj.add(&line[pos.into()..], TextRange::new(range.start() + pos, range.end()));
135 inj.add_unmapped("\n");
136 }
137 inj.add_unmapped("\n}");
138
139 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
140
141 for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
142 for r in inj.map_range_up(h.range) {
143 hl.add(HlRange {
144 range: r,
145 highlight: h.highlight | HlMod::Injected,
146 binding_hash: h.binding_hash,
147 });
148 }
149 }
150
151 for range in new_comments {
152 hl.add(HlRange {
153 range,
154 highlight: HlTag::Comment | HlMod::Documentation,
155 binding_hash: None,
156 });
157 }
158}
diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs
deleted file mode 100644
index d6be9708d..000000000
--- a/crates/ide/src/syntax_highlighting/injection.rs
+++ /dev/null
@@ -1,240 +0,0 @@
1//! Syntax highlighting injections such as highlighting of documentation tests.
2
3use std::{collections::BTreeMap, convert::TryFrom};
4
5use hir::Semantics;
6use ide_db::call_info::ActiveParameter;
7use itertools::Itertools;
8use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};
9
10use crate::{Analysis, Highlight, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase};
11
12use super::HighlightedRangeStack;
13
14pub(super) fn highlight_injection(
15 acc: &mut HighlightedRangeStack,
16 sema: &Semantics<RootDatabase>,
17 literal: ast::String,
18 expanded: SyntaxToken,
19) -> Option<()> {
20 let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
21 if !active_parameter.name.starts_with("ra_fixture") {
22 return None;
23 }
24 let value = literal.value()?;
25 let marker_info = MarkerInfo::new(&*value);
26 let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());
27
28 if let Some(range) = literal.open_quote_text_range() {
29 acc.add(HighlightedRange {
30 range,
31 highlight: HighlightTag::StringLiteral.into(),
32 binding_hash: None,
33 })
34 }
35
36 for mut h in analysis.highlight(tmp_file_id).unwrap() {
37 let range = marker_info.map_range_up(h.range);
38 if let Some(range) = literal.map_range_up(range) {
39 h.range = range;
40 acc.add(h);
41 }
42 }
43
44 if let Some(range) = literal.close_quote_text_range() {
45 acc.add(HighlightedRange {
46 range,
47 highlight: HighlightTag::StringLiteral.into(),
48 binding_hash: None,
49 })
50 }
51
52 Some(())
53}
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
101/// Mapping from extracted documentation code to original code
102type RangesMap = BTreeMap<TextSize, TextSize>;
103
104const RUSTDOC_FENCE: &'static str = "```";
105const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
106 "",
107 "rust",
108 "should_panic",
109 "ignore",
110 "no_run",
111 "compile_fail",
112 "edition2015",
113 "edition2018",
114 "edition2021",
115];
116
117/// Extracts Rust code from documentation comments as well as a mapping from
118/// the extracted source code back to the original source ranges.
119/// Lastly, a vector of new comment highlight ranges (spanning only the
120/// comment prefix) is returned which is used in the syntax highlighting
121/// injection to replace the previous (line-spanning) comment ranges.
122pub(super) fn extract_doc_comments(
123 node: &SyntaxNode,
124) -> Option<(String, RangesMap, Vec<HighlightedRange>)> {
125 // wrap the doctest into function body to get correct syntax highlighting
126 let prefix = "fn doctest() {\n";
127 let suffix = "}\n";
128 // Mapping from extracted documentation code to original code
129 let mut range_mapping: RangesMap = BTreeMap::new();
130 let mut line_start = TextSize::try_from(prefix.len()).unwrap();
131 let mut is_codeblock = false;
132 let mut is_doctest = false;
133 // Replace the original, line-spanning comment ranges by new, only comment-prefix
134 // spanning comment ranges.
135 let mut new_comments = Vec::new();
136 let doctest = node
137 .children_with_tokens()
138 .filter_map(|el| el.into_token().and_then(ast::Comment::cast))
139 .filter(|comment| comment.kind().doc.is_some())
140 .filter(|comment| {
141 if let Some(idx) = comment.text().find(RUSTDOC_FENCE) {
142 is_codeblock = !is_codeblock;
143 // Check whether code is rust by inspecting fence guards
144 let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..];
145 let is_rust =
146 guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
147 is_doctest = is_codeblock && is_rust;
148 false
149 } else {
150 is_doctest
151 }
152 })
153 .map(|comment| {
154 let prefix_len = comment.prefix().len();
155 let line: &str = comment.text().as_str();
156 let range = comment.syntax().text_range();
157
158 // whitespace after comment is ignored
159 let pos = if let Some(ws) = line.chars().nth(prefix_len).filter(|c| c.is_whitespace()) {
160 prefix_len + ws.len_utf8()
161 } else {
162 prefix_len
163 };
164
165 // lines marked with `#` should be ignored in output, we skip the `#` char
166 let pos = if let Some(ws) = line.chars().nth(pos).filter(|&c| c == '#') {
167 pos + ws.len_utf8()
168 } else {
169 pos
170 };
171
172 range_mapping.insert(line_start, range.start() + TextSize::try_from(pos).unwrap());
173 new_comments.push(HighlightedRange {
174 range: TextRange::new(
175 range.start(),
176 range.start() + TextSize::try_from(pos).unwrap(),
177 ),
178 highlight: HighlightTag::Comment | HighlightModifier::Documentation,
179 binding_hash: None,
180 });
181 line_start += range.len() - TextSize::try_from(pos).unwrap();
182 line_start += TextSize::try_from('\n'.len_utf8()).unwrap();
183
184 line[pos..].to_owned()
185 })
186 .join("\n");
187
188 if doctest.is_empty() {
189 return None;
190 }
191
192 let doctest = format!("{}{}{}", prefix, doctest, suffix);
193 Some((doctest, range_mapping, new_comments))
194}
195
196/// Injection of syntax highlighting of doctests.
197pub(super) fn highlight_doc_comment(
198 text: String,
199 range_mapping: RangesMap,
200 new_comments: Vec<HighlightedRange>,
201 stack: &mut HighlightedRangeStack,
202) {
203 let (analysis, tmp_file_id) = Analysis::from_single_file(text);
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 {
236 stack.add(comment);
237 }
238 stack.pop_and_inject(None);
239 stack.pop_and_inject(Some(Highlight::from(HighlightTag::Dummy) | HighlightModifier::Injected));
240}
diff --git a/crates/ide/src/syntax_highlighting/injector.rs b/crates/ide/src/syntax_highlighting/injector.rs
new file mode 100644
index 000000000..e8f17eb69
--- /dev/null
+++ b/crates/ide/src/syntax_highlighting/injector.rs
@@ -0,0 +1,81 @@
1//! Extracts a subsequence of a text document, remembering the mapping of ranges
2//! between original and extracted texts.
3use std::ops::{self, Sub};
4
5use stdx::equal_range_by;
6use syntax::{TextRange, TextSize};
7
8use super::highlights::ordering;
9
10#[derive(Default)]
11pub(super) struct Injector {
12 buf: String,
13 ranges: Vec<(TextRange, Option<Delta<TextSize>>)>,
14}
15
16impl 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 self.add_impl(text, Some(source_range.start()));
21 }
22 pub(super) fn add_unmapped(&mut self, text: &str) {
23 self.add_impl(text, None);
24 }
25 fn add_impl(&mut self, text: &str, source: Option<TextSize>) {
26 let len = TextSize::of(text);
27 let target_range = TextRange::at(TextSize::of(&self.buf), len);
28 self.ranges.push((target_range, source.map(|it| Delta::new(target_range.start(), it))));
29 self.buf.push_str(text);
30 }
31
32 pub(super) fn text(&self) -> &str {
33 &self.buf
34 }
35 pub(super) fn map_range_up(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
36 let (start, len) = equal_range_by(&self.ranges, |&(r, _)| ordering(r, range));
37 (start..start + len).filter_map(move |i| {
38 let (target_range, delta) = self.ranges[i];
39 let intersection = target_range.intersect(range).unwrap();
40 Some(intersection + delta?)
41 })
42 }
43}
44
45#[derive(Clone, Copy)]
46enum Delta<T> {
47 Add(T),
48 Sub(T),
49}
50
51impl<T> Delta<T> {
52 fn new(from: T, to: T) -> Delta<T>
53 where
54 T: Ord + Sub<Output = T>,
55 {
56 if to >= from {
57 Delta::Add(to - from)
58 } else {
59 Delta::Sub(from - to)
60 }
61 }
62}
63
64impl ops::Add<Delta<TextSize>> for TextSize {
65 type Output = TextSize;
66
67 fn add(self, rhs: Delta<TextSize>) -> TextSize {
68 match rhs {
69 Delta::Add(it) => self + it,
70 Delta::Sub(it) => self - it,
71 }
72 }
73}
74
75impl ops::Add<Delta<TextSize>> for TextRange {
76 type Output = TextRange;
77
78 fn add(self, rhs: Delta<TextSize>) -> TextRange {
79 TextRange::at(self.start() + rhs, self.len())
80 }
81}
diff --git a/crates/ide/src/syntax_highlighting/macro_rules.rs b/crates/ide/src/syntax_highlighting/macro_rules.rs
index 4462af47e..21d8a9835 100644
--- a/crates/ide/src/syntax_highlighting/macro_rules.rs
+++ b/crates/ide/src/syntax_highlighting/macro_rules.rs
@@ -1,7 +1,7 @@
1//! Syntax highlighting for macro_rules!. 1//! Syntax highlighting for macro_rules!.
2use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T}; 2use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
3 3
4use crate::{HighlightTag, HighlightedRange}; 4use crate::{HlRange, HlTag};
5 5
6#[derive(Default)] 6#[derive(Default)]
7pub(super) struct MacroRulesHighlighter { 7pub(super) struct MacroRulesHighlighter {
@@ -19,13 +19,13 @@ impl MacroRulesHighlighter {
19 } 19 }
20 } 20 }
21 21
22 pub(super) fn highlight(&self, element: SyntaxElement) -> Option<HighlightedRange> { 22 pub(super) fn highlight(&self, element: SyntaxElement) -> Option<HlRange> {
23 if let Some(state) = self.state.as_ref() { 23 if let Some(state) = self.state.as_ref() {
24 if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) { 24 if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) {
25 if let Some(range) = is_metavariable(element) { 25 if let Some(range) = is_metavariable(element) {
26 return Some(HighlightedRange { 26 return Some(HlRange {
27 range, 27 range,
28 highlight: HighlightTag::UnresolvedReference.into(), 28 highlight: HlTag::UnresolvedReference.into(),
29 binding_hash: None, 29 binding_hash: None,
30 }); 30 });
31 } 31 }
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs
index 8b8867079..2f39bcc8e 100644
--- a/crates/ide/src/syntax_highlighting/tags.rs
+++ b/crates/ide/src/syntax_highlighting/tags.rs
@@ -7,15 +7,15 @@ use crate::SymbolKind;
7 7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub struct Highlight { 9pub struct Highlight {
10 pub tag: HighlightTag, 10 pub tag: HlTag,
11 pub modifiers: HighlightModifiers, 11 pub mods: HlMods,
12} 12}
13 13
14#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 14#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
15pub struct HighlightModifiers(u32); 15pub struct HlMods(u32);
16 16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
18pub enum HighlightTag { 18pub enum HlTag {
19 Symbol(SymbolKind), 19 Symbol(SymbolKind),
20 20
21 BoolLiteral, 21 BoolLiteral,
@@ -33,13 +33,13 @@ pub enum HighlightTag {
33 Operator, 33 Operator,
34 UnresolvedReference, 34 UnresolvedReference,
35 35
36 // For things which don't have proper Tag, but want to use modifiers. 36 // For things which don't have a specific highlight.
37 Dummy, 37 None,
38} 38}
39 39
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
41#[repr(u8)] 41#[repr(u8)]
42pub enum HighlightModifier { 42pub enum HlMod {
43 /// Used to differentiate individual elements within attributes. 43 /// Used to differentiate individual elements within attributes.
44 Attribute = 0, 44 Attribute = 0,
45 /// Used with keywords like `if` and `break`. 45 /// Used with keywords like `if` and `break`.
@@ -61,10 +61,10 @@ pub enum HighlightModifier {
61 Unsafe, 61 Unsafe,
62} 62}
63 63
64impl HighlightTag { 64impl HlTag {
65 fn as_str(self) -> &'static str { 65 fn as_str(self) -> &'static str {
66 match self { 66 match self {
67 HighlightTag::Symbol(symbol) => match symbol { 67 HlTag::Symbol(symbol) => match symbol {
68 SymbolKind::Const => "constant", 68 SymbolKind::Const => "constant",
69 SymbolKind::Static => "static", 69 SymbolKind::Static => "static",
70 SymbolKind::Enum => "enum", 70 SymbolKind::Enum => "enum",
@@ -86,59 +86,59 @@ impl HighlightTag {
86 SymbolKind::SelfParam => "self_keyword", 86 SymbolKind::SelfParam => "self_keyword",
87 SymbolKind::Impl => "self_type", 87 SymbolKind::Impl => "self_type",
88 }, 88 },
89 HighlightTag::Attribute => "attribute", 89 HlTag::Attribute => "attribute",
90 HighlightTag::BoolLiteral => "bool_literal", 90 HlTag::BoolLiteral => "bool_literal",
91 HighlightTag::BuiltinType => "builtin_type", 91 HlTag::BuiltinType => "builtin_type",
92 HighlightTag::ByteLiteral => "byte_literal", 92 HlTag::ByteLiteral => "byte_literal",
93 HighlightTag::CharLiteral => "char_literal", 93 HlTag::CharLiteral => "char_literal",
94 HighlightTag::Comment => "comment", 94 HlTag::Comment => "comment",
95 HighlightTag::EscapeSequence => "escape_sequence", 95 HlTag::EscapeSequence => "escape_sequence",
96 HighlightTag::FormatSpecifier => "format_specifier", 96 HlTag::FormatSpecifier => "format_specifier",
97 HighlightTag::Dummy => "dummy", 97 HlTag::Keyword => "keyword",
98 HighlightTag::Keyword => "keyword", 98 HlTag::Punctuation => "punctuation",
99 HighlightTag::Punctuation => "punctuation", 99 HlTag::NumericLiteral => "numeric_literal",
100 HighlightTag::NumericLiteral => "numeric_literal", 100 HlTag::Operator => "operator",
101 HighlightTag::Operator => "operator", 101 HlTag::StringLiteral => "string_literal",
102 HighlightTag::StringLiteral => "string_literal", 102 HlTag::UnresolvedReference => "unresolved_reference",
103 HighlightTag::UnresolvedReference => "unresolved_reference", 103 HlTag::None => "none",
104 } 104 }
105 } 105 }
106} 106}
107 107
108impl fmt::Display for HighlightTag { 108impl fmt::Display for HlTag {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 fmt::Display::fmt(self.as_str(), f) 110 fmt::Display::fmt(self.as_str(), f)
111 } 111 }
112} 112}
113 113
114impl HighlightModifier { 114impl HlMod {
115 const ALL: &'static [HighlightModifier; HighlightModifier::Unsafe as u8 as usize + 1] = &[ 115 const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[
116 HighlightModifier::Attribute, 116 HlMod::Attribute,
117 HighlightModifier::ControlFlow, 117 HlMod::ControlFlow,
118 HighlightModifier::Definition, 118 HlMod::Definition,
119 HighlightModifier::Documentation, 119 HlMod::Documentation,
120 HighlightModifier::Injected, 120 HlMod::Injected,
121 HighlightModifier::Mutable, 121 HlMod::Mutable,
122 HighlightModifier::Consuming, 122 HlMod::Consuming,
123 HighlightModifier::Callable, 123 HlMod::Callable,
124 HighlightModifier::Static, 124 HlMod::Static,
125 HighlightModifier::Associated, 125 HlMod::Associated,
126 HighlightModifier::Unsafe, 126 HlMod::Unsafe,
127 ]; 127 ];
128 128
129 fn as_str(self) -> &'static str { 129 fn as_str(self) -> &'static str {
130 match self { 130 match self {
131 HighlightModifier::Attribute => "attribute", 131 HlMod::Attribute => "attribute",
132 HighlightModifier::ControlFlow => "control", 132 HlMod::ControlFlow => "control",
133 HighlightModifier::Definition => "declaration", 133 HlMod::Definition => "declaration",
134 HighlightModifier::Documentation => "documentation", 134 HlMod::Documentation => "documentation",
135 HighlightModifier::Injected => "injected", 135 HlMod::Injected => "injected",
136 HighlightModifier::Mutable => "mutable", 136 HlMod::Mutable => "mutable",
137 HighlightModifier::Consuming => "consuming", 137 HlMod::Consuming => "consuming",
138 HighlightModifier::Unsafe => "unsafe", 138 HlMod::Unsafe => "unsafe",
139 HighlightModifier::Callable => "callable", 139 HlMod::Callable => "callable",
140 HighlightModifier::Static => "static", 140 HlMod::Static => "static",
141 HighlightModifier::Associated => "associated", 141 HlMod::Associated => "associated",
142 } 142 }
143 } 143 }
144 144
@@ -147,7 +147,7 @@ impl HighlightModifier {
147 } 147 }
148} 148}
149 149
150impl fmt::Display for HighlightModifier { 150impl fmt::Display for HlMod {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 fmt::Display::fmt(self.as_str(), f) 152 fmt::Display::fmt(self.as_str(), f)
153 } 153 }
@@ -156,60 +156,63 @@ impl fmt::Display for HighlightModifier {
156impl fmt::Display for Highlight { 156impl fmt::Display for Highlight {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "{}", self.tag)?; 158 write!(f, "{}", self.tag)?;
159 for modifier in self.modifiers.iter() { 159 for modifier in self.mods.iter() {
160 write!(f, ".{}", modifier)? 160 write!(f, ".{}", modifier)?
161 } 161 }
162 Ok(()) 162 Ok(())
163 } 163 }
164} 164}
165 165
166impl From<HighlightTag> for Highlight { 166impl From<HlTag> for Highlight {
167 fn from(tag: HighlightTag) -> Highlight { 167 fn from(tag: HlTag) -> Highlight {
168 Highlight::new(tag) 168 Highlight::new(tag)
169 } 169 }
170} 170}
171 171
172impl Highlight { 172impl Highlight {
173 pub(crate) fn new(tag: HighlightTag) -> Highlight { 173 pub(crate) fn new(tag: HlTag) -> Highlight {
174 Highlight { tag, modifiers: HighlightModifiers::default() } 174 Highlight { tag, mods: HlMods::default() }
175 }
176 pub fn is_empty(&self) -> bool {
177 self.tag == HlTag::None && self.mods == HlMods::default()
175 } 178 }
176} 179}
177 180
178impl ops::BitOr<HighlightModifier> for HighlightTag { 181impl ops::BitOr<HlMod> for HlTag {
179 type Output = Highlight; 182 type Output = Highlight;
180 183
181 fn bitor(self, rhs: HighlightModifier) -> Highlight { 184 fn bitor(self, rhs: HlMod) -> Highlight {
182 Highlight::new(self) | rhs 185 Highlight::new(self) | rhs
183 } 186 }
184} 187}
185 188
186impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers { 189impl ops::BitOrAssign<HlMod> for HlMods {
187 fn bitor_assign(&mut self, rhs: HighlightModifier) { 190 fn bitor_assign(&mut self, rhs: HlMod) {
188 self.0 |= rhs.mask(); 191 self.0 |= rhs.mask();
189 } 192 }
190} 193}
191 194
192impl ops::BitOrAssign<HighlightModifier> for Highlight { 195impl ops::BitOrAssign<HlMod> for Highlight {
193 fn bitor_assign(&mut self, rhs: HighlightModifier) { 196 fn bitor_assign(&mut self, rhs: HlMod) {
194 self.modifiers |= rhs; 197 self.mods |= rhs;
195 } 198 }
196} 199}
197 200
198impl ops::BitOr<HighlightModifier> for Highlight { 201impl ops::BitOr<HlMod> for Highlight {
199 type Output = Highlight; 202 type Output = Highlight;
200 203
201 fn bitor(mut self, rhs: HighlightModifier) -> Highlight { 204 fn bitor(mut self, rhs: HlMod) -> Highlight {
202 self |= rhs; 205 self |= rhs;
203 self 206 self
204 } 207 }
205} 208}
206 209
207impl HighlightModifiers { 210impl HlMods {
208 pub fn contains(self, m: HighlightModifier) -> bool { 211 pub fn contains(self, m: HlMod) -> bool {
209 self.0 & m.mask() == m.mask() 212 self.0 & m.mask() == m.mask()
210 } 213 }
211 214
212 pub fn iter(self) -> impl Iterator<Item = HighlightModifier> { 215 pub fn iter(self) -> impl Iterator<Item = HlMod> {
213 HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) 216 HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
214 } 217 }
215} 218}
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..7d1d2a839 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="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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>
@@ -51,9 +56,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
51 <span class="comment documentation">/// # Examples</span> 56 <span class="comment documentation">/// # Examples</span>
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="none 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="none injected"> </span><span class="keyword injected">mut</span><span class="none injected"> </span><span class="variable declaration injected mutable">foo</span><span class="punctuation injected">:</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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">-&gt;</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">-&gt;</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>
@@ -63,27 +68,27 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
63 <span class="comment documentation">/// # Examples</span> 68 <span class="comment documentation">/// # Examples</span>
64 <span class="comment documentation">///</span> 69 <span class="comment documentation">///</span>
65 <span class="comment documentation">/// ```</span> 70 <span class="comment documentation">/// ```</span>
66 <span class="comment documentation">/// </span><span class="keyword injected">use</span><span class="dummy injected"> </span><span class="module injected">x</span><span class="operator injected">::</span><span class="module injected">y</span><span class="punctuation injected">;</span> 71 <span class="comment documentation">/// </span><span class="keyword injected">use</span><span class="none injected"> </span><span class="module injected">x</span><span class="operator injected">::</span><span class="module injected">y</span><span class="punctuation injected">;</span>
67 <span class="comment documentation">///</span> 72 <span class="comment documentation">///</span>
68 <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration 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> 73 <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">foo</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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>
69 <span class="comment documentation">///</span> 74 <span class="comment documentation">///</span>
70 <span class="comment documentation">/// </span><span class="comment injected">// calls bar on foo</span> 75 <span class="comment documentation">/// </span><span class="comment injected">// calls bar on foo</span>
71 <span class="comment documentation">/// </span><span class="macro injected">assert!</span><span class="punctuation injected">(</span><span class="dummy injected">foo</span><span class="operator injected">.</span><span class="dummy injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> 76 <span class="comment documentation">/// </span><span class="macro injected">assert!</span><span class="punctuation injected">(</span><span class="none injected">foo</span><span class="operator injected">.</span><span class="none injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span>
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="none injected"> </span><span class="variable declaration injected">bar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="variable injected">foo</span><span class="operator injected">.</span><span class="field injected">bar</span><span class="none injected"> </span><span class="operator injected">||</span><span class="none 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="none injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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="none injected"> </span><span class="variable declaration injected">foobar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none 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">&gt;</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">&gt;</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/test_data/injection.html b/crates/ide/src/syntax_highlighting/test_data/injection.html
index a54d303b4..6703a84e5 100644
--- a/crates/ide/src/syntax_highlighting/test_data/injection.html
+++ b/crates/ide/src/syntax_highlighting/test_data/injection.html
@@ -40,9 +40,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
40<span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> 40<span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
41 <span class="function">f</span><span class="punctuation">(</span><span class="string_literal">r"</span> 41 <span class="function">f</span><span class="punctuation">(</span><span class="string_literal">r"</span>
42<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> 42<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
43 <span class="function">foo</span><span class="punctuation">(</span>$0<span class="punctuation">{</span> 43 <span class="function">foo</span><span class="punctuation">(</span><span class="keyword">$0</span><span class="punctuation">{</span>
44 <span class="numeric_literal">92</span> 44 <span class="numeric_literal">92</span>
45 <span class="punctuation">}</span>$0<span class="punctuation">)</span> 45 <span class="punctuation">}</span><span class="keyword">$0</span><span class="punctuation">)</span>
46<span class="punctuation">}</span><span class="string_literal">"</span><span class="punctuation">)</span><span class="punctuation">;</span> 46<span class="punctuation">}</span><span class="string_literal">"</span><span class="punctuation">)</span><span class="punctuation">;</span>
47<span class="punctuation">}</span> 47<span class="punctuation">}</span>
48 </code></pre> \ No newline at end of file 48 </code></pre> \ No newline at end of file
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
448impl Foo { 448impl 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`.