From 3c40b15d625df192eb17c34f15f7dbc23f5284b9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 9 Jun 2021 15:02:11 +0200 Subject: Don't descend MacroCall TokenTree delimiters --- crates/hir/src/semantics.rs | 12 ++++++++++-- .../ide/src/syntax_highlighting/test_data/highlighting.html | 5 +++++ crates/ide/src/syntax_highlighting/tests.rs | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 2d08a7704..c437dbdc6 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -17,7 +17,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use syntax::{ algo::find_node_at_offset, ast::{self, GenericParamsOwner, LoopBodyOwner}, - match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextSize, + match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize, }; use crate::{ @@ -394,7 +394,15 @@ impl<'db> SemanticsImpl<'db> { match node { ast::MacroCall(macro_call) => { let tt = macro_call.token_tree()?; - if !tt.syntax().text_range().contains_range(token.value.text_range()) { + let l_delim = match tt.left_delimiter_token() { + Some(it) => it.text_range().end(), + None => tt.syntax().text_range().start() + }; + let r_delim = match tt.right_delimiter_token() { + Some(it) => it.text_range().start(), + None => tt.syntax().text_range().end() + }; + if !TextRange::new(l_delim, r_delim).contains_range(token.value.text_range()) { return None; } let file_id = sa.expand(self.db, token.with_value(¯o_call))?; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 0264e39a3..a7b5c3b89 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -148,6 +148,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } } +macro_rules! dont_color_me_braces { + () => {0} +} + macro_rules! noop { ($expr:expr) => { $expr @@ -171,6 +175,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd // comment fn main() { println!("Hello, {}!", 92); + dont_color_me_braces!(); let mut vec = Vec::new(); if true { diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 662b53481..6ad2a362a 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -122,6 +122,10 @@ def_fn! { } } +macro_rules! dont_color_me_braces { + () => {0} +} + macro_rules! noop { ($expr:expr) => { $expr @@ -145,6 +149,7 @@ macro without_args { // comment fn main() { println!("Hello, {}!", 92); + dont_color_me_braces!(); let mut vec = Vec::new(); if true { -- cgit v1.2.3