Simplify
This commit is contained in:
parent
80f522091a
commit
c4a119f433
@ -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());
|
body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
|
||||||
|
|
||||||
let fun = Function {
|
let fun = Function {
|
||||||
name: "fun_name".to_string(),
|
name: make::name_ref("fun_name"),
|
||||||
self_param,
|
self_param,
|
||||||
params,
|
params,
|
||||||
control_flow,
|
control_flow,
|
||||||
@ -170,7 +170,7 @@ fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option<Fu
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Function {
|
struct Function {
|
||||||
name: String,
|
name: ast::NameRef,
|
||||||
self_param: Option<ast::SelfParam>,
|
self_param: Option<ast::SelfParam>,
|
||||||
params: Vec<Param>,
|
params: Vec<Param>,
|
||||||
control_flow: ControlFlow,
|
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 = fun.params.iter().map(|param| param.to_arg(ctx));
|
||||||
let args = make::arg_list(args);
|
let args = make::arg_list(args);
|
||||||
|
let name = fun.name.clone();
|
||||||
let call_expr = if fun.self_param.is_some() {
|
let call_expr = if fun.self_param.is_some() {
|
||||||
let self_arg = make::expr_path(make::ext::ident_path("self"));
|
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 {
|
} 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)
|
make::expr_call(func, args)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This assist should not apply for if-let.
|
// This assist should not apply for if-let.
|
||||||
if expr.condition()?.pat().is_some() {
|
if expr.condition()?.is_pattern_cond() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
// Not support moving if let to arm guard
|
// Not support moving if let to arm guard
|
||||||
if cond.pat().is_some() {
|
if cond.is_pattern_cond() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +248,7 @@ fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Opti
|
|||||||
"is_err" => "is_ok",
|
"is_err" => "is_ok",
|
||||||
_ => return None,
|
_ => 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 => {
|
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
|
||||||
if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
|
if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
|
||||||
|
@ -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 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 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:
|
// Candidates to look for:
|
||||||
// - `mod.rs` in the same folder
|
// - `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")?];
|
let mut paths = vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
|
||||||
|
|
||||||
// `submod/bla.rs` -> `submod.rs`
|
// `submod/bla.rs` -> `submod.rs`
|
||||||
if let Some(newmod) = (|| {
|
let parent_mod = (|| {
|
||||||
let name = parent.name_and_extension()?.0;
|
let (name, _) = parent.name_and_extension()?;
|
||||||
parent.parent()?.join(&format!("{}.rs", name))
|
parent.parent()?.join(&format!("{}.rs", name))
|
||||||
})() {
|
})();
|
||||||
paths.push(newmod);
|
paths.extend(parent_mod);
|
||||||
}
|
|
||||||
|
|
||||||
for path in &paths {
|
for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) {
|
||||||
if let Some(parent_id) = source_root.file_for_path(path) {
|
for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
|
||||||
for krate in ctx.sema.db.relevant_crates(*parent_id).iter() {
|
let crate_def_map = ctx.sema.db.crate_def_map(krate);
|
||||||
let crate_def_map = ctx.sema.db.crate_def_map(*krate);
|
|
||||||
for (_, module) in crate_def_map.modules() {
|
for (_, module) in crate_def_map.modules() {
|
||||||
if module.origin.is_inline() {
|
if module.origin.is_inline() {
|
||||||
// We don't handle inline `mod parent {}`s, they use different paths.
|
// We don't handle inline `mod parent {}`s, they use different paths.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if module.origin.file_id() == Some(*parent_id) {
|
if module.origin.file_id() == Some(parent_id) {
|
||||||
return make_fixes(ctx.sema.db, *parent_id, module_name, file_id);
|
return make_fixes(ctx.sema.db, parent_id, module_name, file_id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ impl ast::Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Preorder walk all the expression's child expressions preserving events.
|
/// 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.
|
/// 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) {
|
pub fn preorder(&self, cb: &mut dyn FnMut(WalkEvent<ast::Expr>) -> bool) {
|
||||||
let mut preorder = self.syntax().preorder();
|
let mut preorder = self.syntax().preorder();
|
||||||
|
@ -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 {
|
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
|
||||||
expr_from_text(&format!("{}{}", f, arg_list))
|
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))
|
expr_from_text(&format!("{}.{}{}", receiver, method, arg_list))
|
||||||
}
|
}
|
||||||
pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
|
pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
|
||||||
expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", 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 {
|
pub fn expr_paren(expr: ast::Expr) -> ast::Expr {
|
||||||
expr_from_text(&format!("({})", expr))
|
expr_from_text(&format!("({})", expr))
|
||||||
}
|
}
|
||||||
|
@ -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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum FieldKind {
|
pub enum FieldKind {
|
||||||
Name(ast::NameRef),
|
Name(ast::NameRef),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user