aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/handlers/auto_import.rs92
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs6
-rw-r--r--crates/ra_assists/src/handlers/fix_visibility.rs157
-rw-r--r--crates/ra_assists/src/handlers/introduce_variable.rs20
-rw-r--r--crates/ra_assists/src/handlers/raw_string.rs48
-rw-r--r--crates/ra_assists/src/tests.rs32
-rw-r--r--crates/ra_assists/src/utils.rs3
7 files changed, 178 insertions, 180 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs
index 5092bf336..d1cafa7d9 100644
--- a/crates/ra_assists/src/handlers/auto_import.rs
+++ b/crates/ra_assists/src/handlers/auto_import.rs
@@ -488,16 +488,17 @@ mod tests {
488 check_assist( 488 check_assist(
489 auto_import, 489 auto_import,
490 r" 490 r"
491 //- /lib.rs crate:crate_with_macro 491//- /lib.rs crate:crate_with_macro
492 #[macro_export] 492#[macro_export]
493 macro_rules! foo { 493macro_rules! foo {
494 () => () 494 () => ()
495 } 495}
496 496
497 //- /main.rs crate:main deps:crate_with_macro 497//- /main.rs crate:main deps:crate_with_macro
498 fn main() { 498fn main() {
499 foo<|> 499 foo<|>
500 }", 500}
501",
501 r"use crate_with_macro::foo; 502 r"use crate_with_macro::foo;
502 503
503fn main() { 504fn main() {
@@ -847,13 +848,14 @@ fn main() {
847 check_assist( 848 check_assist(
848 auto_import, 849 auto_import,
849 r" 850 r"
850 //- /lib.rs crate:dep 851//- /lib.rs crate:dep
851 pub struct Struct; 852pub struct Struct;
852 853
853 //- /main.rs crate:main deps:dep 854//- /main.rs crate:main deps:dep
854 fn main() { 855fn main() {
855 Struct<|> 856 Struct<|>
856 }", 857}
858",
857 r"use dep::Struct; 859 r"use dep::Struct;
858 860
859fn main() { 861fn main() {
@@ -869,20 +871,22 @@ fn main() {
869 check_assist( 871 check_assist(
870 auto_import, 872 auto_import,
871 r" 873 r"
872 //- /lib.rs crate:dep 874//- /lib.rs crate:dep
873 pub mod fmt { 875pub mod fmt {
874 pub trait Display {} 876 pub trait Display {}
875 } 877}
876 878
877 pub fn panic_fmt() {} 879pub fn panic_fmt() {}
878 880
879 //- /main.rs crate:main deps:dep 881//- /main.rs crate:main deps:dep
880 struct S; 882struct S;
881 883
882 impl f<|>mt::Display for S {}", 884impl f<|>mt::Display for S {}
885",
883 r"use dep::fmt; 886 r"use dep::fmt;
884 887
885struct S; 888struct S;
889
886impl fmt::Display for S {} 890impl fmt::Display for S {}
887", 891",
888 ); 892 );
@@ -894,21 +898,20 @@ impl fmt::Display for S {}
894 check_assist( 898 check_assist(
895 auto_import, 899 auto_import,
896 r" 900 r"
897 //- /lib.rs crate:dep 901//- /lib.rs crate:dep
898 902macro_rules! mac {
899 macro_rules! mac { 903 () => {
900 () => { 904 pub struct Cheese;
901 pub struct Cheese; 905 };
902 }; 906}
903 }
904
905 mac!();
906 907
907 //- /main.rs crate:main deps:dep 908mac!();
908 909
909 fn main() { 910//- /main.rs crate:main deps:dep
910 Cheese<|>; 911fn main() {
911 }", 912 Cheese<|>;
913}
914",
912 r"use dep::Cheese; 915 r"use dep::Cheese;
913 916
914fn main() { 917fn main() {
@@ -924,16 +927,15 @@ fn main() {
924 check_assist( 927 check_assist(
925 auto_import, 928 auto_import,
926 r" 929 r"
927 //- /lib.rs crate:dep 930//- /lib.rs crate:dep
928 931pub struct FMT;
929 pub struct FMT; 932pub struct fmt;
930 pub struct fmt;
931
932 //- /main.rs crate:main deps:dep
933 933
934 fn main() { 934//- /main.rs crate:main deps:dep
935 FMT<|>; 935fn main() {
936 }", 936 FMT<|>;
937}
938",
937 r"use dep::FMT; 939 r"use dep::FMT;
938 940
939fn main() { 941fn main() {
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index 3c12c1daa..64270c86f 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -763,9 +763,9 @@ mod tests {
763fn foo(opt: Option<i32>) { 763fn foo(opt: Option<i32>) {
764 match opt<|> { 764 match opt<|> {
765 } 765 }
766}"#; 766}
767 let before = 767"#;
768 &format!("//- /main.rs crate:main deps:core\n{}{}", before, FamousDefs::FIXTURE); 768 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
769 769
770 check_assist( 770 check_assist(
771 fill_match_arms, 771 fill_match_arms,
diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs
index 531b3560f..54601d1f3 100644
--- a/crates/ra_assists/src/handlers/fix_visibility.rs
+++ b/crates/ra_assists/src/handlers/fix_visibility.rs
@@ -255,15 +255,14 @@ mod tests {
255 check_assist( 255 check_assist(
256 fix_visibility, 256 fix_visibility,
257 r" 257 r"
258 //- /main.rs 258//- /main.rs
259 mod foo; 259mod foo;
260 fn main() { foo::Foo<|> } 260fn main() { foo::Foo<|> }
261 261
262 //- /foo.rs 262//- /foo.rs
263 struct Foo; 263struct Foo;
264 ", 264",
265 r"$0pub(crate) struct Foo; 265 r"$0pub(crate) struct Foo;
266
267", 266",
268 ); 267 );
269 } 268 }
@@ -279,14 +278,14 @@ mod tests {
279 ); 278 );
280 check_assist( 279 check_assist(
281 fix_visibility, 280 fix_visibility,
282 r"//- /lib.rs 281 r"
283 mod foo; 282//- /lib.rs
284 fn main() { foo::Foo { <|>bar: () }; } 283mod foo;
285 //- /foo.rs 284fn main() { foo::Foo { <|>bar: () }; }
286 pub struct Foo { bar: () } 285//- /foo.rs
287 ", 286pub struct Foo { bar: () }
287",
288 r"pub struct Foo { $0pub(crate) bar: () } 288 r"pub struct Foo { $0pub(crate) bar: () }
289
290", 289",
291 ); 290 );
292 check_assist_not_applicable( 291 check_assist_not_applicable(
@@ -296,12 +295,13 @@ mod tests {
296 ); 295 );
297 check_assist_not_applicable( 296 check_assist_not_applicable(
298 fix_visibility, 297 fix_visibility,
299 r"//- /lib.rs 298 r"
300 mod foo; 299//- /lib.rs
301 fn main() { foo::Foo { <|>bar: () }; } 300mod foo;
302 //- /foo.rs 301fn main() { foo::Foo { <|>bar: () }; }
303 pub struct Foo { pub bar: () } 302//- /foo.rs
304 ", 303pub struct Foo { pub bar: () }
304",
305 ); 305 );
306 } 306 }
307 307
@@ -316,14 +316,14 @@ mod tests {
316 ); 316 );
317 check_assist( 317 check_assist(
318 fix_visibility, 318 fix_visibility,
319 r"//- /lib.rs 319 r"
320 mod foo; 320//- /lib.rs
321 fn main() { foo::Foo::Bar { <|>bar: () }; } 321mod foo;
322 //- /foo.rs 322fn main() { foo::Foo::Bar { <|>bar: () }; }
323 pub enum Foo { Bar { bar: () } } 323//- /foo.rs
324 ", 324pub enum Foo { Bar { bar: () } }
325",
325 r"pub enum Foo { Bar { $0pub(crate) bar: () } } 326 r"pub enum Foo { Bar { $0pub(crate) bar: () } }
326
327", 327",
328 ); 328 );
329 check_assist_not_applicable( 329 check_assist_not_applicable(
@@ -333,12 +333,13 @@ mod tests {
333 ); 333 );
334 check_assist_not_applicable( 334 check_assist_not_applicable(
335 fix_visibility, 335 fix_visibility,
336 r"//- /lib.rs 336 r"
337 mod foo; 337//- /lib.rs
338 fn main() { foo::Foo { <|>bar: () }; } 338mod foo;
339 //- /foo.rs 339fn main() { foo::Foo { <|>bar: () }; }
340 pub struct Foo { pub bar: () } 340//- /foo.rs
341 ", 341pub struct Foo { pub bar: () }
342",
342 ); 343 );
343 } 344 }
344 345
@@ -355,14 +356,14 @@ mod tests {
355 ); 356 );
356 check_assist( 357 check_assist(
357 fix_visibility, 358 fix_visibility,
358 r"//- /lib.rs 359 r"
359 mod foo; 360//- /lib.rs
360 fn main() { foo::Foo { <|>bar: () }; } 361mod foo;
361 //- /foo.rs 362fn main() { foo::Foo { <|>bar: () }; }
362 pub union Foo { bar: () } 363//- /foo.rs
363 ", 364pub union Foo { bar: () }
365",
364 r"pub union Foo { $0pub(crate) bar: () } 366 r"pub union Foo { $0pub(crate) bar: () }
365
366", 367",
367 ); 368 );
368 check_assist_not_applicable( 369 check_assist_not_applicable(
@@ -372,12 +373,13 @@ mod tests {
372 ); 373 );
373 check_assist_not_applicable( 374 check_assist_not_applicable(
374 fix_visibility, 375 fix_visibility,
375 r"//- /lib.rs 376 r"
376 mod foo; 377//- /lib.rs
377 fn main() { foo::Foo { <|>bar: () }; } 378mod foo;
378 //- /foo.rs 379fn main() { foo::Foo { <|>bar: () }; }
379 pub union Foo { pub bar: () } 380//- /foo.rs
380 ", 381pub union Foo { pub bar: () }
382",
381 ); 383 );
382 } 384 }
383 385
@@ -458,19 +460,18 @@ mod tests {
458 check_assist( 460 check_assist(
459 fix_visibility, 461 fix_visibility,
460 r" 462 r"
461 //- /main.rs 463//- /main.rs
462 mod foo; 464mod foo;
463 fn main() { foo::bar<|>::baz(); } 465fn main() { foo::bar<|>::baz(); }
464 466
465 //- /foo.rs 467//- /foo.rs
466 mod bar { 468mod bar {
467 pub fn baz() {} 469 pub fn baz() {}
468 } 470}
469 ", 471",
470 r"$0pub(crate) mod bar { 472 r"$0pub(crate) mod bar {
471 pub fn baz() {} 473 pub fn baz() {}
472} 474}
473
474", 475",
475 ); 476 );
476 477
@@ -486,17 +487,15 @@ mod tests {
486 check_assist( 487 check_assist(
487 fix_visibility, 488 fix_visibility,
488 r" 489 r"
489 //- /main.rs 490//- /main.rs
490 mod foo; 491mod foo;
491 fn main() { foo::bar<|>::baz(); } 492fn main() { foo::bar<|>::baz(); }
492 493
493 //- /foo.rs 494//- /foo.rs
494 mod bar; 495mod bar;
495 496//- /foo/bar.rs
496 //- /foo/bar.rs 497pub fn baz() {}
497 pub fn baz() {} 498",
498 }
499 ",
500 r"$0pub(crate) mod bar; 499 r"$0pub(crate) mod bar;
501", 500",
502 ); 501 );
@@ -506,14 +505,16 @@ mod tests {
506 fn fix_visibility_of_module_declaration_in_other_file() { 505 fn fix_visibility_of_module_declaration_in_other_file() {
507 check_assist( 506 check_assist(
508 fix_visibility, 507 fix_visibility,
509 r"//- /main.rs 508 r"
510 mod foo; 509//- /main.rs
511 fn main() { foo::bar<|>>::baz(); } 510mod foo;
511fn main() { foo::bar<|>>::baz(); }
512 512
513 //- /foo.rs 513//- /foo.rs
514 mod bar { 514mod bar {
515 pub fn baz() {} 515 pub fn baz() {}
516 }", 516}
517",
517 r"$0pub(crate) mod bar { 518 r"$0pub(crate) mod bar {
518 pub fn baz() {} 519 pub fn baz() {}
519} 520}
@@ -525,10 +526,12 @@ mod tests {
525 fn adds_pub_when_target_is_in_another_crate() { 526 fn adds_pub_when_target_is_in_another_crate() {
526 check_assist( 527 check_assist(
527 fix_visibility, 528 fix_visibility,
528 r"//- /main.rs crate:a deps:foo 529 r"
529 foo::Bar<|> 530//- /main.rs crate:a deps:foo
530 //- /lib.rs crate:foo 531foo::Bar<|>
531 struct Bar;", 532//- /lib.rs crate:foo
533struct Bar;
534",
532 r"$0pub struct Bar; 535 r"$0pub struct Bar;
533", 536",
534 ) 537 )
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs
index 31d6539f7..88b62278f 100644
--- a/crates/ra_assists/src/handlers/introduce_variable.rs
+++ b/crates/ra_assists/src/handlers/introduce_variable.rs
@@ -226,27 +226,31 @@ fn foo() {
226 mark::check!(test_introduce_var_last_expr); 226 mark::check!(test_introduce_var_last_expr);
227 check_assist( 227 check_assist(
228 introduce_variable, 228 introduce_variable,
229 " 229 r#"
230fn foo() { 230fn foo() {
231 bar(<|>1 + 1<|>) 231 bar(<|>1 + 1<|>)
232}", 232}
233 " 233"#,
234 r#"
234fn foo() { 235fn foo() {
235 let $0var_name = 1 + 1; 236 let $0var_name = 1 + 1;
236 bar(var_name) 237 bar(var_name)
237}", 238}
239"#,
238 ); 240 );
239 check_assist( 241 check_assist(
240 introduce_variable, 242 introduce_variable,
241 " 243 r#"
242fn foo() { 244fn foo() {
243 <|>bar(1 + 1)<|> 245 <|>bar(1 + 1)<|>
244}", 246}
245 " 247"#,
248 r#"
246fn foo() { 249fn foo() {
247 let $0var_name = bar(1 + 1); 250 let $0var_name = bar(1 + 1);
248 var_name 251 var_name
249}", 252}
253"#,
250 ) 254 )
251 } 255 }
252 256
diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs
index 16002d2ac..d22d0aa55 100644
--- a/crates/ra_assists/src/handlers/raw_string.rs
+++ b/crates/ra_assists/src/handlers/raw_string.rs
@@ -158,16 +158,16 @@ mod test {
158 check_assist( 158 check_assist(
159 make_raw_string, 159 make_raw_string,
160 r#" 160 r#"
161 fn f() { 161fn f() {
162 let s = <|>"random\nstring"; 162 let s = <|>"random\nstring";
163 } 163}
164 "#, 164"#,
165 r##" 165 r##"
166 fn f() { 166fn f() {
167 let s = r#"random 167 let s = r#"random
168string"#; 168string"#;
169 } 169}
170 "##, 170"##,
171 ) 171 )
172 } 172 }
173 173
@@ -193,16 +193,16 @@ string"#;
193 check_assist( 193 check_assist(
194 make_raw_string, 194 make_raw_string,
195 r###" 195 r###"
196 fn f() { 196fn f() {
197 let s = <|>"#random##\nstring"; 197 let s = <|>"#random##\nstring";
198 } 198}
199 "###, 199"###,
200 r####" 200 r####"
201 fn f() { 201fn f() {
202 let s = r#"#random## 202 let s = r#"#random##
203string"#; 203string"#;
204 } 204}
205 "####, 205"####,
206 ) 206 )
207 } 207 }
208 208
@@ -211,16 +211,16 @@ string"#;
211 check_assist( 211 check_assist(
212 make_raw_string, 212 make_raw_string,
213 r###" 213 r###"
214 fn f() { 214fn f() {
215 let s = <|>"#random\"##\nstring"; 215 let s = <|>"#random\"##\nstring";
216 } 216}
217 "###, 217"###,
218 r####" 218 r####"
219 fn f() { 219fn f() {
220 let s = r###"#random"## 220 let s = r###"#random"##
221string"###; 221string"###;
222 } 222}
223 "####, 223"####,
224 ) 224 )
225 } 225 }
226 226
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs
index 55576813f..858f5ca80 100644
--- a/crates/ra_assists/src/tests.rs
+++ b/crates/ra_assists/src/tests.rs
@@ -4,18 +4,18 @@ use hir::Semantics;
4use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; 4use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::TextRange; 6use ra_syntax::TextRange;
7use test_utils::{ 7use test_utils::{assert_eq_text, extract_offset, extract_range};
8 assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset,
9};
10 8
11use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; 9use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists};
10use stdx::trim_indent;
12 11
13pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { 12pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
14 RootDatabase::with_single_file(text) 13 RootDatabase::with_single_file(text)
15} 14}
16 15
17pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { 16pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
18 check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); 17 let ra_fixture_after = trim_indent(ra_fixture_after);
18 check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after));
19} 19}
20 20
21// FIXME: instead of having a separate function here, maybe use 21// FIXME: instead of having a separate function here, maybe use
@@ -30,8 +30,9 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
30} 30}
31 31
32fn check_doc_test(assist_id: &str, before: &str, after: &str) { 32fn check_doc_test(assist_id: &str, before: &str, after: &str) {
33 let (selection, before) = extract_range_or_offset(before); 33 let after = trim_indent(after);
34 let (db, file_id) = crate::tests::with_single_file(&before); 34 let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before);
35 let before = db.file_text(file_id).to_string();
35 let frange = FileRange { file_id, range: selection.into() }; 36 let frange = FileRange { file_id, range: selection.into() };
36 37
37 let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) 38 let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange)
@@ -51,11 +52,11 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
51 52
52 let actual = { 53 let actual = {
53 let change = assist.source_change.source_file_edits.pop().unwrap(); 54 let change = assist.source_change.source_file_edits.pop().unwrap();
54 let mut actual = before.clone(); 55 let mut actual = before;
55 change.edit.apply(&mut actual); 56 change.edit.apply(&mut actual);
56 actual 57 actual
57 }; 58 };
58 assert_eq_text!(after, &actual); 59 assert_eq_text!(&after, &actual);
59} 60}
60 61
61enum ExpectedResult<'a> { 62enum ExpectedResult<'a> {
@@ -65,19 +66,8 @@ enum ExpectedResult<'a> {
65} 66}
66 67
67fn check(handler: Handler, before: &str, expected: ExpectedResult) { 68fn check(handler: Handler, before: &str, expected: ExpectedResult) {
68 let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { 69 let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
69 let (db, position) = RootDatabase::with_position(before); 70 let text_without_caret = db.file_text(file_with_caret_id).to_string();
70 (
71 db.file_text(position.file_id).as_ref().to_owned(),
72 position.file_id,
73 RangeOrOffset::Offset(position.offset),
74 db,
75 )
76 } else {
77 let (range_or_offset, text_without_caret) = extract_range_or_offset(before);
78 let (db, file_id) = with_single_file(&text_without_caret);
79 (text_without_caret, file_id, range_or_offset, db)
80 };
81 71
82 let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; 72 let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
83 73
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs
index c1ff0de7b..b7c45a619 100644
--- a/crates/ra_assists/src/utils.rs
+++ b/crates/ra_assists/src/utils.rs
@@ -198,8 +198,7 @@ pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>,
198#[allow(non_snake_case)] 198#[allow(non_snake_case)]
199impl FamousDefs<'_, '_> { 199impl FamousDefs<'_, '_> {
200 #[cfg(test)] 200 #[cfg(test)]
201 pub(crate) const FIXTURE: &'static str = r#" 201 pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core
202//- /libcore.rs crate:core
203pub mod convert { 202pub mod convert {
204 pub trait From<T> { 203 pub trait From<T> {
205 fn from(T) -> Self; 204 fn from(T) -> Self;