mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Make stringify!
prettify its input
This will insert whitespace if the invocation is inside another macro
This commit is contained in:
parent
f22eea9053
commit
d05eae6ada
6 changed files with 48 additions and 133 deletions
|
@ -1,7 +1,7 @@
|
|||
//! Builtin macro
|
||||
use crate::{
|
||||
db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroCallLoc, MacroDefId,
|
||||
MacroDefKind, TextSize,
|
||||
MacroDefKind,
|
||||
};
|
||||
|
||||
use base_db::{AnchoredPath, Edition, FileId};
|
||||
|
@ -148,25 +148,14 @@ fn line_expand(
|
|||
}
|
||||
|
||||
fn stringify_expand(
|
||||
db: &dyn AstDatabase,
|
||||
id: MacroCallId,
|
||||
_tt: &tt::Subtree,
|
||||
_db: &dyn AstDatabase,
|
||||
_id: MacroCallId,
|
||||
tt: &tt::Subtree,
|
||||
) -> ExpandResult<tt::Subtree> {
|
||||
let loc = db.lookup_intern_macro(id);
|
||||
|
||||
let macro_content = {
|
||||
let arg = match loc.kind.arg(db) {
|
||||
Some(arg) => arg,
|
||||
None => return ExpandResult::only_err(mbe::ExpandError::UnexpectedToken),
|
||||
};
|
||||
let macro_args = arg;
|
||||
let text = macro_args.text();
|
||||
let without_parens = TextSize::of('(')..text.len() - TextSize::of(')');
|
||||
text.slice(without_parens).to_string()
|
||||
};
|
||||
let pretty = tt::pretty(&tt.token_trees);
|
||||
|
||||
let expanded = quote! {
|
||||
#macro_content
|
||||
#pretty
|
||||
};
|
||||
|
||||
ExpandResult::ok(expanded)
|
||||
|
@ -685,7 +674,11 @@ mod tests {
|
|||
r#"
|
||||
#[rustc_builtin_macro]
|
||||
macro_rules! stringify {() => {}}
|
||||
stringify!(a b c)
|
||||
stringify!(
|
||||
a
|
||||
b
|
||||
c
|
||||
)
|
||||
"#,
|
||||
expect![["\"a b c\""]],
|
||||
);
|
||||
|
|
|
@ -26,7 +26,7 @@ use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
|
|||
use syntax::{
|
||||
algo::skip_trivia_token,
|
||||
ast::{self, AstNode, HasAttrs},
|
||||
Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
||||
Direction, SyntaxNode, SyntaxToken, TextRange,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue