]> git.lizzy.rs Git - rust.git/commitdiff
Improve format-like completions code appearance
authorIgor Aleksanov <popzxc@yandex.ru>
Fri, 2 Oct 2020 09:49:24 +0000 (12:49 +0300)
committerIgor Aleksanov <popzxc@yandex.ru>
Fri, 2 Oct 2020 09:49:33 +0000 (12:49 +0300)
crates/ide/src/completion/complete_postfix.rs
crates/ide/src/completion/complete_postfix/format_like.rs

index 5990742548bf73c444f7e42ecee70b97473bec44..e549e051761f23ee42e9b10abc7b61a3b09db933 100644 (file)
@@ -1,4 +1,7 @@
 //! FIXME: write short doc here
+
+mod format_like;
+
 use assists::utils::TryEnum;
 use syntax::{
     ast::{self, AstNode},
@@ -16,8 +19,6 @@
     CompletionItem, CompletionItemKind,
 };
 
-mod format_like;
-
 pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
     if !ctx.config.enable_postfix_completions {
         return;
index 0772dbf291cfd3259ed4c5d3b1ac19b599e09fc4..f0ef017d1acb4b1fdf0872a62b2fc6c93b2a06a2 100644 (file)
@@ -1,23 +1,22 @@
-//! Postfix completion for `format`-like strings.
-//!
-//! `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
-//!
-//! The following postfix snippets are available:
-//!
-//! - `format` -> `format!(...)`
-//! - `panic` -> `panic!(...)`
-//! - `println` -> `println!(...)`
-//! - `log`:
-//!   + `logd` -> `log::debug!(...)`
-//!   + `logt` -> `log::trace!(...)`
-//!   + `logi` -> `log::info!(...)`
-//!   + `logw` -> `log::warn!(...)`
-//!   + `loge` -> `log::error!(...)`
+// Feature: Postfix completion for `format`-like strings.
+//
+// `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
+//
+// The following postfix snippets are available:
+//
+// - `format` -> `format!(...)`
+// - `panic` -> `panic!(...)`
+// - `println` -> `println!(...)`
+// - `log`:
+//   + `logd` -> `log::debug!(...)`
+//   + `logt` -> `log::trace!(...)`
+//   + `logi` -> `log::info!(...)`
+//   + `logw` -> `log::warn!(...)`
+//   + `loge` -> `log::error!(...)`
 
-use super::postfix_snippet;
 use crate::completion::{
-    completion_config::SnippetCap, completion_context::CompletionContext,
-    completion_item::Completions,
+    complete_postfix::postfix_snippet, completion_config::SnippetCap,
+    completion_context::CompletionContext, completion_item::Completions,
 };
 use syntax::ast;
 
@@ -35,7 +34,7 @@ pub(super) fn add_format_like_completions(
 
     let input = &receiver_text[1..receiver_text.len() - 1];
 
-    let mut parser = FormatStrParser::new(input);
+    let mut parser = FormatStrParser::new(input.to_owned());
 
     if parser.parse().is_ok() {
         for kind in PostfixKind::all_suggestions() {
@@ -129,7 +128,7 @@ enum State {
 }
 
 impl FormatStrParser {
-    pub fn new(input: impl Into<String>) -> Self {
+    pub fn new(input: String) -> Self {
         Self {
             input: input.into(),
             output: String::new(),
@@ -238,14 +237,8 @@ pub fn parse(&mut self) -> Result<(), ()> {
     pub fn into_suggestion(&self, kind: PostfixKind) -> String {
         assert!(self.parsed, "Attempt to get a suggestion from not parsed expression");
 
-        let mut output = format!(r#"{}("{}""#, kind.into_macro_name(), self.output);
-        for expr in &self.extracted_expressions {
-            output += ", ";
-            output += expr;
-        }
-        output.push(')');
-
-        output
+        let expressions_as_string = self.extracted_expressions.join(", ");
+        format!(r#"{}("{}", {})"#, kind.into_macro_name(), self.output, expressions_as_string)
     }
 }
 
@@ -281,7 +274,7 @@ fn format_str_parser() {
         ];
 
         for (input, output) in test_vector {
-            let mut parser = FormatStrParser::new(*input);
+            let mut parser = FormatStrParser::new((*input).to_owned());
             let outcome = parser.parse();
 
             if let Some((result_str, result_args)) = output {
@@ -316,7 +309,7 @@ fn test_into_suggestion() {
         ];
 
         for (kind, input, output) in test_vector {
-            let mut parser = FormatStrParser::new(*input);
+            let mut parser = FormatStrParser::new((*input).to_owned());
             parser.parse().expect("Parsing must succeed");
 
             assert_eq!(&parser.into_suggestion(*kind), output);