From d7440a5f4928415f2d2a9f7b2badaff8a9376a09 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 17:39:12 +0300 Subject: highlight macro idents --- crates/ra_editor/src/lib.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index d9b89155b..9043026c1 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, -- cgit v1.2.3