aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/utils.rs')
-rw-r--r--crates/ra_assists/src/utils.rs30
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.
2pub(crate) mod insert_use; 2pub(crate) mod insert_use;
3 3
4use std::iter; 4use std::{iter, ops};
5 5
6use hir::{Adt, Crate, Semantics, Trait, Type}; 6use hir::{Adt, Crate, Semantics, Trait, Type};
7use ra_ide_db::RootDatabase; 7use ra_ide_db::RootDatabase;
8use ra_syntax::{ 8use ra_syntax::{
9 ast::{self, make, NameOwner}, 9 ast::{self, make, NameOwner},
10 AstNode, T, 10 AstNode, SyntaxNode, T,
11}; 11};
12use rustc_hash::FxHashSet; 12use rustc_hash::FxHashSet;
13 13
14use crate::assist_config::SnippetCap;
15
14pub(crate) use insert_use::insert_use_statement; 16pub(crate) use insert_use::insert_use_statement;
15 17
18pub(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
16pub fn get_missing_assoc_items( 42pub fn get_missing_assoc_items(
17 sema: &Semantics<RootDatabase>, 43 sema: &Semantics<RootDatabase>,
18 impl_def: &ast::ImplDef, 44 impl_def: &ast::ImplDef,