aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs15
-rw-r--r--crates/ra_assists/src/auto_import.rs18
2 files changed, 23 insertions, 10 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index a12c3ed54..c45262efa 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -129,10 +129,13 @@ pub(crate) struct AssistBuilder {
129} 129}
130 130
131impl AssistBuilder { 131impl AssistBuilder {
132 /// Replaces specified `range` of text with a given string.
132 pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) { 133 pub(crate) fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) {
133 self.edit.replace(range, replace_with.into()) 134 self.edit.replace(range, replace_with.into())
134 } 135 }
135 136
137 /// Replaces specified `node` of text with a given string, reindenting the
138 /// string to maintain `node`'s existing indent.
136 pub(crate) fn replace_node_and_indent( 139 pub(crate) fn replace_node_and_indent(
137 &mut self, 140 &mut self,
138 node: &SyntaxNode, 141 node: &SyntaxNode,
@@ -145,27 +148,31 @@ impl AssistBuilder {
145 self.replace(node.text_range(), replace_with) 148 self.replace(node.text_range(), replace_with)
146 } 149 }
147 150
148 pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) { 151 /// Remove specified `range` of text.
149 self.edit = edit;
150 }
151
152 #[allow(unused)] 152 #[allow(unused)]
153 pub(crate) fn delete(&mut self, range: TextRange) { 153 pub(crate) fn delete(&mut self, range: TextRange) {
154 self.edit.delete(range) 154 self.edit.delete(range)
155 } 155 }
156 156
157 /// Append specified `text` at the given `offset`
157 pub(crate) fn insert(&mut self, offset: TextUnit, text: impl Into<String>) { 158 pub(crate) fn insert(&mut self, offset: TextUnit, text: impl Into<String>) {
158 self.edit.insert(offset, text.into()) 159 self.edit.insert(offset, text.into())
159 } 160 }
160 161
162 /// Specify desired position of the cursor after the assist is applied.
161 pub(crate) fn set_cursor(&mut self, offset: TextUnit) { 163 pub(crate) fn set_cursor(&mut self, offset: TextUnit) {
162 self.cursor_position = Some(offset) 164 self.cursor_position = Some(offset)
163 } 165 }
164 166
167 /// Specify that the assist should be active withing the `target` range.
168 ///
169 /// Target ranges are used to sort assists: the smaller the target range,
170 /// the more specific assist is, and so it should be sorted first.
165 pub(crate) fn target(&mut self, target: TextRange) { 171 pub(crate) fn target(&mut self, target: TextRange) {
166 self.target = Some(target) 172 self.target = Some(target)
167 } 173 }
168 174
175 /// Get access to the raw `TextEditBuilder`.
169 pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder { 176 pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder {
170 &mut self.edit 177 &mut self.edit
171 } 178 }
diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs
index 43e75eee1..a32e2f9b6 100644
--- a/crates/ra_assists/src/auto_import.rs
+++ b/crates/ra_assists/src/auto_import.rs
@@ -562,9 +562,12 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
562 AssistId("auto_import"), 562 AssistId("auto_import"),
563 format!("import {} in mod {}", fmt_segments(&segments), name.text()), 563 format!("import {} in mod {}", fmt_segments(&segments), name.text()),
564 |edit| { 564 |edit| {
565 let mut text_edit = TextEditBuilder::default(); 565 apply_auto_import(
566 apply_auto_import(item_list.syntax(), &path, &segments, &mut text_edit); 566 item_list.syntax(),
567 edit.set_edit_builder(text_edit); 567 &path,
568 &segments,
569 edit.text_edit_builder(),
570 );
568 }, 571 },
569 ); 572 );
570 } 573 }
@@ -574,9 +577,12 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
574 AssistId("auto_import"), 577 AssistId("auto_import"),
575 format!("import {} in the current file", fmt_segments(&segments)), 578 format!("import {} in the current file", fmt_segments(&segments)),
576 |edit| { 579 |edit| {
577 let mut text_edit = TextEditBuilder::default(); 580 apply_auto_import(
578 apply_auto_import(current_file.syntax(), &path, &segments, &mut text_edit); 581 current_file.syntax(),
579 edit.set_edit_builder(text_edit); 582 &path,
583 &segments,
584 edit.text_edit_builder(),
585 );
580 }, 586 },
581 ); 587 );
582 } 588 }