Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/add_missing_match_arms.rs')
-rw-r--r--crates/ide-assists/src/handlers/add_missing_match_arms.rs56
1 files changed, 45 insertions, 11 deletions
diff --git a/crates/ide-assists/src/handlers/add_missing_match_arms.rs b/crates/ide-assists/src/handlers/add_missing_match_arms.rs
index 4d3212c515..248ce2ad61 100644
--- a/crates/ide-assists/src/handlers/add_missing_match_arms.rs
+++ b/crates/ide-assists/src/handlers/add_missing_match_arms.rs
@@ -1,15 +1,14 @@
use std::iter::{self, Peekable};
use either::Either;
-use hir::{Adt, AsAssocItem, Crate, FindPathConfig, HasAttrs, ModuleDef, Semantics, sym};
+use hir::{Adt, AsAssocItem, Crate, FindPathConfig, HasAttrs, ModuleDef, Semantics};
use ide_db::RootDatabase;
use ide_db::assists::ExprFillDefaultMode;
use ide_db::syntax_helpers::suggest_name;
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
use itertools::Itertools;
use syntax::ToSmolStr;
-use syntax::ast::edit::IndentLevel;
-use syntax::ast::edit_in_place::Indent;
+use syntax::ast::edit::{AstNodeEdit, IndentLevel};
use syntax::ast::syntax_factory::SyntaxFactory;
use syntax::ast::{self, AstNode, MatchArmList, MatchExpr, Pat, make};
@@ -68,9 +67,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
}
.map(move |pat| (pat, has_guard))
})
- .map(|(pat, has_guard)| {
+ .filter_map(|(pat, has_guard)| {
has_catch_all_arm |= !has_guard && matches!(pat, Pat::WildcardPat(_));
- pat
+ (!has_guard).then_some(pat)
})
// Exclude top level wildcards so that they are expanded by this assist, retains status quo in #8129.
.filter(|pat| !matches!(pat, Pat::WildcardPat(_)))
@@ -80,7 +79,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
let scope = ctx.sema.scope(expr.syntax())?;
let module = scope.module();
- let cfg = ctx.config.find_path_confg(ctx.sema.is_nightly(scope.krate()));
+ let cfg = ctx.config.find_path_config(ctx.sema.is_nightly(scope.krate()));
let self_ty = if ctx.config.prefer_self_ty {
scope
.containing_function()
@@ -261,6 +260,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
true
}
})
+ .map(|arm| arm.reset_indent().indent(IndentLevel(1)))
.collect();
let first_new_arm_idx = arms.len();
@@ -300,7 +300,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
};
let mut editor = builder.make_editor(&old_place);
- new_match_arm_list.indent(IndentLevel::from_node(&old_place));
+ let new_match_arm_list = new_match_arm_list.indent(IndentLevel::from_node(&old_place));
editor.replace(old_place, new_match_arm_list.syntax());
if let Some(cap) = ctx.config.snippet_cap {
@@ -401,7 +401,7 @@ impl ExtendedVariant {
fn should_be_hidden(self, db: &RootDatabase, krate: Crate) -> bool {
match self {
ExtendedVariant::Variant { variant: var, .. } => {
- var.attrs(db).has_doc_hidden() && var.module(db).krate() != krate
+ var.attrs(db).is_doc_hidden() && var.module(db).krate() != krate
}
_ => false,
}
@@ -424,7 +424,7 @@ impl ExtendedEnum {
fn is_non_exhaustive(&self, db: &RootDatabase, krate: Crate) -> bool {
match self {
ExtendedEnum::Enum { enum_: e, .. } => {
- e.attrs(db).by_key(sym::non_exhaustive).exists() && e.module(db).krate() != krate
+ e.attrs(db).is_non_exhaustive() && e.module(db).krate() != krate
}
_ => false,
}
@@ -521,7 +521,7 @@ fn build_pat(
hir::StructKind::Tuple => {
let mut name_generator = suggest_name::NameGenerator::default();
let pats = fields.into_iter().map(|f| {
- let name = name_generator.for_type(&f.ty(db), db, edition);
+ let name = name_generator.for_type(&f.ty(db).to_type(db), db, edition);
match name {
Some(name) => make::ext::simple_ident_pat(make.name(&name)).into(),
None => make.wildcard_pat().into(),
@@ -918,6 +918,39 @@ fn main() {
}
#[test]
+ fn partial_fill_option_with_indentation() {
+ check_assist(
+ add_missing_match_arms,
+ r#"
+//- minicore: option
+fn main() {
+ match None$0 {
+ None => {
+ foo(
+ "foo",
+ "bar",
+ );
+ }
+ }
+}
+"#,
+ r#"
+fn main() {
+ match None {
+ None => {
+ foo(
+ "foo",
+ "bar",
+ );
+ }
+ Some(${1:_}) => ${2:todo!()},$0
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
fn partial_fill_or_pat() {
check_assist(
add_missing_match_arms,
@@ -965,7 +998,8 @@ fn main() {
A::Ds(_value) => { let x = 1; }
A::Es(B::Xs) => (),
A::As => ${1:todo!()},
- A::Cs => ${2:todo!()},$0
+ A::Bs => ${2:todo!()},
+ A::Cs => ${3:todo!()},$0
}
}
"#,