diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
commit | f209843e31af7f0e0212aa28ffec2efad2a70c6f (patch) | |
tree | 548227da78a3bea644f57714d075410c0bdf7469 /crates/ra_assists/src/add_missing_impl_members.rs | |
parent | 58d4983ba5745975446d60f2886d96f8d2adf0f2 (diff) | |
parent | d4a66166c002f0a49e41d856a49cb5685ac93202 (diff) |
Merge #1545
1545: migrate ra_syntax to the new rowan API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/add_missing_impl_members.rs')
-rw-r--r-- | crates/ra_assists/src/add_missing_impl_members.rs | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 6ffdad0b1..b992a4dc8 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs | |||
@@ -5,8 +5,8 @@ use crate::{ | |||
5 | 5 | ||
6 | use hir::{db::HirDatabase, HasSource}; | 6 | use hir::{db::HirDatabase, HasSource}; |
7 | use ra_db::FilePosition; | 7 | use ra_db::FilePosition; |
8 | use ra_syntax::ast::{self, AstNode, ImplItem, ImplItemKind, NameOwner}; | 8 | use ra_syntax::ast::{self, AstNode, ImplItemKind, NameOwner}; |
9 | use ra_syntax::{SmolStr, TreeArc}; | 9 | use ra_syntax::SmolStr; |
10 | 10 | ||
11 | #[derive(PartialEq)] | 11 | #[derive(PartialEq)] |
12 | enum AddMissingImplMembersMode { | 12 | enum AddMissingImplMembersMode { |
@@ -46,16 +46,16 @@ fn add_missing_impl_members_inner( | |||
46 | let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; | 46 | let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; |
47 | let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None); | 47 | let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None); |
48 | 48 | ||
49 | resolve_target_trait_def(ctx.db, &analyzer, impl_node)? | 49 | resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? |
50 | }; | 50 | }; |
51 | 51 | ||
52 | let def_name = |kind| -> Option<&SmolStr> { | 52 | let def_name = |kind| -> Option<SmolStr> { |
53 | match kind { | 53 | match kind { |
54 | ImplItemKind::FnDef(def) => def.name(), | 54 | ast::ImplItemKind::FnDef(def) => def.name(), |
55 | ImplItemKind::TypeAliasDef(def) => def.name(), | 55 | ast::ImplItemKind::TypeAliasDef(def) => def.name(), |
56 | ImplItemKind::ConstDef(def) => def.name(), | 56 | ast::ImplItemKind::ConstDef(def) => def.name(), |
57 | } | 57 | } |
58 | .map(ast::Name::text) | 58 | .map(|it| it.text().clone()) |
59 | }; | 59 | }; |
60 | 60 | ||
61 | let trait_items = trait_def.item_list()?.impl_items(); | 61 | let trait_items = trait_def.item_list()?.impl_items(); |
@@ -78,18 +78,13 @@ fn add_missing_impl_members_inner( | |||
78 | 78 | ||
79 | ctx.add_action(AssistId(assist_id), label, |edit| { | 79 | ctx.add_action(AssistId(assist_id), label, |edit| { |
80 | let n_existing_items = impl_item_list.impl_items().count(); | 80 | let n_existing_items = impl_item_list.impl_items().count(); |
81 | let items: Vec<_> = missing_items | 81 | let items = missing_items.into_iter().map(|it| match it.kind() { |
82 | .into_iter() | 82 | ImplItemKind::FnDef(def) => strip_docstring(add_body(def).into()), |
83 | .map(|it| match it.kind() { | 83 | _ => strip_docstring(it), |
84 | ImplItemKind::FnDef(def) => { | 84 | }); |
85 | strip_docstring(ImplItem::cast(add_body(def).syntax()).unwrap()) | ||
86 | } | ||
87 | _ => strip_docstring(it), | ||
88 | }) | ||
89 | .collect(); | ||
90 | let mut ast_editor = AstEditor::new(impl_item_list); | 85 | let mut ast_editor = AstEditor::new(impl_item_list); |
91 | 86 | ||
92 | ast_editor.append_items(items.iter().map(|it| &**it)); | 87 | ast_editor.append_items(items); |
93 | 88 | ||
94 | let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); | 89 | let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); |
95 | let cursor_position = first_new_item.syntax().range().start(); | 90 | let cursor_position = first_new_item.syntax().range().start(); |
@@ -101,14 +96,14 @@ fn add_missing_impl_members_inner( | |||
101 | ctx.build() | 96 | ctx.build() |
102 | } | 97 | } |
103 | 98 | ||
104 | fn strip_docstring(item: &ast::ImplItem) -> TreeArc<ast::ImplItem> { | 99 | fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { |
105 | let mut ast_editor = AstEditor::new(item); | 100 | let mut ast_editor = AstEditor::new(item); |
106 | ast_editor.strip_attrs_and_docs(); | 101 | ast_editor.strip_attrs_and_docs(); |
107 | ast_editor.ast().to_owned() | 102 | ast_editor.ast().to_owned() |
108 | } | 103 | } |
109 | 104 | ||
110 | fn add_body(fn_def: &ast::FnDef) -> TreeArc<ast::FnDef> { | 105 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
111 | let mut ast_editor = AstEditor::new(fn_def); | 106 | let mut ast_editor = AstEditor::new(fn_def.clone()); |
112 | if fn_def.body().is_none() { | 107 | if fn_def.body().is_none() { |
113 | ast_editor.set_body(&AstBuilder::<ast::Block>::single_expr( | 108 | ast_editor.set_body(&AstBuilder::<ast::Block>::single_expr( |
114 | &AstBuilder::<ast::Expr>::unimplemented(), | 109 | &AstBuilder::<ast::Expr>::unimplemented(), |
@@ -123,9 +118,12 @@ fn resolve_target_trait_def( | |||
123 | db: &impl HirDatabase, | 118 | db: &impl HirDatabase, |
124 | analyzer: &hir::SourceAnalyzer, | 119 | analyzer: &hir::SourceAnalyzer, |
125 | impl_block: &ast::ImplBlock, | 120 | impl_block: &ast::ImplBlock, |
126 | ) -> Option<TreeArc<ast::TraitDef>> { | 121 | ) -> Option<ast::TraitDef> { |
127 | let ast_path = | 122 | let ast_path = impl_block |
128 | impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?.path()?; | 123 | .target_trait() |
124 | .map(|it| it.syntax().clone()) | ||
125 | .and_then(ast::PathType::cast)? | ||
126 | .path()?; | ||
129 | 127 | ||
130 | match analyzer.resolve_path(db, &ast_path) { | 128 | match analyzer.resolve_path(db, &ast_path) { |
131 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).ast), | 129 | Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).ast), |