feat: add bool_to_enum assist for parameters

This commit is contained in:
winstxnhdw 2024-06-21 00:52:30 +08:00
parent 21c63abec7
commit d468746075
No known key found for this signature in database
GPG key ID: 28C6693A1379DAE9

View file

@ -1,3 +1,4 @@
use either::Either;
use hir::{ImportPathConfig, ModuleDef}; use hir::{ImportPathConfig, ModuleDef};
use ide_db::{ use ide_db::{
assists::{AssistId, AssistKind}, assists::{AssistId, AssistKind},
@ -93,27 +94,30 @@ struct BoolNodeData {
fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> { fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
let name: ast::Name = ctx.find_node_at_offset()?; let name: ast::Name = ctx.find_node_at_offset()?;
if let Some(let_stmt) = name.syntax().ancestors().find_map(ast::LetStmt::cast) { if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
let bind_pat = match let_stmt.pat()? { let def = ctx.sema.to_def(&ident_pat)?;
ast::Pat::IdentPat(pat) => pat,
_ => {
cov_mark::hit!(not_applicable_in_non_ident_pat);
return None;
}
};
let def = ctx.sema.to_def(&bind_pat)?;
if !def.ty(ctx.db()).is_bool() { if !def.ty(ctx.db()).is_bool() {
cov_mark::hit!(not_applicable_non_bool_local); cov_mark::hit!(not_applicable_non_bool_local);
return None; return None;
} }
Some(BoolNodeData { let local_definition = Definition::Local(def);
target_node: let_stmt.syntax().clone(), match ident_pat.syntax().parent().and_then(Either::<ast::Param, ast::LetStmt>::cast)? {
name, Either::Left(param) => Some(BoolNodeData {
ty_annotation: let_stmt.ty(), target_node: param.syntax().clone(),
initializer: let_stmt.initializer(), name,
definition: Definition::Local(def), ty_annotation: param.ty(),
}) initializer: None,
definition: local_definition,
}),
Either::Right(let_stmt) => Some(BoolNodeData {
target_node: let_stmt.syntax().clone(),
name,
ty_annotation: let_stmt.ty(),
initializer: let_stmt.initializer(),
definition: local_definition,
}),
}
} else if let Some(const_) = name.syntax().parent().and_then(ast::Const::cast) { } else if let Some(const_) = name.syntax().parent().and_then(ast::Const::cast) {
let def = ctx.sema.to_def(&const_)?; let def = ctx.sema.to_def(&const_)?;
if !def.ty(ctx.db()).is_bool() { if !def.ty(ctx.db()).is_bool() {