diff options
Diffstat (limited to 'crates/ra_assists/src')
29 files changed, 290 insertions, 365 deletions
diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index 005c17776..5b1a4680b 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs | |||
@@ -5,7 +5,7 @@ use hir::Semantics; | |||
5 | use ra_db::{FileId, FileRange}; | 5 | use ra_db::{FileId, FileRange}; |
6 | use ra_fmt::{leading_indent, reindent}; | 6 | use ra_fmt::{leading_indent, reindent}; |
7 | use ra_ide_db::{ | 7 | use ra_ide_db::{ |
8 | source_change::{SingleFileChange, SourceChange}, | 8 | source_change::{SourceChange, SourceFileEdit}, |
9 | RootDatabase, | 9 | RootDatabase, |
10 | }; | 10 | }; |
11 | use ra_syntax::{ | 11 | use ra_syntax::{ |
@@ -150,11 +150,10 @@ impl Assists { | |||
150 | self.add_impl(label, f) | 150 | self.add_impl(label, f) |
151 | } | 151 | } |
152 | fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> { | 152 | fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> { |
153 | let change_label = label.label.clone(); | ||
154 | let source_change = if self.resolve { | 153 | let source_change = if self.resolve { |
155 | let mut builder = AssistBuilder::new(self.file); | 154 | let mut builder = AssistBuilder::new(self.file); |
156 | f(&mut builder); | 155 | f(&mut builder); |
157 | Some(builder.finish(change_label)) | 156 | Some(builder.finish()) |
158 | } else { | 157 | } else { |
159 | None | 158 | None |
160 | }; | 159 | }; |
@@ -171,19 +170,13 @@ impl Assists { | |||
171 | 170 | ||
172 | pub(crate) struct AssistBuilder { | 171 | pub(crate) struct AssistBuilder { |
173 | edit: TextEditBuilder, | 172 | edit: TextEditBuilder, |
174 | cursor_position: Option<TextSize>, | ||
175 | file: FileId, | 173 | file: FileId, |
176 | is_snippet: bool, | 174 | is_snippet: bool, |
177 | } | 175 | } |
178 | 176 | ||
179 | impl AssistBuilder { | 177 | impl AssistBuilder { |
180 | pub(crate) fn new(file: FileId) -> AssistBuilder { | 178 | pub(crate) fn new(file: FileId) -> AssistBuilder { |
181 | AssistBuilder { | 179 | AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false } |
182 | edit: TextEditBuilder::default(), | ||
183 | cursor_position: None, | ||
184 | file, | ||
185 | is_snippet: false, | ||
186 | } | ||
187 | } | 180 | } |
188 | 181 | ||
189 | /// Remove specified `range` of text. | 182 | /// Remove specified `range` of text. |
@@ -241,10 +234,6 @@ impl AssistBuilder { | |||
241 | algo::diff(&node, &new).into_text_edit(&mut self.edit) | 234 | algo::diff(&node, &new).into_text_edit(&mut self.edit) |
242 | } | 235 | } |
243 | 236 | ||
244 | /// Specify desired position of the cursor after the assist is applied. | ||
245 | pub(crate) fn set_cursor(&mut self, offset: TextSize) { | ||
246 | self.cursor_position = Some(offset) | ||
247 | } | ||
248 | // FIXME: better API | 237 | // FIXME: better API |
249 | pub(crate) fn set_file(&mut self, assist_file: FileId) { | 238 | pub(crate) fn set_file(&mut self, assist_file: FileId) { |
250 | self.file = assist_file; | 239 | self.file = assist_file; |
@@ -256,14 +245,10 @@ impl AssistBuilder { | |||
256 | &mut self.edit | 245 | &mut self.edit |
257 | } | 246 | } |
258 | 247 | ||
259 | fn finish(self, change_label: String) -> SourceChange { | 248 | fn finish(self) -> SourceChange { |
260 | let edit = self.edit.finish(); | 249 | let edit = self.edit.finish(); |
261 | if edit.is_empty() && self.cursor_position.is_none() { | 250 | let source_file_edit = SourceFileEdit { file_id: self.file, edit }; |
262 | panic!("Only call `add_assist` if the assist can be applied") | 251 | let mut res: SourceChange = source_file_edit.into(); |
263 | } | ||
264 | let mut res = | ||
265 | SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position } | ||
266 | .into_source_change(self.file); | ||
267 | if self.is_snippet { | 252 | if self.is_snippet { |
268 | res.is_snippet = true; | 253 | res.is_snippet = true; |
269 | } | 254 | } |
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index 770049212..ab20c6649 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs | |||
@@ -86,11 +86,7 @@ mod tests { | |||
86 | 86 | ||
87 | #[test] | 87 | #[test] |
88 | fn add_explicit_type_works_for_simple_expr() { | 88 | fn add_explicit_type_works_for_simple_expr() { |
89 | check_assist( | 89 | check_assist(add_explicit_type, "fn f() { let a<|> = 1; }", "fn f() { let a: i32 = 1; }"); |
90 | add_explicit_type, | ||
91 | "fn f() { let a<|> = 1; }", | ||
92 | "fn f() { let a<|>: i32 = 1; }", | ||
93 | ); | ||
94 | } | 90 | } |
95 | 91 | ||
96 | #[test] | 92 | #[test] |
@@ -98,7 +94,7 @@ mod tests { | |||
98 | check_assist( | 94 | check_assist( |
99 | add_explicit_type, | 95 | add_explicit_type, |
100 | "fn f() { let a<|>: _ = 1; }", | 96 | "fn f() { let a<|>: _ = 1; }", |
101 | "fn f() { let a<|>: i32 = 1; }", | 97 | "fn f() { let a: i32 = 1; }", |
102 | ); | 98 | ); |
103 | } | 99 | } |
104 | 100 | ||
@@ -122,7 +118,7 @@ mod tests { | |||
122 | } | 118 | } |
123 | 119 | ||
124 | fn f() { | 120 | fn f() { |
125 | let a<|>: Option<i32> = Option::Some(1); | 121 | let a: Option<i32> = Option::Some(1); |
126 | }"#, | 122 | }"#, |
127 | ); | 123 | ); |
128 | } | 124 | } |
@@ -132,7 +128,7 @@ mod tests { | |||
132 | check_assist( | 128 | check_assist( |
133 | add_explicit_type, | 129 | add_explicit_type, |
134 | r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", | 130 | r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", |
135 | r"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }", | 131 | r"macro_rules! v { () => {0u64} } fn f() { let a: u64 = v!(); }", |
136 | ); | 132 | ); |
137 | } | 133 | } |
138 | 134 | ||
@@ -140,8 +136,8 @@ mod tests { | |||
140 | fn add_explicit_type_works_for_macro_call_recursive() { | 136 | fn add_explicit_type_works_for_macro_call_recursive() { |
141 | check_assist( | 137 | check_assist( |
142 | add_explicit_type, | 138 | add_explicit_type, |
143 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|> = v!(); }", | 139 | r#"macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|> = v!(); }"#, |
144 | "macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a<|>: u64 = v!(); }", | 140 | r#"macro_rules! u { () => {0u64} } macro_rules! v { () => {u!()} } fn f() { let a: u64 = v!(); }"#, |
145 | ); | 141 | ); |
146 | } | 142 | } |
147 | 143 | ||
@@ -208,7 +204,7 @@ struct Test<K, T = u8> { | |||
208 | } | 204 | } |
209 | 205 | ||
210 | fn main() { | 206 | fn main() { |
211 | let test<|>: Test<i32> = Test { t: 23, k: 33 }; | 207 | let test: Test<i32> = Test { t: 23, k: 33 }; |
212 | }"#, | 208 | }"#, |
213 | ); | 209 | ); |
214 | } | 210 | } |
diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 5f6c8b19a..6a675e812 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | |||
@@ -101,7 +101,7 @@ mod tests { | |||
101 | check_assist( | 101 | check_assist( |
102 | add_from_impl_for_enum, | 102 | add_from_impl_for_enum, |
103 | "enum A { <|>One(u32) }", | 103 | "enum A { <|>One(u32) }", |
104 | r#"enum A { <|>One(u32) } | 104 | r#"enum A { One(u32) } |
105 | 105 | ||
106 | impl From<u32> for A { | 106 | impl From<u32> for A { |
107 | fn from(v: u32) -> Self { | 107 | fn from(v: u32) -> Self { |
@@ -116,7 +116,7 @@ impl From<u32> for A { | |||
116 | check_assist( | 116 | check_assist( |
117 | add_from_impl_for_enum, | 117 | add_from_impl_for_enum, |
118 | r#"enum A { <|>One(foo::bar::baz::Boo) }"#, | 118 | r#"enum A { <|>One(foo::bar::baz::Boo) }"#, |
119 | r#"enum A { <|>One(foo::bar::baz::Boo) } | 119 | r#"enum A { One(foo::bar::baz::Boo) } |
120 | 120 | ||
121 | impl From<foo::bar::baz::Boo> for A { | 121 | impl From<foo::bar::baz::Boo> for A { |
122 | fn from(v: foo::bar::baz::Boo) -> Self { | 122 | fn from(v: foo::bar::baz::Boo) -> Self { |
@@ -178,7 +178,7 @@ impl From<String> for A { | |||
178 | pub trait From<T> { | 178 | pub trait From<T> { |
179 | fn from(T) -> Self; | 179 | fn from(T) -> Self; |
180 | }"#, | 180 | }"#, |
181 | r#"enum A { <|>One(u32), Two(String), } | 181 | r#"enum A { One(u32), Two(String), } |
182 | 182 | ||
183 | impl From<u32> for A { | 183 | impl From<u32> for A { |
184 | fn from(v: u32) -> Self { | 184 | fn from(v: u32) -> Self { |
diff --git a/crates/ra_assists/src/handlers/apply_demorgan.rs b/crates/ra_assists/src/handlers/apply_demorgan.rs index 0feba5e11..233e8fb8e 100644 --- a/crates/ra_assists/src/handlers/apply_demorgan.rs +++ b/crates/ra_assists/src/handlers/apply_demorgan.rs | |||
@@ -63,22 +63,22 @@ mod tests { | |||
63 | 63 | ||
64 | #[test] | 64 | #[test] |
65 | fn demorgan_turns_and_into_or() { | 65 | fn demorgan_turns_and_into_or() { |
66 | check_assist(apply_demorgan, "fn f() { !x &&<|> !x }", "fn f() { !(x ||<|> x) }") | 66 | check_assist(apply_demorgan, "fn f() { !x &&<|> !x }", "fn f() { !(x || x) }") |
67 | } | 67 | } |
68 | 68 | ||
69 | #[test] | 69 | #[test] |
70 | fn demorgan_turns_or_into_and() { | 70 | fn demorgan_turns_or_into_and() { |
71 | check_assist(apply_demorgan, "fn f() { !x ||<|> !x }", "fn f() { !(x &&<|> x) }") | 71 | check_assist(apply_demorgan, "fn f() { !x ||<|> !x }", "fn f() { !(x && x) }") |
72 | } | 72 | } |
73 | 73 | ||
74 | #[test] | 74 | #[test] |
75 | fn demorgan_removes_inequality() { | 75 | fn demorgan_removes_inequality() { |
76 | check_assist(apply_demorgan, "fn f() { x != x ||<|> !x }", "fn f() { !(x == x &&<|> x) }") | 76 | check_assist(apply_demorgan, "fn f() { x != x ||<|> !x }", "fn f() { !(x == x && x) }") |
77 | } | 77 | } |
78 | 78 | ||
79 | #[test] | 79 | #[test] |
80 | fn demorgan_general_case() { | 80 | fn demorgan_general_case() { |
81 | check_assist(apply_demorgan, "fn f() { x ||<|> x }", "fn f() { !(!x &&<|> !x) }") | 81 | check_assist(apply_demorgan, "fn f() { x ||<|> x }", "fn f() { !(!x && !x) }") |
82 | } | 82 | } |
83 | 83 | ||
84 | #[test] | 84 | #[test] |
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index f6d25579e..edf96d50e 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -298,7 +298,7 @@ mod tests { | |||
298 | } | 298 | } |
299 | ", | 299 | ", |
300 | r" | 300 | r" |
301 | <|>use PubMod::PubStruct; | 301 | use PubMod::PubStruct; |
302 | 302 | ||
303 | PubStruct | 303 | PubStruct |
304 | 304 | ||
@@ -329,7 +329,7 @@ mod tests { | |||
329 | macro_rules! foo { | 329 | macro_rules! foo { |
330 | ($i:ident) => { fn foo(a: $i) {} } | 330 | ($i:ident) => { fn foo(a: $i) {} } |
331 | } | 331 | } |
332 | foo!(Pub<|>Struct); | 332 | foo!(PubStruct); |
333 | 333 | ||
334 | pub mod PubMod { | 334 | pub mod PubMod { |
335 | pub struct PubStruct; | 335 | pub struct PubStruct; |
@@ -360,7 +360,7 @@ mod tests { | |||
360 | use PubMod::{PubStruct2, PubStruct1}; | 360 | use PubMod::{PubStruct2, PubStruct1}; |
361 | 361 | ||
362 | struct Test { | 362 | struct Test { |
363 | test: Pub<|>Struct2<u8>, | 363 | test: PubStruct2<u8>, |
364 | } | 364 | } |
365 | 365 | ||
366 | pub mod PubMod { | 366 | pub mod PubMod { |
@@ -393,7 +393,7 @@ mod tests { | |||
393 | r" | 393 | r" |
394 | use PubMod3::PubStruct; | 394 | use PubMod3::PubStruct; |
395 | 395 | ||
396 | PubSt<|>ruct | 396 | PubStruct |
397 | 397 | ||
398 | pub mod PubMod1 { | 398 | pub mod PubMod1 { |
399 | pub struct PubStruct; | 399 | pub struct PubStruct; |
@@ -474,7 +474,7 @@ mod tests { | |||
474 | r" | 474 | r" |
475 | use PubMod::test_function; | 475 | use PubMod::test_function; |
476 | 476 | ||
477 | test_function<|> | 477 | test_function |
478 | 478 | ||
479 | pub mod PubMod { | 479 | pub mod PubMod { |
480 | pub fn test_function() {}; | 480 | pub fn test_function() {}; |
@@ -501,7 +501,7 @@ mod tests { | |||
501 | r"use crate_with_macro::foo; | 501 | r"use crate_with_macro::foo; |
502 | 502 | ||
503 | fn main() { | 503 | fn main() { |
504 | foo<|> | 504 | foo |
505 | } | 505 | } |
506 | ", | 506 | ", |
507 | ); | 507 | ); |
@@ -587,7 +587,7 @@ fn main() { | |||
587 | } | 587 | } |
588 | 588 | ||
589 | fn main() { | 589 | fn main() { |
590 | TestStruct::test_function<|> | 590 | TestStruct::test_function |
591 | } | 591 | } |
592 | ", | 592 | ", |
593 | ); | 593 | ); |
@@ -620,7 +620,7 @@ fn main() { | |||
620 | } | 620 | } |
621 | 621 | ||
622 | fn main() { | 622 | fn main() { |
623 | TestStruct::TEST_CONST<|> | 623 | TestStruct::TEST_CONST |
624 | } | 624 | } |
625 | ", | 625 | ", |
626 | ); | 626 | ); |
@@ -659,7 +659,7 @@ fn main() { | |||
659 | } | 659 | } |
660 | 660 | ||
661 | fn main() { | 661 | fn main() { |
662 | test_mod::TestStruct::test_function<|> | 662 | test_mod::TestStruct::test_function |
663 | } | 663 | } |
664 | ", | 664 | ", |
665 | ); | 665 | ); |
@@ -730,7 +730,7 @@ fn main() { | |||
730 | } | 730 | } |
731 | 731 | ||
732 | fn main() { | 732 | fn main() { |
733 | test_mod::TestStruct::TEST_CONST<|> | 733 | test_mod::TestStruct::TEST_CONST |
734 | } | 734 | } |
735 | ", | 735 | ", |
736 | ); | 736 | ); |
@@ -803,7 +803,7 @@ fn main() { | |||
803 | 803 | ||
804 | fn main() { | 804 | fn main() { |
805 | let test_struct = test_mod::TestStruct {}; | 805 | let test_struct = test_mod::TestStruct {}; |
806 | test_struct.test_meth<|>od() | 806 | test_struct.test_method() |
807 | } | 807 | } |
808 | ", | 808 | ", |
809 | ); | 809 | ); |
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index fbe459c9c..c21d75be0 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs | |||
@@ -118,17 +118,13 @@ mod tests { | |||
118 | 118 | ||
119 | #[test] | 119 | #[test] |
120 | fn change_visibility_adds_pub_crate_to_items() { | 120 | fn change_visibility_adds_pub_crate_to_items() { |
121 | check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}"); | 121 | check_assist(change_visibility, "<|>fn foo() {}", "pub(crate) fn foo() {}"); |
122 | check_assist(change_visibility, "f<|>n foo() {}", "pub(crate) f<|>n foo() {}"); | 122 | check_assist(change_visibility, "f<|>n foo() {}", "pub(crate) fn foo() {}"); |
123 | check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}"); | 123 | check_assist(change_visibility, "<|>struct Foo {}", "pub(crate) struct Foo {}"); |
124 | check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}"); | 124 | check_assist(change_visibility, "<|>mod foo {}", "pub(crate) mod foo {}"); |
125 | check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}"); | 125 | check_assist(change_visibility, "<|>trait Foo {}", "pub(crate) trait Foo {}"); |
126 | check_assist(change_visibility, "m<|>od {}", "pub(crate) m<|>od {}"); | 126 | check_assist(change_visibility, "m<|>od {}", "pub(crate) mod {}"); |
127 | check_assist( | 127 | check_assist(change_visibility, "unsafe f<|>n foo() {}", "pub(crate) unsafe fn foo() {}"); |
128 | change_visibility, | ||
129 | "unsafe f<|>n foo() {}", | ||
130 | "pub(crate) unsafe f<|>n foo() {}", | ||
131 | ); | ||
132 | } | 128 | } |
133 | 129 | ||
134 | #[test] | 130 | #[test] |
@@ -136,9 +132,9 @@ mod tests { | |||
136 | check_assist( | 132 | check_assist( |
137 | change_visibility, | 133 | change_visibility, |
138 | r"struct S { <|>field: u32 }", | 134 | r"struct S { <|>field: u32 }", |
139 | r"struct S { <|>pub(crate) field: u32 }", | 135 | r"struct S { pub(crate) field: u32 }", |
140 | ); | 136 | ); |
141 | check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )"); | 137 | check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( pub(crate) u32 )"); |
142 | } | 138 | } |
143 | 139 | ||
144 | #[test] | 140 | #[test] |
@@ -152,17 +148,17 @@ mod tests { | |||
152 | 148 | ||
153 | #[test] | 149 | #[test] |
154 | fn change_visibility_pub_to_pub_crate() { | 150 | fn change_visibility_pub_to_pub_crate() { |
155 | check_assist(change_visibility, "<|>pub fn foo() {}", "<|>pub(crate) fn foo() {}") | 151 | check_assist(change_visibility, "<|>pub fn foo() {}", "pub(crate) fn foo() {}") |
156 | } | 152 | } |
157 | 153 | ||
158 | #[test] | 154 | #[test] |
159 | fn change_visibility_pub_crate_to_pub() { | 155 | fn change_visibility_pub_crate_to_pub() { |
160 | check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "<|>pub fn foo() {}") | 156 | check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "pub fn foo() {}") |
161 | } | 157 | } |
162 | 158 | ||
163 | #[test] | 159 | #[test] |
164 | fn change_visibility_const() { | 160 | fn change_visibility_const() { |
165 | check_assist(change_visibility, "<|>const FOO = 3u8;", "<|>pub(crate) const FOO = 3u8;"); | 161 | check_assist(change_visibility, "<|>const FOO = 3u8;", "pub(crate) const FOO = 3u8;"); |
166 | } | 162 | } |
167 | 163 | ||
168 | #[test] | 164 | #[test] |
@@ -183,7 +179,7 @@ mod tests { | |||
183 | // comments | 179 | // comments |
184 | 180 | ||
185 | #[derive(Debug)] | 181 | #[derive(Debug)] |
186 | <|>pub(crate) struct Foo; | 182 | pub(crate) struct Foo; |
187 | ", | 183 | ", |
188 | ) | 184 | ) |
189 | } | 185 | } |
diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index 66b296081..4cc75a7ce 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs | |||
@@ -97,7 +97,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) | |||
97 | } | 97 | } |
98 | 98 | ||
99 | then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; | 99 | then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; |
100 | let cursor_position = ctx.offset(); | ||
101 | 100 | ||
102 | let target = if_expr.syntax().text_range(); | 101 | let target = if_expr.syntax().text_range(); |
103 | acc.add(AssistId("convert_to_guarded_return"), "Convert to guarded return", target, |edit| { | 102 | acc.add(AssistId("convert_to_guarded_return"), "Convert to guarded return", target, |edit| { |
@@ -148,7 +147,6 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) | |||
148 | } | 147 | } |
149 | }; | 148 | }; |
150 | edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap()); | 149 | edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap()); |
151 | edit.set_cursor(cursor_position); | ||
152 | 150 | ||
153 | fn replace( | 151 | fn replace( |
154 | new_expr: &SyntaxNode, | 152 | new_expr: &SyntaxNode, |
@@ -207,7 +205,7 @@ mod tests { | |||
207 | r#" | 205 | r#" |
208 | fn main() { | 206 | fn main() { |
209 | bar(); | 207 | bar(); |
210 | if<|> !true { | 208 | if !true { |
211 | return; | 209 | return; |
212 | } | 210 | } |
213 | foo(); | 211 | foo(); |
@@ -237,7 +235,7 @@ mod tests { | |||
237 | r#" | 235 | r#" |
238 | fn main(n: Option<String>) { | 236 | fn main(n: Option<String>) { |
239 | bar(); | 237 | bar(); |
240 | le<|>t n = match n { | 238 | let n = match n { |
241 | Some(it) => it, | 239 | Some(it) => it, |
242 | _ => return, | 240 | _ => return, |
243 | }; | 241 | }; |
@@ -263,7 +261,7 @@ mod tests { | |||
263 | "#, | 261 | "#, |
264 | r#" | 262 | r#" |
265 | fn main() { | 263 | fn main() { |
266 | le<|>t x = match Err(92) { | 264 | let x = match Err(92) { |
267 | Ok(it) => it, | 265 | Ok(it) => it, |
268 | _ => return, | 266 | _ => return, |
269 | }; | 267 | }; |
@@ -291,7 +289,7 @@ mod tests { | |||
291 | r#" | 289 | r#" |
292 | fn main(n: Option<String>) { | 290 | fn main(n: Option<String>) { |
293 | bar(); | 291 | bar(); |
294 | le<|>t n = match n { | 292 | let n = match n { |
295 | Ok(it) => it, | 293 | Ok(it) => it, |
296 | _ => return, | 294 | _ => return, |
297 | }; | 295 | }; |
@@ -321,7 +319,7 @@ mod tests { | |||
321 | r#" | 319 | r#" |
322 | fn main() { | 320 | fn main() { |
323 | while true { | 321 | while true { |
324 | if<|> !true { | 322 | if !true { |
325 | continue; | 323 | continue; |
326 | } | 324 | } |
327 | foo(); | 325 | foo(); |
@@ -349,7 +347,7 @@ mod tests { | |||
349 | r#" | 347 | r#" |
350 | fn main() { | 348 | fn main() { |
351 | while true { | 349 | while true { |
352 | le<|>t n = match n { | 350 | let n = match n { |
353 | Some(it) => it, | 351 | Some(it) => it, |
354 | _ => continue, | 352 | _ => continue, |
355 | }; | 353 | }; |
@@ -378,7 +376,7 @@ mod tests { | |||
378 | r#" | 376 | r#" |
379 | fn main() { | 377 | fn main() { |
380 | loop { | 378 | loop { |
381 | if<|> !true { | 379 | if !true { |
382 | continue; | 380 | continue; |
383 | } | 381 | } |
384 | foo(); | 382 | foo(); |
@@ -406,7 +404,7 @@ mod tests { | |||
406 | r#" | 404 | r#" |
407 | fn main() { | 405 | fn main() { |
408 | loop { | 406 | loop { |
409 | le<|>t n = match n { | 407 | let n = match n { |
410 | Some(it) => it, | 408 | Some(it) => it, |
411 | _ => continue, | 409 | _ => continue, |
412 | }; | 410 | }; |
diff --git a/crates/ra_assists/src/handlers/flip_binexpr.rs b/crates/ra_assists/src/handlers/flip_binexpr.rs index 692ba4895..573196576 100644 --- a/crates/ra_assists/src/handlers/flip_binexpr.rs +++ b/crates/ra_assists/src/handlers/flip_binexpr.rs | |||
@@ -85,17 +85,13 @@ mod tests { | |||
85 | check_assist( | 85 | check_assist( |
86 | flip_binexpr, | 86 | flip_binexpr, |
87 | "fn f() { let res = 1 ==<|> 2; }", | 87 | "fn f() { let res = 1 ==<|> 2; }", |
88 | "fn f() { let res = 2 ==<|> 1; }", | 88 | "fn f() { let res = 2 == 1; }", |
89 | ) | 89 | ) |
90 | } | 90 | } |
91 | 91 | ||
92 | #[test] | 92 | #[test] |
93 | fn flip_binexpr_works_for_gt() { | 93 | fn flip_binexpr_works_for_gt() { |
94 | check_assist( | 94 | check_assist(flip_binexpr, "fn f() { let res = 1 ><|> 2; }", "fn f() { let res = 2 < 1; }") |
95 | flip_binexpr, | ||
96 | "fn f() { let res = 1 ><|> 2; }", | ||
97 | "fn f() { let res = 2 <<|> 1; }", | ||
98 | ) | ||
99 | } | 95 | } |
100 | 96 | ||
101 | #[test] | 97 | #[test] |
@@ -103,7 +99,7 @@ mod tests { | |||
103 | check_assist( | 99 | check_assist( |
104 | flip_binexpr, | 100 | flip_binexpr, |
105 | "fn f() { let res = 1 <=<|> 2; }", | 101 | "fn f() { let res = 1 <=<|> 2; }", |
106 | "fn f() { let res = 2 >=<|> 1; }", | 102 | "fn f() { let res = 2 >= 1; }", |
107 | ) | 103 | ) |
108 | } | 104 | } |
109 | 105 | ||
@@ -112,7 +108,7 @@ mod tests { | |||
112 | check_assist( | 108 | check_assist( |
113 | flip_binexpr, | 109 | flip_binexpr, |
114 | "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", | 110 | "fn f() { let res = (1 + 1) ==<|> (2 + 2); }", |
115 | "fn f() { let res = (2 + 2) ==<|> (1 + 1); }", | 111 | "fn f() { let res = (2 + 2) == (1 + 1); }", |
116 | ) | 112 | ) |
117 | } | 113 | } |
118 | 114 | ||
@@ -132,7 +128,7 @@ mod tests { | |||
132 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { | 128 | fn dyn_eq(&self, other: &dyn Diagnostic) -> bool { |
133 | match other.downcast_ref::<Self>() { | 129 | match other.downcast_ref::<Self>() { |
134 | None => false, | 130 | None => false, |
135 | Some(it) => self ==<|> it, | 131 | Some(it) => self == it, |
136 | } | 132 | } |
137 | } | 133 | } |
138 | "#, | 134 | "#, |
diff --git a/crates/ra_assists/src/handlers/flip_comma.rs b/crates/ra_assists/src/handlers/flip_comma.rs index dfe2a7fed..a57a1c463 100644 --- a/crates/ra_assists/src/handlers/flip_comma.rs +++ b/crates/ra_assists/src/handlers/flip_comma.rs | |||
@@ -45,7 +45,7 @@ mod tests { | |||
45 | check_assist( | 45 | check_assist( |
46 | flip_comma, | 46 | flip_comma, |
47 | "fn foo(x: i32,<|> y: Result<(), ()>) {}", | 47 | "fn foo(x: i32,<|> y: Result<(), ()>) {}", |
48 | "fn foo(y: Result<(), ()>,<|> x: i32) {}", | 48 | "fn foo(y: Result<(), ()>, x: i32) {}", |
49 | ) | 49 | ) |
50 | } | 50 | } |
51 | 51 | ||
diff --git a/crates/ra_assists/src/handlers/flip_trait_bound.rs b/crates/ra_assists/src/handlers/flip_trait_bound.rs index 8a08702ab..0115adc8b 100644 --- a/crates/ra_assists/src/handlers/flip_trait_bound.rs +++ b/crates/ra_assists/src/handlers/flip_trait_bound.rs | |||
@@ -60,7 +60,7 @@ mod tests { | |||
60 | check_assist( | 60 | check_assist( |
61 | flip_trait_bound, | 61 | flip_trait_bound, |
62 | "struct S<T> where T: A <|>+ B { }", | 62 | "struct S<T> where T: A <|>+ B { }", |
63 | "struct S<T> where T: B <|>+ A { }", | 63 | "struct S<T> where T: B + A { }", |
64 | ) | 64 | ) |
65 | } | 65 | } |
66 | 66 | ||
@@ -69,13 +69,13 @@ mod tests { | |||
69 | check_assist( | 69 | check_assist( |
70 | flip_trait_bound, | 70 | flip_trait_bound, |
71 | "impl X for S<T> where T: A +<|> B { }", | 71 | "impl X for S<T> where T: A +<|> B { }", |
72 | "impl X for S<T> where T: B +<|> A { }", | 72 | "impl X for S<T> where T: B + A { }", |
73 | ) | 73 | ) |
74 | } | 74 | } |
75 | 75 | ||
76 | #[test] | 76 | #[test] |
77 | fn flip_trait_bound_works_for_fn() { | 77 | fn flip_trait_bound_works_for_fn() { |
78 | check_assist(flip_trait_bound, "fn f<T: A <|>+ B>(t: T) { }", "fn f<T: B <|>+ A>(t: T) { }") | 78 | check_assist(flip_trait_bound, "fn f<T: A <|>+ B>(t: T) { }", "fn f<T: B + A>(t: T) { }") |
79 | } | 79 | } |
80 | 80 | ||
81 | #[test] | 81 | #[test] |
@@ -83,7 +83,7 @@ mod tests { | |||
83 | check_assist( | 83 | check_assist( |
84 | flip_trait_bound, | 84 | flip_trait_bound, |
85 | "fn f<T>(t: T) where T: A +<|> B { }", | 85 | "fn f<T>(t: T) where T: A +<|> B { }", |
86 | "fn f<T>(t: T) where T: B +<|> A { }", | 86 | "fn f<T>(t: T) where T: B + A { }", |
87 | ) | 87 | ) |
88 | } | 88 | } |
89 | 89 | ||
@@ -92,7 +92,7 @@ mod tests { | |||
92 | check_assist( | 92 | check_assist( |
93 | flip_trait_bound, | 93 | flip_trait_bound, |
94 | "fn f<T>(t: T) where T: A <|>+ 'static { }", | 94 | "fn f<T>(t: T) where T: A <|>+ 'static { }", |
95 | "fn f<T>(t: T) where T: 'static <|>+ A { }", | 95 | "fn f<T>(t: T) where T: 'static + A { }", |
96 | ) | 96 | ) |
97 | } | 97 | } |
98 | 98 | ||
@@ -101,7 +101,7 @@ mod tests { | |||
101 | check_assist( | 101 | check_assist( |
102 | flip_trait_bound, | 102 | flip_trait_bound, |
103 | "struct S<T> where T: A<T> <|>+ b_mod::B<T> + C<T> { }", | 103 | "struct S<T> where T: A<T> <|>+ b_mod::B<T> + C<T> { }", |
104 | "struct S<T> where T: b_mod::B<T> <|>+ A<T> + C<T> { }", | 104 | "struct S<T> where T: b_mod::B<T> + A<T> + C<T> { }", |
105 | ) | 105 | ) |
106 | } | 106 | } |
107 | 107 | ||
@@ -110,7 +110,7 @@ mod tests { | |||
110 | check_assist( | 110 | check_assist( |
111 | flip_trait_bound, | 111 | flip_trait_bound, |
112 | "struct S<T> where T: A + B + C + D + E + F +<|> G + H + I + J { }", | 112 | "struct S<T> where T: A + B + C + D + E + F +<|> G + H + I + J { }", |
113 | "struct S<T> where T: A + B + C + D + E + G +<|> F + H + I + J { }", | 113 | "struct S<T> where T: A + B + C + D + E + G + F + H + I + J { }", |
114 | ) | 114 | ) |
115 | } | 115 | } |
116 | } | 116 | } |
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index 46d675a4e..d26e68847 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs | |||
@@ -116,7 +116,6 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
116 | let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() }; | 116 | let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() }; |
117 | builder.replace(desc.file_range.range, replacement) | 117 | builder.replace(desc.file_range.range, replacement) |
118 | } | 118 | } |
119 | builder.set_cursor(delete_range.start()) | ||
120 | }) | 119 | }) |
121 | } | 120 | } |
122 | 121 | ||
@@ -149,7 +148,7 @@ fn foo() { | |||
149 | r" | 148 | r" |
150 | fn bar(a: usize) {} | 149 | fn bar(a: usize) {} |
151 | fn foo() { | 150 | fn foo() { |
152 | <|>1 + 1; | 151 | 1 + 1; |
153 | if 1 > 10 { | 152 | if 1 > 10 { |
154 | } | 153 | } |
155 | 154 | ||
@@ -183,7 +182,7 @@ fn foo() { | |||
183 | r" | 182 | r" |
184 | fn bar(a: usize) {} | 183 | fn bar(a: usize) {} |
185 | fn foo() { | 184 | fn foo() { |
186 | <|>(1 + 1) + 1; | 185 | (1 + 1) + 1; |
187 | if (1 + 1) > 10 { | 186 | if (1 + 1) > 10 { |
188 | } | 187 | } |
189 | 188 | ||
@@ -217,7 +216,7 @@ fn foo() { | |||
217 | r" | 216 | r" |
218 | fn bar(a: usize) {} | 217 | fn bar(a: usize) {} |
219 | fn foo() { | 218 | fn foo() { |
220 | <|>bar(1) + 1; | 219 | bar(1) + 1; |
221 | if bar(1) > 10 { | 220 | if bar(1) > 10 { |
222 | } | 221 | } |
223 | 222 | ||
@@ -251,7 +250,7 @@ fn foo() { | |||
251 | r" | 250 | r" |
252 | fn bar(a: usize): usize { a } | 251 | fn bar(a: usize): usize { a } |
253 | fn foo() { | 252 | fn foo() { |
254 | <|>(bar(1) as u64) + 1; | 253 | (bar(1) as u64) + 1; |
255 | if (bar(1) as u64) > 10 { | 254 | if (bar(1) as u64) > 10 { |
256 | } | 255 | } |
257 | 256 | ||
@@ -283,7 +282,7 @@ fn foo() { | |||
283 | }", | 282 | }", |
284 | r" | 283 | r" |
285 | fn foo() { | 284 | fn foo() { |
286 | <|>{ 10 + 1 } + 1; | 285 | { 10 + 1 } + 1; |
287 | if { 10 + 1 } > 10 { | 286 | if { 10 + 1 } > 10 { |
288 | } | 287 | } |
289 | 288 | ||
@@ -315,7 +314,7 @@ fn foo() { | |||
315 | }", | 314 | }", |
316 | r" | 315 | r" |
317 | fn foo() { | 316 | fn foo() { |
318 | <|>( 10 + 1 ) + 1; | 317 | ( 10 + 1 ) + 1; |
319 | if ( 10 + 1 ) > 10 { | 318 | if ( 10 + 1 ) > 10 { |
320 | } | 319 | } |
321 | 320 | ||
@@ -353,7 +352,7 @@ fn foo() { | |||
353 | }", | 352 | }", |
354 | r" | 353 | r" |
355 | fn foo() { | 354 | fn foo() { |
356 | <|>let b = bar(10 + 1) * 10; | 355 | let b = bar(10 + 1) * 10; |
357 | let c = bar(10 + 1) as usize; | 356 | let c = bar(10 + 1) as usize; |
358 | }", | 357 | }", |
359 | ); | 358 | ); |
@@ -373,7 +372,7 @@ fn foo() { | |||
373 | r" | 372 | r" |
374 | fn foo() { | 373 | fn foo() { |
375 | let x = vec![1, 2, 3]; | 374 | let x = vec![1, 2, 3]; |
376 | <|>let b = x[0] * 10; | 375 | let b = x[0] * 10; |
377 | let c = x[0] as usize; | 376 | let c = x[0] as usize; |
378 | }", | 377 | }", |
379 | ); | 378 | ); |
@@ -393,7 +392,7 @@ fn foo() { | |||
393 | r" | 392 | r" |
394 | fn foo() { | 393 | fn foo() { |
395 | let bar = vec![1]; | 394 | let bar = vec![1]; |
396 | <|>let b = bar.len() * 10; | 395 | let b = bar.len() * 10; |
397 | let c = bar.len() as usize; | 396 | let c = bar.len() as usize; |
398 | }", | 397 | }", |
399 | ); | 398 | ); |
@@ -421,7 +420,7 @@ struct Bar { | |||
421 | 420 | ||
422 | fn foo() { | 421 | fn foo() { |
423 | let bar = Bar { foo: 1 }; | 422 | let bar = Bar { foo: 1 }; |
424 | <|>let b = bar.foo * 10; | 423 | let b = bar.foo * 10; |
425 | let c = bar.foo as usize; | 424 | let c = bar.foo as usize; |
426 | }", | 425 | }", |
427 | ); | 426 | ); |
@@ -442,7 +441,7 @@ fn foo() -> Option<usize> { | |||
442 | r" | 441 | r" |
443 | fn foo() -> Option<usize> { | 442 | fn foo() -> Option<usize> { |
444 | let bar = Some(1); | 443 | let bar = Some(1); |
445 | <|>let b = bar? * 10; | 444 | let b = bar? * 10; |
446 | let c = bar? as usize; | 445 | let c = bar? as usize; |
447 | None | 446 | None |
448 | }", | 447 | }", |
@@ -462,7 +461,7 @@ fn foo() { | |||
462 | r" | 461 | r" |
463 | fn foo() { | 462 | fn foo() { |
464 | let bar = 10; | 463 | let bar = 10; |
465 | <|>let b = &bar * 10; | 464 | let b = &bar * 10; |
466 | }", | 465 | }", |
467 | ); | 466 | ); |
468 | } | 467 | } |
@@ -478,7 +477,7 @@ fn foo() { | |||
478 | }", | 477 | }", |
479 | r" | 478 | r" |
480 | fn foo() { | 479 | fn foo() { |
481 | <|>let b = (10, 20)[0]; | 480 | let b = (10, 20)[0]; |
482 | }", | 481 | }", |
483 | ); | 482 | ); |
484 | } | 483 | } |
@@ -494,7 +493,7 @@ fn foo() { | |||
494 | }", | 493 | }", |
495 | r" | 494 | r" |
496 | fn foo() { | 495 | fn foo() { |
497 | <|>let b = [1, 2, 3].len(); | 496 | let b = [1, 2, 3].len(); |
498 | }", | 497 | }", |
499 | ); | 498 | ); |
500 | } | 499 | } |
@@ -511,7 +510,7 @@ fn foo() { | |||
511 | }", | 510 | }", |
512 | r" | 511 | r" |
513 | fn foo() { | 512 | fn foo() { |
514 | <|>let b = (10 + 20) * 10; | 513 | let b = (10 + 20) * 10; |
515 | let c = (10 + 20) as usize; | 514 | let c = (10 + 20) as usize; |
516 | }", | 515 | }", |
517 | ); | 516 | ); |
@@ -531,7 +530,7 @@ fn foo() { | |||
531 | r" | 530 | r" |
532 | fn foo() { | 531 | fn foo() { |
533 | let d = 10; | 532 | let d = 10; |
534 | <|>let b = d * 10; | 533 | let b = d * 10; |
535 | let c = d as usize; | 534 | let c = d as usize; |
536 | }", | 535 | }", |
537 | ); | 536 | ); |
@@ -549,7 +548,7 @@ fn foo() { | |||
549 | }", | 548 | }", |
550 | r" | 549 | r" |
551 | fn foo() { | 550 | fn foo() { |
552 | <|>let b = { 10 } * 10; | 551 | let b = { 10 } * 10; |
553 | let c = { 10 } as usize; | 552 | let c = { 10 } as usize; |
554 | }", | 553 | }", |
555 | ); | 554 | ); |
@@ -569,7 +568,7 @@ fn foo() { | |||
569 | }", | 568 | }", |
570 | r" | 569 | r" |
571 | fn foo() { | 570 | fn foo() { |
572 | <|>let b = (10 + 20) * 10; | 571 | let b = (10 + 20) * 10; |
573 | let c = (10 + 20, 20); | 572 | let c = (10 + 20, 20); |
574 | let d = [10 + 20, 10]; | 573 | let d = [10 + 20, 10]; |
575 | let e = (10 + 20); | 574 | let e = (10 + 20); |
@@ -588,7 +587,7 @@ fn foo() { | |||
588 | }", | 587 | }", |
589 | r" | 588 | r" |
590 | fn foo() { | 589 | fn foo() { |
591 | <|>for i in vec![10, 20] {} | 590 | for i in vec![10, 20] {} |
592 | }", | 591 | }", |
593 | ); | 592 | ); |
594 | } | 593 | } |
@@ -604,7 +603,7 @@ fn foo() { | |||
604 | }", | 603 | }", |
605 | r" | 604 | r" |
606 | fn foo() { | 605 | fn foo() { |
607 | <|>while 1 > 0 {} | 606 | while 1 > 0 {} |
608 | }", | 607 | }", |
609 | ); | 608 | ); |
610 | } | 609 | } |
@@ -622,7 +621,7 @@ fn foo() { | |||
622 | }", | 621 | }", |
623 | r" | 622 | r" |
624 | fn foo() { | 623 | fn foo() { |
625 | <|>loop { | 624 | loop { |
626 | break 1 + 1; | 625 | break 1 + 1; |
627 | } | 626 | } |
628 | }", | 627 | }", |
@@ -640,7 +639,7 @@ fn foo() { | |||
640 | }", | 639 | }", |
641 | r" | 640 | r" |
642 | fn foo() { | 641 | fn foo() { |
643 | <|>return 1 > 0; | 642 | return 1 > 0; |
644 | }", | 643 | }", |
645 | ); | 644 | ); |
646 | } | 645 | } |
@@ -656,7 +655,7 @@ fn foo() { | |||
656 | }", | 655 | }", |
657 | r" | 656 | r" |
658 | fn foo() { | 657 | fn foo() { |
659 | <|>match 1 > 0 {} | 658 | match 1 > 0 {} |
660 | }", | 659 | }", |
661 | ); | 660 | ); |
662 | } | 661 | } |
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 56c610fed..31d6539f7 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs | |||
@@ -4,7 +4,7 @@ use ra_syntax::{ | |||
4 | BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, | 4 | BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, |
5 | WHITESPACE, | 5 | WHITESPACE, |
6 | }, | 6 | }, |
7 | SyntaxNode, TextSize, | 7 | SyntaxNode, |
8 | }; | 8 | }; |
9 | use stdx::format_to; | 9 | use stdx::format_to; |
10 | use test_utils::mark; | 10 | use test_utils::mark; |
@@ -23,7 +23,7 @@ use crate::{AssistContext, AssistId, Assists}; | |||
23 | // -> | 23 | // -> |
24 | // ``` | 24 | // ``` |
25 | // fn main() { | 25 | // fn main() { |
26 | // let var_name = (1 + 2); | 26 | // let $0var_name = (1 + 2); |
27 | // var_name * 4; | 27 | // var_name * 4; |
28 | // } | 28 | // } |
29 | // ``` | 29 | // ``` |
@@ -46,14 +46,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
46 | acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { | 46 | acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { |
47 | let mut buf = String::new(); | 47 | let mut buf = String::new(); |
48 | 48 | ||
49 | let cursor_offset = if wrap_in_block { | 49 | if wrap_in_block { |
50 | buf.push_str("{ let var_name = "); | 50 | buf.push_str("{ let var_name = "); |
51 | TextSize::of("{ let ") | ||
52 | } else { | 51 | } else { |
53 | buf.push_str("let var_name = "); | 52 | buf.push_str("let var_name = "); |
54 | TextSize::of("let ") | ||
55 | }; | 53 | }; |
56 | format_to!(buf, "{}", expr.syntax()); | 54 | format_to!(buf, "{}", expr.syntax()); |
55 | |||
57 | let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); | 56 | let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); |
58 | let is_full_stmt = if let Some(expr_stmt) = &full_stmt { | 57 | let is_full_stmt = if let Some(expr_stmt) = &full_stmt { |
59 | Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) | 58 | Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) |
@@ -65,28 +64,43 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
65 | if full_stmt.unwrap().semicolon_token().is_none() { | 64 | if full_stmt.unwrap().semicolon_token().is_none() { |
66 | buf.push_str(";"); | 65 | buf.push_str(";"); |
67 | } | 66 | } |
68 | edit.replace(expr.syntax().text_range(), buf); | 67 | let offset = expr.syntax().text_range(); |
69 | } else { | 68 | match ctx.config.snippet_cap { |
70 | buf.push_str(";"); | 69 | Some(cap) => { |
71 | 70 | let snip = buf.replace("let var_name", "let $0var_name"); | |
72 | // We want to maintain the indent level, | 71 | edit.replace_snippet(cap, offset, snip) |
73 | // but we do not want to duplicate possible | 72 | } |
74 | // extra newlines in the indent block | 73 | None => edit.replace(offset, buf), |
75 | let text = indent.text(); | ||
76 | if text.starts_with('\n') { | ||
77 | buf.push_str("\n"); | ||
78 | buf.push_str(text.trim_start_matches('\n')); | ||
79 | } else { | ||
80 | buf.push_str(text); | ||
81 | } | 74 | } |
75 | return; | ||
76 | } | ||
82 | 77 | ||
83 | edit.replace(expr.syntax().text_range(), "var_name".to_string()); | 78 | buf.push_str(";"); |
84 | edit.insert(anchor_stmt.text_range().start(), buf); | 79 | |
85 | if wrap_in_block { | 80 | // We want to maintain the indent level, |
86 | edit.insert(anchor_stmt.text_range().end(), " }"); | 81 | // but we do not want to duplicate possible |
82 | // extra newlines in the indent block | ||
83 | let text = indent.text(); | ||
84 | if text.starts_with('\n') { | ||
85 | buf.push_str("\n"); | ||
86 | buf.push_str(text.trim_start_matches('\n')); | ||
87 | } else { | ||
88 | buf.push_str(text); | ||
89 | } | ||
90 | |||
91 | edit.replace(expr.syntax().text_range(), "var_name".to_string()); | ||
92 | let offset = anchor_stmt.text_range().start(); | ||
93 | match ctx.config.snippet_cap { | ||
94 | Some(cap) => { | ||
95 | let snip = buf.replace("let var_name", "let $0var_name"); | ||
96 | edit.insert_snippet(cap, offset, snip) | ||
87 | } | 97 | } |
98 | None => edit.insert(offset, buf), | ||
99 | } | ||
100 | |||
101 | if wrap_in_block { | ||
102 | edit.insert(anchor_stmt.text_range().end(), " }"); | ||
88 | } | 103 | } |
89 | edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); | ||
90 | }) | 104 | }) |
91 | } | 105 | } |
92 | 106 | ||
@@ -144,15 +158,15 @@ mod tests { | |||
144 | fn test_introduce_var_simple() { | 158 | fn test_introduce_var_simple() { |
145 | check_assist( | 159 | check_assist( |
146 | introduce_variable, | 160 | introduce_variable, |
147 | " | 161 | r#" |
148 | fn foo() { | 162 | fn foo() { |
149 | foo(<|>1 + 1<|>); | 163 | foo(<|>1 + 1<|>); |
150 | }", | 164 | }"#, |
151 | " | 165 | r#" |
152 | fn foo() { | 166 | fn foo() { |
153 | let <|>var_name = 1 + 1; | 167 | let $0var_name = 1 + 1; |
154 | foo(var_name); | 168 | foo(var_name); |
155 | }", | 169 | }"#, |
156 | ); | 170 | ); |
157 | } | 171 | } |
158 | 172 | ||
@@ -167,14 +181,14 @@ fn foo() { | |||
167 | mark::check!(test_introduce_var_expr_stmt); | 181 | mark::check!(test_introduce_var_expr_stmt); |
168 | check_assist( | 182 | check_assist( |
169 | introduce_variable, | 183 | introduce_variable, |
170 | " | 184 | r#" |
171 | fn foo() { | 185 | fn foo() { |
172 | <|>1 + 1<|>; | 186 | <|>1 + 1<|>; |
173 | }", | 187 | }"#, |
174 | " | 188 | r#" |
175 | fn foo() { | 189 | fn foo() { |
176 | let <|>var_name = 1 + 1; | 190 | let $0var_name = 1 + 1; |
177 | }", | 191 | }"#, |
178 | ); | 192 | ); |
179 | check_assist( | 193 | check_assist( |
180 | introduce_variable, | 194 | introduce_variable, |
@@ -185,7 +199,7 @@ fn foo() { | |||
185 | }", | 199 | }", |
186 | " | 200 | " |
187 | fn foo() { | 201 | fn foo() { |
188 | let <|>var_name = { let x = 0; x }; | 202 | let $0var_name = { let x = 0; x }; |
189 | something_else(); | 203 | something_else(); |
190 | }", | 204 | }", |
191 | ); | 205 | ); |
@@ -201,7 +215,7 @@ fn foo() { | |||
201 | }", | 215 | }", |
202 | " | 216 | " |
203 | fn foo() { | 217 | fn foo() { |
204 | let <|>var_name = 1; | 218 | let $0var_name = 1; |
205 | var_name + 1; | 219 | var_name + 1; |
206 | }", | 220 | }", |
207 | ); | 221 | ); |
@@ -218,7 +232,7 @@ fn foo() { | |||
218 | }", | 232 | }", |
219 | " | 233 | " |
220 | fn foo() { | 234 | fn foo() { |
221 | let <|>var_name = 1 + 1; | 235 | let $0var_name = 1 + 1; |
222 | bar(var_name) | 236 | bar(var_name) |
223 | }", | 237 | }", |
224 | ); | 238 | ); |
@@ -230,7 +244,7 @@ fn foo() { | |||
230 | }", | 244 | }", |
231 | " | 245 | " |
232 | fn foo() { | 246 | fn foo() { |
233 | let <|>var_name = bar(1 + 1); | 247 | let $0var_name = bar(1 + 1); |
234 | var_name | 248 | var_name |
235 | }", | 249 | }", |
236 | ) | 250 | ) |
@@ -253,7 +267,7 @@ fn main() { | |||
253 | fn main() { | 267 | fn main() { |
254 | let x = true; | 268 | let x = true; |
255 | let tuple = match x { | 269 | let tuple = match x { |
256 | true => { let <|>var_name = 2 + 2; (var_name, true) } | 270 | true => { let $0var_name = 2 + 2; (var_name, true) } |
257 | _ => (0, false) | 271 | _ => (0, false) |
258 | }; | 272 | }; |
259 | } | 273 | } |
@@ -283,7 +297,7 @@ fn main() { | |||
283 | let tuple = match x { | 297 | let tuple = match x { |
284 | true => { | 298 | true => { |
285 | let y = 1; | 299 | let y = 1; |
286 | let <|>var_name = 2 + y; | 300 | let $0var_name = 2 + y; |
287 | (var_name, true) | 301 | (var_name, true) |
288 | } | 302 | } |
289 | _ => (0, false) | 303 | _ => (0, false) |
@@ -304,7 +318,7 @@ fn main() { | |||
304 | ", | 318 | ", |
305 | " | 319 | " |
306 | fn main() { | 320 | fn main() { |
307 | let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; | 321 | let lambda = |x: u32| { let $0var_name = x * 2; var_name }; |
308 | } | 322 | } |
309 | ", | 323 | ", |
310 | ); | 324 | ); |
@@ -321,7 +335,7 @@ fn main() { | |||
321 | ", | 335 | ", |
322 | " | 336 | " |
323 | fn main() { | 337 | fn main() { |
324 | let lambda = |x: u32| { let <|>var_name = x * 2; var_name }; | 338 | let lambda = |x: u32| { let $0var_name = x * 2; var_name }; |
325 | } | 339 | } |
326 | ", | 340 | ", |
327 | ); | 341 | ); |
@@ -338,7 +352,7 @@ fn main() { | |||
338 | ", | 352 | ", |
339 | " | 353 | " |
340 | fn main() { | 354 | fn main() { |
341 | let <|>var_name = Some(true); | 355 | let $0var_name = Some(true); |
342 | let o = var_name; | 356 | let o = var_name; |
343 | } | 357 | } |
344 | ", | 358 | ", |
@@ -356,7 +370,7 @@ fn main() { | |||
356 | ", | 370 | ", |
357 | " | 371 | " |
358 | fn main() { | 372 | fn main() { |
359 | let <|>var_name = bar.foo(); | 373 | let $0var_name = bar.foo(); |
360 | let v = var_name; | 374 | let v = var_name; |
361 | } | 375 | } |
362 | ", | 376 | ", |
@@ -374,7 +388,7 @@ fn foo() -> u32 { | |||
374 | ", | 388 | ", |
375 | " | 389 | " |
376 | fn foo() -> u32 { | 390 | fn foo() -> u32 { |
377 | let <|>var_name = 2 + 2; | 391 | let $0var_name = 2 + 2; |
378 | return var_name; | 392 | return var_name; |
379 | } | 393 | } |
380 | ", | 394 | ", |
@@ -396,7 +410,7 @@ fn foo() -> u32 { | |||
396 | fn foo() -> u32 { | 410 | fn foo() -> u32 { |
397 | 411 | ||
398 | 412 | ||
399 | let <|>var_name = 2 + 2; | 413 | let $0var_name = 2 + 2; |
400 | return var_name; | 414 | return var_name; |
401 | } | 415 | } |
402 | ", | 416 | ", |
@@ -413,7 +427,7 @@ fn foo() -> u32 { | |||
413 | " | 427 | " |
414 | fn foo() -> u32 { | 428 | fn foo() -> u32 { |
415 | 429 | ||
416 | let <|>var_name = 2 + 2; | 430 | let $0var_name = 2 + 2; |
417 | return var_name; | 431 | return var_name; |
418 | } | 432 | } |
419 | ", | 433 | ", |
@@ -438,7 +452,7 @@ fn foo() -> u32 { | |||
438 | // bar | 452 | // bar |
439 | 453 | ||
440 | 454 | ||
441 | let <|>var_name = 2 + 2; | 455 | let $0var_name = 2 + 2; |
442 | return var_name; | 456 | return var_name; |
443 | } | 457 | } |
444 | ", | 458 | ", |
@@ -459,7 +473,7 @@ fn main() { | |||
459 | " | 473 | " |
460 | fn main() { | 474 | fn main() { |
461 | let result = loop { | 475 | let result = loop { |
462 | let <|>var_name = 2 + 2; | 476 | let $0var_name = 2 + 2; |
463 | break var_name; | 477 | break var_name; |
464 | }; | 478 | }; |
465 | } | 479 | } |
@@ -478,7 +492,7 @@ fn main() { | |||
478 | ", | 492 | ", |
479 | " | 493 | " |
480 | fn main() { | 494 | fn main() { |
481 | let <|>var_name = 0f32 as u32; | 495 | let $0var_name = 0f32 as u32; |
482 | let v = var_name; | 496 | let v = var_name; |
483 | } | 497 | } |
484 | ", | 498 | ", |
diff --git a/crates/ra_assists/src/handlers/invert_if.rs b/crates/ra_assists/src/handlers/invert_if.rs index 527c7caef..59d278eb9 100644 --- a/crates/ra_assists/src/handlers/invert_if.rs +++ b/crates/ra_assists/src/handlers/invert_if.rs | |||
@@ -72,7 +72,7 @@ mod tests { | |||
72 | check_assist( | 72 | check_assist( |
73 | invert_if, | 73 | invert_if, |
74 | "fn f() { i<|>f x != 3 { 1 } else { 3 + 2 } }", | 74 | "fn f() { i<|>f x != 3 { 1 } else { 3 + 2 } }", |
75 | "fn f() { i<|>f x == 3 { 3 + 2 } else { 1 } }", | 75 | "fn f() { if x == 3 { 3 + 2 } else { 1 } }", |
76 | ) | 76 | ) |
77 | } | 77 | } |
78 | 78 | ||
@@ -81,7 +81,7 @@ mod tests { | |||
81 | check_assist( | 81 | check_assist( |
82 | invert_if, | 82 | invert_if, |
83 | "fn f() { <|>if !cond { 3 * 2 } else { 1 } }", | 83 | "fn f() { <|>if !cond { 3 * 2 } else { 1 } }", |
84 | "fn f() { <|>if cond { 1 } else { 3 * 2 } }", | 84 | "fn f() { if cond { 1 } else { 3 * 2 } }", |
85 | ) | 85 | ) |
86 | } | 86 | } |
87 | 87 | ||
@@ -90,7 +90,7 @@ mod tests { | |||
90 | check_assist( | 90 | check_assist( |
91 | invert_if, | 91 | invert_if, |
92 | "fn f() { i<|>f cond { 3 * 2 } else { 1 } }", | 92 | "fn f() { i<|>f cond { 3 * 2 } else { 1 } }", |
93 | "fn f() { i<|>f !cond { 1 } else { 3 * 2 } }", | 93 | "fn f() { if !cond { 1 } else { 3 * 2 } }", |
94 | ) | 94 | ) |
95 | } | 95 | } |
96 | 96 | ||
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index ac3e53c27..972d16241 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -58,8 +58,6 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<() | |||
58 | let target = tree.syntax().text_range(); | 58 | let target = tree.syntax().text_range(); |
59 | acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| { | 59 | acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| { |
60 | builder.rewrite(rewriter); | 60 | builder.rewrite(rewriter); |
61 | // FIXME: we only need because our diff is imprecise | ||
62 | builder.set_cursor(offset); | ||
63 | }) | 61 | }) |
64 | } | 62 | } |
65 | 63 | ||
@@ -142,7 +140,7 @@ use std::fmt<|>::Debug; | |||
142 | use std::fmt::Display; | 140 | use std::fmt::Display; |
143 | ", | 141 | ", |
144 | r" | 142 | r" |
145 | use std::fmt<|>::{Debug, Display}; | 143 | use std::fmt::{Debug, Display}; |
146 | ", | 144 | ", |
147 | ) | 145 | ) |
148 | } | 146 | } |
@@ -156,7 +154,7 @@ use std::fmt::Debug; | |||
156 | use std::fmt<|>::Display; | 154 | use std::fmt<|>::Display; |
157 | ", | 155 | ", |
158 | r" | 156 | r" |
159 | use std::fmt:<|>:{Display, Debug}; | 157 | use std::fmt::{Display, Debug}; |
160 | ", | 158 | ", |
161 | ); | 159 | ); |
162 | } | 160 | } |
@@ -169,7 +167,7 @@ use std::fmt:<|>:{Display, Debug}; | |||
169 | use std::{fmt<|>::Debug, fmt::Display}; | 167 | use std::{fmt<|>::Debug, fmt::Display}; |
170 | ", | 168 | ", |
171 | r" | 169 | r" |
172 | use std::{fmt<|>::{Debug, Display}}; | 170 | use std::{fmt::{Debug, Display}}; |
173 | ", | 171 | ", |
174 | ); | 172 | ); |
175 | check_assist( | 173 | check_assist( |
@@ -178,7 +176,7 @@ use std::{fmt<|>::{Debug, Display}}; | |||
178 | use std::{fmt::Debug, fmt<|>::Display}; | 176 | use std::{fmt::Debug, fmt<|>::Display}; |
179 | ", | 177 | ", |
180 | r" | 178 | r" |
181 | use std::{fmt::<|>{Display, Debug}}; | 179 | use std::{fmt::{Display, Debug}}; |
182 | ", | 180 | ", |
183 | ); | 181 | ); |
184 | } | 182 | } |
@@ -192,7 +190,7 @@ use std<|>::cell::*; | |||
192 | use std::str; | 190 | use std::str; |
193 | ", | 191 | ", |
194 | r" | 192 | r" |
195 | use std<|>::{cell::*, str}; | 193 | use std::{cell::*, str}; |
196 | ", | 194 | ", |
197 | ) | 195 | ) |
198 | } | 196 | } |
@@ -206,7 +204,7 @@ use std<|>::cell::*; | |||
206 | use std::str::*; | 204 | use std::str::*; |
207 | ", | 205 | ", |
208 | r" | 206 | r" |
209 | use std<|>::{cell::*, str::*}; | 207 | use std::{cell::*, str::*}; |
210 | ", | 208 | ", |
211 | ) | 209 | ) |
212 | } | 210 | } |
@@ -222,7 +220,7 @@ use foo::baz; | |||
222 | /// Doc comment | 220 | /// Doc comment |
223 | ", | 221 | ", |
224 | r" | 222 | r" |
225 | use foo<|>::{bar, baz}; | 223 | use foo::{bar, baz}; |
226 | 224 | ||
227 | /// Doc comment | 225 | /// Doc comment |
228 | ", | 226 | ", |
@@ -241,7 +239,7 @@ use { | |||
241 | ", | 239 | ", |
242 | r" | 240 | r" |
243 | use { | 241 | use { |
244 | foo<|>::{bar, baz}, | 242 | foo::{bar, baz}, |
245 | }; | 243 | }; |
246 | ", | 244 | ", |
247 | ); | 245 | ); |
@@ -255,7 +253,7 @@ use { | |||
255 | ", | 253 | ", |
256 | r" | 254 | r" |
257 | use { | 255 | use { |
258 | foo::{bar<|>, baz}, | 256 | foo::{bar, baz}, |
259 | }; | 257 | }; |
260 | ", | 258 | ", |
261 | ); | 259 | ); |
@@ -272,7 +270,7 @@ use foo::<|>{ | |||
272 | }; | 270 | }; |
273 | ", | 271 | ", |
274 | r" | 272 | r" |
275 | use foo::{<|> | 273 | use foo::{ |
276 | FooBar, | 274 | FooBar, |
277 | bar::baz}; | 275 | bar::baz}; |
278 | ", | 276 | ", |
diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index d4e38aa6a..ca04ec671 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs | |||
@@ -3,7 +3,7 @@ use std::iter::successors; | |||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::neighbor, | 4 | algo::neighbor, |
5 | ast::{self, AstNode}, | 5 | ast::{self, AstNode}, |
6 | Direction, TextSize, | 6 | Direction, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | use crate::{AssistContext, AssistId, Assists, TextRange}; | 9 | use crate::{AssistContext, AssistId, Assists, TextRange}; |
@@ -41,17 +41,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
41 | let current_expr = current_arm.expr()?; | 41 | let current_expr = current_arm.expr()?; |
42 | let current_text_range = current_arm.syntax().text_range(); | 42 | let current_text_range = current_arm.syntax().text_range(); |
43 | 43 | ||
44 | enum CursorPos { | ||
45 | InExpr(TextSize), | ||
46 | InPat(TextSize), | ||
47 | } | ||
48 | let cursor_pos = ctx.offset(); | ||
49 | let cursor_pos = if current_expr.syntax().text_range().contains(cursor_pos) { | ||
50 | CursorPos::InExpr(current_text_range.end() - cursor_pos) | ||
51 | } else { | ||
52 | CursorPos::InPat(cursor_pos) | ||
53 | }; | ||
54 | |||
55 | // We check if the following match arms match this one. We could, but don't, | 44 | // We check if the following match arms match this one. We could, but don't, |
56 | // compare to the previous match arm as well. | 45 | // compare to the previous match arm as well. |
57 | let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) | 46 | let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) |
@@ -87,10 +76,6 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
87 | let start = arms_to_merge.first().unwrap().syntax().text_range().start(); | 76 | let start = arms_to_merge.first().unwrap().syntax().text_range().start(); |
88 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); | 77 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); |
89 | 78 | ||
90 | edit.set_cursor(match cursor_pos { | ||
91 | CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset, | ||
92 | CursorPos::InPat(offset) => offset, | ||
93 | }); | ||
94 | edit.replace(TextRange::new(start, end), arm); | 79 | edit.replace(TextRange::new(start, end), arm); |
95 | }) | 80 | }) |
96 | } | 81 | } |
@@ -132,7 +117,7 @@ mod tests { | |||
132 | fn main() { | 117 | fn main() { |
133 | let x = X::A; | 118 | let x = X::A; |
134 | let y = match x { | 119 | let y = match x { |
135 | X::A | X::B => { 1i32<|> } | 120 | X::A | X::B => { 1i32 } |
136 | X::C => { 2i32 } | 121 | X::C => { 2i32 } |
137 | } | 122 | } |
138 | } | 123 | } |
@@ -164,7 +149,7 @@ mod tests { | |||
164 | fn main() { | 149 | fn main() { |
165 | let x = X::A; | 150 | let x = X::A; |
166 | let y = match x { | 151 | let y = match x { |
167 | X::A | X::B | X::C | X::D => {<|> 1i32 }, | 152 | X::A | X::B | X::C | X::D => { 1i32 }, |
168 | X::E => { 2i32 }, | 153 | X::E => { 2i32 }, |
169 | } | 154 | } |
170 | } | 155 | } |
@@ -197,7 +182,7 @@ mod tests { | |||
197 | let x = X::A; | 182 | let x = X::A; |
198 | let y = match x { | 183 | let y = match x { |
199 | X::A => { 1i32 }, | 184 | X::A => { 1i32 }, |
200 | _ => { 2i<|>32 } | 185 | _ => { 2i32 } |
201 | } | 186 | } |
202 | } | 187 | } |
203 | "#, | 188 | "#, |
@@ -226,7 +211,7 @@ mod tests { | |||
226 | 211 | ||
227 | fn main() { | 212 | fn main() { |
228 | match X::A { | 213 | match X::A { |
229 | X::A<|> | X::B | X::C => 92, | 214 | X::A | X::B | X::C => 92, |
230 | X::D => 62, | 215 | X::D => 62, |
231 | _ => panic!(), | 216 | _ => panic!(), |
232 | } | 217 | } |
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs index a41aacfc3..be2a7eddc 100644 --- a/crates/ra_assists/src/handlers/move_bounds.rs +++ b/crates/ra_assists/src/handlers/move_bounds.rs | |||
@@ -99,7 +99,7 @@ mod tests { | |||
99 | fn foo<T: u32, <|>F: FnOnce(T) -> T>() {} | 99 | fn foo<T: u32, <|>F: FnOnce(T) -> T>() {} |
100 | "#, | 100 | "#, |
101 | r#" | 101 | r#" |
102 | fn foo<T, <|>F>() where T: u32, F: FnOnce(T) -> T {} | 102 | fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {} |
103 | "#, | 103 | "#, |
104 | ); | 104 | ); |
105 | } | 105 | } |
@@ -112,7 +112,7 @@ mod tests { | |||
112 | impl<U: u32, <|>T> A<U, T> {} | 112 | impl<U: u32, <|>T> A<U, T> {} |
113 | "#, | 113 | "#, |
114 | r#" | 114 | r#" |
115 | impl<U, <|>T> A<U, T> where U: u32 {} | 115 | impl<U, T> A<U, T> where U: u32 {} |
116 | "#, | 116 | "#, |
117 | ); | 117 | ); |
118 | } | 118 | } |
@@ -125,7 +125,7 @@ mod tests { | |||
125 | struct A<<|>T: Iterator<Item = u32>> {} | 125 | struct A<<|>T: Iterator<Item = u32>> {} |
126 | "#, | 126 | "#, |
127 | r#" | 127 | r#" |
128 | struct A<<|>T> where T: Iterator<Item = u32> {} | 128 | struct A<T> where T: Iterator<Item = u32> {} |
129 | "#, | 129 | "#, |
130 | ); | 130 | ); |
131 | } | 131 | } |
@@ -138,7 +138,7 @@ mod tests { | |||
138 | struct Pair<<|>T: u32>(T, T); | 138 | struct Pair<<|>T: u32>(T, T); |
139 | "#, | 139 | "#, |
140 | r#" | 140 | r#" |
141 | struct Pair<<|>T>(T, T) where T: u32; | 141 | struct Pair<T>(T, T) where T: u32; |
142 | "#, | 142 | "#, |
143 | ); | 143 | ); |
144 | } | 144 | } |
diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index fc0335b57..7edcf0748 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast, | 2 | ast::{AstNode, IfExpr, MatchArm}, |
3 | ast::{AstNode, AstToken, IfExpr, MatchArm}, | 3 | SyntaxKind::WHITESPACE, |
4 | TextSize, | ||
5 | }; | 4 | }; |
6 | 5 | ||
7 | use crate::{AssistContext, AssistId, Assists}; | 6 | use crate::{AssistContext, AssistId, Assists}; |
@@ -42,24 +41,15 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> | |||
42 | 41 | ||
43 | let target = guard.syntax().text_range(); | 42 | let target = guard.syntax().text_range(); |
44 | acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { | 43 | acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { |
45 | let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { | 44 | match space_before_guard { |
46 | Some(tok) => { | 45 | Some(element) if element.kind() == WHITESPACE => { |
47 | if ast::Whitespace::cast(tok.clone()).is_some() { | 46 | edit.delete(element.text_range()); |
48 | let ele = tok.text_range(); | ||
49 | edit.delete(ele); | ||
50 | ele.len() | ||
51 | } else { | ||
52 | TextSize::from(0) | ||
53 | } | ||
54 | } | 47 | } |
55 | _ => TextSize::from(0), | 48 | _ => (), |
56 | }; | 49 | }; |
57 | 50 | ||
58 | edit.delete(guard.syntax().text_range()); | 51 | edit.delete(guard.syntax().text_range()); |
59 | edit.replace_node_and_indent(arm_expr.syntax(), buf); | 52 | edit.replace_node_and_indent(arm_expr.syntax(), buf); |
60 | edit.set_cursor( | ||
61 | arm_expr.syntax().text_range().start() + TextSize::from(3) - offseting_amount, | ||
62 | ); | ||
63 | }) | 53 | }) |
64 | } | 54 | } |
65 | 55 | ||
@@ -124,7 +114,6 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex | |||
124 | } | 114 | } |
125 | 115 | ||
126 | edit.insert(match_pat.syntax().text_range().end(), buf); | 116 | edit.insert(match_pat.syntax().text_range().end(), buf); |
127 | edit.set_cursor(match_pat.syntax().text_range().end() + TextSize::from(1)); | ||
128 | }, | 117 | }, |
129 | ) | 118 | ) |
130 | } | 119 | } |
@@ -172,7 +161,7 @@ mod tests { | |||
172 | let t = 'a'; | 161 | let t = 'a'; |
173 | let chars = "abcd"; | 162 | let chars = "abcd"; |
174 | match t { | 163 | match t { |
175 | '\r' => if chars.clone().next() == Some('\n') { <|>false }, | 164 | '\r' => if chars.clone().next() == Some('\n') { false }, |
176 | _ => true | 165 | _ => true |
177 | } | 166 | } |
178 | } | 167 | } |
@@ -195,7 +184,7 @@ mod tests { | |||
195 | r#" | 184 | r#" |
196 | fn f() { | 185 | fn f() { |
197 | match x { | 186 | match x { |
198 | y @ 4 | y @ 5 => if y > 5 { <|>true }, | 187 | y @ 4 | y @ 5 => if y > 5 { true }, |
199 | _ => false | 188 | _ => false |
200 | } | 189 | } |
201 | } | 190 | } |
@@ -222,7 +211,7 @@ mod tests { | |||
222 | let t = 'a'; | 211 | let t = 'a'; |
223 | let chars = "abcd"; | 212 | let chars = "abcd"; |
224 | match t { | 213 | match t { |
225 | '\r' <|>if chars.clone().next() == Some('\n') => false, | 214 | '\r' if chars.clone().next() == Some('\n') => false, |
226 | _ => true | 215 | _ => true |
227 | } | 216 | } |
228 | } | 217 | } |
@@ -266,7 +255,7 @@ mod tests { | |||
266 | let t = 'a'; | 255 | let t = 'a'; |
267 | let chars = "abcd"; | 256 | let chars = "abcd"; |
268 | match t { | 257 | match t { |
269 | '\r' <|>if chars.clone().next().is_some() => { }, | 258 | '\r' if chars.clone().next().is_some() => { }, |
270 | _ => true | 259 | _ => true |
271 | } | 260 | } |
272 | } | 261 | } |
@@ -296,7 +285,7 @@ mod tests { | |||
296 | let mut t = 'a'; | 285 | let mut t = 'a'; |
297 | let chars = "abcd"; | 286 | let chars = "abcd"; |
298 | match t { | 287 | match t { |
299 | '\r' <|>if chars.clone().next().is_some() => { | 288 | '\r' if chars.clone().next().is_some() => { |
300 | t = 'e'; | 289 | t = 'e'; |
301 | false | 290 | false |
302 | }, | 291 | }, |
diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs index c20ffe0b3..16002d2ac 100644 --- a/crates/ra_assists/src/handlers/raw_string.rs +++ b/crates/ra_assists/src/handlers/raw_string.rs | |||
@@ -164,7 +164,7 @@ mod test { | |||
164 | "#, | 164 | "#, |
165 | r##" | 165 | r##" |
166 | fn f() { | 166 | fn f() { |
167 | let s = <|>r#"random | 167 | let s = r#"random |
168 | string"#; | 168 | string"#; |
169 | } | 169 | } |
170 | "##, | 170 | "##, |
@@ -182,7 +182,7 @@ string"#; | |||
182 | "#, | 182 | "#, |
183 | r##" | 183 | r##" |
184 | fn f() { | 184 | fn f() { |
185 | format!(<|>r#"x = {}"#, 92) | 185 | format!(r#"x = {}"#, 92) |
186 | } | 186 | } |
187 | "##, | 187 | "##, |
188 | ) | 188 | ) |
@@ -199,7 +199,7 @@ string"#; | |||
199 | "###, | 199 | "###, |
200 | r####" | 200 | r####" |
201 | fn f() { | 201 | fn f() { |
202 | let s = <|>r#"#random## | 202 | let s = r#"#random## |
203 | string"#; | 203 | string"#; |
204 | } | 204 | } |
205 | "####, | 205 | "####, |
@@ -217,7 +217,7 @@ string"#; | |||
217 | "###, | 217 | "###, |
218 | r####" | 218 | r####" |
219 | fn f() { | 219 | fn f() { |
220 | let s = <|>r###"#random"## | 220 | let s = r###"#random"## |
221 | string"###; | 221 | string"###; |
222 | } | 222 | } |
223 | "####, | 223 | "####, |
@@ -235,7 +235,7 @@ string"###; | |||
235 | "#, | 235 | "#, |
236 | r##" | 236 | r##" |
237 | fn f() { | 237 | fn f() { |
238 | let s = <|>r#"random string"#; | 238 | let s = r#"random string"#; |
239 | } | 239 | } |
240 | "##, | 240 | "##, |
241 | ) | 241 | ) |
@@ -289,7 +289,7 @@ string"###; | |||
289 | "#, | 289 | "#, |
290 | r##" | 290 | r##" |
291 | fn f() { | 291 | fn f() { |
292 | let s = <|>r#"random string"#; | 292 | let s = r#"random string"#; |
293 | } | 293 | } |
294 | "##, | 294 | "##, |
295 | ) | 295 | ) |
@@ -306,7 +306,7 @@ string"###; | |||
306 | "##, | 306 | "##, |
307 | r###" | 307 | r###" |
308 | fn f() { | 308 | fn f() { |
309 | let s = <|>r##"random"string"##; | 309 | let s = r##"random"string"##; |
310 | } | 310 | } |
311 | "###, | 311 | "###, |
312 | ) | 312 | ) |
@@ -348,7 +348,7 @@ string"###; | |||
348 | "##, | 348 | "##, |
349 | r#" | 349 | r#" |
350 | fn f() { | 350 | fn f() { |
351 | let s = <|>r"random string"; | 351 | let s = r"random string"; |
352 | } | 352 | } |
353 | "#, | 353 | "#, |
354 | ) | 354 | ) |
@@ -365,7 +365,7 @@ string"###; | |||
365 | "##, | 365 | "##, |
366 | r#" | 366 | r#" |
367 | fn f() { | 367 | fn f() { |
368 | let s = <|>r"random\"str\"ing"; | 368 | let s = r"random\"str\"ing"; |
369 | } | 369 | } |
370 | "#, | 370 | "#, |
371 | ) | 371 | ) |
@@ -382,7 +382,7 @@ string"###; | |||
382 | "###, | 382 | "###, |
383 | r##" | 383 | r##" |
384 | fn f() { | 384 | fn f() { |
385 | let s = <|>r#"random string"#; | 385 | let s = r#"random string"#; |
386 | } | 386 | } |
387 | "##, | 387 | "##, |
388 | ) | 388 | ) |
@@ -436,7 +436,7 @@ string"###; | |||
436 | "##, | 436 | "##, |
437 | r#" | 437 | r#" |
438 | fn f() { | 438 | fn f() { |
439 | let s = <|>"random string"; | 439 | let s = "random string"; |
440 | } | 440 | } |
441 | "#, | 441 | "#, |
442 | ) | 442 | ) |
@@ -453,7 +453,7 @@ string"###; | |||
453 | "##, | 453 | "##, |
454 | r#" | 454 | r#" |
455 | fn f() { | 455 | fn f() { |
456 | let s = <|>"random\"str\"ing"; | 456 | let s = "random\"str\"ing"; |
457 | } | 457 | } |
458 | "#, | 458 | "#, |
459 | ) | 459 | ) |
@@ -470,7 +470,7 @@ string"###; | |||
470 | "###, | 470 | "###, |
471 | r##" | 471 | r##" |
472 | fn f() { | 472 | fn f() { |
473 | let s = <|>"random string"; | 473 | let s = "random string"; |
474 | } | 474 | } |
475 | "##, | 475 | "##, |
476 | ) | 476 | ) |
diff --git a/crates/ra_assists/src/handlers/remove_dbg.rs b/crates/ra_assists/src/handlers/remove_dbg.rs index 8eef578cf..961ee1731 100644 --- a/crates/ra_assists/src/handlers/remove_dbg.rs +++ b/crates/ra_assists/src/handlers/remove_dbg.rs | |||
@@ -29,26 +29,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
29 | 29 | ||
30 | let macro_range = macro_call.syntax().text_range(); | 30 | let macro_range = macro_call.syntax().text_range(); |
31 | 31 | ||
32 | // If the cursor is inside the macro call, we'll try to maintain the cursor | ||
33 | // position by subtracting the length of dbg!( from the start of the file | ||
34 | // range, otherwise we'll default to using the start of the macro call | ||
35 | let cursor_pos = { | ||
36 | let file_range = ctx.frange.range; | ||
37 | |||
38 | let offset_start = file_range | ||
39 | .start() | ||
40 | .checked_sub(macro_range.start()) | ||
41 | .unwrap_or_else(|| TextSize::from(0)); | ||
42 | |||
43 | let dbg_size = TextSize::of("dbg!("); | ||
44 | |||
45 | if offset_start > dbg_size { | ||
46 | file_range.start() - dbg_size | ||
47 | } else { | ||
48 | macro_range.start() | ||
49 | } | ||
50 | }; | ||
51 | |||
52 | let macro_content = { | 32 | let macro_content = { |
53 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 33 | let macro_args = macro_call.token_tree()?.syntax().clone(); |
54 | 34 | ||
@@ -58,9 +38,8 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
58 | }; | 38 | }; |
59 | 39 | ||
60 | let target = macro_call.syntax().text_range(); | 40 | let target = macro_call.syntax().text_range(); |
61 | acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |edit| { | 41 | acc.add(AssistId("remove_dbg"), "Remove dbg!()", target, |builder| { |
62 | edit.replace(macro_range, macro_content); | 42 | builder.replace(macro_range, macro_content); |
63 | edit.set_cursor(cursor_pos); | ||
64 | }) | 43 | }) |
65 | } | 44 | } |
66 | 45 | ||
@@ -94,13 +73,13 @@ mod tests { | |||
94 | 73 | ||
95 | #[test] | 74 | #[test] |
96 | fn test_remove_dbg() { | 75 | fn test_remove_dbg() { |
97 | check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1"); | 76 | check_assist(remove_dbg, "<|>dbg!(1 + 1)", "1 + 1"); |
98 | 77 | ||
99 | check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)"); | 78 | check_assist(remove_dbg, "dbg!<|>((1 + 1))", "(1 + 1)"); |
100 | 79 | ||
101 | check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1"); | 80 | check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 + 1"); |
102 | 81 | ||
103 | check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1"); | 82 | check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = 1 + 1"); |
104 | 83 | ||
105 | check_assist( | 84 | check_assist( |
106 | remove_dbg, | 85 | remove_dbg, |
@@ -113,7 +92,7 @@ fn foo(n: usize) { | |||
113 | ", | 92 | ", |
114 | " | 93 | " |
115 | fn foo(n: usize) { | 94 | fn foo(n: usize) { |
116 | if let Some(_) = n.<|>checked_sub(4) { | 95 | if let Some(_) = n.checked_sub(4) { |
117 | // ... | 96 | // ... |
118 | } | 97 | } |
119 | } | 98 | } |
@@ -122,8 +101,8 @@ fn foo(n: usize) { | |||
122 | } | 101 | } |
123 | #[test] | 102 | #[test] |
124 | fn test_remove_dbg_with_brackets_and_braces() { | 103 | fn test_remove_dbg_with_brackets_and_braces() { |
125 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1"); | 104 | check_assist(remove_dbg, "dbg![<|>1 + 1]", "1 + 1"); |
126 | check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1"); | 105 | check_assist(remove_dbg, "dbg!{<|>1 + 1}", "1 + 1"); |
127 | } | 106 | } |
128 | 107 | ||
129 | #[test] | 108 | #[test] |
diff --git a/crates/ra_assists/src/handlers/remove_mut.rs b/crates/ra_assists/src/handlers/remove_mut.rs index dce546db7..fe4eada03 100644 --- a/crates/ra_assists/src/handlers/remove_mut.rs +++ b/crates/ra_assists/src/handlers/remove_mut.rs | |||
@@ -26,8 +26,7 @@ pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
26 | }; | 26 | }; |
27 | 27 | ||
28 | let target = mut_token.text_range(); | 28 | let target = mut_token.text_range(); |
29 | acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |edit| { | 29 | acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |builder| { |
30 | edit.set_cursor(delete_from); | 30 | builder.delete(TextRange::new(delete_from, delete_to)); |
31 | edit.delete(TextRange::new(delete_from, delete_to)); | ||
32 | }) | 31 | }) |
33 | } | 32 | } |
diff --git a/crates/ra_assists/src/handlers/reorder_fields.rs b/crates/ra_assists/src/handlers/reorder_fields.rs index 757f6406e..897da2832 100644 --- a/crates/ra_assists/src/handlers/reorder_fields.rs +++ b/crates/ra_assists/src/handlers/reorder_fields.rs | |||
@@ -23,7 +23,7 @@ use crate::{AssistContext, AssistId, Assists}; | |||
23 | // ``` | 23 | // ``` |
24 | // | 24 | // |
25 | pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 25 | pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
26 | reorder::<ast::RecordLit>(acc, ctx.clone()).or_else(|| reorder::<ast::RecordPat>(acc, ctx)) | 26 | reorder::<ast::RecordLit>(acc, ctx).or_else(|| reorder::<ast::RecordPat>(acc, ctx)) |
27 | } | 27 | } |
28 | 28 | ||
29 | fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 29 | fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
@@ -140,7 +140,7 @@ mod tests { | |||
140 | "#, | 140 | "#, |
141 | r#" | 141 | r#" |
142 | struct Foo {foo: i32, bar: i32}; | 142 | struct Foo {foo: i32, bar: i32}; |
143 | const test: Foo = <|>Foo {foo: 1, bar: 0} | 143 | const test: Foo = Foo {foo: 1, bar: 0} |
144 | "#, | 144 | "#, |
145 | ) | 145 | ) |
146 | } | 146 | } |
@@ -164,7 +164,7 @@ mod tests { | |||
164 | 164 | ||
165 | fn f(f: Foo) -> { | 165 | fn f(f: Foo) -> { |
166 | match f { | 166 | match f { |
167 | <|>Foo { ref mut bar, baz: 0, .. } => (), | 167 | Foo { ref mut bar, baz: 0, .. } => (), |
168 | _ => () | 168 | _ => () |
169 | } | 169 | } |
170 | } | 170 | } |
@@ -202,7 +202,7 @@ mod tests { | |||
202 | impl Foo { | 202 | impl Foo { |
203 | fn new() -> Foo { | 203 | fn new() -> Foo { |
204 | let foo = String::new(); | 204 | let foo = String::new(); |
205 | <|>Foo { | 205 | Foo { |
206 | foo, | 206 | foo, |
207 | bar: foo.clone(), | 207 | bar: foo.clone(), |
208 | extra: "Extra field", | 208 | extra: "Extra field", |
diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index 65f5fc6ab..e016f51c3 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs | |||
@@ -68,7 +68,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
68 | .indent(IndentLevel::from_node(if_expr.syntax())) | 68 | .indent(IndentLevel::from_node(if_expr.syntax())) |
69 | }; | 69 | }; |
70 | 70 | ||
71 | edit.set_cursor(if_expr.syntax().text_range().start()); | ||
72 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | 71 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); |
73 | }) | 72 | }) |
74 | } | 73 | } |
@@ -83,7 +82,7 @@ mod tests { | |||
83 | fn test_replace_if_let_with_match_unwraps_simple_expressions() { | 82 | fn test_replace_if_let_with_match_unwraps_simple_expressions() { |
84 | check_assist( | 83 | check_assist( |
85 | replace_if_let_with_match, | 84 | replace_if_let_with_match, |
86 | " | 85 | r#" |
87 | impl VariantData { | 86 | impl VariantData { |
88 | pub fn is_struct(&self) -> bool { | 87 | pub fn is_struct(&self) -> bool { |
89 | if <|>let VariantData::Struct(..) = *self { | 88 | if <|>let VariantData::Struct(..) = *self { |
@@ -92,16 +91,16 @@ impl VariantData { | |||
92 | false | 91 | false |
93 | } | 92 | } |
94 | } | 93 | } |
95 | } ", | 94 | } "#, |
96 | " | 95 | r#" |
97 | impl VariantData { | 96 | impl VariantData { |
98 | pub fn is_struct(&self) -> bool { | 97 | pub fn is_struct(&self) -> bool { |
99 | <|>match *self { | 98 | match *self { |
100 | VariantData::Struct(..) => true, | 99 | VariantData::Struct(..) => true, |
101 | _ => false, | 100 | _ => false, |
102 | } | 101 | } |
103 | } | 102 | } |
104 | } ", | 103 | } "#, |
105 | ) | 104 | ) |
106 | } | 105 | } |
107 | 106 | ||
@@ -109,7 +108,7 @@ impl VariantData { | |||
109 | fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() { | 108 | fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() { |
110 | check_assist( | 109 | check_assist( |
111 | replace_if_let_with_match, | 110 | replace_if_let_with_match, |
112 | " | 111 | r#" |
113 | fn foo() { | 112 | fn foo() { |
114 | if <|>let VariantData::Struct(..) = a { | 113 | if <|>let VariantData::Struct(..) = a { |
115 | bar( | 114 | bar( |
@@ -118,10 +117,10 @@ fn foo() { | |||
118 | } else { | 117 | } else { |
119 | false | 118 | false |
120 | } | 119 | } |
121 | } ", | 120 | } "#, |
122 | " | 121 | r#" |
123 | fn foo() { | 122 | fn foo() { |
124 | <|>match a { | 123 | match a { |
125 | VariantData::Struct(..) => { | 124 | VariantData::Struct(..) => { |
126 | bar( | 125 | bar( |
127 | 123 | 126 | 123 |
@@ -129,7 +128,7 @@ fn foo() { | |||
129 | } | 128 | } |
130 | _ => false, | 129 | _ => false, |
131 | } | 130 | } |
132 | } ", | 131 | } "#, |
133 | ) | 132 | ) |
134 | } | 133 | } |
135 | 134 | ||
@@ -137,7 +136,7 @@ fn foo() { | |||
137 | fn replace_if_let_with_match_target() { | 136 | fn replace_if_let_with_match_target() { |
138 | check_assist_target( | 137 | check_assist_target( |
139 | replace_if_let_with_match, | 138 | replace_if_let_with_match, |
140 | " | 139 | r#" |
141 | impl VariantData { | 140 | impl VariantData { |
142 | pub fn is_struct(&self) -> bool { | 141 | pub fn is_struct(&self) -> bool { |
143 | if <|>let VariantData::Struct(..) = *self { | 142 | if <|>let VariantData::Struct(..) = *self { |
@@ -146,7 +145,7 @@ impl VariantData { | |||
146 | false | 145 | false |
147 | } | 146 | } |
148 | } | 147 | } |
149 | } ", | 148 | } "#, |
150 | "if let VariantData::Struct(..) = *self { | 149 | "if let VariantData::Struct(..) = *self { |
151 | true | 150 | true |
152 | } else { | 151 | } else { |
@@ -176,7 +175,7 @@ enum Option<T> { Some(T), None } | |||
176 | use Option::*; | 175 | use Option::*; |
177 | 176 | ||
178 | fn foo(x: Option<i32>) { | 177 | fn foo(x: Option<i32>) { |
179 | <|>match x { | 178 | match x { |
180 | Some(x) => println!("{}", x), | 179 | Some(x) => println!("{}", x), |
181 | None => println!("none"), | 180 | None => println!("none"), |
182 | } | 181 | } |
@@ -206,7 +205,7 @@ enum Result<T, E> { Ok(T), Err(E) } | |||
206 | use Result::*; | 205 | use Result::*; |
207 | 206 | ||
208 | fn foo(x: Result<i32, ()>) { | 207 | fn foo(x: Result<i32, ()>) { |
209 | <|>match x { | 208 | match x { |
210 | Ok(x) => println!("{}", x), | 209 | Ok(x) => println!("{}", x), |
211 | Err(_) => println!("none"), | 210 | Err(_) => println!("none"), |
212 | } | 211 | } |
diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs index 482957dc6..761557ac0 100644 --- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs | |||
@@ -58,12 +58,9 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> | |||
58 | let stmt = make::expr_stmt(if_); | 58 | let stmt = make::expr_stmt(if_); |
59 | 59 | ||
60 | let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap(); | 60 | let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap(); |
61 | let target_offset = | ||
62 | let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start(); | ||
63 | let stmt = stmt.replace_descendant(placeholder.into(), original_pat); | 61 | let stmt = stmt.replace_descendant(placeholder.into(), original_pat); |
64 | 62 | ||
65 | edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); | 63 | edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); |
66 | edit.set_cursor(target_offset); | ||
67 | }) | 64 | }) |
68 | } | 65 | } |
69 | 66 | ||
@@ -88,7 +85,7 @@ fn main() { | |||
88 | enum E<T> { X(T), Y(T) } | 85 | enum E<T> { X(T), Y(T) } |
89 | 86 | ||
90 | fn main() { | 87 | fn main() { |
91 | if let <|>x = E::X(92) { | 88 | if let x = E::X(92) { |
92 | } | 89 | } |
93 | } | 90 | } |
94 | ", | 91 | ", |
diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index d9f84208d..0197a8cf0 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -89,7 +89,7 @@ std::fmt::Debug<|> | |||
89 | " | 89 | " |
90 | use std::fmt::Debug; | 90 | use std::fmt::Debug; |
91 | 91 | ||
92 | Debug<|> | 92 | Debug |
93 | ", | 93 | ", |
94 | ); | 94 | ); |
95 | } | 95 | } |
@@ -106,7 +106,7 @@ fn main() { | |||
106 | " | 106 | " |
107 | use std::fmt::Debug; | 107 | use std::fmt::Debug; |
108 | 108 | ||
109 | Debug<|> | 109 | Debug |
110 | 110 | ||
111 | fn main() { | 111 | fn main() { |
112 | } | 112 | } |
@@ -130,7 +130,7 @@ use std::fmt::Debug; | |||
130 | fn main() { | 130 | fn main() { |
131 | } | 131 | } |
132 | 132 | ||
133 | Debug<|> | 133 | Debug |
134 | ", | 134 | ", |
135 | ); | 135 | ); |
136 | } | 136 | } |
@@ -145,7 +145,7 @@ std::fmt<|>::Debug | |||
145 | " | 145 | " |
146 | use std::fmt; | 146 | use std::fmt; |
147 | 147 | ||
148 | fmt<|>::Debug | 148 | fmt::Debug |
149 | ", | 149 | ", |
150 | ); | 150 | ); |
151 | } | 151 | } |
@@ -164,7 +164,7 @@ impl std::fmt::Debug<|> for Foo { | |||
164 | use stdx; | 164 | use stdx; |
165 | use std::fmt::Debug; | 165 | use std::fmt::Debug; |
166 | 166 | ||
167 | impl Debug<|> for Foo { | 167 | impl Debug for Foo { |
168 | } | 168 | } |
169 | ", | 169 | ", |
170 | ); | 170 | ); |
@@ -181,7 +181,7 @@ impl std::fmt::Debug<|> for Foo { | |||
181 | " | 181 | " |
182 | use std::fmt::Debug; | 182 | use std::fmt::Debug; |
183 | 183 | ||
184 | impl Debug<|> for Foo { | 184 | impl Debug for Foo { |
185 | } | 185 | } |
186 | ", | 186 | ", |
187 | ); | 187 | ); |
@@ -198,7 +198,7 @@ impl Debug<|> for Foo { | |||
198 | " | 198 | " |
199 | use std::fmt::Debug; | 199 | use std::fmt::Debug; |
200 | 200 | ||
201 | impl Debug<|> for Foo { | 201 | impl Debug for Foo { |
202 | } | 202 | } |
203 | ", | 203 | ", |
204 | ); | 204 | ); |
@@ -217,7 +217,7 @@ impl std::io<|> for Foo { | |||
217 | " | 217 | " |
218 | use std::{io, fmt}; | 218 | use std::{io, fmt}; |
219 | 219 | ||
220 | impl io<|> for Foo { | 220 | impl io for Foo { |
221 | } | 221 | } |
222 | ", | 222 | ", |
223 | ); | 223 | ); |
@@ -236,7 +236,7 @@ impl std::fmt::Debug<|> for Foo { | |||
236 | " | 236 | " |
237 | use std::fmt::{self, Debug, }; | 237 | use std::fmt::{self, Debug, }; |
238 | 238 | ||
239 | impl Debug<|> for Foo { | 239 | impl Debug for Foo { |
240 | } | 240 | } |
241 | ", | 241 | ", |
242 | ); | 242 | ); |
@@ -255,7 +255,7 @@ impl std::fmt<|> for Foo { | |||
255 | " | 255 | " |
256 | use std::fmt::{self, Debug}; | 256 | use std::fmt::{self, Debug}; |
257 | 257 | ||
258 | impl fmt<|> for Foo { | 258 | impl fmt for Foo { |
259 | } | 259 | } |
260 | ", | 260 | ", |
261 | ); | 261 | ); |
@@ -274,7 +274,7 @@ impl std::fmt::nested<|> for Foo { | |||
274 | " | 274 | " |
275 | use std::fmt::{Debug, nested::{Display, self}}; | 275 | use std::fmt::{Debug, nested::{Display, self}}; |
276 | 276 | ||
277 | impl nested<|> for Foo { | 277 | impl nested for Foo { |
278 | } | 278 | } |
279 | ", | 279 | ", |
280 | ); | 280 | ); |
@@ -293,7 +293,7 @@ impl std::fmt::nested<|> for Foo { | |||
293 | " | 293 | " |
294 | use std::fmt::{Debug, nested::{self, Display}}; | 294 | use std::fmt::{Debug, nested::{self, Display}}; |
295 | 295 | ||
296 | impl nested<|> for Foo { | 296 | impl nested for Foo { |
297 | } | 297 | } |
298 | ", | 298 | ", |
299 | ); | 299 | ); |
@@ -312,7 +312,7 @@ impl std::fmt::nested::Debug<|> for Foo { | |||
312 | " | 312 | " |
313 | use std::fmt::{Debug, nested::{Display, Debug}}; | 313 | use std::fmt::{Debug, nested::{Display, Debug}}; |
314 | 314 | ||
315 | impl Debug<|> for Foo { | 315 | impl Debug for Foo { |
316 | } | 316 | } |
317 | ", | 317 | ", |
318 | ); | 318 | ); |
@@ -331,7 +331,7 @@ impl std::fmt::nested::Display<|> for Foo { | |||
331 | " | 331 | " |
332 | use std::fmt::{nested::Display, Debug}; | 332 | use std::fmt::{nested::Display, Debug}; |
333 | 333 | ||
334 | impl Display<|> for Foo { | 334 | impl Display for Foo { |
335 | } | 335 | } |
336 | ", | 336 | ", |
337 | ); | 337 | ); |
@@ -350,7 +350,7 @@ impl std::fmt::Display<|> for Foo { | |||
350 | " | 350 | " |
351 | use std::fmt::{Display, nested::Debug}; | 351 | use std::fmt::{Display, nested::Debug}; |
352 | 352 | ||
353 | impl Display<|> for Foo { | 353 | impl Display for Foo { |
354 | } | 354 | } |
355 | ", | 355 | ", |
356 | ); | 356 | ); |
@@ -374,7 +374,7 @@ use crate::{ | |||
374 | AssocItem, | 374 | AssocItem, |
375 | }; | 375 | }; |
376 | 376 | ||
377 | fn foo() { lower<|>::trait_env() } | 377 | fn foo() { lower::trait_env() } |
378 | ", | 378 | ", |
379 | ); | 379 | ); |
380 | } | 380 | } |
@@ -392,7 +392,7 @@ impl foo::Debug<|> for Foo { | |||
392 | " | 392 | " |
393 | use std::fmt as foo; | 393 | use std::fmt as foo; |
394 | 394 | ||
395 | impl Debug<|> for Foo { | 395 | impl Debug for Foo { |
396 | } | 396 | } |
397 | ", | 397 | ", |
398 | ); | 398 | ); |
@@ -435,7 +435,7 @@ mod foo { | |||
435 | mod bar { | 435 | mod bar { |
436 | use std::fmt::Debug; | 436 | use std::fmt::Debug; |
437 | 437 | ||
438 | Debug<|> | 438 | Debug |
439 | } | 439 | } |
440 | } | 440 | } |
441 | ", | 441 | ", |
@@ -458,7 +458,7 @@ fn main() { | |||
458 | use std::fmt::Debug; | 458 | use std::fmt::Debug; |
459 | 459 | ||
460 | fn main() { | 460 | fn main() { |
461 | Debug<|> | 461 | Debug |
462 | } | 462 | } |
463 | ", | 463 | ", |
464 | ); | 464 | ); |
diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index b379b55a8..cff7dfb81 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | |||
@@ -9,7 +9,10 @@ use ra_syntax::{ | |||
9 | AstNode, | 9 | AstNode, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; | 12 | use crate::{ |
13 | utils::{render_snippet, Cursor, TryEnum}, | ||
14 | AssistContext, AssistId, Assists, | ||
15 | }; | ||
13 | 16 | ||
14 | // Assist: replace_unwrap_with_match | 17 | // Assist: replace_unwrap_with_match |
15 | // | 18 | // |
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; | |||
29 | // let x: Result<i32, i32> = Result::Ok(92); | 32 | // let x: Result<i32, i32> = Result::Ok(92); |
30 | // let y = match x { | 33 | // let y = match x { |
31 | // Ok(a) => a, | 34 | // Ok(a) => a, |
32 | // _ => unreachable!(), | 35 | // $0_ => unreachable!(), |
33 | // }; | 36 | // }; |
34 | // } | 37 | // } |
35 | // ``` | 38 | // ``` |
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
43 | let ty = ctx.sema.type_of_expr(&caller)?; | 46 | let ty = ctx.sema.type_of_expr(&caller)?; |
44 | let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); | 47 | let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); |
45 | let target = method_call.syntax().text_range(); | 48 | let target = method_call.syntax().text_range(); |
46 | acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| { | 49 | acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| { |
47 | let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); | 50 | let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); |
48 | let it = make::bind_pat(make::name("a")).into(); | 51 | let it = make::bind_pat(make::name("a")).into(); |
49 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); | 52 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); |
@@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
58 | let match_expr = make::expr_match(caller.clone(), match_arm_list) | 61 | let match_expr = make::expr_match(caller.clone(), match_arm_list) |
59 | .indent(IndentLevel::from_node(method_call.syntax())); | 62 | .indent(IndentLevel::from_node(method_call.syntax())); |
60 | 63 | ||
61 | edit.set_cursor(caller.syntax().text_range().start()); | 64 | let range = method_call.syntax().text_range(); |
62 | edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); | 65 | match ctx.config.snippet_cap { |
66 | Some(cap) => { | ||
67 | let err_arm = match_expr | ||
68 | .syntax() | ||
69 | .descendants() | ||
70 | .filter_map(ast::MatchArm::cast) | ||
71 | .last() | ||
72 | .unwrap(); | ||
73 | let snippet = | ||
74 | render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax())); | ||
75 | builder.replace_snippet(cap, range, snippet) | ||
76 | } | ||
77 | None => builder.replace(range, match_expr.to_string()), | ||
78 | } | ||
63 | }) | 79 | }) |
64 | } | 80 | } |
65 | 81 | ||
66 | #[cfg(test)] | 82 | #[cfg(test)] |
67 | mod tests { | 83 | mod tests { |
68 | use super::*; | ||
69 | use crate::tests::{check_assist, check_assist_target}; | 84 | use crate::tests::{check_assist, check_assist_target}; |
70 | 85 | ||
86 | use super::*; | ||
87 | |||
71 | #[test] | 88 | #[test] |
72 | fn test_replace_result_unwrap_with_match() { | 89 | fn test_replace_result_unwrap_with_match() { |
73 | check_assist( | 90 | check_assist( |
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) } | |||
85 | fn i<T>(a: T) -> T { a } | 102 | fn i<T>(a: T) -> T { a } |
86 | fn main() { | 103 | fn main() { |
87 | let x: Result<i32, i32> = Result::Ok(92); | 104 | let x: Result<i32, i32> = Result::Ok(92); |
88 | let y = <|>match i(x) { | 105 | let y = match i(x) { |
89 | Ok(a) => a, | 106 | Ok(a) => a, |
90 | _ => unreachable!(), | 107 | $0_ => unreachable!(), |
91 | }; | 108 | }; |
92 | } | 109 | } |
93 | ", | 110 | ", |
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None } | |||
111 | fn i<T>(a: T) -> T { a } | 128 | fn i<T>(a: T) -> T { a } |
112 | fn main() { | 129 | fn main() { |
113 | let x = Option::Some(92); | 130 | let x = Option::Some(92); |
114 | let y = <|>match i(x) { | 131 | let y = match i(x) { |
115 | Some(a) => a, | 132 | Some(a) => a, |
116 | _ => unreachable!(), | 133 | $0_ => unreachable!(), |
117 | }; | 134 | }; |
118 | } | 135 | } |
119 | ", | 136 | ", |
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) } | |||
137 | fn i<T>(a: T) -> T { a } | 154 | fn i<T>(a: T) -> T { a } |
138 | fn main() { | 155 | fn main() { |
139 | let x: Result<i32, i32> = Result::Ok(92); | 156 | let x: Result<i32, i32> = Result::Ok(92); |
140 | let y = <|>match i(x) { | 157 | let y = match i(x) { |
141 | Ok(a) => a, | 158 | Ok(a) => a, |
142 | _ => unreachable!(), | 159 | $0_ => unreachable!(), |
143 | }.count_zeroes(); | 160 | }.count_zeroes(); |
144 | } | 161 | } |
145 | ", | 162 | ", |
diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index b2757e50c..c7a874480 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs | |||
@@ -26,12 +26,10 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
26 | if new_tree == use_tree { | 26 | if new_tree == use_tree { |
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | let cursor = ctx.offset(); | ||
30 | 29 | ||
31 | let target = colon_colon.text_range(); | 30 | let target = colon_colon.text_range(); |
32 | acc.add(AssistId("split_import"), "Split import", target, |edit| { | 31 | acc.add(AssistId("split_import"), "Split import", target, |edit| { |
33 | edit.replace_ast(use_tree, new_tree); | 32 | edit.replace_ast(use_tree, new_tree); |
34 | edit.set_cursor(cursor); | ||
35 | }) | 33 | }) |
36 | } | 34 | } |
37 | 35 | ||
@@ -46,7 +44,7 @@ mod tests { | |||
46 | check_assist( | 44 | check_assist( |
47 | split_import, | 45 | split_import, |
48 | "use crate::<|>db::RootDatabase;", | 46 | "use crate::<|>db::RootDatabase;", |
49 | "use crate::<|>{db::RootDatabase};", | 47 | "use crate::{db::RootDatabase};", |
50 | ) | 48 | ) |
51 | } | 49 | } |
52 | 50 | ||
@@ -55,7 +53,7 @@ mod tests { | |||
55 | check_assist( | 53 | check_assist( |
56 | split_import, | 54 | split_import, |
57 | "use crate:<|>:db::{RootDatabase, FileSymbol}", | 55 | "use crate:<|>:db::{RootDatabase, FileSymbol}", |
58 | "use crate:<|>:{db::{RootDatabase, FileSymbol}}", | 56 | "use crate::{db::{RootDatabase, FileSymbol}}", |
59 | ) | 57 | ) |
60 | } | 58 | } |
61 | 59 | ||
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index b76182d79..8440c7d0f 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs | |||
@@ -62,7 +62,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
62 | let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); | 62 | let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); |
63 | let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); | 63 | let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); |
64 | 64 | ||
65 | edit.set_cursor(ancestor_then_branch.syntax().text_range().end()); | ||
66 | edit.delete(range_to_del_rest); | 65 | edit.delete(range_to_del_rest); |
67 | edit.delete(range_to_del_else_if); | 66 | edit.delete(range_to_del_else_if); |
68 | edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); | 67 | edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); |
@@ -79,7 +78,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
79 | return acc.add(assist_id, assist_label, target, |edit| { | 78 | return acc.add(assist_id, assist_label, target, |edit| { |
80 | let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); | 79 | let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); |
81 | 80 | ||
82 | edit.set_cursor(then_branch.syntax().text_range().end()); | ||
83 | edit.delete(range_to_del); | 81 | edit.delete(range_to_del); |
84 | edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); | 82 | edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); |
85 | }); | 83 | }); |
@@ -97,8 +95,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
97 | 95 | ||
98 | let target = expr_to_unwrap.syntax().text_range(); | 96 | let target = expr_to_unwrap.syntax().text_range(); |
99 | acc.add(assist_id, assist_label, target, |edit| { | 97 | acc.add(assist_id, assist_label, target, |edit| { |
100 | edit.set_cursor(expr.syntax().text_range().start()); | ||
101 | |||
102 | edit.replace( | 98 | edit.replace( |
103 | expr.syntax().text_range(), | 99 | expr.syntax().text_range(), |
104 | update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), | 100 | update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), |
@@ -154,7 +150,7 @@ mod tests { | |||
154 | r#" | 150 | r#" |
155 | fn main() { | 151 | fn main() { |
156 | bar(); | 152 | bar(); |
157 | <|>foo(); | 153 | foo(); |
158 | 154 | ||
159 | //comment | 155 | //comment |
160 | bar(); | 156 | bar(); |
@@ -188,7 +184,7 @@ mod tests { | |||
188 | 184 | ||
189 | //comment | 185 | //comment |
190 | bar(); | 186 | bar(); |
191 | }<|> | 187 | } |
192 | println!("bar"); | 188 | println!("bar"); |
193 | } | 189 | } |
194 | "#, | 190 | "#, |
@@ -222,7 +218,7 @@ mod tests { | |||
222 | 218 | ||
223 | //comment | 219 | //comment |
224 | //bar(); | 220 | //bar(); |
225 | }<|> | 221 | } |
226 | println!("bar"); | 222 | println!("bar"); |
227 | } | 223 | } |
228 | "#, | 224 | "#, |
@@ -258,7 +254,7 @@ mod tests { | |||
258 | //bar(); | 254 | //bar(); |
259 | } else if false { | 255 | } else if false { |
260 | println!("bar"); | 256 | println!("bar"); |
261 | }<|> | 257 | } |
262 | println!("foo"); | 258 | println!("foo"); |
263 | } | 259 | } |
264 | "#, | 260 | "#, |
@@ -298,7 +294,7 @@ mod tests { | |||
298 | println!("bar"); | 294 | println!("bar"); |
299 | } else if true { | 295 | } else if true { |
300 | println!("foo"); | 296 | println!("foo"); |
301 | }<|> | 297 | } |
302 | println!("else"); | 298 | println!("else"); |
303 | } | 299 | } |
304 | "#, | 300 | "#, |
@@ -336,7 +332,7 @@ mod tests { | |||
336 | //bar(); | 332 | //bar(); |
337 | } else if false { | 333 | } else if false { |
338 | println!("bar"); | 334 | println!("bar"); |
339 | }<|> | 335 | } |
340 | println!("foo"); | 336 | println!("foo"); |
341 | } | 337 | } |
342 | "#, | 338 | "#, |
@@ -383,7 +379,7 @@ mod tests { | |||
383 | "#, | 379 | "#, |
384 | r#" | 380 | r#" |
385 | fn main() { | 381 | fn main() { |
386 | <|>if true { | 382 | if true { |
387 | foo(); | 383 | foo(); |
388 | 384 | ||
389 | //comment | 385 | //comment |
@@ -417,7 +413,7 @@ mod tests { | |||
417 | r#" | 413 | r#" |
418 | fn main() { | 414 | fn main() { |
419 | for i in 0..5 { | 415 | for i in 0..5 { |
420 | <|>foo(); | 416 | foo(); |
421 | 417 | ||
422 | //comment | 418 | //comment |
423 | bar(); | 419 | bar(); |
@@ -447,7 +443,7 @@ mod tests { | |||
447 | "#, | 443 | "#, |
448 | r#" | 444 | r#" |
449 | fn main() { | 445 | fn main() { |
450 | <|>if true { | 446 | if true { |
451 | foo(); | 447 | foo(); |
452 | 448 | ||
453 | //comment | 449 | //comment |
@@ -480,7 +476,7 @@ mod tests { | |||
480 | "#, | 476 | "#, |
481 | r#" | 477 | r#" |
482 | fn main() { | 478 | fn main() { |
483 | <|>if true { | 479 | if true { |
484 | foo(); | 480 | foo(); |
485 | 481 | ||
486 | //comment | 482 | //comment |
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index 9ba3da786..62dd3547f 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs | |||
@@ -7,8 +7,7 @@ use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | |||
7 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; | 7 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; |
8 | use ra_syntax::TextRange; | 8 | use ra_syntax::TextRange; |
9 | use test_utils::{ | 9 | use test_utils::{ |
10 | add_cursor, assert_eq_text, extract_offset, extract_range, extract_range_or_offset, | 10 | assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset, |
11 | RangeOrOffset, | ||
12 | }; | 11 | }; |
13 | 12 | ||
14 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; | 13 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; |
@@ -103,21 +102,6 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult) { | |||
103 | 102 | ||
104 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); | 103 | let mut actual = db.file_text(change.file_id).as_ref().to_owned(); |
105 | change.edit.apply(&mut actual); | 104 | change.edit.apply(&mut actual); |
106 | |||
107 | if !source_change.is_snippet { | ||
108 | match source_change.cursor_position { | ||
109 | None => { | ||
110 | if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { | ||
111 | let off = change | ||
112 | .edit | ||
113 | .apply_to_offset(before_cursor_pos) | ||
114 | .expect("cursor position is affected by the edit"); | ||
115 | actual = add_cursor(&actual, off) | ||
116 | } | ||
117 | } | ||
118 | Some(off) => actual = add_cursor(&actual, off.offset), | ||
119 | }; | ||
120 | } | ||
121 | assert_eq_text!(after, &actual); | 105 | assert_eq_text!(after, &actual); |
122 | } | 106 | } |
123 | (Some(assist), ExpectedResult::Target(target)) => { | 107 | (Some(assist), ExpectedResult::Target(target)) => { |
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 3e6654c17..250e56a69 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs | |||
@@ -443,7 +443,7 @@ fn main() { | |||
443 | "#####, | 443 | "#####, |
444 | r#####" | 444 | r#####" |
445 | fn main() { | 445 | fn main() { |
446 | let var_name = (1 + 2); | 446 | let $0var_name = (1 + 2); |
447 | var_name * 4; | 447 | var_name * 4; |
448 | } | 448 | } |
449 | "#####, | 449 | "#####, |
@@ -764,7 +764,7 @@ fn main() { | |||
764 | let x: Result<i32, i32> = Result::Ok(92); | 764 | let x: Result<i32, i32> = Result::Ok(92); |
765 | let y = match x { | 765 | let y = match x { |
766 | Ok(a) => a, | 766 | Ok(a) => a, |
767 | _ => unreachable!(), | 767 | $0_ => unreachable!(), |
768 | }; | 768 | }; |
769 | } | 769 | } |
770 | "#####, | 770 | "#####, |