aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/assists/add_impl.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-03 12:08:32 +0000
committerAleksey Kladov <[email protected]>2019-01-03 12:21:49 +0000
commita5935687cbc62e893753ec81a00655281b03feae (patch)
treef57f648b33797c8482735da800808f9709467ccf /crates/ra_editor/src/assists/add_impl.rs
parent5323e599964005bc58cf89c27201973bc1ff51dd (diff)
split assists over several files
Diffstat (limited to 'crates/ra_editor/src/assists/add_impl.rs')
-rw-r--r--crates/ra_editor/src/assists/add_impl.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/crates/ra_editor/src/assists/add_impl.rs b/crates/ra_editor/src/assists/add_impl.rs
new file mode 100644
index 000000000..50e00688e
--- /dev/null
+++ b/crates/ra_editor/src/assists/add_impl.rs
@@ -0,0 +1,78 @@
1use join_to_string::join;
2use ra_text_edit::TextEditBuilder;
3use ra_syntax::{
4 ast::{self, AstNode, NameOwner, TypeParamsOwner},
5 SourceFileNode,
6 TextUnit,
7};
8
9use crate::{find_node_at_offset, assists::LocalEdit};
10
11pub fn add_impl<'a>(
12 file: &'a SourceFileNode,
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()?;
17
18 Some(move || {
19 let type_params = nominal.type_param_list();
20 let mut edit = TextEditBuilder::new();
21 let start_offset = nominal.syntax().range().end();
22 let mut buf = String::new();
23 buf.push_str("\n\nimpl");
24 if let Some(type_params) = type_params {
25 type_params.syntax().text().push_to(&mut buf);
26 }
27 buf.push_str(" ");
28 buf.push_str(name.text().as_str());
29 if let Some(type_params) = type_params {
30 let lifetime_params = type_params
31 .lifetime_params()
32 .filter_map(|it| it.lifetime())
33 .map(|it| it.text());
34 let type_params = type_params
35 .type_params()
36 .filter_map(|it| it.name())
37 .map(|it| it.text());
38 join(lifetime_params.chain(type_params))
39 .surround_with("<", ">")
40 .to_buf(&mut buf);
41 }
42 buf.push_str(" {\n");
43 let offset = start_offset + TextUnit::of_str(&buf);
44 buf.push_str("\n}");
45 edit.insert(start_offset, buf);
46 LocalEdit {
47 label: "add impl".to_string(),
48 edit: edit.finish(),
49 cursor_position: Some(offset),
50 }
51 })
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use crate::test_utils::check_action;
58
59 #[test]
60 fn test_add_impl() {
61 check_action(
62 "struct Foo {<|>}\n",
63 "struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
64 |file, off| add_impl(file, off).map(|f| f()),
65 );
66 check_action(
67 "struct Foo<T: Clone> {<|>}",
68 "struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
69 |file, off| add_impl(file, off).map(|f| f()),
70 );
71 check_action(
72 "struct Foo<'a, T: Foo<'a>> {<|>}",
73 "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 );
76 }
77
78}