Skip to content

Commit 2557cb8

Browse files
committed
Improve format-like completions code appearance
1 parent 777ccb5 commit 2557cb8

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

crates/ide/src/completion/complete_postfix.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! FIXME: write short doc here
2+
3+
mod format_like;
4+
25
use assists::utils::TryEnum;
36
use syntax::{
47
ast::{self, AstNode},
@@ -16,8 +19,6 @@ use crate::{
1619
CompletionItem, CompletionItemKind,
1720
};
1821

19-
mod format_like;
20-
2122
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
2223
if !ctx.config.enable_postfix_completions {
2324
return;

crates/ide/src/completion/complete_postfix/format_like.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
//! Postfix completion for `format`-like strings.
2-
//!
3-
//! `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
4-
//!
5-
//! The following postfix snippets are available:
6-
//!
7-
//! - `format` -> `format!(...)`
8-
//! - `panic` -> `panic!(...)`
9-
//! - `println` -> `println!(...)`
10-
//! - `log`:
11-
//! + `logd` -> `log::debug!(...)`
12-
//! + `logt` -> `log::trace!(...)`
13-
//! + `logi` -> `log::info!(...)`
14-
//! + `logw` -> `log::warn!(...)`
15-
//! + `loge` -> `log::error!(...)`
1+
// Feature: Postfix completion for `format`-like strings.
2+
//
3+
// `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
4+
//
5+
// The following postfix snippets are available:
6+
//
7+
// - `format` -> `format!(...)`
8+
// - `panic` -> `panic!(...)`
9+
// - `println` -> `println!(...)`
10+
// - `log`:
11+
// + `logd` -> `log::debug!(...)`
12+
// + `logt` -> `log::trace!(...)`
13+
// + `logi` -> `log::info!(...)`
14+
// + `logw` -> `log::warn!(...)`
15+
// + `loge` -> `log::error!(...)`
1616

17-
use super::postfix_snippet;
1817
use crate::completion::{
19-
completion_config::SnippetCap, completion_context::CompletionContext,
20-
completion_item::Completions,
18+
complete_postfix::postfix_snippet, completion_config::SnippetCap,
19+
completion_context::CompletionContext, completion_item::Completions,
2120
};
2221
use syntax::ast;
2322

@@ -35,7 +34,7 @@ pub(super) fn add_format_like_completions(
3534

3635
let input = &receiver_text[1..receiver_text.len() - 1];
3736

38-
let mut parser = FormatStrParser::new(input);
37+
let mut parser = FormatStrParser::new(input.to_owned());
3938

4039
if parser.parse().is_ok() {
4140
for kind in PostfixKind::all_suggestions() {
@@ -129,7 +128,7 @@ enum State {
129128
}
130129

131130
impl FormatStrParser {
132-
pub fn new(input: impl Into<String>) -> Self {
131+
pub fn new(input: String) -> Self {
133132
Self {
134133
input: input.into(),
135134
output: String::new(),
@@ -238,14 +237,8 @@ impl FormatStrParser {
238237
pub fn into_suggestion(&self, kind: PostfixKind) -> String {
239238
assert!(self.parsed, "Attempt to get a suggestion from not parsed expression");
240239

241-
let mut output = format!(r#"{}("{}""#, kind.into_macro_name(), self.output);
242-
for expr in &self.extracted_expressions {
243-
output += ", ";
244-
output += expr;
245-
}
246-
output.push(')');
247-
248-
output
240+
let expressions_as_string = self.extracted_expressions.join(", ");
241+
format!(r#"{}("{}", {})"#, kind.into_macro_name(), self.output, expressions_as_string)
249242
}
250243
}
251244

@@ -281,7 +274,7 @@ mod tests {
281274
];
282275

283276
for (input, output) in test_vector {
284-
let mut parser = FormatStrParser::new(*input);
277+
let mut parser = FormatStrParser::new((*input).to_owned());
285278
let outcome = parser.parse();
286279

287280
if let Some((result_str, result_args)) = output {
@@ -316,7 +309,7 @@ mod tests {
316309
];
317310

318311
for (kind, input, output) in test_vector {
319-
let mut parser = FormatStrParser::new(*input);
312+
let mut parser = FormatStrParser::new((*input).to_owned());
320313
parser.parse().expect("Parsing must succeed");
321314

322315
assert_eq!(&parser.into_suggestion(*kind), output);

0 commit comments

Comments
 (0)