2021-10-14 14:49:46 -05:00
|
|
|
use hir::{HirDisplay, ModuleDef, PathResolution, Semantics};
|
|
|
|
use ide_db::{
|
|
|
|
assists::{AssistId, AssistKind},
|
|
|
|
defs::Definition,
|
2022-03-06 12:01:30 -06:00
|
|
|
syntax_helpers::node_ext::preorder_expr,
|
2021-10-14 14:49:46 -05:00
|
|
|
RootDatabase,
|
|
|
|
};
|
|
|
|
use stdx::to_upper_snake_case;
|
|
|
|
use syntax::{
|
|
|
|
ast::{self, make, HasName},
|
2023-07-09 17:49:29 -05:00
|
|
|
ted, AstNode, WalkEvent,
|
2021-10-14 14:49:46 -05:00
|
|
|
};
|
|
|
|
|
2023-07-09 17:49:29 -05:00
|
|
|
use crate::assist_context::{AssistContext, Assists};
|
2021-10-14 14:49:46 -05:00
|
|
|
|
|
|
|
// Assist: promote_local_to_const
|
|
|
|
//
|
|
|
|
// Promotes a local variable to a const item changing its name to a `SCREAMING_SNAKE_CASE` variant
|
|
|
|
// if the local uses no non-const expressions.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let foo$0 = true;
|
|
|
|
//
|
|
|
|
// if foo {
|
|
|
|
// println!("It's true");
|
|
|
|
// } else {
|
|
|
|
// println!("It's false");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// const $0FOO: bool = true;
|
|
|
|
//
|
|
|
|
// if FOO {
|
|
|
|
// println!("It's true");
|
|
|
|
// } else {
|
|
|
|
// println!("It's false");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-10-14 14:49:46 -05:00
|
|
|
let pat = ctx.find_node_at_offset::<ast::IdentPat>()?;
|
|
|
|
let name = pat.name()?;
|
|
|
|
if !pat.is_simple_ident() {
|
|
|
|
cov_mark::hit!(promote_local_non_simple_ident);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let let_stmt = pat.syntax().parent().and_then(ast::LetStmt::cast)?;
|
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
let module = ctx.sema.scope(pat.syntax())?.module();
|
2021-10-14 14:49:46 -05:00
|
|
|
let local = ctx.sema.to_def(&pat)?;
|
|
|
|
let ty = ctx.sema.type_of_pat(&pat.into())?.original;
|
|
|
|
|
2023-04-07 08:45:04 -05:00
|
|
|
let ty = match ty.display_source_code(ctx.db(), module.into(), false) {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(_) => {
|
|
|
|
cov_mark::hit!(promote_local_not_applicable_if_ty_not_inferred);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2021-10-14 14:49:46 -05:00
|
|
|
|
|
|
|
let initializer = let_stmt.initializer()?;
|
|
|
|
if !is_body_const(&ctx.sema, &initializer) {
|
|
|
|
cov_mark::hit!(promote_local_non_const);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let target = let_stmt.syntax().text_range();
|
|
|
|
acc.add(
|
|
|
|
AssistId("promote_local_to_const", AssistKind::Refactor),
|
|
|
|
"Promote local to constant",
|
|
|
|
target,
|
2023-07-09 17:49:29 -05:00
|
|
|
|edit| {
|
2021-10-14 14:49:46 -05:00
|
|
|
let name = to_upper_snake_case(&name.to_string());
|
|
|
|
let usages = Definition::Local(local).usages(&ctx.sema).all();
|
|
|
|
if let Some(usages) = usages.references.get(&ctx.file_id()) {
|
2023-07-11 16:28:17 -05:00
|
|
|
let name = make::name_ref(&name);
|
|
|
|
|
2021-10-14 14:49:46 -05:00
|
|
|
for usage in usages {
|
2023-07-10 16:49:27 -05:00
|
|
|
let Some(usage) = usage.name.as_name_ref().cloned() else { continue };
|
2023-07-09 17:49:29 -05:00
|
|
|
let usage = edit.make_mut(usage);
|
2023-07-11 16:28:17 -05:00
|
|
|
ted::replace(usage.syntax(), name.clone_for_update().syntax());
|
2021-10-14 14:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:49:29 -05:00
|
|
|
let item = make::item_const(None, make::name(&name), make::ty(&ty), initializer)
|
|
|
|
.clone_for_update();
|
|
|
|
let let_stmt = edit.make_mut(let_stmt);
|
|
|
|
|
|
|
|
if let Some((cap, name)) = ctx.config.snippet_cap.zip(item.name()) {
|
|
|
|
edit.add_tabstop_before(cap, name);
|
2021-10-14 14:49:46 -05:00
|
|
|
}
|
2023-07-09 17:49:29 -05:00
|
|
|
|
|
|
|
ted::replace(let_stmt.syntax(), item.syntax());
|
2021-10-14 14:49:46 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bool {
|
2021-10-14 14:49:46 -05:00
|
|
|
let mut is_const = true;
|
|
|
|
preorder_expr(expr, &mut |ev| {
|
|
|
|
let expr = match ev {
|
|
|
|
WalkEvent::Enter(_) if !is_const => return true,
|
|
|
|
WalkEvent::Enter(expr) => expr,
|
|
|
|
WalkEvent::Leave(_) => return false,
|
|
|
|
};
|
|
|
|
match expr {
|
|
|
|
ast::Expr::CallExpr(call) => {
|
|
|
|
if let Some(ast::Expr::PathExpr(path_expr)) = call.expr() {
|
|
|
|
if let Some(PathResolution::Def(ModuleDef::Function(func))) =
|
|
|
|
path_expr.path().and_then(|path| sema.resolve_path(&path))
|
|
|
|
{
|
|
|
|
is_const &= func.is_const(sema.db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::Expr::MethodCallExpr(call) => {
|
|
|
|
is_const &=
|
|
|
|
sema.resolve_method_call(&call).map(|it| it.is_const(sema.db)).unwrap_or(true)
|
|
|
|
}
|
|
|
|
ast::Expr::BoxExpr(_)
|
|
|
|
| ast::Expr::ForExpr(_)
|
|
|
|
| ast::Expr::ReturnExpr(_)
|
|
|
|
| ast::Expr::TryExpr(_)
|
|
|
|
| ast::Expr::YieldExpr(_)
|
|
|
|
| ast::Expr::AwaitExpr(_) => is_const = false,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
!is_const
|
|
|
|
});
|
|
|
|
is_const
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
check_assist(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x$0 = 0;
|
|
|
|
let y = x;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
const $0X: i32 = 0;
|
|
|
|
let y = X;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-11 16:28:17 -05:00
|
|
|
#[test]
|
|
|
|
fn multiple_uses() {
|
|
|
|
check_assist(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x$0 = 0;
|
|
|
|
let y = x;
|
|
|
|
let z = (x, x, x, x);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
const $0X: i32 = 0;
|
|
|
|
let y = X;
|
|
|
|
let z = (X, X, X, X);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:49:46 -05:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_non_const_meth_call() {
|
|
|
|
cov_mark::check!(promote_local_non_const);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self) {}
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
let x$0 = Foo.foo();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_non_const_call() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn bar(self) {}
|
|
|
|
fn foo() {
|
|
|
|
let x$0 = bar();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_unknown_ty() {
|
2023-04-07 08:45:04 -05:00
|
|
|
cov_mark::check!(promote_local_not_applicable_if_ty_not_inferred);
|
2021-10-14 14:49:46 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x$0 = bar();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_non_simple_ident() {
|
|
|
|
cov_mark::check!(promote_local_non_simple_ident);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let ref x$0 = ();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
promote_local_to_const,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let mut x$0 = ();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|