2021-01-09 14:45:47 +03:00
|
|
|
pub(crate) mod tags;
|
|
|
|
|
2021-01-08 01:39:02 +03:00
|
|
|
mod highlights;
|
|
|
|
mod injector;
|
|
|
|
|
2021-01-09 23:07:32 +03:00
|
|
|
mod highlight;
|
2020-10-14 00:56:41 +02:00
|
|
|
mod format;
|
2020-10-14 19:23:59 +02:00
|
|
|
mod macro_rules;
|
2021-01-09 23:07:32 +03:00
|
|
|
mod inject;
|
2021-01-09 14:45:47 +03:00
|
|
|
|
|
|
|
mod html;
|
2020-02-27 14:15:32 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-02-26 17:08:15 +01:00
|
|
|
|
2021-01-09 23:07:32 +03:00
|
|
|
use hir::{Name, Semantics};
|
2021-01-20 15:25:34 +01:00
|
|
|
use ide_db::{RootDatabase, SymbolKind};
|
2020-08-12 18:26:51 +02:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
use syntax::{
|
2020-04-28 11:01:51 +02:00
|
|
|
ast::{self, HasFormatSpecifier},
|
2021-01-09 23:07:32 +03:00
|
|
|
AstNode, AstToken, Direction, NodeOrToken,
|
|
|
|
SyntaxKind::*,
|
|
|
|
SyntaxNode, TextRange, WalkEvent, T,
|
2020-01-21 00:06:47 +08:00
|
|
|
};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2020-10-14 19:23:59 +02:00
|
|
|
use crate::{
|
2020-11-02 16:31:38 +01:00
|
|
|
syntax_highlighting::{
|
2021-01-10 11:57:17 +03:00
|
|
|
format::highlight_format_string, highlights::Highlights,
|
2021-01-09 23:07:32 +03:00
|
|
|
macro_rules::MacroRulesHighlighter, tags::Highlight,
|
2020-11-02 16:31:38 +01:00
|
|
|
},
|
2021-01-20 15:25:34 +01:00
|
|
|
FileId, HlMod, HlTag,
|
2020-10-14 19:23:59 +02:00
|
|
|
};
|
2019-03-23 17:34:49 +01:00
|
|
|
|
2020-02-26 23:13:48 +01:00
|
|
|
pub(crate) use html::highlight_as_html;
|
|
|
|
|
2021-01-09 15:54:38 +03:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-01-09 14:48:15 +03:00
|
|
|
pub struct HlRange {
|
2019-03-23 17:34:49 +01:00
|
|
|
pub range: TextRange,
|
2020-02-26 19:39:32 +01:00
|
|
|
pub highlight: Highlight,
|
2019-05-25 16:23:58 +02:00
|
|
|
pub binding_hash: Option<u64>,
|
2019-03-23 17:34:49 +01:00
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2020-05-31 11:29:19 +02:00
|
|
|
// Feature: Semantic Syntax Highlighting
|
|
|
|
//
|
|
|
|
// rust-analyzer highlights the code semantically.
|
|
|
|
// 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 tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
|
|
|
|
// 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.
|
2020-02-25 14:44:47 -05:00
|
|
|
pub(crate) fn highlight(
|
2020-02-25 08:38:50 -05:00
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
2020-02-27 11:37:21 +01:00
|
|
|
range_to_highlight: Option<TextRange>,
|
2020-06-14 14:43:43 +02:00
|
|
|
syntactic_name_ref_highlighting: bool,
|
2021-01-09 14:48:15 +03:00
|
|
|
) -> Vec<HlRange> {
|
2020-08-12 16:32:36 +02:00
|
|
|
let _p = profile::span("highlight");
|
2020-02-18 18:35:10 +01:00
|
|
|
let sema = Semantics::new(db);
|
2020-02-27 11:37:21 +01:00
|
|
|
|
|
|
|
// Determine the root based on the given range.
|
|
|
|
let (root, range_to_highlight) = {
|
|
|
|
let source_file = sema.parse(file_id);
|
|
|
|
match range_to_highlight {
|
|
|
|
Some(range) => {
|
|
|
|
let node = match source_file.syntax().covering_element(range) {
|
|
|
|
NodeOrToken::Node(it) => it,
|
|
|
|
NodeOrToken::Token(it) => it.parent(),
|
|
|
|
};
|
|
|
|
(node, range)
|
|
|
|
}
|
|
|
|
None => (source_file.syntax().clone(), source_file.syntax().text_range()),
|
|
|
|
}
|
|
|
|
};
|
2020-02-25 08:38:50 -05:00
|
|
|
|
2021-02-04 11:37:14 +01:00
|
|
|
let mut hl = highlights::Highlights::new(root.text_range());
|
2021-01-09 23:07:32 +03:00
|
|
|
traverse(&mut hl, &sema, &root, range_to_highlight, syntactic_name_ref_highlighting);
|
|
|
|
hl.to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse(
|
|
|
|
hl: &mut Highlights,
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
root: &SyntaxNode,
|
|
|
|
range_to_highlight: TextRange,
|
|
|
|
syntactic_name_ref_highlighting: bool,
|
|
|
|
) {
|
|
|
|
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
2019-05-25 16:23:58 +02:00
|
|
|
|
2020-10-14 19:23:59 +02:00
|
|
|
let mut current_macro_call: Option<ast::MacroCall> = None;
|
2020-12-15 15:37:37 +01:00
|
|
|
let mut current_macro_rules: Option<ast::MacroRules> = None;
|
2020-10-14 22:21:58 +02:00
|
|
|
let mut macro_rules_highlighter = MacroRulesHighlighter::default();
|
2020-11-21 12:51:05 +01:00
|
|
|
let mut inside_attribute = false;
|
2020-01-21 00:06:47 +08:00
|
|
|
|
2020-02-27 14:00:51 +01: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.
|
2020-01-21 00:06:47 +08:00
|
|
|
for event in root.preorder_with_tokens() {
|
2020-02-27 11:39:54 +01:00
|
|
|
let event_range = match &event {
|
2021-01-08 01:39:02 +03:00
|
|
|
WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(),
|
2020-02-27 11:39:54 +01:00
|
|
|
};
|
2020-02-25 14:44:47 -05:00
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
// Element outside of the viewport, no need to highlight
|
2020-04-24 23:40:41 +02:00
|
|
|
if range_to_highlight.intersect(event_range).is_none() {
|
2020-02-27 11:39:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
// Track "inside macro" state
|
2020-02-27 11:56:42 +01:00
|
|
|
match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
|
|
|
|
WalkEvent::Enter(Some(mc)) => {
|
2020-02-27 14:00:51 +01:00
|
|
|
if let Some(range) = macro_call_range(&mc) {
|
2021-01-09 15:18:49 +03:00
|
|
|
hl.add(HlRange {
|
2020-02-27 11:56:42 +01:00
|
|
|
range,
|
2021-01-09 14:44:01 +03:00
|
|
|
highlight: HlTag::Symbol(SymbolKind::Macro).into(),
|
2020-02-27 11:56:42 +01:00
|
|
|
binding_hash: None,
|
|
|
|
});
|
2020-02-27 11:39:54 +01:00
|
|
|
}
|
2020-10-14 19:23:59 +02:00
|
|
|
current_macro_call = Some(mc.clone());
|
2020-02-27 11:56:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WalkEvent::Leave(Some(mc)) => {
|
2020-12-15 15:37:37 +01:00
|
|
|
assert_eq!(current_macro_call, Some(mc));
|
2020-02-27 11:56:42 +01:00
|
|
|
current_macro_call = None;
|
2020-12-15 15:37:37 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) {
|
|
|
|
WalkEvent::Enter(Some(mac)) => {
|
|
|
|
macro_rules_highlighter.init();
|
|
|
|
current_macro_rules = Some(mac);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WalkEvent::Leave(Some(mac)) => {
|
|
|
|
assert_eq!(current_macro_rules, Some(mac));
|
|
|
|
current_macro_rules = None;
|
2020-10-14 22:21:58 +02:00
|
|
|
macro_rules_highlighter = MacroRulesHighlighter::default();
|
2020-02-27 11:56:42 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2020-04-28 11:01:51 +02:00
|
|
|
match &event {
|
2020-11-21 12:51:05 +01:00
|
|
|
WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
|
|
|
|
inside_attribute = true
|
|
|
|
}
|
2021-01-09 23:07:32 +03:00
|
|
|
WalkEvent::Leave(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
|
|
|
|
inside_attribute = false
|
|
|
|
}
|
2020-04-28 11:01:51 +02:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
let element = match event {
|
2020-02-27 11:56:42 +01:00
|
|
|
WalkEvent::Enter(it) => it,
|
2021-01-09 23:07:32 +03:00
|
|
|
WalkEvent::Leave(it) => {
|
|
|
|
if let Some(node) = it.as_node() {
|
|
|
|
inject::doc_comment(hl, node);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-27 11:56:42 +01:00
|
|
|
};
|
2020-02-27 16:05:35 +01:00
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
let range = element.text_range();
|
2020-02-27 11:56:42 +01:00
|
|
|
|
2020-12-15 15:37:37 +01:00
|
|
|
if current_macro_rules.is_some() {
|
|
|
|
if let Some(tok) = element.as_token() {
|
|
|
|
macro_rules_highlighter.advance(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:02:42 +02:00
|
|
|
let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
|
2020-02-27 14:00:51 +01:00
|
|
|
// Inside a macro -- expand it first
|
2020-02-27 16:05:35 +01:00
|
|
|
let token = match element.clone().into_token() {
|
2020-02-27 14:00:51 +01:00
|
|
|
Some(it) if it.parent().kind() == TOKEN_TREE => it,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
let token = sema.descend_into_macros(token.clone());
|
|
|
|
let parent = token.parent();
|
2020-04-17 09:37:18 +02:00
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
// We only care Name and Name_ref
|
|
|
|
match (token.kind(), parent.kind()) {
|
|
|
|
(IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
|
|
|
|
_ => token.into(),
|
2020-01-21 00:06:47 +08:00
|
|
|
}
|
2020-02-27 14:00:51 +01:00
|
|
|
} else {
|
2020-02-27 16:05:35 +01:00
|
|
|
element.clone()
|
2020-02-27 14:00:51 +01:00
|
|
|
};
|
2020-02-27 11:56:42 +01:00
|
|
|
|
2020-11-06 22:21:56 +01:00
|
|
|
if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) {
|
|
|
|
if token.is_raw() {
|
|
|
|
let expanded = element_to_highlight.as_token().unwrap().clone();
|
2021-01-09 23:07:32 +03:00
|
|
|
if inject::ra_fixture(hl, &sema, token, expanded).is_some() {
|
2020-11-06 22:21:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-27 16:05:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:57:17 +03:00
|
|
|
if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-09 23:07:32 +03:00
|
|
|
if let Some((mut highlight, binding_hash)) = highlight::element(
|
2020-06-14 14:43:43 +02:00
|
|
|
&sema,
|
|
|
|
&mut bindings_shadow_count,
|
|
|
|
syntactic_name_ref_highlighting,
|
|
|
|
element_to_highlight.clone(),
|
|
|
|
) {
|
2020-11-21 12:51:05 +01:00
|
|
|
if inside_attribute {
|
2021-01-09 14:44:01 +03:00
|
|
|
highlight = highlight | HlMod::Attribute;
|
2020-11-21 12:51:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:57:17 +03:00
|
|
|
hl.add(HlRange { range, highlight, binding_hash });
|
|
|
|
}
|
2020-10-14 19:23:59 +02:00
|
|
|
|
2021-01-10 11:57:17 +03:00
|
|
|
if let Some(string) = element_to_highlight.as_token().cloned().and_then(ast::String::cast) {
|
|
|
|
highlight_format_string(hl, &string, range);
|
|
|
|
// Highlight escape sequences
|
|
|
|
if let Some(char_ranges) = string.char_ranges() {
|
|
|
|
for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) {
|
|
|
|
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-06-17 15:27:13 +02:00
|
|
|
}
|
2020-04-22 15:28:35 +02:00
|
|
|
}
|
2020-04-17 09:37:18 +02:00
|
|
|
}
|
2019-05-25 16:23:58 +02:00
|
|
|
}
|
2020-01-21 00:06:47 +08:00
|
|
|
}
|
2020-05-31 11:29:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 14:00:51 +01:00
|
|
|
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
|
2020-01-21 00:06:47 +08:00
|
|
|
let path = macro_call.path()?;
|
|
|
|
let name_ref = path.segment()?.name_ref()?;
|
|
|
|
|
|
|
|
let range_start = name_ref.syntax().text_range().start();
|
|
|
|
let mut range_end = name_ref.syntax().text_range().end();
|
|
|
|
for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
|
|
|
|
match sibling.kind() {
|
|
|
|
T![!] | IDENT => range_end = sibling.text_range().end(),
|
|
|
|
_ => (),
|
|
|
|
}
|
2019-05-23 19:42:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 23:40:41 +02:00
|
|
|
Some(TextRange::new(range_start, range_end))
|
2020-01-21 00:06:47 +08:00
|
|
|
}
|