aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assist_ctx.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assist_ctx.rs')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs30
1 files changed, 19 insertions, 11 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index cbf1963b7..77275156c 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -1,8 +1,11 @@
1//! This module defines `AssistCtx` -- the API surface that is exposed to assists. 1//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
2use hir::Semantics; 2use hir::Semantics;
3use ra_db::FileRange; 3use ra_db::{FileId, FileRange};
4use ra_fmt::{leading_indent, reindent}; 4use ra_fmt::{leading_indent, reindent};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::{
6 source_change::{SingleFileChange, SourceChange},
7 RootDatabase,
8};
6use ra_syntax::{ 9use ra_syntax::{
7 algo::{self, find_covering_element, find_node_at_offset, SyntaxRewriter}, 10 algo::{self, find_covering_element, find_node_at_offset, SyntaxRewriter},
8 AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, 11 AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
@@ -10,7 +13,7 @@ use ra_syntax::{
10}; 13};
11use ra_text_edit::TextEditBuilder; 14use ra_text_edit::TextEditBuilder;
12 15
13use crate::{AssistAction, AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist}; 16use crate::{AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
14 17
15#[derive(Clone, Debug)] 18#[derive(Clone, Debug)]
16pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); 19pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@@ -19,7 +22,7 @@ pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
19pub(crate) struct AssistInfo { 22pub(crate) struct AssistInfo {
20 pub(crate) label: AssistLabel, 23 pub(crate) label: AssistLabel,
21 pub(crate) group_label: Option<GroupLabel>, 24 pub(crate) group_label: Option<GroupLabel>,
22 pub(crate) action: Option<AssistAction>, 25 pub(crate) action: Option<SourceChange>,
23} 26}
24 27
25impl AssistInfo { 28impl AssistInfo {
@@ -27,7 +30,7 @@ impl AssistInfo {
27 AssistInfo { label, group_label: None, action: None } 30 AssistInfo { label, group_label: None, action: None }
28 } 31 }
29 32
30 fn resolved(self, action: AssistAction) -> AssistInfo { 33 fn resolved(self, action: SourceChange) -> AssistInfo {
31 AssistInfo { action: Some(action), ..self } 34 AssistInfo { action: Some(action), ..self }
32 } 35 }
33 36
@@ -98,13 +101,13 @@ impl<'a> AssistCtx<'a> {
98 f: impl FnOnce(&mut ActionBuilder), 101 f: impl FnOnce(&mut ActionBuilder),
99 ) -> Option<Assist> { 102 ) -> Option<Assist> {
100 let label = AssistLabel::new(id, label.into(), None, target); 103 let label = AssistLabel::new(id, label.into(), None, target);
101 104 let change_label = label.label.clone();
102 let mut info = AssistInfo::new(label); 105 let mut info = AssistInfo::new(label);
103 if self.should_compute_edit { 106 if self.should_compute_edit {
104 let action = { 107 let action = {
105 let mut edit = ActionBuilder::new(&self); 108 let mut edit = ActionBuilder::new(&self);
106 f(&mut edit); 109 f(&mut edit);
107 edit.build() 110 edit.build(change_label, self.frange.file_id)
108 }; 111 };
109 info = info.resolved(action) 112 info = info.resolved(action)
110 }; 113 };
@@ -157,13 +160,13 @@ impl<'a> AssistGroup<'a> {
157 f: impl FnOnce(&mut ActionBuilder), 160 f: impl FnOnce(&mut ActionBuilder),
158 ) { 161 ) {
159 let label = AssistLabel::new(id, label.into(), Some(self.group.clone()), target); 162 let label = AssistLabel::new(id, label.into(), Some(self.group.clone()), target);
160 163 let change_label = label.label.clone();
161 let mut info = AssistInfo::new(label).with_group(self.group.clone()); 164 let mut info = AssistInfo::new(label).with_group(self.group.clone());
162 if self.ctx.should_compute_edit { 165 if self.ctx.should_compute_edit {
163 let action = { 166 let action = {
164 let mut edit = ActionBuilder::new(&self.ctx); 167 let mut edit = ActionBuilder::new(&self.ctx);
165 f(&mut edit); 168 f(&mut edit);
166 edit.build() 169 edit.build(change_label, self.ctx.frange.file_id)
167 }; 170 };
168 info = info.resolved(action) 171 info = info.resolved(action)
169 }; 172 };
@@ -255,11 +258,16 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
255 self.file = assist_file 258 self.file = assist_file
256 } 259 }
257 260
258 fn build(self) -> AssistAction { 261 fn build(self, change_label: String, current_file: FileId) -> SourceChange {
259 let edit = self.edit.finish(); 262 let edit = self.edit.finish();
260 if edit.is_empty() && self.cursor_position.is_none() { 263 if edit.is_empty() && self.cursor_position.is_none() {
261 panic!("Only call `add_assist` if the assist can be applied") 264 panic!("Only call `add_assist` if the assist can be applied")
262 } 265 }
263 AssistAction { edit, cursor_position: self.cursor_position, file: self.file } 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 }
271 .into_source_change(file)
264 } 272 }
265} 273}