Don't descend MacroCall TokenTree delimiters

This commit is contained in:
Lukas Wirth 2021-06-09 15:02:11 +02:00
parent 590472607c
commit 3c40b15d62
3 changed files with 20 additions and 2 deletions

View file

@ -17,7 +17,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use syntax::{ use syntax::{
algo::find_node_at_offset, algo::find_node_at_offset,
ast::{self, GenericParamsOwner, LoopBodyOwner}, ast::{self, GenericParamsOwner, LoopBodyOwner},
match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextSize, match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
}; };
use crate::{ use crate::{
@ -394,7 +394,15 @@ impl<'db> SemanticsImpl<'db> {
match node { match node {
ast::MacroCall(macro_call) => { ast::MacroCall(macro_call) => {
let tt = macro_call.token_tree()?; 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; return None;
} }
let file_id = sa.expand(self.db, token.with_value(&macro_call))?; let file_id = sa.expand(self.db, token.with_value(&macro_call))?;

View file

@ -148,6 +148,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="brace">}</span> <span class="brace">}</span>
<span class="brace">}</span> <span class="brace">}</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">dont_color_me_braces</span> <span class="brace">{</span>
<span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="numeric_literal">0</span><span class="brace">}</span>
<span class="brace">}</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="brace">{</span> <span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="brace">{</span>
<span class="parenthesis">(</span><span class="punctuation">$</span>expr<span class="colon">:</span>expr<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span> <span class="parenthesis">(</span><span class="punctuation">$</span>expr<span class="colon">:</span>expr<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
<span class="punctuation">$</span>expr <span class="punctuation">$</span>expr
@ -171,6 +175,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment">// comment</span> <span class="comment">// comment</span>
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span> <span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
<span class="macro">println!</span><span class="parenthesis">(</span><span class="string_literal">"Hello, {}!"</span><span class="comma">,</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span> <span class="macro">println!</span><span class="parenthesis">(</span><span class="string_literal">"Hello, {}!"</span><span class="comma">,</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="macro">dont_color_me_braces!</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> <span class="operator">=</span> <span class="unresolved_reference">Vec</span><span class="operator">::</span><span class="unresolved_reference">new</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span> <span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> <span class="operator">=</span> <span class="unresolved_reference">Vec</span><span class="operator">::</span><span class="unresolved_reference">new</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="keyword control">if</span> <span class="bool_literal">true</span> <span class="brace">{</span> <span class="keyword control">if</span> <span class="bool_literal">true</span> <span class="brace">{</span>

View file

@ -122,6 +122,10 @@ def_fn! {
} }
} }
macro_rules! dont_color_me_braces {
() => {0}
}
macro_rules! noop { macro_rules! noop {
($expr:expr) => { ($expr:expr) => {
$expr $expr
@ -145,6 +149,7 @@ macro without_args {
// comment // comment
fn main() { fn main() {
println!("Hello, {}!", 92); println!("Hello, {}!", 92);
dont_color_me_braces!();
let mut vec = Vec::new(); let mut vec = Vec::new();
if true { if true {