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!(...)`
16
16
17
- use super :: postfix_snippet;
18
17
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 ,
21
20
} ;
22
21
use syntax:: ast;
23
22
@@ -35,7 +34,7 @@ pub(super) fn add_format_like_completions(
35
34
36
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
39
if parser. parse ( ) . is_ok ( ) {
41
40
for kind in PostfixKind :: all_suggestions ( ) {
@@ -129,7 +128,7 @@ enum State {
129
128
}
130
129
131
130
impl FormatStrParser {
132
- pub fn new ( input : impl Into < String > ) -> Self {
131
+ pub fn new ( input : String ) -> Self {
133
132
Self {
134
133
input : input. into ( ) ,
135
134
output : String :: new ( ) ,
@@ -238,14 +237,8 @@ impl FormatStrParser {
238
237
pub fn into_suggestion ( & self , kind : PostfixKind ) -> String {
239
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) ;
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)
249
242
}
250
243
}
251
244
@@ -281,7 +274,7 @@ mod tests {
281
274
] ;
282
275
283
276
for ( input, output) in test_vector {
284
- let mut parser = FormatStrParser :: new ( * input) ;
277
+ let mut parser = FormatStrParser :: new ( ( * input) . to_owned ( ) ) ;
285
278
let outcome = parser. parse ( ) ;
286
279
287
280
if let Some ( ( result_str, result_args) ) = output {
@@ -316,7 +309,7 @@ mod tests {
316
309
] ;
317
310
318
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
313
parser. parse ( ) . expect ( "Parsing must succeed" ) ;
321
314
322
315
assert_eq ! ( & parser. into_suggestion( * kind) , output) ;
0 commit comments