diff options
author | Igor Aleksanov <[email protected]> | 2020-10-02 11:23:49 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-02 11:23:49 +0100 |
commit | b7ac540f150e74ec7577df08511f977a67cd40e1 (patch) | |
tree | cb97830531db763e949a6c36315efc003fef3d19 /crates/ide/src | |
parent | 2557cb8518a70b0d3b8689be6cb3c8d33342cd0d (diff) |
Use ast::String for extracting string literal contents
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/completion/complete_postfix.rs | 8 | ||||
-rw-r--r-- | crates/ide/src/completion/complete_postfix/format_like.rs | 25 |
2 files changed, 19 insertions, 14 deletions
diff --git a/crates/ide/src/completion/complete_postfix.rs b/crates/ide/src/completion/complete_postfix.rs index e549e0517..db5319618 100644 --- a/crates/ide/src/completion/complete_postfix.rs +++ b/crates/ide/src/completion/complete_postfix.rs | |||
@@ -4,7 +4,7 @@ mod format_like; | |||
4 | 4 | ||
5 | use assists::utils::TryEnum; | 5 | use assists::utils::TryEnum; |
6 | use syntax::{ | 6 | use syntax::{ |
7 | ast::{self, AstNode}, | 7 | ast::{self, AstNode, AstToken}, |
8 | TextRange, TextSize, | 8 | TextRange, TextSize, |
9 | }; | 9 | }; |
10 | use text_edit::TextEdit; | 10 | use text_edit::TextEdit; |
@@ -212,7 +212,11 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | |||
212 | ) | 212 | ) |
213 | .add_to(acc); | 213 | .add_to(acc); |
214 | 214 | ||
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 | } | ||
216 | } | 220 | } |
217 | 221 | ||
218 | fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String { | 222 | fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String { |
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 | }; |
21 | use syntax::ast; | 21 | use syntax::ast::{self, AstToken}; |
22 | 22 | ||
23 | pub(super) fn add_format_like_completions( | 23 | pub(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. |
50 | fn is_string_literal(item: &str) -> bool { | 49 | fn 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, |