diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/introduce_variable.rs | 18 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 7 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 18 |
3 files changed, 37 insertions, 6 deletions
diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/assists/introduce_variable.rs index 43378c4b0..8245dc99f 100644 --- a/crates/ra_assists/src/assists/introduce_variable.rs +++ b/crates/ra_assists/src/assists/introduce_variable.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use format_buf::format; | 1 | use format_buf::format; |
4 | use hir::db::HirDatabase; | 2 | use hir::db::HirDatabase; |
5 | use ra_syntax::{ | 3 | use ra_syntax::{ |
@@ -14,6 +12,22 @@ use test_utils::tested_by; | |||
14 | 12 | ||
15 | use crate::{Assist, AssistCtx, AssistId}; | 13 | use crate::{Assist, AssistCtx, AssistId}; |
16 | 14 | ||
15 | // Assist: introduce_variable | ||
16 | // | ||
17 | // Extracts subexpression into a variable. | ||
18 | // | ||
19 | // ``` | ||
20 | // fn main() { | ||
21 | // <|>(1 + 2)<|> * 4; | ||
22 | // } | ||
23 | // ``` | ||
24 | // -> | ||
25 | // ``` | ||
26 | // fn main() { | ||
27 | // let var_name = (1 + 2); | ||
28 | // var_name * 4; | ||
29 | // } | ||
30 | // ``` | ||
17 | pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 31 | pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
18 | if ctx.frange.range.is_empty() { | 32 | if ctx.frange.range.is_empty() { |
19 | return None; | 33 | return None; |
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 872bbdf17..0ccf9d730 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -7,13 +7,12 @@ mod generated; | |||
7 | 7 | ||
8 | use hir::mock::MockDatabase; | 8 | use hir::mock::MockDatabase; |
9 | use ra_db::FileRange; | 9 | use ra_db::FileRange; |
10 | use ra_syntax::TextRange; | 10 | use test_utils::{assert_eq_text, extract_range_or_offset}; |
11 | use test_utils::{assert_eq_text, extract_offset}; | ||
12 | 11 | ||
13 | fn check(assist_id: &str, before: &str, after: &str) { | 12 | fn check(assist_id: &str, before: &str, after: &str) { |
14 | let (before_cursor_pos, before) = extract_offset(before); | 13 | let (selection, before) = extract_range_or_offset(before); |
15 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 14 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); |
16 | let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 15 | let frange = FileRange { file_id, range: selection.into() }; |
17 | 16 | ||
18 | let (_assist_id, action) = crate::assists(&db, frange) | 17 | let (_assist_id, action) = crate::assists(&db, frange) |
19 | .into_iter() | 18 | .into_iter() |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index d390db33c..493bd94d0 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -255,3 +255,21 @@ fn main() { | |||
255 | "#####, | 255 | "#####, |
256 | ) | 256 | ) |
257 | } | 257 | } |
258 | |||
259 | #[test] | ||
260 | fn doctest_introduce_variable() { | ||
261 | check( | ||
262 | "introduce_variable", | ||
263 | r#####" | ||
264 | fn main() { | ||
265 | <|>(1 + 2)<|> * 4; | ||
266 | } | ||
267 | "#####, | ||
268 | r#####" | ||
269 | fn main() { | ||
270 | let var_name = (1 + 2); | ||
271 | var_name * 4; | ||
272 | } | ||
273 | "#####, | ||
274 | ) | ||
275 | } | ||