diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-06 15:39:49 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-06 15:39:49 +0100 |
commit | 1252107a3c7964ac2e5e7726173b56bb4dfbe621 (patch) | |
tree | 5189803adf9b8d89cb4823362e568748de039396 /crates/ra_assists/src | |
parent | efd8e34c396f1524623a495e47111f1047cf2879 (diff) | |
parent | 020ca6695f4d58f651984c4fbe2227d891896bb3 (diff) |
Merge #4345
4345: Refactor assists a bit r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 40 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_function.rs | 14 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 16 | ||||
-rw-r--r-- | crates/ra_assists/src/tests.rs | 4 |
4 files changed, 29 insertions, 45 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 77275156c..871671de2 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -13,7 +13,7 @@ use ra_syntax::{ | |||
13 | }; | 13 | }; |
14 | use ra_text_edit::TextEditBuilder; | 14 | use ra_text_edit::TextEditBuilder; |
15 | 15 | ||
16 | use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist}; | 16 | use crate::{AssistId, AssistLabel, GroupLabel, ResolvedAssist}; |
17 | 17 | ||
18 | #[derive(Clone, Debug)] | 18 | #[derive(Clone, Debug)] |
19 | pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); | 19 | pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); |
@@ -22,16 +22,16 @@ pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); | |||
22 | pub(crate) struct AssistInfo { | 22 | pub(crate) struct AssistInfo { |
23 | pub(crate) label: AssistLabel, | 23 | pub(crate) label: AssistLabel, |
24 | pub(crate) group_label: Option<GroupLabel>, | 24 | pub(crate) group_label: Option<GroupLabel>, |
25 | pub(crate) action: Option<SourceChange>, | 25 | pub(crate) source_change: Option<SourceChange>, |
26 | } | 26 | } |
27 | 27 | ||
28 | impl AssistInfo { | 28 | impl AssistInfo { |
29 | fn new(label: AssistLabel) -> AssistInfo { | 29 | fn new(label: AssistLabel) -> AssistInfo { |
30 | AssistInfo { label, group_label: None, action: None } | 30 | AssistInfo { label, group_label: None, source_change: None } |
31 | } | 31 | } |
32 | 32 | ||
33 | fn resolved(self, action: SourceChange) -> AssistInfo { | 33 | fn resolved(self, source_change: SourceChange) -> AssistInfo { |
34 | AssistInfo { action: Some(action), ..self } | 34 | AssistInfo { source_change: Some(source_change), ..self } |
35 | } | 35 | } |
36 | 36 | ||
37 | fn with_group(self, group_label: GroupLabel) -> AssistInfo { | 37 | fn with_group(self, group_label: GroupLabel) -> AssistInfo { |
@@ -40,7 +40,7 @@ impl AssistInfo { | |||
40 | 40 | ||
41 | pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> { | 41 | pub(crate) fn into_resolved(self) -> Option<ResolvedAssist> { |
42 | let label = self.label; | 42 | let label = self.label; |
43 | self.action.map(|action| ResolvedAssist { label, action }) | 43 | self.source_change.map(|source_change| ResolvedAssist { label, source_change }) |
44 | } | 44 | } |
45 | } | 45 | } |
46 | 46 | ||
@@ -104,12 +104,12 @@ impl<'a> AssistCtx<'a> { | |||
104 | let change_label = label.label.clone(); | 104 | let change_label = label.label.clone(); |
105 | let mut info = AssistInfo::new(label); | 105 | let mut info = AssistInfo::new(label); |
106 | if self.should_compute_edit { | 106 | if self.should_compute_edit { |
107 | let action = { | 107 | let source_change = { |
108 | let mut edit = ActionBuilder::new(&self); | 108 | let mut edit = ActionBuilder::new(&self); |
109 | f(&mut edit); | 109 | f(&mut edit); |
110 | edit.build(change_label, self.frange.file_id) | 110 | edit.build(change_label) |
111 | }; | 111 | }; |
112 | info = info.resolved(action) | 112 | info = info.resolved(source_change) |
113 | }; | 113 | }; |
114 | 114 | ||
115 | Some(Assist(vec![info])) | 115 | Some(Assist(vec![info])) |
@@ -163,12 +163,12 @@ impl<'a> AssistGroup<'a> { | |||
163 | let change_label = label.label.clone(); | 163 | let change_label = label.label.clone(); |
164 | let mut info = AssistInfo::new(label).with_group(self.group.clone()); | 164 | let mut info = AssistInfo::new(label).with_group(self.group.clone()); |
165 | if self.ctx.should_compute_edit { | 165 | if self.ctx.should_compute_edit { |
166 | let action = { | 166 | let source_change = { |
167 | let mut edit = ActionBuilder::new(&self.ctx); | 167 | let mut edit = ActionBuilder::new(&self.ctx); |
168 | f(&mut edit); | 168 | f(&mut edit); |
169 | edit.build(change_label, self.ctx.frange.file_id) | 169 | edit.build(change_label) |
170 | }; | 170 | }; |
171 | info = info.resolved(action) | 171 | info = info.resolved(source_change) |
172 | }; | 172 | }; |
173 | 173 | ||
174 | self.assists.push(info) | 174 | self.assists.push(info) |
@@ -186,7 +186,7 @@ impl<'a> AssistGroup<'a> { | |||
186 | pub(crate) struct ActionBuilder<'a, 'b> { | 186 | pub(crate) struct ActionBuilder<'a, 'b> { |
187 | edit: TextEditBuilder, | 187 | edit: TextEditBuilder, |
188 | cursor_position: Option<TextSize>, | 188 | cursor_position: Option<TextSize>, |
189 | file: AssistFile, | 189 | file: FileId, |
190 | ctx: &'a AssistCtx<'b>, | 190 | ctx: &'a AssistCtx<'b>, |
191 | } | 191 | } |
192 | 192 | ||
@@ -195,7 +195,7 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { | |||
195 | Self { | 195 | Self { |
196 | edit: TextEditBuilder::default(), | 196 | edit: TextEditBuilder::default(), |
197 | cursor_position: None, | 197 | cursor_position: None, |
198 | file: AssistFile::default(), | 198 | file: ctx.frange.file_id, |
199 | ctx, | 199 | ctx, |
200 | } | 200 | } |
201 | } | 201 | } |
@@ -254,20 +254,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> { | |||
254 | algo::diff(&node, &new).into_text_edit(&mut self.edit) | 254 | algo::diff(&node, &new).into_text_edit(&mut self.edit) |
255 | } | 255 | } |
256 | 256 | ||
257 | pub(crate) fn set_file(&mut self, assist_file: AssistFile) { | 257 | pub(crate) fn set_file(&mut self, assist_file: FileId) { |
258 | self.file = assist_file | 258 | self.file = assist_file; |
259 | } | 259 | } |
260 | 260 | ||
261 | fn build(self, change_label: String, current_file: FileId) -> SourceChange { | 261 | fn build(self, change_label: String) -> SourceChange { |
262 | let edit = self.edit.finish(); | 262 | let edit = self.edit.finish(); |
263 | if edit.is_empty() && self.cursor_position.is_none() { | 263 | if edit.is_empty() && self.cursor_position.is_none() { |
264 | panic!("Only call `add_assist` if the assist can be applied") | 264 | panic!("Only call `add_assist` if the assist can be applied") |
265 | } | 265 | } |
266 | let file = match self.file { | ||
267 | AssistFile::CurrentFile => current_file, | ||
268 | AssistFile::TargetFile(it) => it, | ||
269 | }; | ||
270 | SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } | 266 | SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } |
271 | .into_source_change(file) | 267 | .into_source_change(self.file) |
272 | } | 268 | } |
273 | } | 269 | } |
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 1d9d4e638..278079665 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs | |||
@@ -3,9 +3,10 @@ use ra_syntax::{ | |||
3 | SyntaxKind, SyntaxNode, TextSize, | 3 | SyntaxKind, SyntaxNode, TextSize, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{Assist, AssistCtx, AssistFile, AssistId}; | 6 | use crate::{Assist, AssistCtx, AssistId}; |
7 | use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner}; | 7 | use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner}; |
8 | use hir::HirDisplay; | 8 | use hir::HirDisplay; |
9 | use ra_db::FileId; | ||
9 | use rustc_hash::{FxHashMap, FxHashSet}; | 10 | use rustc_hash::{FxHashMap, FxHashSet}; |
10 | 11 | ||
11 | // Assist: add_function | 12 | // Assist: add_function |
@@ -70,7 +71,7 @@ struct FunctionTemplate { | |||
70 | insert_offset: TextSize, | 71 | insert_offset: TextSize, |
71 | cursor_offset: TextSize, | 72 | cursor_offset: TextSize, |
72 | fn_def: ast::SourceFile, | 73 | fn_def: ast::SourceFile, |
73 | file: AssistFile, | 74 | file: FileId, |
74 | } | 75 | } |
75 | 76 | ||
76 | struct FunctionBuilder { | 77 | struct FunctionBuilder { |
@@ -78,7 +79,7 @@ struct FunctionBuilder { | |||
78 | fn_name: ast::Name, | 79 | fn_name: ast::Name, |
79 | type_params: Option<ast::TypeParamList>, | 80 | type_params: Option<ast::TypeParamList>, |
80 | params: ast::ParamList, | 81 | params: ast::ParamList, |
81 | file: AssistFile, | 82 | file: FileId, |
82 | needs_pub: bool, | 83 | needs_pub: bool, |
83 | } | 84 | } |
84 | 85 | ||
@@ -92,7 +93,7 @@ impl FunctionBuilder { | |||
92 | target_module: Option<hir::InFile<hir::ModuleSource>>, | 93 | target_module: Option<hir::InFile<hir::ModuleSource>>, |
93 | ) -> Option<Self> { | 94 | ) -> Option<Self> { |
94 | let needs_pub = target_module.is_some(); | 95 | let needs_pub = target_module.is_some(); |
95 | let mut file = AssistFile::default(); | 96 | let mut file = ctx.frange.file_id; |
96 | let target = if let Some(target_module) = target_module { | 97 | let target = if let Some(target_module) = target_module { |
97 | let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?; | 98 | let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?; |
98 | file = in_file; | 99 | file = in_file; |
@@ -253,9 +254,8 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu | |||
253 | fn next_space_for_fn_in_module( | 254 | fn next_space_for_fn_in_module( |
254 | db: &dyn hir::db::AstDatabase, | 255 | db: &dyn hir::db::AstDatabase, |
255 | module: hir::InFile<hir::ModuleSource>, | 256 | module: hir::InFile<hir::ModuleSource>, |
256 | ) -> Option<(AssistFile, GeneratedFunctionTarget)> { | 257 | ) -> Option<(FileId, GeneratedFunctionTarget)> { |
257 | let file = module.file_id.original_file(db); | 258 | let file = module.file_id.original_file(db); |
258 | let assist_file = AssistFile::TargetFile(file); | ||
259 | let assist_item = match module.value { | 259 | let assist_item = match module.value { |
260 | hir::ModuleSource::SourceFile(it) => { | 260 | hir::ModuleSource::SourceFile(it) => { |
261 | if let Some(last_item) = it.items().last() { | 261 | if let Some(last_item) = it.items().last() { |
@@ -272,7 +272,7 @@ fn next_space_for_fn_in_module( | |||
272 | } | 272 | } |
273 | } | 273 | } |
274 | }; | 274 | }; |
275 | Some((assist_file, assist_item)) | 275 | Some((file, assist_item)) |
276 | } | 276 | } |
277 | 277 | ||
278 | #[cfg(test)] | 278 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 8cd8f89c4..13ea45ec7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -18,7 +18,7 @@ pub mod utils; | |||
18 | pub mod ast_transform; | 18 | pub mod ast_transform; |
19 | 19 | ||
20 | use hir::Semantics; | 20 | use hir::Semantics; |
21 | use ra_db::{FileId, FileRange}; | 21 | use ra_db::FileRange; |
22 | use ra_ide_db::{source_change::SourceChange, RootDatabase}; | 22 | use ra_ide_db::{source_change::SourceChange, RootDatabase}; |
23 | use ra_syntax::TextRange; | 23 | use ra_syntax::TextRange; |
24 | 24 | ||
@@ -59,19 +59,7 @@ impl AssistLabel { | |||
59 | #[derive(Debug, Clone)] | 59 | #[derive(Debug, Clone)] |
60 | pub struct ResolvedAssist { | 60 | pub struct ResolvedAssist { |
61 | pub label: AssistLabel, | 61 | pub label: AssistLabel, |
62 | pub action: SourceChange, | 62 | pub source_change: SourceChange, |
63 | } | ||
64 | |||
65 | #[derive(Debug, Clone, Copy)] | ||
66 | enum AssistFile { | ||
67 | CurrentFile, | ||
68 | TargetFile(FileId), | ||
69 | } | ||
70 | |||
71 | impl Default for AssistFile { | ||
72 | fn default() -> Self { | ||
73 | Self::CurrentFile | ||
74 | } | ||
75 | } | 63 | } |
76 | 64 | ||
77 | /// Return all the assists applicable at the given position. | 65 | /// Return all the assists applicable at the given position. |
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index 572462bfc..17e3ece9f 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs | |||
@@ -57,7 +57,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
57 | }); | 57 | }); |
58 | 58 | ||
59 | let actual = { | 59 | let actual = { |
60 | let change = assist.action.source_file_edits.pop().unwrap(); | 60 | let change = assist.source_change.source_file_edits.pop().unwrap(); |
61 | let mut actual = before.clone(); | 61 | let mut actual = before.clone(); |
62 | change.edit.apply(&mut actual); | 62 | change.edit.apply(&mut actual); |
63 | actual | 63 | actual |
@@ -94,7 +94,7 @@ fn check(assist: Handler, before: &str, expected: ExpectedResult) { | |||
94 | 94 | ||
95 | match (assist(assist_ctx), expected) { | 95 | match (assist(assist_ctx), expected) { |
96 | (Some(assist), ExpectedResult::After(after)) => { | 96 | (Some(assist), ExpectedResult::After(after)) => { |
97 | let mut action = assist.0[0].action.clone().unwrap(); | 97 | let mut action = assist.0[0].source_change.clone().unwrap(); |
98 | let change = action.source_file_edits.pop().unwrap(); | 98 | let change = action.source_file_edits.pop().unwrap(); |
99 | 99 | ||
100 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); | 100 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); |