Skip to content

Commit 895a162

Browse files
committed
Fix macro patterns not getting formatted properly
1 parent e2f1a9a commit 895a162

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

crates/ide/src/expand_macro.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,34 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
6969

7070
// FIXME: Intermix attribute and bang! expansions
7171
// currently we only recursively expand one of the two types
72-
let mut expanded = None;
73-
let mut name = None;
74-
let mut kind = SyntaxKind::ERROR;
75-
for node in tok.ancestors() {
72+
let mut anc = tok.ancestors();
73+
let (name, expanded, kind) = loop {
74+
let node = anc.next()?;
75+
7676
if let Some(item) = ast::Item::cast(node.clone()) {
7777
if let Some(def) = sema.resolve_attr_macro_call(&item) {
78-
name = Some(def.name(db).to_string());
79-
expanded = expand_attr_macro_recur(&sema, &item);
80-
kind = SyntaxKind::MACRO_ITEMS;
81-
break;
78+
break (
79+
def.name(db).to_string(),
80+
expand_attr_macro_recur(&sema, &item)?,
81+
SyntaxKind::MACRO_ITEMS,
82+
);
8283
}
8384
}
8485
if let Some(mac) = ast::MacroCall::cast(node) {
85-
name = Some(mac.path()?.segment()?.name_ref()?.to_string());
86-
expanded = expand_macro_recur(&sema, &mac);
87-
kind = mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS);
88-
break;
86+
break (
87+
mac.path()?.segment()?.name_ref()?.to_string(),
88+
expand_macro_recur(&sema, &mac)?,
89+
mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS),
90+
);
8991
}
90-
}
92+
};
9193

9294
// FIXME:
9395
// macro expansion may lose all white space information
9496
// But we hope someday we can use ra_fmt for that
95-
let expansion = format(db, kind, position.file_id, expanded?);
97+
let expansion = format(db, kind, position.file_id, expanded);
9698

97-
Some(ExpandedMacro { name: name.unwrap_or_else(|| "???".to_owned()), expansion })
99+
Some(ExpandedMacro { name, expansion })
98100
}
99101

100102
fn expand_macro_recur(
@@ -188,8 +190,15 @@ fn _format(
188190
let captured_stdout = String::from_utf8(output.stdout).ok()?;
189191

190192
if output.status.success() && !captured_stdout.trim().is_empty() {
191-
let foo = captured_stdout.replace(DOLLAR_CRATE_REPLACE, "$crate");
192-
let trim_indent = stdx::trim_indent(foo.trim().strip_prefix(prefix)?.strip_suffix(suffix)?);
193+
let output = captured_stdout.replace(DOLLAR_CRATE_REPLACE, "$crate");
194+
let output = output.trim().strip_prefix(prefix)?;
195+
let output = match kind {
196+
SyntaxKind::MACRO_PAT => {
197+
output.strip_suffix(suffix).or_else(|| output.strip_suffix(": u32,\n);"))?
198+
}
199+
_ => output.strip_suffix(suffix)?,
200+
};
201+
let trim_indent = stdx::trim_indent(output);
193202
tracing::debug!("expand_macro: formatting succeeded");
194203
Some(trim_indent)
195204
} else {

0 commit comments

Comments
 (0)