From 2557cb8518a70b0d3b8689be6cb3c8d33342cd0d Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 2 Oct 2020 12:49:24 +0300 Subject: Improve format-like completions code appearance --- crates/ide/src/completion/complete_postfix.rs | 5 +- .../src/completion/complete_postfix/format_like.rs | 53 ++++++++++------------ 2 files changed, 26 insertions(+), 32 deletions(-) (limited to 'crates') 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 @@ //! FIXME: write short doc here + +mod format_like; + use assists::utils::TryEnum; use syntax::{ ast::{self, AstNode}, @@ -16,8 +19,6 @@ use crate::{ CompletionItem, CompletionItemKind, }; -mod format_like; - pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.config.enable_postfix_completions { 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 @@ -//! 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) -> Self { + pub fn new(input: String) -> Self { Self { input: input.into(), output: String::new(), @@ -238,14 +237,8 @@ impl FormatStrParser { 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 @@ mod tests { ]; 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 @@ mod tests { ]; 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); -- cgit v1.2.3