]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide/src/syntax_highlighting.rs
Fix syntax highlighting of recursive macros
[rust.git] / crates / ra_ide / src / syntax_highlighting.rs
1 mod tags;
2 mod html;
3 mod injection;
4 #[cfg(test)]
5 mod tests;
6
7 use hir::{Name, Semantics};
8 use ra_ide_db::{
9     defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass},
10     RootDatabase,
11 };
12 use ra_prof::profile;
13 use ra_syntax::{
14     ast::{self, HasFormatSpecifier},
15     AstNode, AstToken, Direction, NodeOrToken, SyntaxElement,
16     SyntaxKind::*,
17     TextRange, WalkEvent, T,
18 };
19 use rustc_hash::FxHashMap;
20
21 use crate::FileId;
22
23 use ast::FormatSpecifier;
24 pub(crate) use html::highlight_as_html;
25 pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
26
27 #[derive(Debug, Clone)]
28 pub struct HighlightedRange {
29     pub range: TextRange,
30     pub highlight: Highlight,
31     pub binding_hash: Option<u64>,
32 }
33
34 // Feature: Semantic Syntax Highlighting
35 //
36 // rust-analyzer highlights the code semantically.
37 // For example, `bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
38 // rust-analyzer does not specify colors directly, instead it assigns tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
39 // It's up to the client to map those to specific colors.
40 //
41 // The general rule is that a reference to an entity gets colored the same way as the entity itself.
42 // We also give special modifier for `mut` and `&mut` local variables.
43 pub(crate) fn highlight(
44     db: &RootDatabase,
45     file_id: FileId,
46     range_to_highlight: Option<TextRange>,
47 ) -> Vec<HighlightedRange> {
48     let _p = profile("highlight");
49     let sema = Semantics::new(db);
50
51     // Determine the root based on the given range.
52     let (root, range_to_highlight) = {
53         let source_file = sema.parse(file_id);
54         match range_to_highlight {
55             Some(range) => {
56                 let node = match source_file.syntax().covering_element(range) {
57                     NodeOrToken::Node(it) => it,
58                     NodeOrToken::Token(it) => it.parent(),
59                 };
60                 (node, range)
61             }
62             None => (source_file.syntax().clone(), source_file.syntax().text_range()),
63         }
64     };
65
66     let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
67     // We use a stack for the DFS traversal below.
68     // When we leave a node, the we use it to flatten the highlighted ranges.
69     let mut stack = HighlightedRangeStack::new();
70
71     let mut current_macro_call: Option<ast::MacroCall> = None;
72     let mut format_string: Option<SyntaxElement> = None;
73
74     // Walk all nodes, keeping track of whether we are inside a macro or not.
75     // If in macro, expand it first and highlight the expanded code.
76     for event in root.preorder_with_tokens() {
77         match &event {
78             WalkEvent::Enter(_) => stack.push(),
79             WalkEvent::Leave(_) => stack.pop(),
80         };
81
82         let event_range = match &event {
83             WalkEvent::Enter(it) => it.text_range(),
84             WalkEvent::Leave(it) => it.text_range(),
85         };
86
87         // Element outside of the viewport, no need to highlight
88         if range_to_highlight.intersect(event_range).is_none() {
89             continue;
90         }
91
92         // Track "inside macro" state
93         match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
94             WalkEvent::Enter(Some(mc)) => {
95                 current_macro_call = Some(mc.clone());
96                 if let Some(range) = macro_call_range(&mc) {
97                     stack.add(HighlightedRange {
98                         range,
99                         highlight: HighlightTag::Macro.into(),
100                         binding_hash: None,
101                     });
102                 }
103                 if let Some(name) = mc.is_macro_rules() {
104                     if let Some((highlight, binding_hash)) = highlight_element(
105                         &sema,
106                         &mut bindings_shadow_count,
107                         name.syntax().clone().into(),
108                     ) {
109                         stack.add(HighlightedRange {
110                             range: name.syntax().text_range(),
111                             highlight,
112                             binding_hash,
113                         });
114                     }
115                 }
116                 continue;
117             }
118             WalkEvent::Leave(Some(mc)) => {
119                 assert!(current_macro_call == Some(mc));
120                 current_macro_call = None;
121                 format_string = None;
122                 continue;
123             }
124             _ => (),
125         }
126
127         // Check for Rust code in documentation
128         match &event {
129             WalkEvent::Leave(NodeOrToken::Node(node)) => {
130                 if let Some((doctest, range_mapping, new_comments)) =
131                     injection::extract_doc_comments(node)
132                 {
133                     injection::highlight_doc_comment(
134                         doctest,
135                         range_mapping,
136                         new_comments,
137                         &mut stack,
138                     );
139                 }
140             }
141             _ => (),
142         }
143
144         let element = match event {
145             WalkEvent::Enter(it) => it,
146             WalkEvent::Leave(_) => continue,
147         };
148
149         let range = element.text_range();
150
151         let element_to_highlight = if current_macro_call.is_some() {
152             // Inside a macro -- expand it first
153             let token = match element.clone().into_token() {
154                 Some(it) if it.parent().kind() == TOKEN_TREE => it,
155                 _ => continue,
156             };
157             let token = sema.descend_into_macros(token.clone());
158             let parent = token.parent();
159
160             // Check if macro takes a format string and remember it for highlighting later.
161             // The macros that accept a format string expand to a compiler builtin macros
162             // `format_args` and `format_args_nl`.
163             if let Some(name) = parent
164                 .parent()
165                 .and_then(ast::MacroCall::cast)
166                 .and_then(|mc| mc.path())
167                 .and_then(|p| p.segment())
168                 .and_then(|s| s.name_ref())
169             {
170                 match name.text().as_str() {
171                     "format_args" | "format_args_nl" => {
172                         format_string = parent
173                             .children_with_tokens()
174                             .filter(|t| t.kind() != WHITESPACE)
175                             .nth(1)
176                             .filter(|e| {
177                                 ast::String::can_cast(e.kind())
178                                     || ast::RawString::can_cast(e.kind())
179                             })
180                     }
181                     _ => {}
182                 }
183             }
184
185             // We only care Name and Name_ref
186             match (token.kind(), parent.kind()) {
187                 (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
188                 _ => token.into(),
189             }
190         } else {
191             element.clone()
192         };
193
194         if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
195             let expanded = element_to_highlight.as_token().unwrap().clone();
196             if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() {
197                 continue;
198             }
199         }
200
201         let is_format_string = format_string.as_ref() == Some(&element_to_highlight);
202
203         if let Some((highlight, binding_hash)) =
204             highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone())
205         {
206             stack.add(HighlightedRange { range, highlight, binding_hash });
207             if let Some(string) =
208                 element_to_highlight.as_token().cloned().and_then(ast::String::cast)
209             {
210                 stack.push();
211                 if is_format_string {
212                     string.lex_format_specifier(|piece_range, kind| {
213                         if let Some(highlight) = highlight_format_specifier(kind) {
214                             stack.add(HighlightedRange {
215                                 range: piece_range + range.start(),
216                                 highlight: highlight.into(),
217                                 binding_hash: None,
218                             });
219                         }
220                     });
221                 }
222                 stack.pop();
223             } else if let Some(string) =
224                 element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
225             {
226                 stack.push();
227                 if is_format_string {
228                     string.lex_format_specifier(|piece_range, kind| {
229                         if let Some(highlight) = highlight_format_specifier(kind) {
230                             stack.add(HighlightedRange {
231                                 range: piece_range + range.start(),
232                                 highlight: highlight.into(),
233                                 binding_hash: None,
234                             });
235                         }
236                     });
237                 }
238                 stack.pop();
239             }
240         }
241     }
242
243     stack.flattened()
244 }
245
246 #[derive(Debug)]
247 struct HighlightedRangeStack {
248     stack: Vec<Vec<HighlightedRange>>,
249 }
250
251 /// We use a stack to implement the flattening logic for the highlighted
252 /// syntax ranges.
253 impl HighlightedRangeStack {
254     fn new() -> Self {
255         Self { stack: vec![Vec::new()] }
256     }
257
258     fn push(&mut self) {
259         self.stack.push(Vec::new());
260     }
261
262     /// Flattens the highlighted ranges.
263     ///
264     /// For example `#[cfg(feature = "foo")]` contains the nested ranges:
265     /// 1) parent-range: Attribute [0, 23)
266     /// 2) child-range: String [16, 21)
267     ///
268     /// The following code implements the flattening, for our example this results to:
269     /// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
270     fn pop(&mut self) {
271         let children = self.stack.pop().unwrap();
272         let prev = self.stack.last_mut().unwrap();
273         let needs_flattening = !children.is_empty()
274             && !prev.is_empty()
275             && prev.last().unwrap().range.contains_range(children.first().unwrap().range);
276         if !needs_flattening {
277             prev.extend(children);
278         } else {
279             let mut parent = prev.pop().unwrap();
280             for ele in children {
281                 assert!(parent.range.contains_range(ele.range));
282
283                 let cloned = Self::intersect(&mut parent, &ele);
284                 if !parent.range.is_empty() {
285                     prev.push(parent);
286                 }
287                 prev.push(ele);
288                 parent = cloned;
289             }
290             if !parent.range.is_empty() {
291                 prev.push(parent);
292             }
293         }
294     }
295
296     /// Intersects the `HighlightedRange` `parent` with `child`.
297     /// `parent` is mutated in place, becoming the range before `child`.
298     /// Returns the range (of the same type as `parent`) *after* `child`.
299     fn intersect(parent: &mut HighlightedRange, child: &HighlightedRange) -> HighlightedRange {
300         assert!(parent.range.contains_range(child.range));
301
302         let mut cloned = parent.clone();
303         parent.range = TextRange::new(parent.range.start(), child.range.start());
304         cloned.range = TextRange::new(child.range.end(), cloned.range.end());
305
306         cloned
307     }
308
309     /// Similar to `pop`, but can modify arbitrary prior ranges (where `pop`)
310     /// can only modify the last range currently on the stack.
311     /// Can be used to do injections that span multiple ranges, like the
312     /// doctest injection below.
313     /// If `delete` is set to true, the parent range is deleted instead of
314     /// intersected.
315     ///
316     /// Note that `pop` can be simulated by `pop_and_inject(false)` but the
317     /// latter is computationally more expensive.
318     fn pop_and_inject(&mut self, delete: bool) {
319         let mut children = self.stack.pop().unwrap();
320         let prev = self.stack.last_mut().unwrap();
321         children.sort_by_key(|range| range.range.start());
322         prev.sort_by_key(|range| range.range.start());
323
324         for child in children {
325             if let Some(idx) =
326                 prev.iter().position(|parent| parent.range.contains_range(child.range))
327             {
328                 let cloned = Self::intersect(&mut prev[idx], &child);
329                 let insert_idx = if delete || prev[idx].range.is_empty() {
330                     prev.remove(idx);
331                     idx
332                 } else {
333                     idx + 1
334                 };
335                 prev.insert(insert_idx, child);
336                 if !delete && !cloned.range.is_empty() {
337                     prev.insert(insert_idx + 1, cloned);
338                 }
339             } else if let Some(_idx) =
340                 prev.iter().position(|parent| parent.range.contains(child.range.start()))
341             {
342                 unreachable!("child range should be completely contained in parent range");
343             } else {
344                 let idx = prev
345                     .binary_search_by_key(&child.range.start(), |range| range.range.start())
346                     .unwrap_or_else(|x| x);
347                 prev.insert(idx, child);
348             }
349         }
350     }
351
352     fn add(&mut self, range: HighlightedRange) {
353         self.stack
354             .last_mut()
355             .expect("during DFS traversal, the stack must not be empty")
356             .push(range)
357     }
358
359     fn flattened(mut self) -> Vec<HighlightedRange> {
360         assert_eq!(
361             self.stack.len(),
362             1,
363             "after DFS traversal, the stack should only contain a single element"
364         );
365         let mut res = self.stack.pop().unwrap();
366         res.sort_by_key(|range| range.range.start());
367         // Check that ranges are sorted and disjoint
368         assert!(res
369             .iter()
370             .zip(res.iter().skip(1))
371             .all(|(left, right)| left.range.end() <= right.range.start()));
372         res
373     }
374 }
375
376 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
377     Some(match kind {
378         FormatSpecifier::Open
379         | FormatSpecifier::Close
380         | FormatSpecifier::Colon
381         | FormatSpecifier::Fill
382         | FormatSpecifier::Align
383         | FormatSpecifier::Sign
384         | FormatSpecifier::NumberSign
385         | FormatSpecifier::DollarSign
386         | FormatSpecifier::Dot
387         | FormatSpecifier::Asterisk
388         | FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier,
389         FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
390         FormatSpecifier::Identifier => HighlightTag::Local,
391     })
392 }
393
394 fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
395     let path = macro_call.path()?;
396     let name_ref = path.segment()?.name_ref()?;
397
398     let range_start = name_ref.syntax().text_range().start();
399     let mut range_end = name_ref.syntax().text_range().end();
400     for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
401         match sibling.kind() {
402             T![!] | IDENT => range_end = sibling.text_range().end(),
403             _ => (),
404         }
405     }
406
407     Some(TextRange::new(range_start, range_end))
408 }
409
410 fn highlight_element(
411     sema: &Semantics<RootDatabase>,
412     bindings_shadow_count: &mut FxHashMap<Name, u32>,
413     element: SyntaxElement,
414 ) -> Option<(Highlight, Option<u64>)> {
415     let db = sema.db;
416     let mut binding_hash = None;
417     let highlight: Highlight = match element.kind() {
418         FN_DEF => {
419             bindings_shadow_count.clear();
420             return None;
421         }
422
423         // Highlight definitions depending on the "type" of the definition.
424         NAME => {
425             let name = element.into_node().and_then(ast::Name::cast).unwrap();
426             let name_kind = classify_name(sema, &name);
427
428             if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
429                 if let Some(name) = local.name(db) {
430                     let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
431                     *shadow_count += 1;
432                     binding_hash = Some(calc_binding_hash(&name, *shadow_count))
433                 }
434             };
435
436             match name_kind {
437                 Some(NameClass::Definition(def)) => {
438                     highlight_name(db, def) | HighlightModifier::Definition
439                 }
440                 Some(NameClass::ConstReference(def)) => highlight_name(db, def),
441                 Some(NameClass::FieldShorthand { .. }) => HighlightTag::Field.into(),
442                 None => highlight_name_by_syntax(name) | HighlightModifier::Definition,
443             }
444         }
445
446         // Highlight references like the definitions they resolve to
447         NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
448             Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute
449         }
450         NAME_REF => {
451             let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
452             match classify_name_ref(sema, &name_ref) {
453                 Some(name_kind) => match name_kind {
454                     NameRefClass::Definition(def) => {
455                         if let Definition::Local(local) = &def {
456                             if let Some(name) = local.name(db) {
457                                 let shadow_count =
458                                     bindings_shadow_count.entry(name.clone()).or_default();
459                                 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
460                             }
461                         };
462                         highlight_name(db, def)
463                     }
464                     NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
465                 },
466                 None => HighlightTag::UnresolvedReference.into(),
467             }
468         }
469
470         // Simple token-based highlighting
471         COMMENT => HighlightTag::Comment.into(),
472         STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
473         ATTR => HighlightTag::Attribute.into(),
474         INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(),
475         BYTE => HighlightTag::ByteLiteral.into(),
476         CHAR => HighlightTag::CharLiteral.into(),
477         QUESTION => Highlight::new(HighlightTag::Operator) | HighlightModifier::ControlFlow,
478         LIFETIME => {
479             let h = Highlight::new(HighlightTag::Lifetime);
480             match element.parent().map(|it| it.kind()) {
481                 Some(LIFETIME_PARAM) | Some(LABEL) => h | HighlightModifier::Definition,
482                 _ => h,
483             }
484         }
485         T![*] => {
486             let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
487
488             let expr = prefix_expr.expr()?;
489             let ty = sema.type_of_expr(&expr)?;
490             if !ty.is_raw_ptr() {
491                 return None;
492             }
493
494             let mut h = Highlight::new(HighlightTag::Operator);
495             h |= HighlightModifier::Unsafe;
496             h
497         }
498         T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
499             Highlight::new(HighlightTag::Macro)
500         }
501
502         k if k.is_keyword() => {
503             let h = Highlight::new(HighlightTag::Keyword);
504             match k {
505                 T![break]
506                 | T![continue]
507                 | T![else]
508                 | T![if]
509                 | T![loop]
510                 | T![match]
511                 | T![return]
512                 | T![while]
513                 | T![in] => h | HighlightModifier::ControlFlow,
514                 T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow,
515                 T![unsafe] => h | HighlightModifier::Unsafe,
516                 T![true] | T![false] => HighlightTag::BoolLiteral.into(),
517                 T![self] => HighlightTag::SelfKeyword.into(),
518                 _ => h,
519             }
520         }
521
522         _ => return None,
523     };
524
525     return Some((highlight, binding_hash));
526
527     fn calc_binding_hash(name: &Name, shadow_count: u32) -> u64 {
528         fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
529             use std::{collections::hash_map::DefaultHasher, hash::Hasher};
530
531             let mut hasher = DefaultHasher::new();
532             x.hash(&mut hasher);
533             hasher.finish()
534         }
535
536         hash((name, shadow_count))
537     }
538 }
539
540 fn is_child_of_impl(element: SyntaxElement) -> bool {
541     match element.parent() {
542         Some(e) => e.kind() == IMPL_DEF,
543         _ => false,
544     }
545 }
546
547 fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
548     match def {
549         Definition::Macro(_) => HighlightTag::Macro,
550         Definition::Field(_) => HighlightTag::Field,
551         Definition::ModuleDef(def) => match def {
552             hir::ModuleDef::Module(_) => HighlightTag::Module,
553             hir::ModuleDef::Function(func) => {
554                 let mut h = HighlightTag::Function.into();
555                 if func.is_unsafe(db) {
556                     h |= HighlightModifier::Unsafe;
557                 }
558                 return h;
559             }
560             hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
561             hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum,
562             hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union,
563             hir::ModuleDef::EnumVariant(_) => HighlightTag::EnumVariant,
564             hir::ModuleDef::Const(_) => HighlightTag::Constant,
565             hir::ModuleDef::Trait(_) => HighlightTag::Trait,
566             hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
567             hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
568             hir::ModuleDef::Static(s) => {
569                 let mut h = Highlight::new(HighlightTag::Static);
570                 if s.is_mut(db) {
571                     h |= HighlightModifier::Mutable;
572                 }
573                 return h;
574             }
575         },
576         Definition::SelfType(_) => HighlightTag::SelfType,
577         Definition::TypeParam(_) => HighlightTag::TypeParam,
578         // FIXME: distinguish between locals and parameters
579         Definition::Local(local) => {
580             let mut h = Highlight::new(HighlightTag::Local);
581             if local.is_mut(db) || local.ty(db).is_mutable_reference() {
582                 h |= HighlightModifier::Mutable;
583             }
584             return h;
585         }
586     }
587     .into()
588 }
589
590 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
591     let default = HighlightTag::UnresolvedReference;
592
593     let parent = match name.syntax().parent() {
594         Some(it) => it,
595         _ => return default.into(),
596     };
597
598     let tag = match parent.kind() {
599         STRUCT_DEF => HighlightTag::Struct,
600         ENUM_DEF => HighlightTag::Enum,
601         UNION_DEF => HighlightTag::Union,
602         TRAIT_DEF => HighlightTag::Trait,
603         TYPE_ALIAS_DEF => HighlightTag::TypeAlias,
604         TYPE_PARAM => HighlightTag::TypeParam,
605         RECORD_FIELD_DEF => HighlightTag::Field,
606         MODULE => HighlightTag::Module,
607         FN_DEF => HighlightTag::Function,
608         CONST_DEF => HighlightTag::Constant,
609         STATIC_DEF => HighlightTag::Static,
610         ENUM_VARIANT => HighlightTag::EnumVariant,
611         BIND_PAT => HighlightTag::Local,
612         _ => default,
613     };
614
615     tag.into()
616 }