Skip to content

Commit 7890856

Browse files
committed
Fix complete unit return semicolon in arg-list
Example --- ```rust fn foo() {} fn baz(_: impl FnOnce()) {} fn bar() { baz(fo$0); } ``` **Before this PR** ```rust fn foo() {} fn baz(_: impl FnOnce()) {} fn bar() { baz(foo();$0); } ``` **After this PR** ```rust fn foo() {} fn baz(_: impl FnOnce()) {} fn bar() { baz(foo()$0); } ```
1 parent 4bf516e commit 7890856

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

crates/ide-completion/src/context.rs

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod analysis;
44
#[cfg(test)]
55
mod tests;
66

7-
use std::{iter, ops::ControlFlow};
7+
use std::iter;
88

99
use base_db::RootQueryDb as _;
1010
use hir::{
@@ -21,7 +21,6 @@ use syntax::{
2121
SyntaxKind::{self, *},
2222
SyntaxToken, T, TextRange, TextSize,
2323
ast::{self, AttrKind, NameOrNameRef},
24-
match_ast,
2524
};
2625

2726
use crate::{
@@ -818,48 +817,20 @@ impl<'db> CompletionContext<'db> {
818817
.extend(exclude_traits.iter().map(|&t| (t.into(), AutoImportExclusionType::Always)));
819818

820819
// FIXME: This should be part of `CompletionAnalysis` / `expand_and_analyze`
821-
let complete_semicolon = if config.add_semicolon_to_unit {
822-
let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| {
823-
match_ast! {
824-
match ancestor {
825-
ast::BlockExpr(_) => ControlFlow::Break(false),
826-
ast::ClosureExpr(_) => ControlFlow::Break(true),
827-
_ => ControlFlow::Continue(())
828-
}
829-
}
830-
});
831-
832-
if inside_closure_ret == ControlFlow::Break(true) {
833-
CompleteSemicolon::DoNotComplete
834-
} else {
835-
let next_non_trivia_token =
836-
std::iter::successors(token.next_token(), |it| it.next_token())
837-
.find(|it| !it.kind().is_trivia());
838-
let in_match_arm = token.parent_ancestors().try_for_each(|ancestor| {
839-
if ast::MatchArm::can_cast(ancestor.kind()) {
840-
ControlFlow::Break(true)
841-
} else if matches!(
842-
ancestor.kind(),
843-
SyntaxKind::EXPR_STMT | SyntaxKind::BLOCK_EXPR
844-
) {
845-
ControlFlow::Break(false)
846-
} else {
847-
ControlFlow::Continue(())
848-
}
849-
});
850-
// FIXME: This will assume expr macros are not inside match, we need to somehow go to the "parent" of the root node.
851-
let in_match_arm = match in_match_arm {
852-
ControlFlow::Continue(()) => false,
853-
ControlFlow::Break(it) => it,
854-
};
855-
let complete_token = if in_match_arm { T![,] } else { T![;] };
856-
if next_non_trivia_token.map(|it| it.kind()) == Some(complete_token) {
857-
CompleteSemicolon::DoNotComplete
858-
} else if in_match_arm {
859-
CompleteSemicolon::CompleteComma
860-
} else {
861-
CompleteSemicolon::CompleteSemi
862-
}
820+
let complete_semicolon = if !config.add_semicolon_to_unit {
821+
CompleteSemicolon::DoNotComplete
822+
} else if let Some(term_node) =
823+
sema.token_ancestors_with_macros(token.clone()).find(|node| {
824+
matches!(node.kind(), BLOCK_EXPR | MATCH_ARM | CLOSURE_EXPR | ARG_LIST | PAREN_EXPR)
825+
})
826+
{
827+
let next_token = iter::successors(token.next_token(), |it| it.next_token())
828+
.map(|it| it.kind())
829+
.find(|kind| !kind.is_trivia());
830+
match term_node.kind() {
831+
MATCH_ARM if next_token != Some(T![,]) => CompleteSemicolon::CompleteComma,
832+
BLOCK_EXPR if next_token != Some(T![;]) => CompleteSemicolon::CompleteSemi,
833+
_ => CompleteSemicolon::DoNotComplete,
863834
}
864835
} else {
865836
CompleteSemicolon::DoNotComplete

crates/ide-completion/src/render/function.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,27 @@ fn baz(_: impl FnOnce()) {}
884884
fn bar() {
885885
baz(|| foo()$0);
886886
}
887+
"#,
888+
);
889+
}
890+
891+
#[test]
892+
fn no_semicolon_in_arg_list() {
893+
check_edit(
894+
r#"foo"#,
895+
r#"
896+
fn foo() {}
897+
fn baz(_: impl FnOnce()) {}
898+
fn bar() {
899+
baz(fo$0);
900+
}
901+
"#,
902+
r#"
903+
fn foo() {}
904+
fn baz(_: impl FnOnce()) {}
905+
fn bar() {
906+
baz(foo()$0);
907+
}
887908
"#,
888909
);
889910
}

0 commit comments

Comments
 (0)