12 use hir::{AsAssocItem, Local, Name, Semantics, VariantDef};
14 defs::{Definition, NameClass, NameRefClass},
17 use rustc_hash::FxHashMap;
19 ast::{self, HasFormatSpecifier},
20 AstNode, AstToken, Direction, NodeOrToken, SyntaxElement,
21 SyntaxKind::{self, *},
22 SyntaxNode, SyntaxToken, TextRange, WalkEvent, T,
26 syntax_highlighting::{
27 format::FormatStringHighlighter, macro_rules::MacroRulesHighlighter, tags::Highlight,
29 FileId, HighlightModifier, HighlightTag, SymbolKind,
32 pub(crate) use html::highlight_as_html;
34 #[derive(Debug, Clone)]
35 pub struct HighlightedRange {
37 pub highlight: Highlight,
38 pub binding_hash: Option<u64>,
41 // Feature: Semantic Syntax Highlighting
43 // rust-analyzer highlights the code semantically.
44 // For example, `bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
45 // rust-analyzer does not specify colors directly, instead it assigns tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
46 // It's up to the client to map those to specific colors.
48 // The general rule is that a reference to an entity gets colored the same way as the entity itself.
49 // We also give special modifier for `mut` and `&mut` local variables.
50 pub(crate) fn highlight(
53 range_to_highlight: Option<TextRange>,
54 syntactic_name_ref_highlighting: bool,
55 ) -> Vec<HighlightedRange> {
56 let _p = profile::span("highlight");
57 let sema = Semantics::new(db);
59 // Determine the root based on the given range.
60 let (root, range_to_highlight) = {
61 let source_file = sema.parse(file_id);
62 match range_to_highlight {
64 let node = match source_file.syntax().covering_element(range) {
65 NodeOrToken::Node(it) => it,
66 NodeOrToken::Token(it) => it.parent(),
70 None => (source_file.syntax().clone(), source_file.syntax().text_range()),
74 let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
75 let mut stack = highlights::Highlights::new(range_to_highlight);
77 let mut current_macro_call: Option<ast::MacroCall> = None;
78 let mut current_macro_rules: Option<ast::MacroRules> = None;
79 let mut format_string_highlighter = FormatStringHighlighter::default();
80 let mut macro_rules_highlighter = MacroRulesHighlighter::default();
81 let mut inside_attribute = false;
83 // Walk all nodes, keeping track of whether we are inside a macro or not.
84 // If in macro, expand it first and highlight the expanded code.
85 for event in root.preorder_with_tokens() {
86 let event_range = match &event {
87 WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(),
90 // Element outside of the viewport, no need to highlight
91 if range_to_highlight.intersect(event_range).is_none() {
95 // Track "inside macro" state
96 match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
97 WalkEvent::Enter(Some(mc)) => {
98 if let Some(range) = macro_call_range(&mc) {
99 stack.add(HighlightedRange {
101 highlight: HighlightTag::Symbol(SymbolKind::Macro).into(),
105 current_macro_call = Some(mc.clone());
108 WalkEvent::Leave(Some(mc)) => {
109 assert_eq!(current_macro_call, Some(mc));
110 current_macro_call = None;
111 format_string_highlighter = FormatStringHighlighter::default();
116 match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) {
117 WalkEvent::Enter(Some(mac)) => {
118 macro_rules_highlighter.init();
119 current_macro_rules = Some(mac);
122 WalkEvent::Leave(Some(mac)) => {
123 assert_eq!(current_macro_rules, Some(mac));
124 current_macro_rules = None;
125 macro_rules_highlighter = MacroRulesHighlighter::default();
131 // Check for Rust code in documentation
132 WalkEvent::Leave(NodeOrToken::Node(node)) => {
133 if ast::Attr::can_cast(node.kind()) {
134 inside_attribute = false
136 if let Some((new_comments, inj)) = injection::extract_doc_comments(node) {
137 injection::highlight_doc_comment(new_comments, inj, &mut stack);
140 WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
141 inside_attribute = true
146 let element = match event {
147 WalkEvent::Enter(it) => it,
148 WalkEvent::Leave(_) => continue,
151 let range = element.text_range();
153 if current_macro_rules.is_some() {
154 if let Some(tok) = element.as_token() {
155 macro_rules_highlighter.advance(tok);
159 let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
160 // Inside a macro -- expand it first
161 let token = match element.clone().into_token() {
162 Some(it) if it.parent().kind() == TOKEN_TREE => it,
165 let token = sema.descend_into_macros(token.clone());
166 let parent = token.parent();
168 format_string_highlighter.check_for_format_string(&parent);
170 // We only care Name and Name_ref
171 match (token.kind(), parent.kind()) {
172 (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
179 if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) {
181 let expanded = element_to_highlight.as_token().unwrap().clone();
182 if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() {
188 if let Some((mut highlight, binding_hash)) = highlight_element(
190 &mut bindings_shadow_count,
191 syntactic_name_ref_highlighting,
192 element_to_highlight.clone(),
194 if inside_attribute {
195 highlight = highlight | HighlightModifier::Attribute;
198 if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() {
199 stack.add(HighlightedRange { range, highlight, binding_hash });
202 if let Some(string) =
203 element_to_highlight.as_token().cloned().and_then(ast::String::cast)
205 format_string_highlighter.highlight_format_string(&mut stack, &string, range);
206 // Highlight escape sequences
207 if let Some(char_ranges) = string.char_ranges() {
208 for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) {
209 if string.text()[piece_range.start().into()..].starts_with('\\') {
210 stack.add(HighlightedRange {
211 range: piece_range + range.start(),
212 highlight: HighlightTag::EscapeSequence.into(),
225 fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
226 let path = macro_call.path()?;
227 let name_ref = path.segment()?.name_ref()?;
229 let range_start = name_ref.syntax().text_range().start();
230 let mut range_end = name_ref.syntax().text_range().end();
231 for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
232 match sibling.kind() {
233 T![!] | IDENT => range_end = sibling.text_range().end(),
238 Some(TextRange::new(range_start, range_end))
241 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
242 fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
243 while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
244 if parent.kind() != *kind {
248 // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
249 // in the same pattern is unstable: rust-lang/rust#68354.
250 node = node.parent().unwrap().into();
254 // Only true if we matched all expected kinds
258 fn is_consumed_lvalue(
259 node: NodeOrToken<SyntaxNode, SyntaxToken>,
263 // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
264 parents_match(node, &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST]) && !local.ty(db).is_copy(db)
267 fn highlight_element(
268 sema: &Semantics<RootDatabase>,
269 bindings_shadow_count: &mut FxHashMap<Name, u32>,
270 syntactic_name_ref_highlighting: bool,
271 element: SyntaxElement,
272 ) -> Option<(Highlight, Option<u64>)> {
274 let mut binding_hash = None;
275 let highlight: Highlight = match element.kind() {
277 bindings_shadow_count.clear();
281 // Highlight definitions depending on the "type" of the definition.
283 let name = element.into_node().and_then(ast::Name::cast).unwrap();
284 let name_kind = NameClass::classify(sema, &name);
286 if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
287 if let Some(name) = local.name(db) {
288 let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
290 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
295 Some(NameClass::ExternCrate(_)) => HighlightTag::Symbol(SymbolKind::Module).into(),
296 Some(NameClass::Definition(def)) => {
297 highlight_def(db, def) | HighlightModifier::Definition
299 Some(NameClass::ConstReference(def)) => highlight_def(db, def),
300 Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
301 let mut h = HighlightTag::Symbol(SymbolKind::Field).into();
302 if let Definition::Field(field) = field_ref {
303 if let VariantDef::Union(_) = field.parent_def(db) {
304 h |= HighlightModifier::Unsafe;
310 None => highlight_name_by_syntax(name) | HighlightModifier::Definition,
314 // Highlight references like the definitions they resolve to
315 NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
316 // even though we track whether we are in an attribute or not we still need this special case
317 // as otherwise we would emit unresolved references for name refs inside attributes
318 Highlight::from(HighlightTag::Symbol(SymbolKind::Function))
321 let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
322 highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| {
323 match NameRefClass::classify(sema, &name_ref) {
324 Some(name_kind) => match name_kind {
325 NameRefClass::ExternCrate(_) => {
326 HighlightTag::Symbol(SymbolKind::Module).into()
328 NameRefClass::Definition(def) => {
329 if let Definition::Local(local) = &def {
330 if let Some(name) = local.name(db) {
332 bindings_shadow_count.entry(name.clone()).or_default();
333 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
337 let mut h = highlight_def(db, def);
339 if let Definition::Local(local) = &def {
340 if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) {
341 h |= HighlightModifier::Consuming;
345 if let Some(parent) = name_ref.syntax().parent() {
346 if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
347 if let Definition::Field(field) = def {
348 if let VariantDef::Union(_) = field.parent_def(db) {
349 h |= HighlightModifier::Unsafe;
357 NameRefClass::FieldShorthand { .. } => {
358 HighlightTag::Symbol(SymbolKind::Field).into()
361 None if syntactic_name_ref_highlighting => {
362 highlight_name_ref_by_syntax(name_ref, sema)
364 None => HighlightTag::UnresolvedReference.into(),
369 // Simple token-based highlighting
371 let comment = element.into_token().and_then(ast::Comment::cast)?;
372 let h = HighlightTag::Comment;
373 match comment.kind().doc {
374 Some(_) => h | HighlightModifier::Documentation,
378 STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
379 ATTR => HighlightTag::Attribute.into(),
380 INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(),
381 BYTE => HighlightTag::ByteLiteral.into(),
382 CHAR => HighlightTag::CharLiteral.into(),
383 QUESTION => Highlight::new(HighlightTag::Operator) | HighlightModifier::ControlFlow,
385 let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
387 match NameClass::classify_lifetime(sema, &lifetime) {
388 Some(NameClass::Definition(def)) => {
389 highlight_def(db, def) | HighlightModifier::Definition
391 None => match NameRefClass::classify_lifetime(sema, &lifetime) {
392 Some(NameRefClass::Definition(def)) => highlight_def(db, def),
393 _ => Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam)),
396 Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam))
397 | HighlightModifier::Definition
401 p if p.is_punct() => match p {
403 let h = HighlightTag::Operator.into();
404 let is_unsafe = element
406 .and_then(ast::RefExpr::cast)
407 .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr))
410 h | HighlightModifier::Unsafe
415 T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => {
416 HighlightTag::Operator.into()
418 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
419 HighlightTag::Symbol(SymbolKind::Macro).into()
421 T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
422 HighlightTag::BuiltinType.into()
424 T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
425 HighlightTag::Keyword.into()
427 T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
428 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
430 let expr = prefix_expr.expr()?;
431 let ty = sema.type_of_expr(&expr)?;
433 HighlightTag::Operator | HighlightModifier::Unsafe
434 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
435 HighlightTag::Operator.into()
437 HighlightTag::Punctuation.into()
440 T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
441 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
443 let expr = prefix_expr.expr()?;
445 ast::Expr::Literal(_) => HighlightTag::NumericLiteral,
446 _ => HighlightTag::Operator,
450 _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
451 HighlightTag::Operator.into()
453 _ if element.parent().and_then(ast::BinExpr::cast).is_some() => {
454 HighlightTag::Operator.into()
456 _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
457 HighlightTag::Operator.into()
459 _ if element.parent().and_then(ast::RangePat::cast).is_some() => {
460 HighlightTag::Operator.into()
462 _ if element.parent().and_then(ast::RestPat::cast).is_some() => {
463 HighlightTag::Operator.into()
465 _ if element.parent().and_then(ast::Attr::cast).is_some() => {
466 HighlightTag::Attribute.into()
468 _ => HighlightTag::Punctuation.into(),
471 k if k.is_keyword() => {
472 let h = Highlight::new(HighlightTag::Keyword);
482 | T![in] => h | HighlightModifier::ControlFlow,
483 T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow,
484 T![unsafe] => h | HighlightModifier::Unsafe,
485 T![true] | T![false] => HighlightTag::BoolLiteral.into(),
487 let self_param_is_mut = element
489 .and_then(ast::SelfParam::cast)
490 .and_then(|p| p.mut_token())
492 let self_path = &element
495 .and_then(SyntaxNode::parent)
496 .and_then(ast::Path::cast)
497 .and_then(|p| sema.resolve_path(&p));
498 let mut h = HighlightTag::Symbol(SymbolKind::SelfParam).into();
500 || matches!(self_path,
501 Some(hir::PathResolution::Local(local))
503 && (local.is_mut(db) || local.ty(db).is_mutable_reference())
506 h |= HighlightModifier::Mutable
509 if let Some(hir::PathResolution::Local(local)) = self_path {
510 if is_consumed_lvalue(element, &local, db) {
511 h |= HighlightModifier::Consuming;
519 .and_then(ast::IdentPat::cast)
520 .and_then(|ident_pat| {
521 if sema.is_unsafe_ident_pat(&ident_pat) {
522 Some(HighlightModifier::Unsafe)
527 .map(|modifier| h | modifier)
536 return Some((highlight, binding_hash));
538 fn calc_binding_hash(name: &Name, shadow_count: u32) -> u64 {
539 fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
540 use std::{collections::hash_map::DefaultHasher, hash::Hasher};
542 let mut hasher = DefaultHasher::new();
547 hash((name, shadow_count))
551 fn is_child_of_impl(element: &SyntaxElement) -> bool {
552 match element.parent() {
553 Some(e) => e.kind() == IMPL,
558 fn highlight_func_by_name_ref(
559 sema: &Semantics<RootDatabase>,
560 name_ref: &ast::NameRef,
561 ) -> Option<Highlight> {
562 let method_call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
563 highlight_method_call(sema, &method_call)
566 fn highlight_method_call(
567 sema: &Semantics<RootDatabase>,
568 method_call: &ast::MethodCallExpr,
569 ) -> Option<Highlight> {
570 let func = sema.resolve_method_call(&method_call)?;
571 let mut h = HighlightTag::Symbol(SymbolKind::Function).into();
572 h |= HighlightModifier::Associated;
573 if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
574 h |= HighlightModifier::Unsafe;
576 if let Some(self_param) = func.self_param(sema.db) {
577 match self_param.access(sema.db) {
578 hir::Access::Shared => (),
579 hir::Access::Exclusive => h |= HighlightModifier::Mutable,
580 hir::Access::Owned => {
581 if let Some(receiver_ty) =
582 method_call.receiver().and_then(|it| sema.type_of_expr(&it))
584 if !receiver_ty.is_copy(sema.db) {
585 h |= HighlightModifier::Consuming
594 fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
596 Definition::Macro(_) => HighlightTag::Symbol(SymbolKind::Macro),
597 Definition::Field(_) => HighlightTag::Symbol(SymbolKind::Field),
598 Definition::ModuleDef(def) => match def {
599 hir::ModuleDef::Module(_) => HighlightTag::Symbol(SymbolKind::Module),
600 hir::ModuleDef::Function(func) => {
601 let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Function));
602 if func.as_assoc_item(db).is_some() {
603 h |= HighlightModifier::Associated;
604 if func.self_param(db).is_none() {
605 h |= HighlightModifier::Static
608 if func.is_unsafe(db) {
609 h |= HighlightModifier::Unsafe;
613 hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Symbol(SymbolKind::Struct),
614 hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Symbol(SymbolKind::Enum),
615 hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Symbol(SymbolKind::Union),
616 hir::ModuleDef::Variant(_) => HighlightTag::Symbol(SymbolKind::Variant),
617 hir::ModuleDef::Const(konst) => {
618 let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Const));
619 if konst.as_assoc_item(db).is_some() {
620 h |= HighlightModifier::Associated
624 hir::ModuleDef::Trait(_) => HighlightTag::Symbol(SymbolKind::Trait),
625 hir::ModuleDef::TypeAlias(type_) => {
626 let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::TypeAlias));
627 if type_.as_assoc_item(db).is_some() {
628 h |= HighlightModifier::Associated
632 hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
633 hir::ModuleDef::Static(s) => {
634 let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Static));
636 h |= HighlightModifier::Mutable;
637 h |= HighlightModifier::Unsafe;
642 Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl),
643 Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam),
644 Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam),
645 Definition::Local(local) => {
646 let tag = if local.is_param(db) {
647 HighlightTag::Symbol(SymbolKind::ValueParam)
649 HighlightTag::Symbol(SymbolKind::Local)
651 let mut h = Highlight::new(tag);
652 if local.is_mut(db) || local.ty(db).is_mutable_reference() {
653 h |= HighlightModifier::Mutable;
655 if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) {
656 h |= HighlightModifier::Callable;
660 Definition::LifetimeParam(_) => HighlightTag::Symbol(SymbolKind::LifetimeParam),
661 Definition::Label(_) => HighlightTag::Symbol(SymbolKind::Label),
666 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
667 let default = HighlightTag::UnresolvedReference;
669 let parent = match name.syntax().parent() {
671 _ => return default.into(),
674 let tag = match parent.kind() {
675 STRUCT => HighlightTag::Symbol(SymbolKind::Struct),
676 ENUM => HighlightTag::Symbol(SymbolKind::Enum),
677 VARIANT => HighlightTag::Symbol(SymbolKind::Variant),
678 UNION => HighlightTag::Symbol(SymbolKind::Union),
679 TRAIT => HighlightTag::Symbol(SymbolKind::Trait),
680 TYPE_ALIAS => HighlightTag::Symbol(SymbolKind::TypeAlias),
681 TYPE_PARAM => HighlightTag::Symbol(SymbolKind::TypeParam),
682 RECORD_FIELD => HighlightTag::Symbol(SymbolKind::Field),
683 MODULE => HighlightTag::Symbol(SymbolKind::Module),
684 FN => HighlightTag::Symbol(SymbolKind::Function),
685 CONST => HighlightTag::Symbol(SymbolKind::Const),
686 STATIC => HighlightTag::Symbol(SymbolKind::Static),
687 IDENT_PAT => HighlightTag::Symbol(SymbolKind::Local),
694 fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight {
695 let default = HighlightTag::UnresolvedReference;
697 let parent = match name.syntax().parent() {
699 _ => return default.into(),
702 match parent.kind() {
703 METHOD_CALL_EXPR => {
704 return ast::MethodCallExpr::cast(parent)
705 .and_then(|method_call| highlight_method_call(sema, &method_call))
706 .unwrap_or_else(|| HighlightTag::Symbol(SymbolKind::Function).into());
709 let h = HighlightTag::Symbol(SymbolKind::Field);
710 let is_union = ast::FieldExpr::cast(parent)
711 .and_then(|field_expr| {
712 let field = sema.resolve_field(&field_expr)?;
713 Some(if let VariantDef::Union(_) = field.parent_def(sema.db) {
721 h | HighlightModifier::Unsafe
727 let path = match parent.parent().and_then(ast::Path::cast) {
729 _ => return default.into(),
731 let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
734 // within path, decide whether it is module or adt by checking for uppercase name
735 return if name.text().chars().next().unwrap_or_default().is_uppercase() {
736 HighlightTag::Symbol(SymbolKind::Struct)
738 HighlightTag::Symbol(SymbolKind::Module)
743 let parent = match expr.syntax().parent() {
745 None => return default.into(),
748 match parent.kind() {
749 CALL_EXPR => HighlightTag::Symbol(SymbolKind::Function).into(),
750 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
751 HighlightTag::Symbol(SymbolKind::Struct)
753 HighlightTag::Symbol(SymbolKind::Const)