diff options
Diffstat (limited to 'crates/ra_editor/src/assists/add_impl.rs')
-rw-r--r-- | crates/ra_editor/src/assists/add_impl.rs | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/crates/ra_editor/src/assists/add_impl.rs b/crates/ra_editor/src/assists/add_impl.rs index 50e00688e..9353e2717 100644 --- a/crates/ra_editor/src/assists/add_impl.rs +++ b/crates/ra_editor/src/assists/add_impl.rs | |||
@@ -1,23 +1,16 @@ | |||
1 | use join_to_string::join; | 1 | use join_to_string::join; |
2 | use ra_text_edit::TextEditBuilder; | ||
3 | use ra_syntax::{ | 2 | use ra_syntax::{ |
4 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, | 3 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, |
5 | SourceFileNode, | ||
6 | TextUnit, | 4 | TextUnit, |
7 | }; | 5 | }; |
8 | 6 | ||
9 | use crate::{find_node_at_offset, assists::LocalEdit}; | 7 | use crate::assists::{AssistCtx, Assist}; |
10 | 8 | ||
11 | pub fn add_impl<'a>( | 9 | pub fn add_impl(ctx: AssistCtx) -> Option<Assist> { |
12 | file: &'a SourceFileNode, | 10 | let nominal = ctx.node_at_offset::<ast::NominalDef>()?; |
13 | offset: TextUnit, | ||
14 | ) -> Option<impl FnOnce() -> LocalEdit + 'a> { | ||
15 | let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?; | ||
16 | let name = nominal.name()?; | 11 | let name = nominal.name()?; |
17 | 12 | ctx.build("add impl", |edit| { | |
18 | Some(move || { | ||
19 | let type_params = nominal.type_param_list(); | 13 | let type_params = nominal.type_param_list(); |
20 | let mut edit = TextEditBuilder::new(); | ||
21 | let start_offset = nominal.syntax().range().end(); | 14 | let start_offset = nominal.syntax().range().end(); |
22 | let mut buf = String::new(); | 15 | let mut buf = String::new(); |
23 | buf.push_str("\n\nimpl"); | 16 | buf.push_str("\n\nimpl"); |
@@ -40,38 +33,33 @@ pub fn add_impl<'a>( | |||
40 | .to_buf(&mut buf); | 33 | .to_buf(&mut buf); |
41 | } | 34 | } |
42 | buf.push_str(" {\n"); | 35 | buf.push_str(" {\n"); |
43 | let offset = start_offset + TextUnit::of_str(&buf); | 36 | edit.set_cursor(start_offset + TextUnit::of_str(&buf)); |
44 | buf.push_str("\n}"); | 37 | buf.push_str("\n}"); |
45 | edit.insert(start_offset, buf); | 38 | edit.insert(start_offset, buf); |
46 | LocalEdit { | ||
47 | label: "add impl".to_string(), | ||
48 | edit: edit.finish(), | ||
49 | cursor_position: Some(offset), | ||
50 | } | ||
51 | }) | 39 | }) |
52 | } | 40 | } |
53 | 41 | ||
54 | #[cfg(test)] | 42 | #[cfg(test)] |
55 | mod tests { | 43 | mod tests { |
56 | use super::*; | 44 | use super::*; |
57 | use crate::test_utils::check_action; | 45 | use crate::assists::check_assist; |
58 | 46 | ||
59 | #[test] | 47 | #[test] |
60 | fn test_add_impl() { | 48 | fn test_add_impl() { |
61 | check_action( | 49 | check_assist( |
50 | add_impl, | ||
62 | "struct Foo {<|>}\n", | 51 | "struct Foo {<|>}\n", |
63 | "struct Foo {}\n\nimpl Foo {\n<|>\n}\n", | 52 | "struct Foo {}\n\nimpl Foo {\n<|>\n}\n", |
64 | |file, off| add_impl(file, off).map(|f| f()), | ||
65 | ); | 53 | ); |
66 | check_action( | 54 | check_assist( |
55 | add_impl, | ||
67 | "struct Foo<T: Clone> {<|>}", | 56 | "struct Foo<T: Clone> {<|>}", |
68 | "struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}", | 57 | "struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}", |
69 | |file, off| add_impl(file, off).map(|f| f()), | ||
70 | ); | 58 | ); |
71 | check_action( | 59 | check_assist( |
60 | add_impl, | ||
72 | "struct Foo<'a, T: Foo<'a>> {<|>}", | 61 | "struct Foo<'a, T: Foo<'a>> {<|>}", |
73 | "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}", | 62 | "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}", |
74 | |file, off| add_impl(file, off).map(|f| f()), | ||
75 | ); | 63 | ); |
76 | } | 64 | } |
77 | 65 | ||