Skip to content

Commit c552e5a

Browse files
committed
Auto merge of rust-lang#14004 - lowr:patch/no-need-to-escape-non-snippet, r=Veykril
Don't escape non-snippets in assist I was misunderstanding that we're always sending snippets as response to assist request. For assists that never return snippets like `move_const_to_impl` we don't need to escape, and I don't think `utils::escape_non_snippet()` is useful at the moment since we guarantee that only a single edit will have `InsertTextFormat.Snippet` and we have `utils::render_snippet()` for that.
2 parents 1575d55 + 01d8b89 commit c552e5a

File tree

2 files changed

+2
-59
lines changed

2 files changed

+2
-59
lines changed

crates/ide-assists/src/handlers/move_const_to_impl.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use syntax::{
55
SyntaxKind,
66
};
77

8-
use crate::{
9-
assist_context::{AssistContext, Assists},
10-
utils,
11-
};
8+
use crate::assist_context::{AssistContext, Assists};
129

1310
// NOTE: Code may break if the self type implements a trait that has associated const with the same
1411
// name, but it's pretty expensive to check that (`hir::Impl::all_for_type()`) and we assume that's
@@ -130,9 +127,7 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
130127

131128
let const_ = const_.clone_for_update();
132129
const_.reindent_to(indent);
133-
let mut const_text = format!("\n{indent}{const_}{fixup}");
134-
utils::escape_non_snippet(&mut const_text);
135-
builder.insert(insert_offset, const_text);
130+
builder.insert(insert_offset, format!("\n{indent}{const_}{fixup}"));
136131
},
137132
)
138133
}
@@ -443,39 +438,4 @@ impl S {
443438
"#,
444439
);
445440
}
446-
447-
#[test]
448-
fn moved_const_body_is_escaped() {
449-
// Note that the last argument is what *lsp clients would see* rather than
450-
// what users would see. Unescaping happens thereafter.
451-
check_assist(
452-
move_const_to_impl,
453-
r#"
454-
struct S;
455-
impl S {
456-
fn f() -> usize {
457-
/// doc comment
458-
/// \\
459-
/// ${snippet}
460-
const C$0: &str = "\ and $1";
461-
462-
C.len()
463-
}
464-
}
465-
"#,
466-
r#"
467-
struct S;
468-
impl S {
469-
/// doc comment
470-
/// \\\\
471-
/// \${snippet}
472-
const C: &str = "\\ and \$1";
473-
474-
fn f() -> usize {
475-
Self::C.len()
476-
}
477-
}
478-
"#,
479-
)
480-
}
481441
}

crates/ide-assists/src/utils.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,6 @@ pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor
208208
}
209209
}
210210

211-
/// Escapes text that should be rendered as-is, typically those that we're copy-pasting what the
212-
/// users wrote.
213-
///
214-
/// This function should only be used when the text doesn't contain snippet **AND** the text
215-
/// wouldn't be included in a snippet.
216-
pub(crate) fn escape_non_snippet(text: &mut String) {
217-
// While we *can* escape `}`, we don't really have to in this specific case. We only need to
218-
// escape it inside `${}` to disambiguate it from the ending token of the syntax, but after we
219-
// escape every occurrence of `$`, we wouldn't have `${}` in the first place.
220-
//
221-
// This will break if the text contains snippet or it will be included in a snippet (hence doc
222-
// comment). Compare `fn escape(buf)` in `render_snippet()` above, where the escaped text is
223-
// included in a snippet.
224-
stdx::replace(text, '\\', r"\\");
225-
stdx::replace(text, '$', r"\$");
226-
}
227-
228211
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
229212
node.children_with_tokens()
230213
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))

0 commit comments

Comments
 (0)