This commit is contained in:
Lukas Wirth 2021-08-07 22:16:15 +02:00
parent 80f522091a
commit c4a119f433
8 changed files with 39 additions and 27 deletions

View File

@ -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<Fu
#[derive(Debug)]
struct Function {
name: String,
name: ast::NameRef,
self_param: Option<ast::SelfParam>,
params: Vec<Param>,
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)
};

View File

@ -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;
}

View File

@ -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;
}

View File

@ -248,7 +248,7 @@ fn invert_special_case(sema: &Semantics<RootDatabase>, 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()? {

View File

@ -38,7 +38,7 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
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<Vec<Assist>> {
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);
}
}
}

View File

@ -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<ast::Expr>) -> bool) {
let mut preorder = self.syntax().preorder();

View File

@ -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<Item = ast::Param>, 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))
}

View File

@ -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),