Skip to content

Commit bc38183

Browse files
committed
fix: Preserve $ and \ in postfix format completions
`parse_format_exprs` doesn't escape these two anymore, so they have to be escaped as a separate step.
1 parent 7dadc64 commit bc38183

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

crates/ide-completion/src/completions/postfix.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub(crate) fn complete_postfix(
258258
}
259259

260260
fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
261-
let text = if receiver_is_ambiguous_float_literal {
261+
let mut text = if receiver_is_ambiguous_float_literal {
262262
let text = receiver.syntax().text();
263263
let without_dot = ..text.len() - TextSize::of('.');
264264
text.slice(without_dot).to_string()
@@ -267,12 +267,18 @@ fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal:
267267
};
268268

269269
// The receiver texts should be interpreted as-is, as they are expected to be
270-
// normal Rust expressions. We escape '\' and '$' so they don't get treated as
271-
// snippet-specific constructs.
272-
//
273-
// Note that we don't need to escape the other characters that can be escaped,
274-
// because they wouldn't be treated as snippet-specific constructs without '$'.
275-
text.replace('\\', "\\\\").replace('$', "\\$")
270+
// normal Rust expressions.
271+
escape_snippet_bits(&mut text);
272+
text
273+
}
274+
275+
/// Escapes `\` and `$` so that they don't get interpreted as snippet-specific constructs.
276+
///
277+
/// Note that we don't need to escape the other characters that can be escaped,
278+
/// because they wouldn't be treated as snippet-specific constructs without '$'.
279+
fn escape_snippet_bits(text: &mut String) {
280+
stdx::replace(text, '\\', "\\\\");
281+
stdx::replace(text, '$', "\\$");
276282
}
277283

278284
fn include_references(initial_element: &ast::Expr) -> (ast::Expr, ast::Expr) {

crates/ide-completion/src/completions/postfix/format_like.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
// image::https://user-images.githubusercontent.com/48062697/113020656-b560f500-917a-11eb-87de-02991f61beb8.gif[]
1818

1919
use ide_db::{
20-
syntax_helpers::format_string_exprs::{parse_format_exprs, with_placeholders},
20+
syntax_helpers::format_string_exprs::{parse_format_exprs, with_placeholders, Arg},
2121
SnippetCap,
2222
};
2323
use syntax::{ast, AstToken};
2424

2525
use crate::{
26-
completions::postfix::build_postfix_snippet_builder, context::CompletionContext, Completions,
26+
completions::postfix::{build_postfix_snippet_builder, escape_snippet_bits},
27+
context::CompletionContext,
28+
Completions,
2729
};
2830

2931
/// Mapping ("postfix completion item" => "macro to use")
@@ -51,7 +53,15 @@ pub(crate) fn add_format_like_completions(
5153
None => return,
5254
};
5355

54-
if let Ok((out, exprs)) = parse_format_exprs(receiver_text.text()) {
56+
if let Ok((mut out, mut exprs)) = parse_format_exprs(receiver_text.text()) {
57+
// Escape any snippet bits in the out text and any of the exprs.
58+
escape_snippet_bits(&mut out);
59+
for arg in &mut exprs {
60+
if let Arg::Ident(text) | Arg::Expr(text) = arg {
61+
escape_snippet_bits(text)
62+
}
63+
}
64+
5565
let exprs = with_placeholders(exprs);
5666
for (label, macro_name) in KINDS {
5767
let snippet = if exprs.is_empty() {

0 commit comments

Comments
 (0)