diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-20 00:54:08 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-20 00:54:08 +0100 |
commit | b26dbf80090ccdd753b35d6254246229e9ff8728 (patch) | |
tree | b54b03a1220f6977d77ba5e27a82c10c5e386eb0 /crates/ra_assists/src/utils.rs | |
parent | a36202390caff0ed97ff778cf4556fc00ddee70d (diff) | |
parent | a04cababaa144d7a6db7b1dd114494b33d281ab9 (diff) |
Merge #4524
4524: Use snippets in add_missing_members r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/utils.rs')
-rw-r--r-- | crates/ra_assists/src/utils.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 8a26a6808..9af27180b 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -15,18 +15,31 @@ use crate::assist_config::SnippetCap; | |||
15 | 15 | ||
16 | pub(crate) use insert_use::insert_use_statement; | 16 | pub(crate) use insert_use::insert_use_statement; |
17 | 17 | ||
18 | pub(crate) fn render_snippet( | 18 | #[derive(Clone, Copy, Debug)] |
19 | _cap: SnippetCap, | 19 | pub(crate) enum Cursor<'a> { |
20 | node: &SyntaxNode, | 20 | Replace(&'a SyntaxNode), |
21 | placeholder: &SyntaxNode, | 21 | Before(&'a SyntaxNode), |
22 | ) -> String { | 22 | } |
23 | assert!(placeholder.ancestors().any(|it| it == *node)); | 23 | |
24 | let range = placeholder.text_range() - node.text_range().start(); | 24 | impl<'a> Cursor<'a> { |
25 | fn node(self) -> &'a SyntaxNode { | ||
26 | match self { | ||
27 | Cursor::Replace(node) | Cursor::Before(node) => node, | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor) -> String { | ||
33 | assert!(cursor.node().ancestors().any(|it| it == *node)); | ||
34 | let range = cursor.node().text_range() - node.text_range().start(); | ||
25 | let range: ops::Range<usize> = range.into(); | 35 | let range: ops::Range<usize> = range.into(); |
26 | 36 | ||
27 | let mut placeholder = placeholder.to_string(); | 37 | let mut placeholder = cursor.node().to_string(); |
28 | escape(&mut placeholder); | 38 | escape(&mut placeholder); |
29 | let tab_stop = format!("${{0:{}}}", placeholder); | 39 | let tab_stop = match cursor { |
40 | Cursor::Replace(placeholder) => format!("${{0:{}}}", placeholder), | ||
41 | Cursor::Before(placeholder) => format!("$0{}", placeholder), | ||
42 | }; | ||
30 | 43 | ||
31 | let mut buf = node.to_string(); | 44 | let mut buf = node.to_string(); |
32 | buf.replace_range(range, &tab_stop); | 45 | buf.replace_range(range, &tab_stop); |