aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/complete_postfix
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/complete_postfix')
-rw-r--r--crates/ide/src/completion/complete_postfix/format_like.rs53
1 files changed, 23 insertions, 30 deletions
diff --git a/crates/ide/src/completion/complete_postfix/format_like.rs b/crates/ide/src/completion/complete_postfix/format_like.rs
index 0772dbf29..f0ef017d1 100644
--- a/crates/ide/src/completion/complete_postfix/format_like.rs
+++ b/crates/ide/src/completion/complete_postfix/format_like.rs
@@ -1,23 +1,22 @@
1//! Postfix completion for `format`-like strings. 1// Feature: Postfix completion for `format`-like strings.
2//! 2//
3//! `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`. 3// `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
4//! 4//
5//! The following postfix snippets are available: 5// The following postfix snippets are available:
6//! 6//
7//! - `format` -> `format!(...)` 7// - `format` -> `format!(...)`
8//! - `panic` -> `panic!(...)` 8// - `panic` -> `panic!(...)`
9//! - `println` -> `println!(...)` 9// - `println` -> `println!(...)`
10//! - `log`: 10// - `log`:
11//! + `logd` -> `log::debug!(...)` 11// + `logd` -> `log::debug!(...)`
12//! + `logt` -> `log::trace!(...)` 12// + `logt` -> `log::trace!(...)`
13//! + `logi` -> `log::info!(...)` 13// + `logi` -> `log::info!(...)`
14//! + `logw` -> `log::warn!(...)` 14// + `logw` -> `log::warn!(...)`
15//! + `loge` -> `log::error!(...)` 15// + `loge` -> `log::error!(...)`
16 16
17use super::postfix_snippet;
18use crate::completion::{ 17use crate::completion::{
19 completion_config::SnippetCap, completion_context::CompletionContext, 18 complete_postfix::postfix_snippet, completion_config::SnippetCap,
20 completion_item::Completions, 19 completion_context::CompletionContext, completion_item::Completions,
21}; 20};
22use syntax::ast; 21use syntax::ast;
23 22
@@ -35,7 +34,7 @@ pub(super) fn add_format_like_completions(
35 34
36 let input = &receiver_text[1..receiver_text.len() - 1]; 35 let input = &receiver_text[1..receiver_text.len() - 1];
37 36
38 let mut parser = FormatStrParser::new(input); 37 let mut parser = FormatStrParser::new(input.to_owned());
39 38
40 if parser.parse().is_ok() { 39 if parser.parse().is_ok() {
41 for kind in PostfixKind::all_suggestions() { 40 for kind in PostfixKind::all_suggestions() {
@@ -129,7 +128,7 @@ enum State {
129} 128}
130 129
131impl FormatStrParser { 130impl FormatStrParser {
132 pub fn new(input: impl Into<String>) -> Self { 131 pub fn new(input: String) -> Self {
133 Self { 132 Self {
134 input: input.into(), 133 input: input.into(),
135 output: String::new(), 134 output: String::new(),
@@ -238,14 +237,8 @@ impl FormatStrParser {
238 pub fn into_suggestion(&self, kind: PostfixKind) -> String { 237 pub fn into_suggestion(&self, kind: PostfixKind) -> String {
239 assert!(self.parsed, "Attempt to get a suggestion from not parsed expression"); 238 assert!(self.parsed, "Attempt to get a suggestion from not parsed expression");
240 239
241 let mut output = format!(r#"{}("{}""#, kind.into_macro_name(), self.output); 240 let expressions_as_string = self.extracted_expressions.join(", ");
242 for expr in &self.extracted_expressions { 241 format!(r#"{}("{}", {})"#, kind.into_macro_name(), self.output, expressions_as_string)
243 output += ", ";
244 output += expr;
245 }
246 output.push(')');
247
248 output
249 } 242 }
250} 243}
251 244
@@ -281,7 +274,7 @@ mod tests {
281 ]; 274 ];
282 275
283 for (input, output) in test_vector { 276 for (input, output) in test_vector {
284 let mut parser = FormatStrParser::new(*input); 277 let mut parser = FormatStrParser::new((*input).to_owned());
285 let outcome = parser.parse(); 278 let outcome = parser.parse();
286 279
287 if let Some((result_str, result_args)) = output { 280 if let Some((result_str, result_args)) = output {
@@ -316,7 +309,7 @@ mod tests {
316 ]; 309 ];
317 310
318 for (kind, input, output) in test_vector { 311 for (kind, input, output) in test_vector {
319 let mut parser = FormatStrParser::new(*input); 312 let mut parser = FormatStrParser::new((*input).to_owned());
320 parser.parse().expect("Parsing must succeed"); 313 parser.parse().expect("Parsing must succeed");
321 314
322 assert_eq!(&parser.into_suggestion(*kind), output); 315 assert_eq!(&parser.into_suggestion(*kind), output);