mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
Auto merge of #15484 - rmehri01:14779_bool_to_enum_assist, r=Veykril
feat: Bool to enum assist This adds the `bool_to_enum` assist, which converts the type of boolean local variables, fields, constants and statics to a new `enum` type, making it easier to distinguish the meaning of `true` and `false` by renaming the variants. Closes #14779
This commit is contained in:
commit
df75809a85
4 changed files with 1572 additions and 0 deletions
|
@ -972,6 +972,11 @@ pub fn tuple_field(visibility: Option<ast::Visibility>, ty: ast::Type) -> ast::T
|
|||
ast_from_text(&format!("struct f({visibility}{ty});"))
|
||||
}
|
||||
|
||||
pub fn variant_list(variants: impl IntoIterator<Item = ast::Variant>) -> ast::VariantList {
|
||||
let variants = variants.into_iter().join(", ");
|
||||
ast_from_text(&format!("enum f {{ {variants} }}"))
|
||||
}
|
||||
|
||||
pub fn variant(name: ast::Name, field_list: Option<ast::FieldList>) -> ast::Variant {
|
||||
let field_list = match field_list {
|
||||
None => String::new(),
|
||||
|
@ -1036,6 +1041,19 @@ pub fn struct_(
|
|||
ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
|
||||
}
|
||||
|
||||
pub fn enum_(
|
||||
visibility: Option<ast::Visibility>,
|
||||
enum_name: ast::Name,
|
||||
variant_list: ast::VariantList,
|
||||
) -> ast::Enum {
|
||||
let visibility = match visibility {
|
||||
None => String::new(),
|
||||
Some(it) => format!("{it} "),
|
||||
};
|
||||
|
||||
ast_from_text(&format!("{visibility}enum {enum_name} {variant_list}"))
|
||||
}
|
||||
|
||||
pub fn attr_outer(meta: ast::Meta) -> ast::Attr {
|
||||
ast_from_text(&format!("#[{meta}]"))
|
||||
}
|
||||
|
@ -1148,6 +1166,16 @@ pub mod tokens {
|
|||
lit.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
|
||||
pub fn ident(text: &str) -> SyntaxToken {
|
||||
assert_eq!(text.trim(), text);
|
||||
let path: ast::Path = super::ext::ident_path(text);
|
||||
path.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == IDENT)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn single_newline() -> SyntaxToken {
|
||||
let res = SOURCE_FILE
|
||||
.tree()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue