Skip to content

Commit e447b3a

Browse files
committed
Improve checks for postfix suggestions
1 parent ea32014 commit e447b3a

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

crates/ide/src/completion/complete_postfix.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
211211
)
212212
.add_to(acc);
213213

214-
if ctx.is_string_literal {
215-
add_format_like_completions(acc, ctx, &dot_receiver, cap, &receiver_text);
216-
}
214+
add_format_like_completions(acc, ctx, &dot_receiver, cap, &receiver_text);
217215
}
218216

219217
fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ pub(super) fn add_format_like_completions(
2727
cap: SnippetCap,
2828
receiver_text: &str,
2929
) {
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+
3135
let input = &receiver_text[1..receiver_text.len() - 1];
3236

3337
let mut parser = FormatStrParser::new(input);
@@ -42,6 +46,20 @@ pub(super) fn add_format_like_completions(
4246
}
4347
}
4448

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.
4563
#[derive(Debug)]
4664
pub struct FormatStrParser {
4765
input: String,
@@ -127,7 +145,7 @@ impl FormatStrParser {
127145
pub fn parse(&mut self) -> Result<(), ()> {
128146
let mut current_expr = String::new();
129147

130-
let mut placeholders_count = 0;
148+
let mut placeholder_id = 1;
131149

132150
// Count of open braces inside of an expression.
133151
// 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 {
163181
(State::MaybeExpr, '}') => {
164182
// This is an empty sequence '{}'. Replace it with placeholder.
165183
self.output.push(chr);
166-
self.extracted_expressions.push(format!("${}", placeholders_count));
167-
placeholders_count += 1;
184+
self.extracted_expressions.push(format!("${}", placeholder_id));
185+
placeholder_id += 1;
168186
self.state = State::NotExpr;
169187
}
170188
(State::MaybeExpr, _) => {

crates/ide/src/completion/completion_context.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ pub(crate) struct CompletionContext<'a> {
7474
pub(super) is_pattern_call: bool,
7575
/// If this is a macro call, i.e. the () are already there.
7676
pub(super) is_macro_call: bool,
77-
/// If this is a string literal, like "lorem ipsum".
78-
pub(super) is_string_literal: bool,
7977
pub(super) is_path_type: bool,
8078
pub(super) has_type_args: bool,
8179
pub(super) attribute_under_caret: Option<ast::Attr>,
@@ -158,7 +156,6 @@ impl<'a> CompletionContext<'a> {
158156
is_call: false,
159157
is_pattern_call: false,
160158
is_macro_call: false,
161-
is_string_literal: false,
162159
is_path_type: false,
163160
has_type_args: false,
164161
dot_receiver_is_ambiguous_float_literal: false,
@@ -473,12 +470,6 @@ impl<'a> CompletionContext<'a> {
473470
} else {
474471
false
475472
};
476-
477-
self.is_string_literal = if let Some(ast::Expr::Literal(l)) = &self.dot_receiver {
478-
matches!(l.kind(), ast::LiteralKind::String { .. })
479-
} else {
480-
false
481-
};
482473
}
483474
if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) {
484475
// As above

0 commit comments

Comments
 (0)