aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/complete_postfix/format_like.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/complete_postfix/format_like.rs')
-rw-r--r--crates/ide/src/completion/complete_postfix/format_like.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/crates/ide/src/completion/complete_postfix/format_like.rs b/crates/ide/src/completion/complete_postfix/format_like.rs
index f0ef017d1..53cb3af39 100644
--- a/crates/ide/src/completion/complete_postfix/format_like.rs
+++ b/crates/ide/src/completion/complete_postfix/format_like.rs
@@ -18,23 +18,22 @@ use crate::completion::{
18 complete_postfix::postfix_snippet, completion_config::SnippetCap, 18 complete_postfix::postfix_snippet, completion_config::SnippetCap,
19 completion_context::CompletionContext, completion_item::Completions, 19 completion_context::CompletionContext, completion_item::Completions,
20}; 20};
21use syntax::ast; 21use syntax::ast::{self, AstToken};
22 22
23pub(super) fn add_format_like_completions( 23pub(super) fn add_format_like_completions(
24 acc: &mut Completions, 24 acc: &mut Completions,
25 ctx: &CompletionContext, 25 ctx: &CompletionContext,
26 dot_receiver: &ast::Expr, 26 dot_receiver: &ast::Expr,
27 cap: SnippetCap, 27 cap: SnippetCap,
28 receiver_text: &str, 28 receiver_text: &ast::String,
29) { 29) {
30 if !is_string_literal(receiver_text) { 30 let input = match string_literal_contents(receiver_text) {
31 // It's not a string literal, do not parse input. 31 // It's not a string literal, do not parse input.
32 return; 32 Some(input) => input,
33 } 33 None => return,
34 34 };
35 let input = &receiver_text[1..receiver_text.len() - 1];
36 35
37 let mut parser = FormatStrParser::new(input.to_owned()); 36 let mut parser = FormatStrParser::new(input);
38 37
39 if parser.parse().is_ok() { 38 if parser.parse().is_ok() {
40 for kind in PostfixKind::all_suggestions() { 39 for kind in PostfixKind::all_suggestions() {
@@ -47,11 +46,13 @@ pub(super) fn add_format_like_completions(
47} 46}
48 47
49/// Checks whether provided item is a string literal. 48/// Checks whether provided item is a string literal.
50fn is_string_literal(item: &str) -> bool { 49fn string_literal_contents(item: &ast::String) -> Option<String> {
51 if item.len() < 2 { 50 let item = item.text();
52 return false; 51 if item.len() >= 2 && item.starts_with("\"") && item.ends_with("\"") {
52 return Some(item[1..item.len() - 1].to_owned());
53 } 53 }
54 item.starts_with("\"") && item.ends_with("\"") 54
55 None
55} 56}
56 57
57/// Parser for a format-like string. It is more allowing in terms of string contents, 58/// Parser for a format-like string. It is more allowing in terms of string contents,