mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-16 15:14:02 +00:00
feat: add bool_to_enum assist for parameters
This commit is contained in:
parent
21c63abec7
commit
d468746075
1 changed files with 20 additions and 16 deletions
|
@ -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);
|
||||||
|
match ident_pat.syntax().parent().and_then(Either::<ast::Param, ast::LetStmt>::cast)? {
|
||||||
|
Either::Left(param) => Some(BoolNodeData {
|
||||||
|
target_node: param.syntax().clone(),
|
||||||
|
name,
|
||||||
|
ty_annotation: param.ty(),
|
||||||
|
initializer: None,
|
||||||
|
definition: local_definition,
|
||||||
|
}),
|
||||||
|
Either::Right(let_stmt) => Some(BoolNodeData {
|
||||||
target_node: let_stmt.syntax().clone(),
|
target_node: let_stmt.syntax().clone(),
|
||||||
name,
|
name,
|
||||||
ty_annotation: let_stmt.ty(),
|
ty_annotation: let_stmt.ty(),
|
||||||
initializer: let_stmt.initializer(),
|
initializer: let_stmt.initializer(),
|
||||||
definition: Definition::Local(def),
|
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() {
|
||||||
|
|
Loading…
Reference in a new issue