diff options
author | Igor Aleksanov <[email protected]> | 2020-09-12 15:32:19 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-02 10:42:39 +0100 |
commit | e447b3a4a2bf089e5e3a190a532c17a4572ea013 (patch) | |
tree | cf5a0b06d5fffb5e95271cd6c818928ff2494b20 /crates/ide/src/completion/complete_postfix/format_like.rs | |
parent | ea320141c6f87383880878b91182355c9ad7dc7b (diff) |
Improve checks for postfix suggestions
Diffstat (limited to 'crates/ide/src/completion/complete_postfix/format_like.rs')
-rw-r--r-- | crates/ide/src/completion/complete_postfix/format_like.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/crates/ide/src/completion/complete_postfix/format_like.rs b/crates/ide/src/completion/complete_postfix/format_like.rs index 93211a35f..6be3c2c92 100644 --- a/crates/ide/src/completion/complete_postfix/format_like.rs +++ b/crates/ide/src/completion/complete_postfix/format_like.rs | |||
@@ -27,7 +27,11 @@ pub(super) fn add_format_like_completions( | |||
27 | cap: SnippetCap, | 27 | cap: SnippetCap, |
28 | receiver_text: &str, | 28 | receiver_text: &str, |
29 | ) { | 29 | ) { |
30 | assert!(receiver_text.len() >= 2); | 30 | if !is_string_literal(receiver_text) { |
31 | // It's not a string literal, do not parse input. | ||
32 | return; | ||
33 | } | ||
34 | |||
31 | let input = &receiver_text[1..receiver_text.len() - 1]; | 35 | let input = &receiver_text[1..receiver_text.len() - 1]; |
32 | 36 | ||
33 | let mut parser = FormatStrParser::new(input); | 37 | let mut parser = FormatStrParser::new(input); |
@@ -42,6 +46,20 @@ pub(super) fn add_format_like_completions( | |||
42 | } | 46 | } |
43 | } | 47 | } |
44 | 48 | ||
49 | /// Checks whether provided item is a string literal. | ||
50 | fn is_string_literal(item: &str) -> bool { | ||
51 | if item.len() < 2 { | ||
52 | return false; | ||
53 | } | ||
54 | if item.chars().nth(0) != Some('"') || item.chars().nth(item.len() - 1) != Some('"') { | ||
55 | return false; | ||
56 | } | ||
57 | |||
58 | true | ||
59 | } | ||
60 | |||
61 | /// Parser for a format-like string. It is more allowing in terms of string contents, | ||
62 | /// as we expect variable placeholders to be filled with expressions. | ||
45 | #[derive(Debug)] | 63 | #[derive(Debug)] |
46 | pub struct FormatStrParser { | 64 | pub struct FormatStrParser { |
47 | input: String, | 65 | input: String, |
@@ -127,7 +145,7 @@ impl FormatStrParser { | |||
127 | pub fn parse(&mut self) -> Result<(), ()> { | 145 | pub fn parse(&mut self) -> Result<(), ()> { |
128 | let mut current_expr = String::new(); | 146 | let mut current_expr = String::new(); |
129 | 147 | ||
130 | let mut placeholders_count = 0; | 148 | let mut placeholder_id = 1; |
131 | 149 | ||
132 | // Count of open braces inside of an expression. | 150 | // Count of open braces inside of an expression. |
133 | // We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g. | 151 | // We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g. |
@@ -163,8 +181,8 @@ impl FormatStrParser { | |||
163 | (State::MaybeExpr, '}') => { | 181 | (State::MaybeExpr, '}') => { |
164 | // This is an empty sequence '{}'. Replace it with placeholder. | 182 | // This is an empty sequence '{}'. Replace it with placeholder. |
165 | self.output.push(chr); | 183 | self.output.push(chr); |
166 | self.extracted_expressions.push(format!("${}", placeholders_count)); | 184 | self.extracted_expressions.push(format!("${}", placeholder_id)); |
167 | placeholders_count += 1; | 185 | placeholder_id += 1; |
168 | self.state = State::NotExpr; | 186 | self.state = State::NotExpr; |
169 | } | 187 | } |
170 | (State::MaybeExpr, _) => { | 188 | (State::MaybeExpr, _) => { |