mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
Unleash macro 2.0 in hightlight and more
This commit is contained in:
parent
a193666361
commit
4520002b63
12 changed files with 49 additions and 31 deletions
|
@ -768,7 +768,7 @@ to_def_impls![
|
||||||
(crate::TypeParam, ast::TypeParam, type_param_to_def),
|
(crate::TypeParam, ast::TypeParam, type_param_to_def),
|
||||||
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
|
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
|
||||||
(crate::ConstParam, ast::ConstParam, const_param_to_def),
|
(crate::ConstParam, ast::ConstParam, const_param_to_def),
|
||||||
(crate::MacroDef, ast::MacroRules, macro_rules_to_def),
|
(crate::MacroDef, ast::Macro, macro_to_def),
|
||||||
(crate::Local, ast::IdentPat, bind_pat_to_def),
|
(crate::Local, ast::IdentPat, bind_pat_to_def),
|
||||||
(crate::Local, ast::SelfParam, self_param_to_def),
|
(crate::Local, ast::SelfParam, self_param_to_def),
|
||||||
(crate::Label, ast::Label, label_to_def),
|
(crate::Label, ast::Label, label_to_def),
|
||||||
|
|
|
@ -191,10 +191,7 @@ impl SourceToDefCtx<'_, '_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: use DynMap as well?
|
// FIXME: use DynMap as well?
|
||||||
pub(super) fn macro_rules_to_def(
|
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
|
||||||
&mut self,
|
|
||||||
src: InFile<ast::MacroRules>,
|
|
||||||
) -> Option<MacroDefId> {
|
|
||||||
let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
|
let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
|
||||||
let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
|
let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
|
||||||
let kind = MacroDefKind::Declarative(ast_id);
|
let kind = MacroDefKind::Declarative(ast_id);
|
||||||
|
|
|
@ -161,7 +161,7 @@ pub(crate) fn doc_owner_to_def(
|
||||||
ast::Variant(it) => sema.to_def(&it)?.into(),
|
ast::Variant(it) => sema.to_def(&it)?.into(),
|
||||||
ast::Trait(it) => sema.to_def(&it)?.into(),
|
ast::Trait(it) => sema.to_def(&it)?.into(),
|
||||||
ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
|
ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
|
||||||
ast::MacroRules(it) => return sema.to_def(&it).map(Definition::Macro),
|
ast::Macro(it) => return sema.to_def(&it).map(Definition::Macro),
|
||||||
ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
|
ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
|
||||||
ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
|
ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
|
|
|
@ -172,7 +172,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
|
||||||
};
|
};
|
||||||
Some(node)
|
Some(node)
|
||||||
},
|
},
|
||||||
ast::MacroRules(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
|
ast::Macro(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@ fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -
|
||||||
SyntaxKind::STATIC,
|
SyntaxKind::STATIC,
|
||||||
SyntaxKind::CONST,
|
SyntaxKind::CONST,
|
||||||
SyntaxKind::MACRO_RULES,
|
SyntaxKind::MACRO_RULES,
|
||||||
|
SyntaxKind::MACRO_DEF,
|
||||||
];
|
];
|
||||||
|
|
||||||
let ancestor = once(root.clone())
|
let ancestor = once(root.clone())
|
||||||
|
|
|
@ -5,7 +5,7 @@ mod injector;
|
||||||
|
|
||||||
mod highlight;
|
mod highlight;
|
||||||
mod format;
|
mod format;
|
||||||
mod macro_rules;
|
mod macro_;
|
||||||
mod inject;
|
mod inject;
|
||||||
|
|
||||||
mod html;
|
mod html;
|
||||||
|
@ -24,8 +24,8 @@ use syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
syntax_highlighting::{
|
syntax_highlighting::{
|
||||||
format::highlight_format_string, highlights::Highlights,
|
format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
|
||||||
macro_rules::MacroRulesHighlighter, tags::Highlight,
|
tags::Highlight,
|
||||||
},
|
},
|
||||||
FileId, HlMod, HlTag,
|
FileId, HlMod, HlTag,
|
||||||
};
|
};
|
||||||
|
@ -93,8 +93,8 @@ fn traverse(
|
||||||
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
|
||||||
|
|
||||||
let mut current_macro_call: Option<ast::MacroCall> = None;
|
let mut current_macro_call: Option<ast::MacroCall> = None;
|
||||||
let mut current_macro_rules: Option<ast::MacroRules> = None;
|
let mut current_macro: Option<ast::Macro> = None;
|
||||||
let mut macro_rules_highlighter = MacroRulesHighlighter::default();
|
let mut macro_highlighter = MacroHighlighter::default();
|
||||||
let mut inside_attribute = false;
|
let mut inside_attribute = false;
|
||||||
|
|
||||||
// Walk all nodes, keeping track of whether we are inside a macro or not.
|
// Walk all nodes, keeping track of whether we are inside a macro or not.
|
||||||
|
@ -129,16 +129,16 @@ fn traverse(
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) {
|
match event.clone().map(|it| it.into_node().and_then(ast::Macro::cast)) {
|
||||||
WalkEvent::Enter(Some(mac)) => {
|
WalkEvent::Enter(Some(mac)) => {
|
||||||
macro_rules_highlighter.init();
|
macro_highlighter.init();
|
||||||
current_macro_rules = Some(mac);
|
current_macro = Some(mac);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
WalkEvent::Leave(Some(mac)) => {
|
WalkEvent::Leave(Some(mac)) => {
|
||||||
assert_eq!(current_macro_rules, Some(mac));
|
assert_eq!(current_macro, Some(mac));
|
||||||
current_macro_rules = None;
|
current_macro = None;
|
||||||
macro_rules_highlighter = MacroRulesHighlighter::default();
|
macro_highlighter = MacroHighlighter::default();
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
@ -164,9 +164,9 @@ fn traverse(
|
||||||
|
|
||||||
let range = element.text_range();
|
let range = element.text_range();
|
||||||
|
|
||||||
if current_macro_rules.is_some() {
|
if current_macro.is_some() {
|
||||||
if let Some(tok) = element.as_token() {
|
if let Some(tok) = element.as_token() {
|
||||||
macro_rules_highlighter.advance(tok);
|
macro_highlighter.advance(tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ fn traverse(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) {
|
if let Some(_) = macro_highlighter.highlight(element_to_highlight.clone()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -109,8 +109,7 @@ fn doc_attributes<'node>(
|
||||||
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
|
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
|
||||||
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
||||||
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
|
||||||
ast::MacroRules(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
|
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
|
||||||
// ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
|
||||||
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
||||||
_ => return None
|
_ => return None
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,18 +4,18 @@ use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
|
||||||
use crate::{HlRange, HlTag};
|
use crate::{HlRange, HlTag};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(super) struct MacroRulesHighlighter {
|
pub(super) struct MacroHighlighter {
|
||||||
state: Option<MacroMatcherParseState>,
|
state: Option<MacroMatcherParseState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacroRulesHighlighter {
|
impl MacroHighlighter {
|
||||||
pub(super) fn init(&mut self) {
|
pub(super) fn init(&mut self) {
|
||||||
self.state = Some(MacroMatcherParseState::default());
|
self.state = Some(MacroMatcherParseState::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn advance(&mut self, token: &SyntaxToken) {
|
pub(super) fn advance(&mut self, token: &SyntaxToken) {
|
||||||
if let Some(state) = self.state.as_mut() {
|
if let Some(state) = self.state.as_mut() {
|
||||||
update_macro_rules_state(state, token);
|
update_macro_state(state, token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,9 +74,9 @@ impl RuleState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_macro_rules_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) {
|
fn update_macro_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) {
|
||||||
if !state.in_invoc_body {
|
if !state.in_invoc_body {
|
||||||
if tok.kind() == T!['{'] {
|
if tok.kind() == T!['{'] || tok.kind() == T!['('] {
|
||||||
state.in_invoc_body = true;
|
state.in_invoc_body = true;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
|
@ -41,7 +41,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
|
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
|
||||||
|
|
||||||
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="function attribute">rustc_builtin_macro</span><span class="attribute attribute">]</span>
|
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="function attribute">rustc_builtin_macro</span><span class="attribute attribute">]</span>
|
||||||
<span class="keyword">macro</span> <span class="unresolved_reference declaration">Copy</span> <span class="brace">{</span><span class="brace">}</span>
|
<span class="keyword">macro</span> <span class="macro declaration">Copy</span> <span class="brace">{</span><span class="brace">}</span>
|
||||||
|
|
||||||
<span class="comment">// Needed for function consuming vs normal</span>
|
<span class="comment">// Needed for function consuming vs normal</span>
|
||||||
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">marker</span> <span class="brace">{</span>
|
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">marker</span> <span class="brace">{</span>
|
||||||
|
@ -158,6 +158,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
<span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="colon">:</span>ty<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">></span> <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="parenthesis">)</span>
|
<span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="colon">:</span>ty<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">></span> <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="parenthesis">)</span>
|
||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
|
|
||||||
|
<span class="keyword">macro</span> <span class="macro declaration">with_args</span><span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="brace">{</span>
|
||||||
|
<span class="punctuation">$</span>i
|
||||||
|
<span class="brace">}</span>
|
||||||
|
|
||||||
|
<span class="keyword">macro</span> <span class="macro declaration">without_args</span> <span class="brace">{</span>
|
||||||
|
<span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">></span> <span class="brace">{</span>
|
||||||
|
<span class="punctuation">$</span>i
|
||||||
|
<span class="brace">}</span>
|
||||||
|
<span class="brace">}</span>
|
||||||
|
|
||||||
<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>
|
||||||
|
|
|
@ -129,6 +129,16 @@ macro_rules! keyword_frag {
|
||||||
($type:ty) => ($type)
|
($type:ty) => ($type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro with_args($i:ident) {
|
||||||
|
$i
|
||||||
|
}
|
||||||
|
|
||||||
|
macro without_args {
|
||||||
|
($i:ident) => {
|
||||||
|
$i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// comment
|
// comment
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, {}!", 92);
|
println!("Hello, {}!", 92);
|
||||||
|
|
|
@ -227,7 +227,7 @@ impl NameClass {
|
||||||
let def: hir::TypeAlias = sema.to_def(&it)?;
|
let def: hir::TypeAlias = sema.to_def(&it)?;
|
||||||
Some(NameClass::Definition(Definition::ModuleDef(def.into())))
|
Some(NameClass::Definition(Definition::ModuleDef(def.into())))
|
||||||
},
|
},
|
||||||
ast::MacroRules(it) => {
|
ast::Macro(it) => {
|
||||||
let def = sema.to_def(&it)?;
|
let def = sema.to_def(&it)?;
|
||||||
Some(NameClass::Definition(Definition::Macro(def)))
|
Some(NameClass::Definition(Definition::Macro(def)))
|
||||||
},
|
},
|
||||||
|
|
|
@ -438,7 +438,7 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> {
|
||||||
ast::TypeAlias(it) => decl(it),
|
ast::TypeAlias(it) => decl(it),
|
||||||
ast::Const(it) => decl(it),
|
ast::Const(it) => decl(it),
|
||||||
ast::Static(it) => decl(it),
|
ast::Static(it) => decl(it),
|
||||||
ast::MacroRules(it) => decl(it),
|
ast::Macro(it) => decl(it),
|
||||||
ast::Union(it) => decl(it),
|
ast::Union(it) => decl(it),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -458,6 +458,7 @@ fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> {
|
||||||
CONST => FileSymbolKind::Const,
|
CONST => FileSymbolKind::Const,
|
||||||
STATIC => FileSymbolKind::Static,
|
STATIC => FileSymbolKind::Static,
|
||||||
MACRO_RULES => FileSymbolKind::Macro,
|
MACRO_RULES => FileSymbolKind::Macro,
|
||||||
|
MACRO_DEF => FileSymbolKind::Macro,
|
||||||
UNION => FileSymbolKind::Union,
|
UNION => FileSymbolKind::Union,
|
||||||
kind => unreachable!("{:?}", kind),
|
kind => unreachable!("{:?}", kind),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue