From d7440a5f4928415f2d2a9f7b2badaff8a9376a09 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 17:39:12 +0300 Subject: [PATCH] highlight macro idents --- crates/ra_analysis/src/syntax_highlighting.rs | 2 +- crates/ra_editor/src/lib.rs | 35 +++++++++++++++++-- editors/code/src/highlighting.ts | 3 +- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 80f51a09c9a..98551df8f45 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -108,7 +108,7 @@ mod tests { assert_eq_dbg( r#"[HighlightedRange { range: [13; 15), tag: "keyword" }, HighlightedRange { range: [16; 20), tag: "function" }, - HighlightedRange { range: [41; 45), tag: "text" }, + HighlightedRange { range: [41; 46), tag: "macro" }, HighlightedRange { range: [49; 52), tag: "keyword" }, HighlightedRange { range: [57; 59), tag: "literal" }, HighlightedRange { range: [49; 52), tag: "keyword" }, diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index d9b89155b66..9043026c1f3 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -24,9 +24,10 @@ use ra_syntax::{ SourceFileNode, Location, SyntaxKind::{self, *}, - SyntaxNodeRef, TextRange, TextUnit, + SyntaxNodeRef, TextRange, TextUnit, Direction, }; use itertools::Itertools; +use rustc_hash::FxHashSet; #[derive(Debug)] pub struct HighlightedRange { @@ -79,8 +80,13 @@ pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option Vec { + // Visited nodes to handle highlighting priorities + let mut highlighted = FxHashSet::default(); let mut res = Vec::new(); for node in file.syntax().descendants() { + if highlighted.contains(&node) { + continue; + } let tag = match node.kind() { COMMENT => "comment", STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", @@ -90,7 +96,30 @@ pub fn highlight(file: &SourceFileNode) -> Vec { INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", LIFETIME => "parameter", k if k.is_keyword() => "keyword", - _ => continue, + _ => { + if let Some(macro_call) = ast::MacroCall::cast(node) { + if let Some(path) = macro_call.path() { + if let Some(segment) = path.segment() { + if let Some(name_ref) = segment.name_ref() { + highlighted.insert(name_ref.syntax()); + let range_start = name_ref.syntax().range().start(); + let mut range_end = name_ref.syntax().range().end(); + for sibling in path.syntax().siblings(Direction::Next) { + match sibling.kind() { + EXCL | IDENT => range_end = sibling.range().end(), + _ => (), + } + } + res.push(HighlightedRange { + range: TextRange::from_to(range_start, range_end), + tag: "macro", + }) + } + } + } + } + continue; + } }; res.push(HighlightedRange { range: node.range(), @@ -235,7 +264,7 @@ fn main() {} r#"[HighlightedRange { range: [1; 11), tag: "comment" }, HighlightedRange { range: [12; 14), tag: "keyword" }, HighlightedRange { range: [15; 19), tag: "function" }, - HighlightedRange { range: [29; 36), tag: "text" }, + HighlightedRange { range: [29; 37), tag: "macro" }, HighlightedRange { range: [38; 50), tag: "string" }, HighlightedRange { range: [52; 54), tag: "literal" }]"#, &hls, diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index d440e77c7f7..2521dff6221 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -28,7 +28,8 @@ export class Highlighter { ['builtin', decor('#DD6718')], ['text', decor('#DCDCCC')], ['attribute', decor('#BFEBBF')], - ['literal', decor('#DFAF8F')] + ['literal', decor('#DFAF8F')], + ['macro', decor('#DFAF8F')] ]; return new Map(decorations);