From 8eb3272ad6f774bccb967ee640b72a9a17273e7b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 22:25:07 +0200 Subject: Use snippets in add function --- crates/ra_assists/src/utils.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index f3fc92ebf..bb9749b06 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -1,18 +1,38 @@ //! Assorted functions shared by several assists. pub(crate) mod insert_use; -use std::iter; +use std::{iter, ops}; use hir::{Adt, Crate, Semantics, Trait, Type}; use ra_ide_db::RootDatabase; use ra_syntax::{ ast::{self, make, NameOwner}, - AstNode, T, + AstNode, SyntaxNode, T, }; use rustc_hash::FxHashSet; pub(crate) use insert_use::insert_use_statement; +pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String { + assert!(placeholder.ancestors().any(|it| it == *node)); + let range = placeholder.text_range() - node.text_range().start(); + let range: ops::Range = range.into(); + + let mut placeholder = placeholder.to_string(); + escape(&mut placeholder); + let tab_stop = format!("${{0:{}}}", placeholder); + + let mut buf = node.to_string(); + buf.replace_range(range, &tab_stop); + return buf; + + fn escape(buf: &mut String) { + stdx::replace(buf, '{', r"\{"); + stdx::replace(buf, '}', r"\}"); + stdx::replace(buf, '$', r"\$"); + } +} + pub fn get_missing_assoc_items( sema: &Semantics, impl_def: &ast::ImplDef, -- cgit v1.2.3 From e6fc0bdffb213f6e94c5bb4081e6d175ccbd518f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 23:12:01 +0200 Subject: Moderate cleanup of add_function --- crates/ra_assists/src/utils.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index bb9749b06..8a26a6808 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -11,9 +11,15 @@ use ra_syntax::{ }; use rustc_hash::FxHashSet; +use crate::assist_config::SnippetCap; + pub(crate) use insert_use::insert_use_statement; -pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String { +pub(crate) fn render_snippet( + _cap: SnippetCap, + node: &SyntaxNode, + placeholder: &SyntaxNode, +) -> String { assert!(placeholder.ancestors().any(|it| it == *node)); let range = placeholder.text_range() - node.text_range().start(); let range: ops::Range = range.into(); -- cgit v1.2.3