7 use hir::{Name, Semantics, VariantDef};
9 defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass},
14 ast::{self, HasFormatSpecifier},
15 AstNode, AstToken, Direction, NodeOrToken, SyntaxElement,
17 TextRange, WalkEvent, T,
19 use rustc_hash::FxHashMap;
23 use ast::FormatSpecifier;
24 pub(crate) use html::highlight_as_html;
25 pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
27 #[derive(Debug, Clone)]
28 pub struct HighlightedRange {
30 pub highlight: Highlight,
31 pub binding_hash: Option<u64>,
34 // Feature: Semantic Syntax Highlighting
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.
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(
46 range_to_highlight: Option<TextRange>,
47 syntactic_name_ref_highlighting: bool,
48 ) -> Vec<HighlightedRange> {
49 let _p = profile("highlight");
50 let sema = Semantics::new(db);
52 // Determine the root based on the given range.
53 let (root, range_to_highlight) = {
54 let source_file = sema.parse(file_id);
55 match range_to_highlight {
57 let node = match source_file.syntax().covering_element(range) {
58 NodeOrToken::Node(it) => it,
59 NodeOrToken::Token(it) => it.parent(),
63 None => (source_file.syntax().clone(), source_file.syntax().text_range()),
67 let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
68 // We use a stack for the DFS traversal below.
69 // When we leave a node, the we use it to flatten the highlighted ranges.
70 let mut stack = HighlightedRangeStack::new();
72 let mut current_macro_call: Option<ast::MacroCall> = None;
73 let mut format_string: Option<SyntaxElement> = None;
75 // Walk all nodes, keeping track of whether we are inside a macro or not.
76 // If in macro, expand it first and highlight the expanded code.
77 for event in root.preorder_with_tokens() {
79 WalkEvent::Enter(_) => stack.push(),
80 WalkEvent::Leave(_) => stack.pop(),
83 let event_range = match &event {
84 WalkEvent::Enter(it) => it.text_range(),
85 WalkEvent::Leave(it) => it.text_range(),
88 // Element outside of the viewport, no need to highlight
89 if range_to_highlight.intersect(event_range).is_none() {
93 // Track "inside macro" state
94 match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
95 WalkEvent::Enter(Some(mc)) => {
96 current_macro_call = Some(mc.clone());
97 if let Some(range) = macro_call_range(&mc) {
98 stack.add(HighlightedRange {
100 highlight: HighlightTag::Macro.into(),
104 if let Some(name) = mc.is_macro_rules() {
105 if let Some((highlight, binding_hash)) = highlight_element(
107 &mut bindings_shadow_count,
108 syntactic_name_ref_highlighting,
109 name.syntax().clone().into(),
111 stack.add(HighlightedRange {
112 range: name.syntax().text_range(),
120 WalkEvent::Leave(Some(mc)) => {
121 assert!(current_macro_call == Some(mc));
122 current_macro_call = None;
123 format_string = None;
128 // Check for Rust code in documentation
130 WalkEvent::Leave(NodeOrToken::Node(node)) => {
131 if let Some((doctest, range_mapping, new_comments)) =
132 injection::extract_doc_comments(node)
134 injection::highlight_doc_comment(
145 let element = match event {
146 WalkEvent::Enter(it) => it,
147 WalkEvent::Leave(_) => continue,
150 let range = element.text_range();
152 let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
153 // Inside a macro -- expand it first
154 let token = match element.clone().into_token() {
155 Some(it) if it.parent().kind() == TOKEN_TREE => it,
158 let token = sema.descend_into_macros(token.clone());
159 let parent = token.parent();
161 // Check if macro takes a format string and remember it for highlighting later.
162 // The macros that accept a format string expand to a compiler builtin macros
163 // `format_args` and `format_args_nl`.
164 if let Some(name) = parent
166 .and_then(ast::MacroCall::cast)
167 .and_then(|mc| mc.path())
168 .and_then(|p| p.segment())
169 .and_then(|s| s.name_ref())
171 match name.text().as_str() {
172 "format_args" | "format_args_nl" => {
173 format_string = parent
174 .children_with_tokens()
175 .filter(|t| t.kind() != WHITESPACE)
178 ast::String::can_cast(e.kind())
179 || ast::RawString::can_cast(e.kind())
186 // We only care Name and Name_ref
187 match (token.kind(), parent.kind()) {
188 (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
195 if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
196 let expanded = element_to_highlight.as_token().unwrap().clone();
197 if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() {
202 let is_format_string = format_string.as_ref() == Some(&element_to_highlight);
204 if let Some((highlight, binding_hash)) = highlight_element(
206 &mut bindings_shadow_count,
207 syntactic_name_ref_highlighting,
208 element_to_highlight.clone(),
210 stack.add(HighlightedRange { range, highlight, binding_hash });
211 if let Some(string) =
212 element_to_highlight.as_token().cloned().and_then(ast::String::cast)
214 if is_format_string {
216 string.lex_format_specifier(|piece_range, kind| {
217 if let Some(highlight) = highlight_format_specifier(kind) {
218 stack.add(HighlightedRange {
219 range: piece_range + range.start(),
220 highlight: highlight.into(),
227 // Highlight escape sequences
228 if let Some(char_ranges) = string.char_ranges() {
230 for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) {
231 if string.text()[piece_range.start().into()..].starts_with('\\') {
232 stack.add(HighlightedRange {
233 range: piece_range + range.start(),
234 highlight: HighlightTag::EscapeSequence.into(),
239 stack.pop_and_inject(None);
241 } else if let Some(string) =
242 element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
244 if is_format_string {
246 string.lex_format_specifier(|piece_range, kind| {
247 if let Some(highlight) = highlight_format_specifier(kind) {
248 stack.add(HighlightedRange {
249 range: piece_range + range.start(),
250 highlight: highlight.into(),
265 struct HighlightedRangeStack {
266 stack: Vec<Vec<HighlightedRange>>,
269 /// We use a stack to implement the flattening logic for the highlighted
271 impl HighlightedRangeStack {
273 Self { stack: vec![Vec::new()] }
277 self.stack.push(Vec::new());
280 /// Flattens the highlighted ranges.
282 /// For example `#[cfg(feature = "foo")]` contains the nested ranges:
283 /// 1) parent-range: Attribute [0, 23)
284 /// 2) child-range: String [16, 21)
286 /// The following code implements the flattening, for our example this results to:
287 /// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
289 let children = self.stack.pop().unwrap();
290 let prev = self.stack.last_mut().unwrap();
291 let needs_flattening = !children.is_empty()
293 && prev.last().unwrap().range.contains_range(children.first().unwrap().range);
294 if !needs_flattening {
295 prev.extend(children);
297 let mut parent = prev.pop().unwrap();
298 for ele in children {
299 assert!(parent.range.contains_range(ele.range));
301 let cloned = Self::intersect(&mut parent, &ele);
302 if !parent.range.is_empty() {
308 if !parent.range.is_empty() {
314 /// Intersects the `HighlightedRange` `parent` with `child`.
315 /// `parent` is mutated in place, becoming the range before `child`.
316 /// Returns the range (of the same type as `parent`) *after* `child`.
317 fn intersect(parent: &mut HighlightedRange, child: &HighlightedRange) -> HighlightedRange {
318 assert!(parent.range.contains_range(child.range));
320 let mut cloned = parent.clone();
321 parent.range = TextRange::new(parent.range.start(), child.range.start());
322 cloned.range = TextRange::new(child.range.end(), cloned.range.end());
327 /// Remove the `HighlightRange` of `parent` that's currently covered by `child`.
328 fn intersect_partial(parent: &mut HighlightedRange, child: &HighlightedRange) {
330 parent.range.start() <= child.range.start()
331 && parent.range.end() >= child.range.start()
332 && child.range.end() > parent.range.end()
335 parent.range = TextRange::new(parent.range.start(), child.range.start());
338 /// Similar to `pop`, but can modify arbitrary prior ranges (where `pop`)
339 /// can only modify the last range currently on the stack.
340 /// Can be used to do injections that span multiple ranges, like the
341 /// doctest injection below.
342 /// If `overwrite_parent` is non-optional, the highlighting of the parent range
343 /// is overwritten with the argument.
345 /// Note that `pop` can be simulated by `pop_and_inject(false)` but the
346 /// latter is computationally more expensive.
347 fn pop_and_inject(&mut self, overwrite_parent: Option<Highlight>) {
348 let mut children = self.stack.pop().unwrap();
349 let prev = self.stack.last_mut().unwrap();
350 children.sort_by_key(|range| range.range.start());
351 prev.sort_by_key(|range| range.range.start());
353 for child in children {
355 prev.iter().position(|parent| parent.range.contains_range(child.range))
357 if let Some(tag) = overwrite_parent {
358 prev[idx].highlight = tag;
361 let cloned = Self::intersect(&mut prev[idx], &child);
362 let insert_idx = if prev[idx].range.is_empty() {
368 prev.insert(insert_idx, child);
369 if !cloned.range.is_empty() {
370 prev.insert(insert_idx + 1, cloned);
374 prev.iter().position(|parent| parent.range.contains(child.range.start()));
375 match (overwrite_parent, maybe_idx) {
376 (Some(_), Some(idx)) => {
377 Self::intersect_partial(&mut prev[idx], &child);
378 let insert_idx = if prev[idx].range.is_empty() {
384 prev.insert(insert_idx, child);
388 .binary_search_by_key(&child.range.start(), |range| range.range.start())
389 .unwrap_or_else(|x| x);
390 prev.insert(idx, child);
393 unreachable!("child range should be completely contained in parent range");
400 fn add(&mut self, range: HighlightedRange) {
403 .expect("during DFS traversal, the stack must not be empty")
407 fn flattened(mut self) -> Vec<HighlightedRange> {
411 "after DFS traversal, the stack should only contain a single element"
413 let mut res = self.stack.pop().unwrap();
414 res.sort_by_key(|range| range.range.start());
415 // Check that ranges are sorted and disjoint
418 .zip(res.iter().skip(1))
419 .all(|(left, right)| left.range.end() <= right.range.start()));
424 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
426 FormatSpecifier::Open
427 | FormatSpecifier::Close
428 | FormatSpecifier::Colon
429 | FormatSpecifier::Fill
430 | FormatSpecifier::Align
431 | FormatSpecifier::Sign
432 | FormatSpecifier::NumberSign
433 | FormatSpecifier::DollarSign
434 | FormatSpecifier::Dot
435 | FormatSpecifier::Asterisk
436 | FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier,
437 FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
438 FormatSpecifier::Identifier => HighlightTag::Local,
442 fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
443 let path = macro_call.path()?;
444 let name_ref = path.segment()?.name_ref()?;
446 let range_start = name_ref.syntax().text_range().start();
447 let mut range_end = name_ref.syntax().text_range().end();
448 for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
449 match sibling.kind() {
450 T![!] | IDENT => range_end = sibling.text_range().end(),
455 Some(TextRange::new(range_start, range_end))
458 fn is_possibly_unsafe(name_ref: &ast::NameRef) -> bool {
463 ast::FieldExpr::cast(parent.clone())
465 .or_else(|| ast::RecordPatField::cast(parent).map(|_| true))
470 fn highlight_element(
471 sema: &Semantics<RootDatabase>,
472 bindings_shadow_count: &mut FxHashMap<Name, u32>,
473 syntactic_name_ref_highlighting: bool,
474 element: SyntaxElement,
475 ) -> Option<(Highlight, Option<u64>)> {
477 let mut binding_hash = None;
478 let highlight: Highlight = match element.kind() {
480 bindings_shadow_count.clear();
484 // Highlight definitions depending on the "type" of the definition.
486 let name = element.into_node().and_then(ast::Name::cast).unwrap();
487 let name_kind = classify_name(sema, &name);
489 if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
490 if let Some(name) = local.name(db) {
491 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
493 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
498 Some(NameClass::Definition(def)) => {
499 highlight_name(db, def, false) | HighlightModifier::Definition
501 Some(NameClass::ConstReference(def)) => highlight_name(db, def, false),
502 Some(NameClass::FieldShorthand { field, .. }) => {
503 let mut h = HighlightTag::Field.into();
504 if let Definition::Field(field) = field {
505 if let VariantDef::Union(_) = field.parent_def(db) {
506 h |= HighlightModifier::Unsafe;
512 None => highlight_name_by_syntax(name) | HighlightModifier::Definition,
516 // Highlight references like the definitions they resolve to
517 NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
518 Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute
521 let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
522 let possibly_unsafe = is_possibly_unsafe(&name_ref);
523 match classify_name_ref(sema, &name_ref) {
524 Some(name_kind) => match name_kind {
525 NameRefClass::Definition(def) => {
526 if let Definition::Local(local) = &def {
527 if let Some(name) = local.name(db) {
529 bindings_shadow_count.entry(name.clone()).or_default();
530 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
533 highlight_name(db, def, possibly_unsafe)
535 NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
537 None if syntactic_name_ref_highlighting => {
538 highlight_name_ref_by_syntax(name_ref, sema)
540 None => HighlightTag::UnresolvedReference.into(),
544 // Simple token-based highlighting
546 let comment = element.into_token().and_then(ast::Comment::cast)?;
547 let h = HighlightTag::Comment;
548 match comment.kind().doc {
549 Some(_) => h | HighlightModifier::Documentation,
553 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
554 ATTR => HighlightTag::Attribute.into(),
555 INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(),
556 BYTE => HighlightTag::ByteLiteral.into(),
557 CHAR => HighlightTag::CharLiteral.into(),
558 QUESTION => Highlight::new(HighlightTag::Operator) | HighlightModifier::ControlFlow,
560 let h = Highlight::new(HighlightTag::Lifetime);
561 match element.parent().map(|it| it.kind()) {
562 Some(LIFETIME_PARAM) | Some(LABEL) => h | HighlightModifier::Definition,
566 p if p.is_punct() => match p {
567 T![::] | T![->] | T![=>] | T![&] | T![..] | T![=] | T![@] => {
568 HighlightTag::Operator.into()
570 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
571 HighlightTag::Macro.into()
573 T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
574 HighlightTag::Keyword.into()
576 T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
577 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
579 let expr = prefix_expr.expr()?;
580 let ty = sema.type_of_expr(&expr)?;
582 HighlightTag::Operator | HighlightModifier::Unsafe
583 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
584 HighlightTag::Operator.into()
586 HighlightTag::Punctuation.into()
589 T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
590 HighlightTag::NumericLiteral.into()
592 _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
593 HighlightTag::Operator.into()
595 _ if element.parent().and_then(ast::BinExpr::cast).is_some() => {
596 HighlightTag::Operator.into()
598 _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
599 HighlightTag::Operator.into()
601 _ if element.parent().and_then(ast::RangePat::cast).is_some() => {
602 HighlightTag::Operator.into()
604 _ if element.parent().and_then(ast::RestPat::cast).is_some() => {
605 HighlightTag::Operator.into()
607 _ if element.parent().and_then(ast::Attr::cast).is_some() => {
608 HighlightTag::Attribute.into()
610 _ => HighlightTag::Punctuation.into(),
613 k if k.is_keyword() => {
614 let h = Highlight::new(HighlightTag::Keyword);
624 | T![in] => h | HighlightModifier::ControlFlow,
625 T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow,
626 T![unsafe] => h | HighlightModifier::Unsafe,
627 T![true] | T![false] => HighlightTag::BoolLiteral.into(),
629 let self_param_is_mut = element
631 .and_then(ast::SelfParam::cast)
632 .and_then(|p| p.mut_token())
634 // closure to enforce lazyness
636 sema.resolve_path(&element.parent()?.parent().and_then(ast::Path::cast)?)
639 || matches!(self_path(),
640 Some(hir::PathResolution::Local(local))
642 && (local.is_mut(db) || local.ty(db).is_mutable_reference())
645 HighlightTag::SelfKeyword | HighlightModifier::Mutable
647 HighlightTag::SelfKeyword.into()
657 return Some((highlight, binding_hash));
659 fn calc_binding_hash(name: &Name, shadow_count: u32) -> u64 {
660 fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
661 use std::{collections::hash_map::DefaultHasher, hash::Hasher};
663 let mut hasher = DefaultHasher::new();
668 hash((name, shadow_count))
672 fn is_child_of_impl(element: &SyntaxElement) -> bool {
673 match element.parent() {
674 Some(e) => e.kind() == IMPL,
679 fn highlight_name(db: &RootDatabase, def: Definition, possibly_unsafe: bool) -> Highlight {
681 Definition::Macro(_) => HighlightTag::Macro,
682 Definition::Field(field) => {
683 let mut h = HighlightTag::Field.into();
685 if let VariantDef::Union(_) = field.parent_def(db) {
686 h |= HighlightModifier::Unsafe;
692 Definition::ModuleDef(def) => match def {
693 hir::ModuleDef::Module(_) => HighlightTag::Module,
694 hir::ModuleDef::Function(func) => {
695 let mut h = HighlightTag::Function.into();
696 if func.is_unsafe(db) {
697 h |= HighlightModifier::Unsafe;
701 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
702 hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum,
703 hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Union,
704 hir::ModuleDef::EnumVariant(_) => HighlightTag::EnumVariant,
705 hir::ModuleDef::Const(_) => HighlightTag::Constant,
706 hir::ModuleDef::Trait(_) => HighlightTag::Trait,
707 hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
708 hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
709 hir::ModuleDef::Static(s) => {
710 let mut h = Highlight::new(HighlightTag::Static);
712 h |= HighlightModifier::Mutable;
717 Definition::SelfType(_) => HighlightTag::SelfType,
718 Definition::TypeParam(_) => HighlightTag::TypeParam,
719 Definition::Local(local) => {
721 if local.is_param(db) { HighlightTag::ValueParam } else { HighlightTag::Local };
722 let mut h = Highlight::new(tag);
723 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
724 h |= HighlightModifier::Mutable;
732 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
733 let default = HighlightTag::UnresolvedReference;
735 let parent = match name.syntax().parent() {
737 _ => return default.into(),
740 let tag = match parent.kind() {
741 STRUCT => HighlightTag::Struct,
742 ENUM => HighlightTag::Enum,
743 UNION => HighlightTag::Union,
744 TRAIT => HighlightTag::Trait,
745 TYPE_ALIAS => HighlightTag::TypeAlias,
746 TYPE_PARAM => HighlightTag::TypeParam,
747 RECORD_FIELD => HighlightTag::Field,
748 MODULE => HighlightTag::Module,
749 FN => HighlightTag::Function,
750 CONST => HighlightTag::Constant,
751 STATIC => HighlightTag::Static,
752 VARIANT => HighlightTag::EnumVariant,
753 IDENT_PAT => HighlightTag::Local,
760 fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight {
761 let default = HighlightTag::UnresolvedReference;
763 let parent = match name.syntax().parent() {
765 _ => return default.into(),
768 let tag = match parent.kind() {
769 METHOD_CALL_EXPR => HighlightTag::Function,
771 let h = HighlightTag::Field;
772 let is_union = ast::FieldExpr::cast(parent)
773 .and_then(|field_expr| {
774 let field = sema.resolve_field(&field_expr)?;
775 Some(if let VariantDef::Union(_) = field.parent_def(sema.db) {
782 return if is_union { h | HighlightModifier::Unsafe } else { h.into() };
785 let path = match parent.parent().and_then(ast::Path::cast) {
787 _ => return default.into(),
789 let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
792 // within path, decide whether it is module or adt by checking for uppercase name
793 return if name.text().chars().next().unwrap_or_default().is_uppercase() {
801 let parent = match expr.syntax().parent() {
803 None => return default.into(),
806 match parent.kind() {
807 CALL_EXPR => HighlightTag::Function,
809 if name.text().chars().next().unwrap_or_default().is_uppercase() {
812 HighlightTag::Constant