Skip to content

Commit 0358021

Browse files
authored
Merge pull request #20534 from A4-Tacks/tog-macro-delim-semicolon
Fix ExprStmt delete semicolon for toggle_macro_delimiter
2 parents d86cf44 + 55b1ddb commit 0358021

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

crates/ide-assists/src/handlers/toggle_macro_delimiter.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ide_db::assists::AssistId;
22
use syntax::{
3-
AstNode, T,
3+
AstNode, SyntaxToken, T,
44
ast::{self, syntax_factory::SyntaxFactory},
55
};
66

@@ -39,7 +39,7 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
3939
let makro = ctx.find_node_at_offset::<ast::MacroCall>()?;
4040

4141
let cursor_offset = ctx.offset();
42-
let semicolon = makro.semicolon_token();
42+
let semicolon = macro_semicolon(&makro);
4343
let token_tree = makro.token_tree()?;
4444

4545
let ltoken = token_tree.left_delimiter_token()?;
@@ -95,6 +95,14 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
9595
)
9696
}
9797

98+
fn macro_semicolon(makro: &ast::MacroCall) -> Option<SyntaxToken> {
99+
makro.semicolon_token().or_else(|| {
100+
let macro_expr = ast::MacroExpr::cast(makro.syntax().parent()?)?;
101+
let expr_stmt = ast::ExprStmt::cast(macro_expr.syntax().parent()?)?;
102+
expr_stmt.semicolon_token()
103+
})
104+
}
105+
98106
#[cfg(test)]
99107
mod tests {
100108
use crate::tests::{check_assist, check_assist_not_applicable};
@@ -119,7 +127,29 @@ macro_rules! sth {
119127
120128
sth!{ }
121129
"#,
122-
)
130+
);
131+
132+
check_assist(
133+
toggle_macro_delimiter,
134+
r#"
135+
macro_rules! sth {
136+
() => {};
137+
}
138+
139+
fn foo() {
140+
sth!$0( );
141+
}
142+
"#,
143+
r#"
144+
macro_rules! sth {
145+
() => {};
146+
}
147+
148+
fn foo() {
149+
sth!{ }
150+
}
151+
"#,
152+
);
123153
}
124154

125155
#[test]

0 commit comments

Comments
 (0)