2021-01-09 11:45:47 +00:00
|
|
|
pub(crate) mod tags;
|
|
|
|
|
2021-01-07 22:39:02 +00:00
|
|
|
mod highlights;
|
|
|
|
mod injector;
|
|
|
|
|
2021-01-09 20:07:32 +00:00
|
|
|
mod highlight;
|
2020-10-13 22:56:41 +00:00
|
|
|
mod format;
|
2021-03-27 05:48:15 +00:00
|
|
|
mod macro_;
|
2021-01-09 20:07:32 +00:00
|
|
|
mod inject;
|
2021-01-09 11:45:47 +00:00
|
|
|
|
|
|
|
mod html;
|
2020-02-27 13:15:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-02-26 16:08:15 +00:00
|
|
|
|
2021-03-18 12:16:27 +00:00
|
|
|
use hir::{InFile, Name, Semantics};
|
2022-01-02 18:10:10 +00:00
|
|
|
use ide_db::RootDatabase;
|
2020-08-12 16:26:51 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use syntax::{
|
2022-01-15 12:14:59 +00:00
|
|
|
ast::{self, IsString},
|
2022-01-02 18:10:10 +00:00
|
|
|
AstNode, AstToken, NodeOrToken,
|
2021-01-09 20:07:32 +00:00
|
|
|
SyntaxKind::*,
|
|
|
|
SyntaxNode, TextRange, WalkEvent, T,
|
2020-01-20 16:06:47 +00:00
|
|
|
};
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-10-14 17:23:59 +00:00
|
|
|
use crate::{
|
2020-11-02 15:31:38 +00:00
|
|
|
syntax_highlighting::{
|
2021-03-27 05:48:15 +00:00
|
|
|
format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
|
|
|
|
tags::Highlight,
|
2020-11-02 15:31:38 +00:00
|
|
|
},
|
2021-01-20 14:25:34 +00:00
|
|
|
FileId, HlMod, HlTag,
|
2020-10-14 17:23:59 +00:00
|
|
|
};
|
2019-03-23 16:34:49 +00:00
|
|
|
|
2020-02-26 22:13:48 +00:00
|
|
|
pub(crate) use html::highlight_as_html;
|
|
|
|
|
2021-01-09 12:54:38 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-01-09 11:48:15 +00:00
|
|
|
pub struct HlRange {
|
2019-03-23 16:34:49 +00:00
|
|
|
pub range: TextRange,
|
2020-02-26 18:39:32 +00:00
|
|
|
pub highlight: Highlight,
|
2019-05-25 14:23:58 +00:00
|
|
|
pub binding_hash: Option<u64>,
|
2019-03-23 16:34:49 +00:00
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-05-31 09:29:19 +00:00
|
|
|
// Feature: Semantic Syntax Highlighting
|
|
|
|
//
|
|
|
|
// rust-analyzer highlights the code semantically.
|
2021-05-26 13:23:05 +00:00
|
|
|
// For example, `Bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
|
|
|
|
// rust-analyzer does not specify colors directly, instead it assigns a tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
|
2020-05-31 09:29:19 +00:00
|
|
|
// It's up to the client to map those to specific colors.
|
|
|
|
//
|
|
|
|
// The general rule is that a reference to an entity gets colored the same way as the entity itself.
|
|
|
|
// We also give special modifier for `mut` and `&mut` local variables.
|
2021-03-31 15:20:54 +00:00
|
|
|
//
|
2021-05-26 13:23:05 +00:00
|
|
|
//
|
|
|
|
// .Token Tags
|
|
|
|
//
|
|
|
|
// Rust-analyzer currently emits the following token tags:
|
|
|
|
//
|
|
|
|
// - For items:
|
|
|
|
// +
|
|
|
|
// [horizontal]
|
2021-12-04 17:23:25 +00:00
|
|
|
// attribute:: Emitted for attribute macros.
|
2021-05-26 13:23:05 +00:00
|
|
|
// enum:: Emitted for enums.
|
|
|
|
// function:: Emitted for free-standing functions.
|
2021-12-04 17:23:25 +00:00
|
|
|
// derive:: Emitted for derive macros.
|
|
|
|
// macro:: Emitted for function-like macros.
|
2021-05-26 13:23:05 +00:00
|
|
|
// method:: Emitted for associated functions, also knowns as methods.
|
|
|
|
// namespace:: Emitted for modules.
|
|
|
|
// struct:: Emitted for structs.
|
|
|
|
// trait:: Emitted for traits.
|
|
|
|
// typeAlias:: Emitted for type aliases and `Self` in `impl`s.
|
|
|
|
// union:: Emitted for unions.
|
|
|
|
//
|
|
|
|
// - For literals:
|
|
|
|
// +
|
|
|
|
// [horizontal]
|
|
|
|
// boolean:: Emitted for the boolean literals `true` and `false`.
|
|
|
|
// character:: Emitted for character literals.
|
|
|
|
// number:: Emitted for numeric literals.
|
|
|
|
// string:: Emitted for string literals.
|
|
|
|
// escapeSequence:: Emitted for escaped sequences inside strings like `\n`.
|
|
|
|
// formatSpecifier:: Emitted for format specifiers `{:?}` in `format!`-like macros.
|
|
|
|
//
|
|
|
|
// - For operators:
|
|
|
|
// +
|
|
|
|
// [horizontal]
|
|
|
|
// operator:: Emitted for general operators.
|
|
|
|
// arithmetic:: Emitted for the arithmetic operators `+`, `-`, `*`, `/`, `+=`, `-=`, `*=`, `/=`.
|
|
|
|
// bitwise:: Emitted for the bitwise operators `|`, `&`, `!`, `^`, `|=`, `&=`, `^=`.
|
|
|
|
// comparison:: Emitted for the comparison operators `>`, `<`, `==`, `>=`, `<=`, `!=`.
|
|
|
|
// logical:: Emitted for the logical operators `||`, `&&`, `!`.
|
|
|
|
//
|
|
|
|
// - For punctuation:
|
|
|
|
// +
|
|
|
|
// [horizontal]
|
|
|
|
// punctuation:: Emitted for general punctuation.
|
2021-12-04 17:23:25 +00:00
|
|
|
// attributeBracket:: Emitted for attribute invocation brackets, that is the `#[` and `]` tokens.
|
2021-05-26 13:23:05 +00:00
|
|
|
// angle:: Emitted for `<>` angle brackets.
|
|
|
|
// brace:: Emitted for `{}` braces.
|
|
|
|
// bracket:: Emitted for `[]` brackets.
|
|
|
|
// parenthesis:: Emitted for `()` parentheses.
|
|
|
|
// colon:: Emitted for the `:` token.
|
|
|
|
// comma:: Emitted for the `,` token.
|
|
|
|
// dot:: Emitted for the `.` token.
|
2022-01-02 18:10:10 +00:00
|
|
|
// semi:: Emitted for the `;` token.
|
|
|
|
// macroBang:: Emitted for the `!` token in macro calls.
|
2021-05-26 13:23:05 +00:00
|
|
|
//
|
|
|
|
// //-
|
|
|
|
//
|
|
|
|
// [horizontal]
|
2021-06-28 19:34:30 +00:00
|
|
|
// builtinAttribute:: Emitted for names to builtin attributes in attribute path, the `repr` in `#[repr(u8)]` for example.
|
2021-05-26 13:23:05 +00:00
|
|
|
// builtinType:: Emitted for builtin types like `u32`, `str` and `f32`.
|
|
|
|
// comment:: Emitted for comments.
|
|
|
|
// constParameter:: Emitted for const parameters.
|
|
|
|
// enumMember:: Emitted for enum variants.
|
|
|
|
// generic:: Emitted for generic tokens that have no mapping.
|
|
|
|
// keyword:: Emitted for keywords.
|
|
|
|
// label:: Emitted for labels.
|
|
|
|
// lifetime:: Emitted for lifetimes.
|
|
|
|
// parameter:: Emitted for non-self function parameters.
|
|
|
|
// property:: Emitted for struct and union fields.
|
|
|
|
// selfKeyword:: Emitted for the self function parameter and self path-specifier.
|
2021-12-03 16:15:19 +00:00
|
|
|
// toolModule:: Emitted for tool modules.
|
2021-05-26 13:23:05 +00:00
|
|
|
// typeParameter:: Emitted for type parameters.
|
|
|
|
// unresolvedReference:: Emitted for unresolved references, names that rust-analyzer can't find the definition of.
|
|
|
|
// variable:: Emitted for locals, constants and statics.
|
|
|
|
//
|
|
|
|
//
|
2021-05-26 11:29:00 +00:00
|
|
|
// .Token Modifiers
|
|
|
|
//
|
|
|
|
// Token modifiers allow to style some elements in the source code more precisely.
|
|
|
|
//
|
|
|
|
// Rust-analyzer currently emits the following token modifiers:
|
|
|
|
//
|
|
|
|
// [horizontal]
|
|
|
|
// async:: Emitted for async functions and the `async` and `await` keywords.
|
|
|
|
// attribute:: Emitted for tokens inside attributes.
|
|
|
|
// callable:: Emitted for locals whose types implements one of the `Fn*` traits.
|
2021-05-26 13:23:05 +00:00
|
|
|
// constant:: Emitted for consts.
|
2021-05-26 11:29:00 +00:00
|
|
|
// consuming:: Emitted for locals that are being consumed when use in a function call.
|
|
|
|
// controlFlow:: Emitted for control-flow related tokens, this includes the `?` operator.
|
2021-09-30 15:01:31 +00:00
|
|
|
// crateRoot:: Emitted for crate names, like `serde` and `crate`.
|
2021-05-26 11:29:00 +00:00
|
|
|
// declaration:: Emitted for names of definitions, like `foo` in `fn foo() {}`.
|
2021-09-30 15:58:09 +00:00
|
|
|
// defaultLibrary:: Emitted for items from built-in crates (std, core, alloc, test and proc_macro).
|
2021-05-26 11:29:00 +00:00
|
|
|
// documentation:: Emitted for documentation comments.
|
|
|
|
// injected:: Emitted for doc-string injected highlighting like rust source blocks in documentation.
|
|
|
|
// intraDocLink:: Emitted for intra doc links in doc-strings.
|
|
|
|
// library:: Emitted for items that are defined outside of the current crate.
|
2021-08-04 12:17:56 +00:00
|
|
|
// mutable:: Emitted for mutable locals and statics as well as functions taking `&mut self`.
|
2021-05-27 23:25:32 +00:00
|
|
|
// public:: Emitted for items that are from the current crate and are `pub`.
|
2021-08-09 11:06:14 +00:00
|
|
|
// reference:: Emitted for locals behind a reference and functions taking `self` by reference.
|
2021-05-26 13:23:05 +00:00
|
|
|
// static:: Emitted for "static" functions, also known as functions that do not take a `self` param, as well as statics and consts.
|
2021-05-26 11:29:00 +00:00
|
|
|
// trait:: Emitted for associated trait items.
|
|
|
|
// unsafe:: Emitted for unsafe operations, like unsafe function calls, as well as the `unsafe` token.
|
|
|
|
//
|
|
|
|
//
|
2021-03-31 15:20:54 +00:00
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113164457-06cfb980-9239-11eb-819b-0f93e646acf8.png[]
|
2021-03-31 17:45:15 +00:00
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113187625-f7f50100-9250-11eb-825e-91c58f236071.png[]
|
2020-02-25 19:44:47 +00:00
|
|
|
pub(crate) fn highlight(
|
2020-02-25 13:38:50 +00:00
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
2020-02-27 10:37:21 +00:00
|
|
|
range_to_highlight: Option<TextRange>,
|
2020-06-14 12:43:43 +00:00
|
|
|
syntactic_name_ref_highlighting: bool,
|
2021-01-09 11:48:15 +00:00
|
|
|
) -> Vec<HlRange> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("highlight");
|
2020-02-18 17:35:10 +00:00
|
|
|
let sema = Semantics::new(db);
|
2020-02-27 10:37:21 +00:00
|
|
|
|
|
|
|
// Determine the root based on the given range.
|
|
|
|
let (root, range_to_highlight) = {
|
|
|
|
let source_file = sema.parse(file_id);
|
2021-07-15 19:28:30 +00:00
|
|
|
let source_file = source_file.syntax();
|
2020-02-27 10:37:21 +00:00
|
|
|
match range_to_highlight {
|
|
|
|
Some(range) => {
|
2021-07-15 19:28:30 +00:00
|
|
|
let node = match source_file.covering_element(range) {
|
2020-02-27 10:37:21 +00:00
|
|
|
NodeOrToken::Node(it) => it,
|
2021-07-15 19:28:30 +00:00
|
|
|
NodeOrToken::Token(it) => it.parent().unwrap_or_else(|| source_file.clone()),
|
2020-02-27 10:37:21 +00:00
|
|
|
};
|
|
|
|
(node, range)
|
|
|
|
}
|
2021-07-15 19:28:30 +00:00
|
|
|
None => (source_file.clone(), source_file.text_range()),
|
2020-02-27 10:37:21 +00:00
|
|
|
}
|
|
|
|
};
|
2020-02-25 13:38:50 +00:00
|
|
|
|
2021-02-04 10:37:14 +00:00
|
|
|
let mut hl = highlights::Highlights::new(root.text_range());
|
2021-03-18 12:16:27 +00:00
|
|
|
traverse(
|
|
|
|
&mut hl,
|
|
|
|
&sema,
|
2022-02-26 12:53:54 +00:00
|
|
|
file_id,
|
|
|
|
&root,
|
2021-05-23 13:45:26 +00:00
|
|
|
sema.scope(&root).krate(),
|
2021-03-18 12:16:27 +00:00
|
|
|
range_to_highlight,
|
|
|
|
syntactic_name_ref_highlighting,
|
|
|
|
);
|
2021-01-09 20:07:32 +00:00
|
|
|
hl.to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse(
|
|
|
|
hl: &mut Highlights,
|
|
|
|
sema: &Semantics<RootDatabase>,
|
2022-02-26 12:53:54 +00:00
|
|
|
file_id: FileId,
|
|
|
|
root: &SyntaxNode,
|
2021-05-23 13:45:26 +00:00
|
|
|
krate: Option<hir::Crate>,
|
2021-01-09 20:07:32 +00:00
|
|
|
range_to_highlight: TextRange,
|
|
|
|
syntactic_name_ref_highlighting: bool,
|
|
|
|
) {
|
2022-02-26 12:53:54 +00:00
|
|
|
let is_unlinked = sema.to_module_def(file_id).is_none();
|
2021-01-09 20:07:32 +00:00
|
|
|
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
2019-05-25 14:23:58 +00:00
|
|
|
|
2020-10-14 17:23:59 +00:00
|
|
|
let mut current_macro_call: Option<ast::MacroCall> = None;
|
2021-09-30 17:12:37 +00:00
|
|
|
let mut current_attr_call = None;
|
2022-03-03 21:59:34 +00:00
|
|
|
let mut current_derive_call = None;
|
2021-03-27 05:48:15 +00:00
|
|
|
let mut current_macro: Option<ast::Macro> = None;
|
|
|
|
let mut macro_highlighter = MacroHighlighter::default();
|
2020-11-21 11:51:05 +00:00
|
|
|
let mut inside_attribute = false;
|
2020-01-20 16:06:47 +00:00
|
|
|
|
2020-02-27 13:00:51 +00:00
|
|
|
// Walk all nodes, keeping track of whether we are inside a macro or not.
|
|
|
|
// If in macro, expand it first and highlight the expanded code.
|
2022-02-26 12:53:54 +00:00
|
|
|
for event in root.preorder_with_tokens() {
|
2022-01-02 18:10:10 +00:00
|
|
|
let range = match &event {
|
2021-01-07 22:39:02 +00:00
|
|
|
WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(),
|
2020-02-27 10:39:54 +00:00
|
|
|
};
|
2020-02-25 19:44:47 +00:00
|
|
|
|
2020-02-27 13:00:51 +00:00
|
|
|
// Element outside of the viewport, no need to highlight
|
2022-01-02 18:10:10 +00:00
|
|
|
if range_to_highlight.intersect(range).is_none() {
|
2020-02-27 10:39:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:10:10 +00:00
|
|
|
// set macro and attribute highlighting states
|
2021-07-15 19:28:30 +00:00
|
|
|
match event.clone() {
|
2022-01-02 18:10:10 +00:00
|
|
|
WalkEvent::Enter(NodeOrToken::Node(node)) => match ast::Item::cast(node.clone()) {
|
|
|
|
Some(ast::Item::MacroCall(mcall)) => {
|
|
|
|
current_macro_call = Some(mcall);
|
|
|
|
continue;
|
2020-02-27 10:39:54 +00:00
|
|
|
}
|
2022-01-02 18:10:10 +00:00
|
|
|
Some(ast::Item::MacroRules(mac)) => {
|
|
|
|
macro_highlighter.init();
|
|
|
|
current_macro = Some(mac.into());
|
|
|
|
continue;
|
2021-06-07 17:32:28 +00:00
|
|
|
}
|
2022-01-02 18:10:10 +00:00
|
|
|
Some(ast::Item::MacroDef(mac)) => {
|
|
|
|
macro_highlighter.init();
|
|
|
|
current_macro = Some(mac.into());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Some(item) if sema.is_attr_macro_call(&item) => current_attr_call = Some(item),
|
2022-02-21 12:21:25 +00:00
|
|
|
Some(item) if current_attr_call.is_none() => {
|
|
|
|
let adt = match item {
|
|
|
|
ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
|
|
|
|
ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
|
|
|
|
ast::Item::Union(it) => Some(ast::Adt::Union(it)),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
match adt {
|
|
|
|
Some(adt) if sema.is_derive_annotated(&adt) => {
|
2022-03-03 21:59:34 +00:00
|
|
|
current_derive_call = Some(ast::Item::from(adt));
|
2022-02-21 12:21:25 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 18:10:10 +00:00
|
|
|
None if ast::Attr::can_cast(node.kind()) => inside_attribute = true,
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
WalkEvent::Leave(NodeOrToken::Node(node)) => match ast::Item::cast(node.clone()) {
|
|
|
|
Some(ast::Item::MacroCall(mcall)) => {
|
|
|
|
assert_eq!(current_macro_call, Some(mcall));
|
|
|
|
current_macro_call = None;
|
|
|
|
}
|
|
|
|
Some(ast::Item::MacroRules(mac)) => {
|
|
|
|
assert_eq!(current_macro, Some(mac.into()));
|
|
|
|
current_macro = None;
|
|
|
|
macro_highlighter = MacroHighlighter::default();
|
|
|
|
}
|
|
|
|
Some(ast::Item::MacroDef(mac)) => {
|
|
|
|
assert_eq!(current_macro, Some(mac.into()));
|
|
|
|
current_macro = None;
|
|
|
|
macro_highlighter = MacroHighlighter::default();
|
|
|
|
}
|
|
|
|
Some(item) if current_attr_call.as_ref().map_or(false, |it| *it == item) => {
|
2022-03-03 21:59:34 +00:00
|
|
|
current_attr_call = None;
|
|
|
|
}
|
|
|
|
Some(item) if current_derive_call.as_ref().map_or(false, |it| *it == item) => {
|
|
|
|
current_derive_call = None;
|
2022-01-02 18:10:10 +00:00
|
|
|
}
|
|
|
|
None if ast::Attr::can_cast(node.kind()) => inside_attribute = false,
|
|
|
|
_ => (),
|
|
|
|
},
|
2020-04-28 09:01:51 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:00:51 +00:00
|
|
|
let element = match event {
|
2022-01-02 21:22:13 +00:00
|
|
|
WalkEvent::Enter(NodeOrToken::Token(tok)) if tok.kind() == WHITESPACE => continue,
|
2020-02-27 10:56:42 +00:00
|
|
|
WalkEvent::Enter(it) => it,
|
2022-01-02 18:10:10 +00:00
|
|
|
WalkEvent::Leave(NodeOrToken::Token(_)) => continue,
|
|
|
|
WalkEvent::Leave(NodeOrToken::Node(node)) => {
|
2022-02-26 12:53:54 +00:00
|
|
|
inject::doc_comment(hl, sema, InFile::new(file_id.into(), &node));
|
2021-01-09 20:07:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-27 10:56:42 +00:00
|
|
|
};
|
2020-02-27 15:05:35 +00:00
|
|
|
|
2021-03-27 05:48:15 +00:00
|
|
|
if current_macro.is_some() {
|
2020-12-15 14:37:37 +00:00
|
|
|
if let Some(tok) = element.as_token() {
|
2021-03-27 05:48:15 +00:00
|
|
|
macro_highlighter.advance(tok);
|
2020-12-15 14:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:10:10 +00:00
|
|
|
// only attempt to descend if we are inside a macro call or attribute
|
|
|
|
// as calling `descend_into_macros_single` gets rather expensive if done for every single token
|
2022-01-03 01:50:49 +00:00
|
|
|
// additionally, do not descend into comments, descending maps down to doc attributes which get
|
|
|
|
// tagged as string literals.
|
2022-03-03 21:59:34 +00:00
|
|
|
let descend_token = (current_macro_call.is_some()
|
|
|
|
|| current_attr_call.is_some()
|
|
|
|
|| current_derive_call.is_some())
|
2022-01-03 01:50:49 +00:00
|
|
|
&& element.kind() != COMMENT;
|
2021-09-30 17:12:37 +00:00
|
|
|
let element_to_highlight = if descend_token {
|
2022-01-02 18:10:10 +00:00
|
|
|
let token = match &element {
|
|
|
|
NodeOrToken::Node(_) => continue,
|
|
|
|
NodeOrToken::Token(tok) => tok.clone(),
|
|
|
|
};
|
2022-03-03 21:59:34 +00:00
|
|
|
let in_mcall_outside_tt = current_attr_call.is_none()
|
2022-01-02 18:10:10 +00:00
|
|
|
&& token.parent().as_ref().map(SyntaxNode::kind) != Some(TOKEN_TREE);
|
|
|
|
let token = match in_mcall_outside_tt {
|
2022-03-03 21:59:34 +00:00
|
|
|
// not in the macros/derives token tree, don't attempt to descend
|
2022-01-02 18:10:10 +00:00
|
|
|
true => token,
|
|
|
|
false => sema.descend_into_macros_single(token),
|
2021-06-07 17:32:28 +00:00
|
|
|
};
|
|
|
|
match token.parent() {
|
|
|
|
Some(parent) => {
|
2022-01-02 18:10:10 +00:00
|
|
|
// Names and NameRefs have special semantics, use them instead of the tokens
|
|
|
|
// as otherwise we won't ever visit them
|
2021-06-07 17:32:28 +00:00
|
|
|
match (token.kind(), parent.kind()) {
|
2021-09-30 17:12:37 +00:00
|
|
|
(T![ident], NAME | NAME_REF) => parent.into(),
|
2022-03-05 22:20:06 +00:00
|
|
|
(T![self] | T![super] | T![crate] | T![Self], NAME_REF) => parent.into(),
|
2021-09-30 19:36:20 +00:00
|
|
|
(INT_NUMBER, NAME_REF) => parent.into(),
|
2022-03-03 21:50:43 +00:00
|
|
|
(LIFETIME_IDENT, LIFETIME) => parent.into(),
|
2021-06-07 17:32:28 +00:00
|
|
|
_ => token.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => token.into(),
|
|
|
|
}
|
2020-02-27 13:00:51 +00:00
|
|
|
} else {
|
2020-02-27 15:05:35 +00:00
|
|
|
element.clone()
|
2020-02-27 13:00:51 +00:00
|
|
|
};
|
2020-02-27 10:56:42 +00:00
|
|
|
|
2022-01-02 18:10:10 +00:00
|
|
|
// FIXME: do proper macro def highlighting https://github.com/rust-analyzer/rust-analyzer/issues/6232
|
|
|
|
// Skip metavariables from being highlighted to prevent keyword highlighting in them
|
|
|
|
if macro_highlighter.highlight(&element_to_highlight).is_some() {
|
2021-11-23 21:05:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:10:10 +00:00
|
|
|
// string highlight injections, note this does not use the descended element as proc-macros
|
|
|
|
// can rewrite string literals which invalidates our indices
|
2021-11-23 21:05:52 +00:00
|
|
|
if let (Some(token), Some(token_to_highlight)) =
|
|
|
|
(element.into_token(), element_to_highlight.as_token())
|
|
|
|
{
|
|
|
|
let string = ast::String::cast(token);
|
|
|
|
let string_to_highlight = ast::String::cast(token_to_highlight.clone());
|
|
|
|
if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
|
|
|
|
if string.is_raw() {
|
|
|
|
if inject::ra_fixture(hl, sema, &string, &expanded_string).is_some() {
|
2021-09-30 17:12:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-06 21:21:56 +00:00
|
|
|
}
|
2021-11-23 21:05:52 +00:00
|
|
|
highlight_format_string(hl, &string, &expanded_string, range);
|
|
|
|
// Highlight escape sequences
|
2022-01-15 12:14:59 +00:00
|
|
|
string.escaped_char_ranges(&mut |piece_range, char| {
|
|
|
|
if char.is_err() {
|
|
|
|
return;
|
2021-11-23 21:05:52 +00:00
|
|
|
}
|
2022-01-15 12:14:59 +00:00
|
|
|
|
|
|
|
if string.text()[piece_range.start().into()..].starts_with('\\') {
|
|
|
|
hl.add(HlRange {
|
|
|
|
range: piece_range + range.start(),
|
|
|
|
highlight: HlTag::EscapeSequence.into(),
|
|
|
|
binding_hash: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-02-27 15:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:10:10 +00:00
|
|
|
// do the normal highlighting
|
2022-01-02 21:22:13 +00:00
|
|
|
let element = match element_to_highlight {
|
|
|
|
NodeOrToken::Node(node) => highlight::node(
|
|
|
|
sema,
|
|
|
|
krate,
|
|
|
|
&mut bindings_shadow_count,
|
|
|
|
syntactic_name_ref_highlighting,
|
|
|
|
node,
|
|
|
|
),
|
2022-02-21 01:42:58 +00:00
|
|
|
NodeOrToken::Token(token) => highlight::token(sema, token).zip(Some(None)),
|
2022-01-02 21:22:13 +00:00
|
|
|
};
|
2022-01-02 18:10:10 +00:00
|
|
|
if let Some((mut highlight, binding_hash)) = element {
|
2022-02-26 12:53:54 +00:00
|
|
|
if is_unlinked && highlight.tag == HlTag::UnresolvedReference {
|
|
|
|
// do not emit unresolved references if the file is unlinked
|
|
|
|
// let the editor do its highlighting for these tokens instead
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-21 11:51:05 +00:00
|
|
|
if inside_attribute {
|
2021-10-16 11:32:55 +00:00
|
|
|
highlight |= HlMod::Attribute
|
2020-11-21 11:51:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 08:57:17 +00:00
|
|
|
hl.add(HlRange { range, highlight, binding_hash });
|
|
|
|
}
|
2020-01-20 16:06:47 +00:00
|
|
|
}
|
2020-05-31 09:29:19 +00:00
|
|
|
}
|