7 use hir::{Name, Semantics};
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 ) -> Vec<HighlightedRange> {
48 let _p = profile("highlight");
49 let sema = Semantics::new(db);
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 {
56 let node = match source_file.syntax().covering_element(range) {
57 NodeOrToken::Node(it) => it,
58 NodeOrToken::Token(it) => it.parent(),
62 None => (source_file.syntax().clone(), source_file.syntax().text_range()),
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();
71 let mut current_macro_call: Option<ast::MacroCall> = None;
72 let mut format_string: Option<SyntaxElement> = None;
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() {
78 WalkEvent::Enter(_) => stack.push(),
79 WalkEvent::Leave(_) => stack.pop(),
82 let event_range = match &event {
83 WalkEvent::Enter(it) => it.text_range(),
84 WalkEvent::Leave(it) => it.text_range(),
87 // Element outside of the viewport, no need to highlight
88 if range_to_highlight.intersect(event_range).is_none() {
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 {
99 highlight: HighlightTag::Macro.into(),
103 if let Some(name) = mc.is_macro_rules() {
104 if let Some((highlight, binding_hash)) = highlight_element(
106 &mut bindings_shadow_count,
107 name.syntax().clone().into(),
109 stack.add(HighlightedRange {
110 range: name.syntax().text_range(),
118 WalkEvent::Leave(Some(mc)) => {
119 assert!(current_macro_call == Some(mc));
120 current_macro_call = None;
121 format_string = None;
127 // Check for Rust code in documentation
129 WalkEvent::Leave(NodeOrToken::Node(node)) => {
130 if let Some((doctest, range_mapping, new_comments)) =
131 injection::extract_doc_comments(node)
133 injection::highlight_doc_comment(
144 let element = match event {
145 WalkEvent::Enter(it) => it,
146 WalkEvent::Leave(_) => continue,
149 let range = element.text_range();
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,
157 let token = sema.descend_into_macros(token.clone());
158 let parent = token.parent();
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
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())
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)
177 ast::String::can_cast(e.kind())
178 || ast::RawString::can_cast(e.kind())
185 // We only care Name and Name_ref
186 match (token.kind(), parent.kind()) {
187 (IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
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() {
201 let is_format_string = format_string.as_ref() == Some(&element_to_highlight);
203 if let Some((highlight, binding_hash)) =
204 highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone())
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)
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(),
223 } else if let Some(string) =
224 element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
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(),
247 struct HighlightedRangeStack {
248 stack: Vec<Vec<HighlightedRange>>,
251 /// We use a stack to implement the flattening logic for the highlighted
253 impl HighlightedRangeStack {
255 Self { stack: vec![Vec::new()] }
259 self.stack.push(Vec::new());
262 /// Flattens the highlighted ranges.
264 /// For example `#[cfg(feature = "foo")]` contains the nested ranges:
265 /// 1) parent-range: Attribute [0, 23)
266 /// 2) child-range: String [16, 21)
268 /// The following code implements the flattening, for our example this results to:
269 /// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
271 let children = self.stack.pop().unwrap();
272 let prev = self.stack.last_mut().unwrap();
273 let needs_flattening = !children.is_empty()
275 && prev.last().unwrap().range.contains_range(children.first().unwrap().range);
276 if !needs_flattening {
277 prev.extend(children);
279 let mut parent = prev.pop().unwrap();
280 for ele in children {
281 assert!(parent.range.contains_range(ele.range));
283 let cloned = Self::intersect(&mut parent, &ele);
284 if !parent.range.is_empty() {
290 if !parent.range.is_empty() {
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));
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());
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
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());
324 for child in children {
326 prev.iter().position(|parent| parent.range.contains_range(child.range))
328 let cloned = Self::intersect(&mut prev[idx], &child);
329 let insert_idx = if delete || prev[idx].range.is_empty() {
335 prev.insert(insert_idx, child);
336 if !delete && !cloned.range.is_empty() {
337 prev.insert(insert_idx + 1, cloned);
339 } else if let Some(_idx) =
340 prev.iter().position(|parent| parent.range.contains(child.range.start()))
342 unreachable!("child range should be completely contained in parent range");
345 .binary_search_by_key(&child.range.start(), |range| range.range.start())
346 .unwrap_or_else(|x| x);
347 prev.insert(idx, child);
352 fn add(&mut self, range: HighlightedRange) {
355 .expect("during DFS traversal, the stack must not be empty")
359 fn flattened(mut self) -> Vec<HighlightedRange> {
363 "after DFS traversal, the stack should only contain a single element"
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
370 .zip(res.iter().skip(1))
371 .all(|(left, right)| left.range.end() <= right.range.start()));
376 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
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,
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()?;
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(),
407 Some(TextRange::new(range_start, range_end))
410 fn highlight_element(
411 sema: &Semantics<RootDatabase>,
412 bindings_shadow_count: &mut FxHashMap<Name, u32>,
413 element: SyntaxElement,
414 ) -> Option<(Highlight, Option<u64>)> {
416 let mut binding_hash = None;
417 let highlight: Highlight = match element.kind() {
419 bindings_shadow_count.clear();
423 // Highlight definitions depending on the "type" of the definition.
425 let name = element.into_node().and_then(ast::Name::cast).unwrap();
426 let name_kind = classify_name(sema, &name);
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();
432 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
437 Some(NameClass::Definition(def)) => {
438 highlight_name(db, def) | HighlightModifier::Definition
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,
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
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) {
458 bindings_shadow_count.entry(name.clone()).or_default();
459 binding_hash = Some(calc_binding_hash(&name, *shadow_count))
462 highlight_name(db, def)
464 NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
466 None => HighlightTag::UnresolvedReference.into(),
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,
479 let h = Highlight::new(HighlightTag::Lifetime);
480 match element.parent().map(|it| it.kind()) {
481 Some(LIFETIME_PARAM) | Some(LABEL) => h | HighlightModifier::Definition,
486 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
488 let expr = prefix_expr.expr()?;
489 let ty = sema.type_of_expr(&expr)?;
490 if !ty.is_raw_ptr() {
494 let mut h = Highlight::new(HighlightTag::Operator);
495 h |= HighlightModifier::Unsafe;
498 T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
499 Highlight::new(HighlightTag::Macro)
502 k if k.is_keyword() => {
503 let h = Highlight::new(HighlightTag::Keyword);
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(),
525 return Some((highlight, binding_hash));
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};
531 let mut hasher = DefaultHasher::new();
536 hash((name, shadow_count))
540 fn is_child_of_impl(element: SyntaxElement) -> bool {
541 match element.parent() {
542 Some(e) => e.kind() == IMPL_DEF,
547 fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
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;
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);
571 h |= HighlightModifier::Mutable;
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;
590 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
591 let default = HighlightTag::UnresolvedReference;
593 let parent = match name.syntax().parent() {
595 _ => return default.into(),
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,