diff options
author | Igor Aleksanov <[email protected]> | 2020-10-02 10:49:24 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-02 10:49:33 +0100 |
commit | 2557cb8518a70b0d3b8689be6cb3c8d33342cd0d (patch) | |
tree | 77ab376be14572be7ec57720aae0eff0d31cb651 | |
parent | 777ccb58f06d10e0bbcdaa5512e73a0f3554ddcc (diff) |
Improve format-like completions code appearance
-rw-r--r-- | crates/ide/src/completion/complete_postfix.rs | 5 | ||||
-rw-r--r-- | crates/ide/src/completion/complete_postfix/format_like.rs | 53 |
2 files changed, 26 insertions, 32 deletions
diff --git a/crates/ide/src/completion/complete_postfix.rs b/crates/ide/src/completion/complete_postfix.rs index 599074254..e549e0517 100644 --- a/crates/ide/src/completion/complete_postfix.rs +++ b/crates/ide/src/completion/complete_postfix.rs | |||
@@ -1,4 +1,7 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | |||
3 | mod format_like; | ||
4 | |||
2 | use assists::utils::TryEnum; | 5 | use assists::utils::TryEnum; |
3 | use syntax::{ | 6 | use syntax::{ |
4 | ast::{self, AstNode}, | 7 | ast::{self, AstNode}, |
@@ -16,8 +19,6 @@ use crate::{ | |||
16 | CompletionItem, CompletionItemKind, | 19 | CompletionItem, CompletionItemKind, |
17 | }; | 20 | }; |
18 | 21 | ||
19 | mod format_like; | ||
20 | |||
21 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | 22 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { |
22 | if !ctx.config.enable_postfix_completions { | 23 | if !ctx.config.enable_postfix_completions { |
23 | return; | 24 | return; |
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 | ||
17 | use super::postfix_snippet; | ||
18 | use crate::completion::{ | 17 | use 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 | }; |
22 | use syntax::ast; | 21 | use 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 | ||
131 | impl FormatStrParser { | 130 | impl 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); |