15 use hir::{InFile, Name, Semantics};
16 use ide_db::RootDatabase;
17 use rustc_hash::FxHashMap;
19 ast::{self, IsString},
20 AstNode, AstToken, NodeOrToken,
22 SyntaxNode, TextRange, WalkEvent, T,
26 syntax_highlighting::{
27 format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
33 pub(crate) use html::highlight_as_html;
35 #[derive(Debug, Clone, Copy)]
38 pub highlight: Highlight,
39 pub binding_hash: Option<u64>,
42 // Feature: Semantic Syntax Highlighting
44 // rust-analyzer highlights the code semantically.
45 // For example, `Bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
46 // rust-analyzer does not specify colors directly, instead it assigns a tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
47 // It's up to the client to map those to specific colors.
49 // The general rule is that a reference to an entity gets colored the same way as the entity itself.
50 // We also give special modifier for `mut` and `&mut` local variables.
55 // Rust-analyzer currently emits the following token tags:
60 // attribute:: Emitted for attribute macros.
61 // enum:: Emitted for enums.
62 // function:: Emitted for free-standing functions.
63 // derive:: Emitted for derive macros.
64 // macro:: Emitted for function-like macros.
65 // method:: Emitted for associated functions, also knowns as methods.
66 // namespace:: Emitted for modules.
67 // struct:: Emitted for structs.
68 // trait:: Emitted for traits.
69 // typeAlias:: Emitted for type aliases and `Self` in `impl`s.
70 // union:: Emitted for unions.
75 // boolean:: Emitted for the boolean literals `true` and `false`.
76 // character:: Emitted for character literals.
77 // number:: Emitted for numeric literals.
78 // string:: Emitted for string literals.
79 // escapeSequence:: Emitted for escaped sequences inside strings like `\n`.
80 // formatSpecifier:: Emitted for format specifiers `{:?}` in `format!`-like macros.
85 // operator:: Emitted for general operators.
86 // arithmetic:: Emitted for the arithmetic operators `+`, `-`, `*`, `/`, `+=`, `-=`, `*=`, `/=`.
87 // bitwise:: Emitted for the bitwise operators `|`, `&`, `!`, `^`, `|=`, `&=`, `^=`.
88 // comparison:: Emitted for the comparison operators `>`, `<`, `==`, `>=`, `<=`, `!=`.
89 // logical:: Emitted for the logical operators `||`, `&&`, `!`.
94 // punctuation:: Emitted for general punctuation.
95 // attributeBracket:: Emitted for attribute invocation brackets, that is the `#[` and `]` tokens.
96 // angle:: Emitted for `<>` angle brackets.
97 // brace:: Emitted for `{}` braces.
98 // bracket:: Emitted for `[]` brackets.
99 // parenthesis:: Emitted for `()` parentheses.
100 // colon:: Emitted for the `:` token.
101 // comma:: Emitted for the `,` token.
102 // dot:: Emitted for the `.` token.
103 // semi:: Emitted for the `;` token.
104 // macroBang:: Emitted for the `!` token in macro calls.
109 // builtinAttribute:: Emitted for names to builtin attributes in attribute path, the `repr` in `#[repr(u8)]` for example.
110 // builtinType:: Emitted for builtin types like `u32`, `str` and `f32`.
111 // comment:: Emitted for comments.
112 // constParameter:: Emitted for const parameters.
113 // enumMember:: Emitted for enum variants.
114 // generic:: Emitted for generic tokens that have no mapping.
115 // keyword:: Emitted for keywords.
116 // label:: Emitted for labels.
117 // lifetime:: Emitted for lifetimes.
118 // parameter:: Emitted for non-self function parameters.
119 // property:: Emitted for struct and union fields.
120 // selfKeyword:: Emitted for the self function parameter and self path-specifier.
121 // toolModule:: Emitted for tool modules.
122 // typeParameter:: Emitted for type parameters.
123 // unresolvedReference:: Emitted for unresolved references, names that rust-analyzer can't find the definition of.
124 // variable:: Emitted for locals, constants and statics.
129 // Token modifiers allow to style some elements in the source code more precisely.
131 // Rust-analyzer currently emits the following token modifiers:
134 // async:: Emitted for async functions and the `async` and `await` keywords.
135 // attribute:: Emitted for tokens inside attributes.
136 // callable:: Emitted for locals whose types implements one of the `Fn*` traits.
137 // constant:: Emitted for consts.
138 // consuming:: Emitted for locals that are being consumed when use in a function call.
139 // controlFlow:: Emitted for control-flow related tokens, this includes the `?` operator.
140 // crateRoot:: Emitted for crate names, like `serde` and `crate`.
141 // declaration:: Emitted for names of definitions, like `foo` in `fn foo() {}`.
142 // defaultLibrary:: Emitted for items from built-in crates (std, core, alloc, test and proc_macro).
143 // documentation:: Emitted for documentation comments.
144 // injected:: Emitted for doc-string injected highlighting like rust source blocks in documentation.
145 // intraDocLink:: Emitted for intra doc links in doc-strings.
146 // library:: Emitted for items that are defined outside of the current crate.
147 // mutable:: Emitted for mutable locals and statics as well as functions taking `&mut self`.
148 // public:: Emitted for items that are from the current crate and are `pub`.
149 // reference:: Emitted for locals behind a reference and functions taking `self` by reference.
150 // static:: Emitted for "static" functions, also known as functions that do not take a `self` param, as well as statics and consts.
151 // trait:: Emitted for associated trait items.
152 // unsafe:: Emitted for unsafe operations, like unsafe function calls, as well as the `unsafe` token.
155 // image::https://user-images.githubusercontent.com/48062697/113164457-06cfb980-9239-11eb-819b-0f93e646acf8.png[]
156 // image::https://user-images.githubusercontent.com/48062697/113187625-f7f50100-9250-11eb-825e-91c58f236071.png[]
157 pub(crate) fn highlight(
160 range_to_highlight: Option<TextRange>,
161 syntactic_name_ref_highlighting: bool,
163 let _p = profile::span("highlight");
164 let sema = Semantics::new(db);
166 // Determine the root based on the given range.
167 let (root, range_to_highlight) = {
168 let source_file = sema.parse(file_id);
169 let source_file = source_file.syntax();
170 match range_to_highlight {
172 let node = match source_file.covering_element(range) {
173 NodeOrToken::Node(it) => it,
174 NodeOrToken::Token(it) => it.parent().unwrap_or_else(|| source_file.clone()),
178 None => (source_file.clone(), source_file.text_range()),
182 let mut hl = highlights::Highlights::new(root.text_range());
186 InFile::new(file_id.into(), &root),
187 sema.scope(&root).krate(),
189 syntactic_name_ref_highlighting,
196 sema: &Semantics<RootDatabase>,
197 root: InFile<&SyntaxNode>,
198 krate: Option<hir::Crate>,
199 range_to_highlight: TextRange,
200 syntactic_name_ref_highlighting: bool,
202 let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
204 let mut current_macro_call: Option<ast::MacroCall> = None;
205 let mut current_attr_call = None;
206 let mut current_macro: Option<ast::Macro> = None;
207 let mut macro_highlighter = MacroHighlighter::default();
208 let mut inside_attribute = false;
210 // Walk all nodes, keeping track of whether we are inside a macro or not.
211 // If in macro, expand it first and highlight the expanded code.
212 for event in root.value.preorder_with_tokens() {
213 let range = match &event {
214 WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(),
217 // Element outside of the viewport, no need to highlight
218 if range_to_highlight.intersect(range).is_none() {
222 // set macro and attribute highlighting states
223 match event.clone() {
224 WalkEvent::Enter(NodeOrToken::Node(node)) => match ast::Item::cast(node.clone()) {
225 Some(ast::Item::MacroCall(mcall)) => {
226 current_macro_call = Some(mcall);
229 Some(ast::Item::MacroRules(mac)) => {
230 macro_highlighter.init();
231 current_macro = Some(mac.into());
234 Some(ast::Item::MacroDef(mac)) => {
235 macro_highlighter.init();
236 current_macro = Some(mac.into());
239 Some(item) if sema.is_attr_macro_call(&item) => current_attr_call = Some(item),
240 Some(item) if current_attr_call.is_none() => {
241 let adt = match item {
242 ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
243 ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
244 ast::Item::Union(it) => Some(ast::Adt::Union(it)),
248 Some(adt) if sema.is_derive_annotated(&adt) => {
249 current_attr_call = Some(adt.into());
254 None if ast::Attr::can_cast(node.kind()) => inside_attribute = true,
257 WalkEvent::Leave(NodeOrToken::Node(node)) => match ast::Item::cast(node.clone()) {
258 Some(ast::Item::MacroCall(mcall)) => {
259 assert_eq!(current_macro_call, Some(mcall));
260 current_macro_call = None;
262 Some(ast::Item::MacroRules(mac)) => {
263 assert_eq!(current_macro, Some(mac.into()));
264 current_macro = None;
265 macro_highlighter = MacroHighlighter::default();
267 Some(ast::Item::MacroDef(mac)) => {
268 assert_eq!(current_macro, Some(mac.into()));
269 current_macro = None;
270 macro_highlighter = MacroHighlighter::default();
272 Some(item) if current_attr_call.as_ref().map_or(false, |it| *it == item) => {
273 current_attr_call = None
275 None if ast::Attr::can_cast(node.kind()) => inside_attribute = false,
281 let element = match event {
282 WalkEvent::Enter(NodeOrToken::Token(tok)) if tok.kind() == WHITESPACE => continue,
283 WalkEvent::Enter(it) => it,
284 WalkEvent::Leave(NodeOrToken::Token(_)) => continue,
285 WalkEvent::Leave(NodeOrToken::Node(node)) => {
286 inject::doc_comment(hl, sema, root.with_value(&node));
291 if current_macro.is_some() {
292 if let Some(tok) = element.as_token() {
293 macro_highlighter.advance(tok);
297 // only attempt to descend if we are inside a macro call or attribute
298 // as calling `descend_into_macros_single` gets rather expensive if done for every single token
299 // additionally, do not descend into comments, descending maps down to doc attributes which get
300 // tagged as string literals.
301 let descend_token = (current_macro_call.is_some() || current_attr_call.is_some())
302 && element.kind() != COMMENT;
303 let element_to_highlight = if descend_token {
304 let token = match &element {
305 NodeOrToken::Node(_) => continue,
306 NodeOrToken::Token(tok) => tok.clone(),
308 let in_mcall_outside_tt = current_macro_call.is_some()
309 && token.parent().as_ref().map(SyntaxNode::kind) != Some(TOKEN_TREE);
310 let token = match in_mcall_outside_tt {
311 // not in the macros token tree, don't attempt to descend
313 false => sema.descend_into_macros_single(token),
315 match token.parent() {
317 // Names and NameRefs have special semantics, use them instead of the tokens
318 // as otherwise we won't ever visit them
319 match (token.kind(), parent.kind()) {
320 (T![ident], NAME | NAME_REF) => parent.into(),
321 (T![self] | T![super] | T![crate], NAME_REF) => parent.into(),
322 (INT_NUMBER, NAME_REF) => parent.into(),
326 None => token.into(),
332 // FIXME: do proper macro def highlighting https://github.com/rust-analyzer/rust-analyzer/issues/6232
333 // Skip metavariables from being highlighted to prevent keyword highlighting in them
334 if macro_highlighter.highlight(&element_to_highlight).is_some() {
338 // string highlight injections, note this does not use the descended element as proc-macros
339 // can rewrite string literals which invalidates our indices
340 if let (Some(token), Some(token_to_highlight)) =
341 (element.into_token(), element_to_highlight.as_token())
343 let string = ast::String::cast(token);
344 let string_to_highlight = ast::String::cast(token_to_highlight.clone());
345 if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
347 if inject::ra_fixture(hl, sema, &string, &expanded_string).is_some() {
351 highlight_format_string(hl, &string, &expanded_string, range);
352 // Highlight escape sequences
353 string.escaped_char_ranges(&mut |piece_range, char| {
358 if string.text()[piece_range.start().into()..].starts_with('\\') {
360 range: piece_range + range.start(),
361 highlight: HlTag::EscapeSequence.into(),
369 // do the normal highlighting
370 let element = match element_to_highlight {
371 NodeOrToken::Node(node) => highlight::node(
374 &mut bindings_shadow_count,
375 syntactic_name_ref_highlighting,
378 NodeOrToken::Token(token) => highlight::token(sema, token).zip(Some(None)),
380 if let Some((mut highlight, binding_hash)) = element {
381 if inside_attribute {
382 highlight |= HlMod::Attribute
385 hl.add(HlRange { range, highlight, binding_hash });