From c4a119f43323c57cfea8f5bfebfd5946e9e296ae Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 7 Aug 2021 22:16:15 +0200 Subject: [PATCH] Simplify --- .../src/handlers/extract_function.rs | 9 ++--- crates/ide_assists/src/handlers/invert_if.rs | 2 +- crates/ide_assists/src/handlers/move_guard.rs | 2 +- crates/ide_assists/src/utils.rs | 2 +- .../src/handlers/unlinked_file.rs | 33 +++++++++---------- crates/syntax/src/ast/expr_ext.rs | 2 +- crates/syntax/src/ast/make.rs | 10 +++++- crates/syntax/src/ast/node_ext.rs | 6 ++++ 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index bae62908a19..44d02e4321f 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -100,7 +100,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option body.extracted_function_params(ctx, &container_info, locals_used.iter().copied()); let fun = Function { - name: "fun_name".to_string(), + name: make::name_ref("fun_name"), self_param, params, control_flow, @@ -170,7 +170,7 @@ fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option, params: Vec, control_flow: ControlFlow, @@ -1077,11 +1077,12 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String let args = fun.params.iter().map(|param| param.to_arg(ctx)); let args = make::arg_list(args); + let name = fun.name.clone(); let call_expr = if fun.self_param.is_some() { let self_arg = make::expr_path(make::ext::ident_path("self")); - make::expr_method_call(self_arg, &fun.name, args) + make::expr_method_call(self_arg, name, args) } else { - let func = make::expr_path(make::ext::ident_path(&fun.name)); + let func = make::expr_path(make::path_unqualified(make::path_segment(name))); make::expr_call(func, args) }; diff --git a/crates/ide_assists/src/handlers/invert_if.rs b/crates/ide_assists/src/handlers/invert_if.rs index 50aab443a71..f7f38dffbda 100644 --- a/crates/ide_assists/src/handlers/invert_if.rs +++ b/crates/ide_assists/src/handlers/invert_if.rs @@ -35,7 +35,7 @@ pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { } // This assist should not apply for if-let. - if expr.condition()?.pat().is_some() { + if expr.condition()?.is_pattern_cond() { return None; } diff --git a/crates/ide_assists/src/handlers/move_guard.rs b/crates/ide_assists/src/handlers/move_guard.rs index 3f22302a974..e38e9e4524d 100644 --- a/crates/ide_assists/src/handlers/move_guard.rs +++ b/crates/ide_assists/src/handlers/move_guard.rs @@ -115,7 +115,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex return None; } // Not support moving if let to arm guard - if cond.pat().is_some() { + if cond.is_pattern_cond() { return None; } diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 0bd89b51f8f..35a997efc93 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -248,7 +248,7 @@ fn invert_special_case(sema: &Semantics, expr: &ast::Expr) -> Opti "is_err" => "is_ok", _ => return None, }; - Some(make::expr_method_call(receiver, method, arg_list)) + Some(make::expr_method_call(receiver, make::name_ref(method), arg_list)) } ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => { if let ast::Expr::ParenExpr(parexpr) = pe.expr()? { diff --git a/crates/ide_diagnostics/src/handlers/unlinked_file.rs b/crates/ide_diagnostics/src/handlers/unlinked_file.rs index 41058a99633..06c1f2cf820 100644 --- a/crates/ide_diagnostics/src/handlers/unlinked_file.rs +++ b/crates/ide_diagnostics/src/handlers/unlinked_file.rs @@ -38,7 +38,7 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option> { let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id)); let our_path = source_root.path_for_file(&file_id)?; - let module_name = our_path.name_and_extension()?.0; + let (module_name, _) = our_path.name_and_extension()?; // Candidates to look for: // - `mod.rs` in the same folder @@ -48,26 +48,23 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option> { let mut paths = vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?]; // `submod/bla.rs` -> `submod.rs` - if let Some(newmod) = (|| { - let name = parent.name_and_extension()?.0; + let parent_mod = (|| { + let (name, _) = parent.name_and_extension()?; parent.parent()?.join(&format!("{}.rs", name)) - })() { - paths.push(newmod); - } + })(); + paths.extend(parent_mod); - for path in &paths { - if let Some(parent_id) = source_root.file_for_path(path) { - for krate in ctx.sema.db.relevant_crates(*parent_id).iter() { - let crate_def_map = ctx.sema.db.crate_def_map(*krate); - for (_, module) in crate_def_map.modules() { - if module.origin.is_inline() { - // We don't handle inline `mod parent {}`s, they use different paths. - continue; - } + for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) { + for &krate in ctx.sema.db.relevant_crates(parent_id).iter() { + let crate_def_map = ctx.sema.db.crate_def_map(krate); + for (_, module) in crate_def_map.modules() { + if module.origin.is_inline() { + // We don't handle inline `mod parent {}`s, they use different paths. + continue; + } - if module.origin.file_id() == Some(*parent_id) { - return make_fixes(ctx.sema.db, *parent_id, module_name, file_id); - } + if module.origin.file_id() == Some(parent_id) { + return make_fixes(ctx.sema.db, parent_id, module_name, file_id); } } } diff --git a/crates/syntax/src/ast/expr_ext.rs b/crates/syntax/src/ast/expr_ext.rs index aad5b08e9f9..01ab562c7ee 100644 --- a/crates/syntax/src/ast/expr_ext.rs +++ b/crates/syntax/src/ast/expr_ext.rs @@ -48,7 +48,7 @@ impl ast::Expr { } /// Preorder walk all the expression's child expressions preserving events. - /// If the callback returns true the subtree of the expression will be skipped. + /// If the callback returns true on an [`WalkEvent::Enter`], the subtree of the expression will be skipped. /// Note that the subtree may already be skipped due to the context analysis this function does. pub fn preorder(&self, cb: &mut dyn FnMut(WalkEvent) -> bool) { let mut preorder = self.syntax().preorder(); diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index f84e1755389..e5fff983d0c 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -304,12 +304,20 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { expr_from_text(&format!("{}{}", f, arg_list)) } -pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr { +pub fn expr_method_call( + receiver: ast::Expr, + method: ast::NameRef, + arg_list: ast::ArgList, +) -> ast::Expr { expr_from_text(&format!("{}.{}{}", receiver, method, arg_list)) } pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) } +pub fn expr_closure(pats: impl IntoIterator, expr: ast::Expr) -> ast::Expr { + let params = pats.into_iter().join(", "); + expr_from_text(&format!("|{}| {}", params, expr)) +} pub fn expr_paren(expr: ast::Expr) -> ast::Expr { expr_from_text(&format!("({})", expr)) } diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 57c6e6cab20..68dcac4b034 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -611,6 +611,12 @@ impl ast::Item { } } +impl ast::Condition { + pub fn is_pattern_cond(&self) -> bool { + self.let_token().is_some() + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum FieldKind { Name(ast::NameRef),