aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs5
-rw-r--r--crates/ra_assists/src/doc_tests.rs6
-rw-r--r--crates/ra_assists/src/lib.rs34
3 files changed, 23 insertions, 22 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 82f61bc8f..83dd270c6 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -4,14 +4,13 @@ use ra_db::FileRange;
4use ra_fmt::{leading_indent, reindent}; 4use ra_fmt::{leading_indent, reindent};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
7 algo::{self, find_covering_element, find_node_at_offset}, 7 algo::{self, find_covering_element, find_node_at_offset, SyntaxRewriter},
8 AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, 8 AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
9 TokenAtOffset, 9 TokenAtOffset,
10}; 10};
11use ra_text_edit::TextEditBuilder; 11use ra_text_edit::TextEditBuilder;
12 12
13use crate::{AssistAction, AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist}; 13use crate::{AssistAction, AssistFile, AssistId, AssistLabel, GroupLabel, ResolvedAssist};
14use algo::SyntaxRewriter;
15 14
16#[derive(Clone, Debug)] 15#[derive(Clone, Debug)]
17pub(crate) struct Assist(pub(crate) Vec<AssistInfo>); 16pub(crate) struct Assist(pub(crate) Vec<AssistInfo>);
@@ -42,8 +41,6 @@ impl AssistInfo {
42 } 41 }
43} 42}
44 43
45pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
46
47/// `AssistCtx` allows to apply an assist or check if it could be applied. 44/// `AssistCtx` allows to apply an assist or check if it could be applied.
48/// 45///
49/// Assists use a somewhat over-engineered approach, given the current needs. The 46/// Assists use a somewhat over-engineered approach, given the current needs. The
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs
index c0f9bc1fb..f627f31dc 100644
--- a/crates/ra_assists/src/doc_tests.rs
+++ b/crates/ra_assists/src/doc_tests.rs
@@ -30,6 +30,10 @@ fn check(assist_id: &str, before: &str, after: &str) {
30 ) 30 )
31 }); 31 });
32 32
33 let actual = assist.action.edit.apply(&before); 33 let actual = {
34 let mut actual = before.clone();
35 assist.action.edit.apply(&mut actual);
36 actual
37 };
34 assert_eq_text!(after, &actual); 38 assert_eq_text!(after, &actual);
35} 39}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 5cec10088..0f94f5ee8 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -23,7 +23,7 @@ use ra_ide_db::RootDatabase;
23use ra_syntax::{TextRange, TextSize}; 23use ra_syntax::{TextRange, TextSize};
24use ra_text_edit::TextEdit; 24use ra_text_edit::TextEdit;
25 25
26pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; 26pub(crate) use crate::assist_ctx::{Assist, AssistCtx};
27 27
28/// Unique identifier of the assist, should not be shown to the user 28/// Unique identifier of the assist, should not be shown to the user
29/// directly. 29/// directly.
@@ -109,7 +109,9 @@ pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssi
109} 109}
110 110
111mod handlers { 111mod handlers {
112 use crate::AssistHandler; 112 use crate::{Assist, AssistCtx};
113
114 pub(crate) type Handler = fn(AssistCtx) -> Option<Assist>;
113 115
114 mod add_custom_impl; 116 mod add_custom_impl;
115 mod add_derive; 117 mod add_derive;
@@ -145,12 +147,13 @@ mod handlers {
145 mod reorder_fields; 147 mod reorder_fields;
146 mod unwrap_block; 148 mod unwrap_block;
147 149
148 pub(crate) fn all() -> &'static [AssistHandler] { 150 pub(crate) fn all() -> &'static [Handler] {
149 &[ 151 &[
150 // These are alphabetic for the foolish consistency 152 // These are alphabetic for the foolish consistency
151 add_custom_impl::add_custom_impl, 153 add_custom_impl::add_custom_impl,
152 add_derive::add_derive, 154 add_derive::add_derive,
153 add_explicit_type::add_explicit_type, 155 add_explicit_type::add_explicit_type,
156 add_from_impl_for_enum::add_from_impl_for_enum,
154 add_function::add_function, 157 add_function::add_function,
155 add_impl::add_impl, 158 add_impl::add_impl,
156 add_new::add_new, 159 add_new::add_new,
@@ -176,17 +179,18 @@ mod handlers {
176 raw_string::remove_hash, 179 raw_string::remove_hash,
177 remove_dbg::remove_dbg, 180 remove_dbg::remove_dbg,
178 remove_mut::remove_mut, 181 remove_mut::remove_mut,
182 reorder_fields::reorder_fields,
179 replace_if_let_with_match::replace_if_let_with_match, 183 replace_if_let_with_match::replace_if_let_with_match,
180 replace_let_with_if_let::replace_let_with_if_let, 184 replace_let_with_if_let::replace_let_with_if_let,
181 replace_qualified_name_with_use::replace_qualified_name_with_use, 185 replace_qualified_name_with_use::replace_qualified_name_with_use,
182 replace_unwrap_with_match::replace_unwrap_with_match, 186 replace_unwrap_with_match::replace_unwrap_with_match,
183 split_import::split_import, 187 split_import::split_import,
184 add_from_impl_for_enum::add_from_impl_for_enum,
185 unwrap_block::unwrap_block, 188 unwrap_block::unwrap_block,
186 // These are manually sorted for better priorities 189 // These are manually sorted for better priorities
187 add_missing_impl_members::add_missing_impl_members, 190 add_missing_impl_members::add_missing_impl_members,
188 add_missing_impl_members::add_missing_default_members, 191 add_missing_impl_members::add_missing_default_members,
189 reorder_fields::reorder_fields, 192 // Are you sure you want to add new assist here, and not to the
193 // sorted list above?
190 ] 194 ]
191 } 195 }
192} 196}
@@ -195,12 +199,12 @@ mod handlers {
195mod helpers { 199mod helpers {
196 use std::sync::Arc; 200 use std::sync::Arc;
197 201
202 use hir::Semantics;
198 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; 203 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
199 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; 204 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
200 use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset}; 205 use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset};
201 206
202 use crate::{AssistCtx, AssistFile, AssistHandler}; 207 use crate::{handlers::Handler, AssistCtx, AssistFile};
203 use hir::Semantics;
204 208
205 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { 209 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
206 let (mut db, file_id) = RootDatabase::with_single_file(text); 210 let (mut db, file_id) = RootDatabase::with_single_file(text);
@@ -210,22 +214,18 @@ mod helpers {
210 (db, file_id) 214 (db, file_id)
211 } 215 }
212 216
213 pub(crate) fn check_assist( 217 pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
214 assist: AssistHandler,
215 ra_fixture_before: &str,
216 ra_fixture_after: &str,
217 ) {
218 check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); 218 check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
219 } 219 }
220 220
221 // FIXME: instead of having a separate function here, maybe use 221 // FIXME: instead of having a separate function here, maybe use
222 // `extract_ranges` and mark the target as `<target> </target>` in the 222 // `extract_ranges` and mark the target as `<target> </target>` in the
223 // fixuture? 223 // fixuture?
224 pub(crate) fn check_assist_target(assist: AssistHandler, ra_fixture: &str, target: &str) { 224 pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) {
225 check(assist, ra_fixture, ExpectedResult::Target(target)); 225 check(assist, ra_fixture, ExpectedResult::Target(target));
226 } 226 }
227 227
228 pub(crate) fn check_assist_not_applicable(assist: AssistHandler, ra_fixture: &str) { 228 pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
229 check(assist, ra_fixture, ExpectedResult::NotApplicable); 229 check(assist, ra_fixture, ExpectedResult::NotApplicable);
230 } 230 }
231 231
@@ -235,7 +235,7 @@ mod helpers {
235 Target(&'a str), 235 Target(&'a str),
236 } 236 }
237 237
238 fn check(assist: AssistHandler, before: &str, expected: ExpectedResult) { 238 fn check(assist: Handler, before: &str, expected: ExpectedResult) {
239 let (text_without_caret, file_with_caret_id, range_or_offset, db) = 239 let (text_without_caret, file_with_caret_id, range_or_offset, db) =
240 if before.contains("//-") { 240 if before.contains("//-") {
241 let (mut db, position) = RootDatabase::with_position(before); 241 let (mut db, position) = RootDatabase::with_position(before);
@@ -261,13 +261,13 @@ mod helpers {
261 (Some(assist), ExpectedResult::After(after)) => { 261 (Some(assist), ExpectedResult::After(after)) => {
262 let action = assist.0[0].action.clone().unwrap(); 262 let action = assist.0[0].action.clone().unwrap();
263 263
264 let assisted_file_text = if let AssistFile::TargetFile(file_id) = action.file { 264 let mut actual = if let AssistFile::TargetFile(file_id) = action.file {
265 db.file_text(file_id).as_ref().to_owned() 265 db.file_text(file_id).as_ref().to_owned()
266 } else { 266 } else {
267 text_without_caret 267 text_without_caret
268 }; 268 };
269 action.edit.apply(&mut actual);
269 270
270 let mut actual = action.edit.apply(&assisted_file_text);
271 match action.cursor_position { 271 match action.cursor_position {
272 None => { 272 None => {
273 if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { 273 if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {