rust-analyzer/crates/ide/src/syntax_highlighting.rs

244 lines
8.2 KiB
Rust
Raw Normal View History

2021-01-09 11:45:47 +00:00
pub(crate) mod tags;
mod highlights;
mod injector;
2021-01-09 20:07:32 +00:00
mod highlight;
2020-10-13 22:56:41 +00:00
mod format;
2020-10-14 17:23:59 +00:00
mod macro_rules;
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-01-09 20:07:32 +00:00
use hir::{Name, Semantics};
2021-01-20 14:25:34 +00:00
use ide_db::{RootDatabase, SymbolKind};
2020-08-12 16:26:51 +00:00
use rustc_hash::FxHashMap;
use syntax::{
ast::{self, HasFormatSpecifier},
2021-01-09 20:07:32 +00:00
AstNode, AstToken, Direction, NodeOrToken,
SyntaxKind::*,
SyntaxNode, TextRange, WalkEvent, T,
};
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-01-10 08:57:17 +00:00
format::highlight_format_string, highlights::Highlights,
2021-01-09 20:07:32 +00:00
macro_rules::MacroRulesHighlighter, 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
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,
pub binding_hash: Option<u64>,
2019-03-23 16:34:49 +00:00
}
2019-01-08 19:33:36 +00: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.
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>,
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");
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);
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 13:38:50 +00:00
2021-01-09 12:18:49 +00:00
let mut hl = highlights::Highlights::new(range_to_highlight);
2021-01-09 20:07:32 +00: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();
2020-10-14 17:23:59 +00:00
let mut current_macro_call: Option<ast::MacroCall> = None;
2020-12-15 14:37:37 +00:00
let mut current_macro_rules: Option<ast::MacroRules> = None;
2020-10-14 20:21:58 +00:00
let mut macro_rules_highlighter = MacroRulesHighlighter::default();
let mut inside_attribute = false;
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.
for event in root.preorder_with_tokens() {
2020-02-27 10:39:54 +00:00
let event_range = match &event {
WalkEvent::Enter(it) | WalkEvent::Leave(it) => it.text_range(),
2020-02-27 10:39:54 +00:00
};
2020-02-27 13:00:51 +00:00
// Element outside of the viewport, no need to highlight
2020-04-24 21:40:41 +00:00
if range_to_highlight.intersect(event_range).is_none() {
2020-02-27 10:39:54 +00:00
continue;
}
2020-02-27 13:00:51 +00:00
// Track "inside macro" state
2020-02-27 10:56:42 +00:00
match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
WalkEvent::Enter(Some(mc)) => {
2020-02-27 13:00:51 +00:00
if let Some(range) = macro_call_range(&mc) {
2021-01-09 12:18:49 +00:00
hl.add(HlRange {
2020-02-27 10:56:42 +00:00
range,
2021-01-09 11:44:01 +00:00
highlight: HlTag::Symbol(SymbolKind::Macro).into(),
2020-02-27 10:56:42 +00:00
binding_hash: None,
});
2020-02-27 10:39:54 +00:00
}
2020-10-14 17:23:59 +00:00
current_macro_call = Some(mc.clone());
2020-02-27 10:56:42 +00:00
continue;
}
WalkEvent::Leave(Some(mc)) => {
2020-12-15 14:37:37 +00:00
assert_eq!(current_macro_call, Some(mc));
2020-02-27 10:56:42 +00:00
current_macro_call = None;
2020-12-15 14:37:37 +00: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 20:21:58 +00:00
macro_rules_highlighter = MacroRulesHighlighter::default();
2020-02-27 10:56:42 +00:00
}
_ => (),
}
match &event {
WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
inside_attribute = true
}
2021-01-09 20:07:32 +00:00
WalkEvent::Leave(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
inside_attribute = false
}
_ => (),
}
2020-02-27 13:00:51 +00:00
let element = match event {
2020-02-27 10:56:42 +00:00
WalkEvent::Enter(it) => it,
2021-01-09 20:07:32 +00:00
WalkEvent::Leave(it) => {
if let Some(node) = it.as_node() {
inject::doc_comment(hl, node);
}
continue;
}
2020-02-27 10:56:42 +00:00
};
2020-02-27 15:05:35 +00:00
2020-02-27 13:00:51 +00:00
let range = element.text_range();
2020-02-27 10:56:42 +00:00
2020-12-15 14:37:37 +00:00
if current_macro_rules.is_some() {
if let Some(tok) = element.as_token() {
macro_rules_highlighter.advance(tok);
}
}
let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
2020-02-27 13:00:51 +00:00
// Inside a macro -- expand it first
2020-02-27 15:05:35 +00:00
let token = match element.clone().into_token() {
2020-02-27 13:00:51 +00:00
Some(it) if it.parent().kind() == TOKEN_TREE => it,
_ => continue,
};
let token = sema.descend_into_macros(token.clone());
let parent = token.parent();
2020-02-27 13:00:51 +00:00
// We only care Name and Name_ref
match (token.kind(), parent.kind()) {
(IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
_ => 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
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 20:07:32 +00:00
if inject::ra_fixture(hl, &sema, token, expanded).is_some() {
continue;
}
2020-02-27 15:05:35 +00:00
}
}
2021-01-10 08:57:17 +00:00
if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) {
continue;
}
2021-01-09 20:07:32 +00:00
if let Some((mut highlight, binding_hash)) = highlight::element(
&sema,
&mut bindings_shadow_count,
syntactic_name_ref_highlighting,
element_to_highlight.clone(),
) {
if inside_attribute {
2021-01-09 11:44:01 +00:00
highlight = highlight | HlMod::Attribute;
}
2021-01-10 08:57:17 +00:00
hl.add(HlRange { range, highlight, binding_hash });
}
2020-10-14 17:23:59 +00:00
2021-01-10 08:57:17 +00: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-02-27 13:00:51 +00:00
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
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(),
_ => (),
}
}
2020-04-24 21:40:41 +00:00
Some(TextRange::new(range_start, range_end))
}