From 26ed92568596ce45ad96c3e2ea5d54099702537f Mon Sep 17 00:00:00 2001 From: Sergey Parilin Date: Thu, 11 Apr 2019 00:00:56 +0300 Subject: fill struct fields diagnostic --- crates/ra_assists/src/ast_editor.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 6854294ae..726e5c0a3 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -4,6 +4,7 @@ use arrayvec::ArrayVec; use ra_text_edit::TextEditBuilder; use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction}; use ra_fmt::leading_indent; +use hir::Name; pub struct AstEditor { original_ast: TreeArc, @@ -235,6 +236,10 @@ pub struct AstBuilder { } impl AstBuilder { + pub fn from_name(name: &Name) -> TreeArc { + ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name)) + } + fn from_text(text: &str) -> TreeArc { ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text)) } -- cgit v1.2.3 From 12f8472d2800b2d7c05cb1fc466c80072ed8e283 Mon Sep 17 00:00:00 2001 From: Sergey Parilin Date: Mon, 6 May 2019 17:26:09 +0300 Subject: removed duplicating fill_struct_fields assist --- crates/ra_assists/src/fill_struct_fields.rs | 226 ---------------------------- crates/ra_assists/src/lib.rs | 2 - 2 files changed, 228 deletions(-) delete mode 100644 crates/ra_assists/src/fill_struct_fields.rs (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs deleted file mode 100644 index 54b70e17d..000000000 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ /dev/null @@ -1,226 +0,0 @@ -use hir::{AdtDef, db::HirDatabase}; - -use ra_syntax::ast::{self, AstNode}; - -use crate::{AssistCtx, Assist, AssistId, ast_editor::{AstEditor, AstBuilder}}; - -pub(crate) fn fill_struct_fields(mut ctx: AssistCtx) -> Option { - let struct_lit = ctx.node_at_offset::()?; - let named_field_list = struct_lit.named_field_list()?; - - // Collect all fields from struct definition - let mut fields = { - let analyzer = - hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, struct_lit.syntax(), None); - let struct_lit_ty = analyzer.type_of(ctx.db, struct_lit.into())?; - let struct_def = match struct_lit_ty.as_adt() { - Some((AdtDef::Struct(s), _)) => s, - _ => return None, - }; - struct_def.fields(ctx.db) - }; - - // Filter out existing fields - for ast_field in named_field_list.fields() { - let name_from_ast = ast_field.name_ref()?.text().to_string(); - fields.retain(|field| field.name(ctx.db).to_string() != name_from_ast); - } - if fields.is_empty() { - return None; - } - - let db = ctx.db; - ctx.add_action(AssistId("fill_struct_fields"), "fill struct fields", |edit| { - let mut ast_editor = AstEditor::new(named_field_list); - if named_field_list.fields().count() == 0 && fields.len() > 2 { - ast_editor.make_multiline(); - }; - - for field in fields { - let field = AstBuilder::::from_pieces( - &AstBuilder::::new(&field.name(db).to_string()), - Some(&AstBuilder::::unit()), - ); - ast_editor.append_field(&field); - } - - edit.target(struct_lit.syntax().range()); - edit.set_cursor(struct_lit.syntax().range().start()); - - ast_editor.into_text_edit(edit.text_edit_builder()); - }); - ctx.build() -} - -#[cfg(test)] -mod tests { - use crate::helpers::{check_assist, check_assist_target}; - - use super::fill_struct_fields; - - #[test] - fn fill_struct_fields_empty_body() { - check_assist( - fill_struct_fields, - r#" - struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - e: &'a str, - } - - fn main() { - let s = S<|> {} - } - "#, - r#" - struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - e: &'a str, - } - - fn main() { - let s = <|>S { - a: (), - b: (), - c: (), - d: (), - e: (), - } - } - "#, - ); - } - - #[test] - fn fill_struct_fields_target() { - check_assist_target( - fill_struct_fields, - r#" - struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - e: &'a str, - } - - fn main() { - let s = S<|> {} - } - "#, - "S {}", - ); - } - - #[test] - fn fill_struct_fields_preserve_self() { - check_assist( - fill_struct_fields, - r#" - struct Foo { - foo: u8, - bar: String, - baz: i128, - } - - impl Foo { - pub fn new() -> Self { - Self <|>{} - } - } - "#, - r#" - struct Foo { - foo: u8, - bar: String, - baz: i128, - } - - impl Foo { - pub fn new() -> Self { - <|>Self { - foo: (), - bar: (), - baz: (), - } - } - } - "#, - ); - } - - #[test] - fn fill_struct_fields_partial() { - check_assist( - fill_struct_fields, - r#" - struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - e: &'a str, - } - - fn main() { - let s = S { - c: (1, 2), - e: "foo",<|> - } - } - "#, - r#" - struct S<'a, D> { - a: u32, - b: String, - c: (i32, i32), - d: D, - e: &'a str, - } - - fn main() { - let s = <|>S { - c: (1, 2), - e: "foo", - a: (), - b: (), - d: (), - } - } - "#, - ); - } - - #[test] - fn fill_struct_short() { - check_assist( - fill_struct_fields, - r#" - struct S { - foo: u32, - bar: String, - } - - fn main() { - let s = S {<|> }; - } - "#, - r#" - struct S { - foo: u32, - bar: String, - } - - fn main() { - let s = <|>S { foo: (), bar: () }; - } - "#, - ); - } -} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index a2998ae59..ae97a1ab5 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -93,7 +93,6 @@ mod flip_comma; mod flip_binexpr; mod change_visibility; mod fill_match_arms; -mod fill_struct_fields; mod introduce_variable; mod inline_local_variable; mod replace_if_let_with_match; @@ -110,7 +109,6 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option