Skip to content

Commit b7ac540

Browse files
committed
Use ast::String for extracting string literal contents
1 parent 2557cb8 commit b7ac540

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

crates/ide/src/completion/complete_postfix.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod format_like;
44

55
use assists::utils::TryEnum;
66
use syntax::{
7-
ast::{self, AstNode},
7+
ast::{self, AstNode, AstToken},
88
TextRange, TextSize,
99
};
1010
use text_edit::TextEdit;
@@ -212,7 +212,11 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
212212
)
213213
.add_to(acc);
214214

215-
add_format_like_completions(acc, ctx, &dot_receiver, cap, &receiver_text);
215+
if let ast::Expr::Literal(literal) = dot_receiver.clone() {
216+
if let Some(literal_text) = ast::String::cast(literal.token()) {
217+
add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
218+
}
219+
}
216220
}
217221

218222
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: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@ use crate::completion::{
1818
complete_postfix::postfix_snippet, completion_config::SnippetCap,
1919
completion_context::CompletionContext, completion_item::Completions,
2020
};
21-
use syntax::ast;
21+
use syntax::ast::{self, AstToken};
2222

2323
pub(super) fn add_format_like_completions(
2424
acc: &mut Completions,
2525
ctx: &CompletionContext,
2626
dot_receiver: &ast::Expr,
2727
cap: SnippetCap,
28-
receiver_text: &str,
28+
receiver_text: &ast::String,
2929
) {
30-
if !is_string_literal(receiver_text) {
30+
let input = match string_literal_contents(receiver_text) {
3131
// It's not a string literal, do not parse input.
32-
return;
33-
}
34-
35-
let input = &receiver_text[1..receiver_text.len() - 1];
32+
Some(input) => input,
33+
None => return,
34+
};
3635

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

3938
if parser.parse().is_ok() {
4039
for kind in PostfixKind::all_suggestions() {
@@ -47,11 +46,13 @@ pub(super) fn add_format_like_completions(
4746
}
4847

4948
/// Checks whether provided item is a string literal.
50-
fn is_string_literal(item: &str) -> bool {
51-
if item.len() < 2 {
52-
return false;
49+
fn string_literal_contents(item: &ast::String) -> Option<String> {
50+
let item = item.text();
51+
if item.len() >= 2 && item.starts_with("\"") && item.ends_with("\"") {
52+
return Some(item[1..item.len() - 1].to_owned());
5353
}
54-
item.starts_with("\"") && item.ends_with("\"")
54+
55+
None
5556
}
5657

5758
/// Parser for a format-like string. It is more allowing in terms of string contents,

0 commit comments

Comments
 (0)