diff options
Diffstat (limited to 'crates/ra_assists/src/utils.rs')
-rw-r--r-- | crates/ra_assists/src/utils.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index f3fc92ebf..8a26a6808 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -1,18 +1,44 @@ | |||
1 | //! Assorted functions shared by several assists. | 1 | //! Assorted functions shared by several assists. |
2 | pub(crate) mod insert_use; | 2 | pub(crate) mod insert_use; |
3 | 3 | ||
4 | use std::iter; | 4 | use std::{iter, ops}; |
5 | 5 | ||
6 | use hir::{Adt, Crate, Semantics, Trait, Type}; | 6 | use hir::{Adt, Crate, Semantics, Trait, Type}; |
7 | use ra_ide_db::RootDatabase; | 7 | use ra_ide_db::RootDatabase; |
8 | use ra_syntax::{ | 8 | use ra_syntax::{ |
9 | ast::{self, make, NameOwner}, | 9 | ast::{self, make, NameOwner}, |
10 | AstNode, T, | 10 | AstNode, SyntaxNode, T, |
11 | }; | 11 | }; |
12 | use rustc_hash::FxHashSet; | 12 | use rustc_hash::FxHashSet; |
13 | 13 | ||
14 | use crate::assist_config::SnippetCap; | ||
15 | |||
14 | pub(crate) use insert_use::insert_use_statement; | 16 | pub(crate) use insert_use::insert_use_statement; |
15 | 17 | ||
18 | pub(crate) fn render_snippet( | ||
19 | _cap: SnippetCap, | ||
20 | node: &SyntaxNode, | ||
21 | placeholder: &SyntaxNode, | ||
22 | ) -> String { | ||
23 | assert!(placeholder.ancestors().any(|it| it == *node)); | ||
24 | let range = placeholder.text_range() - node.text_range().start(); | ||
25 | let range: ops::Range<usize> = range.into(); | ||
26 | |||
27 | let mut placeholder = placeholder.to_string(); | ||
28 | escape(&mut placeholder); | ||
29 | let tab_stop = format!("${{0:{}}}", placeholder); | ||
30 | |||
31 | let mut buf = node.to_string(); | ||
32 | buf.replace_range(range, &tab_stop); | ||
33 | return buf; | ||
34 | |||
35 | fn escape(buf: &mut String) { | ||
36 | stdx::replace(buf, '{', r"\{"); | ||
37 | stdx::replace(buf, '}', r"\}"); | ||
38 | stdx::replace(buf, '$', r"\$"); | ||
39 | } | ||
40 | } | ||
41 | |||
16 | pub fn get_missing_assoc_items( | 42 | pub fn get_missing_assoc_items( |
17 | sema: &Semantics<RootDatabase>, | 43 | sema: &Semantics<RootDatabase>, |
18 | impl_def: &ast::ImplDef, | 44 | impl_def: &ast::ImplDef, |