Auto merge of #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.
This commit is contained in:
bors 2023-01-23 16:32:53 +00:00
commit c552e5a55f
2 changed files with 2 additions and 59 deletions

View File

@ -5,10 +5,7 @@ use syntax::{
SyntaxKind,
};
use crate::{
assist_context::{AssistContext, Assists},
utils,
};
use crate::assist_context::{AssistContext, Assists};
// NOTE: Code may break if the self type implements a trait that has associated const with the same
// 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<'_>) ->
let const_ = const_.clone_for_update();
const_.reindent_to(indent);
let mut const_text = format!("\n{indent}{const_}{fixup}");
utils::escape_non_snippet(&mut const_text);
builder.insert(insert_offset, const_text);
builder.insert(insert_offset, format!("\n{indent}{const_}{fixup}"));
},
)
}
@ -443,39 +438,4 @@ impl S {
"#,
);
}
#[test]
fn moved_const_body_is_escaped() {
// Note that the last argument is what *lsp clients would see* rather than
// what users would see. Unescaping happens thereafter.
check_assist(
move_const_to_impl,
r#"
struct S;
impl S {
fn f() -> usize {
/// doc comment
/// \\
/// ${snippet}
const C$0: &str = "\ and $1";
C.len()
}
}
"#,
r#"
struct S;
impl S {
/// doc comment
/// \\\\
/// \${snippet}
const C: &str = "\\ and \$1";
fn f() -> usize {
Self::C.len()
}
}
"#,
)
}
}

View File

@ -208,23 +208,6 @@ pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor
}
}
/// Escapes text that should be rendered as-is, typically those that we're copy-pasting what the
/// users wrote.
///
/// This function should only be used when the text doesn't contain snippet **AND** the text
/// wouldn't be included in a snippet.
pub(crate) fn escape_non_snippet(text: &mut String) {
// While we *can* escape `}`, we don't really have to in this specific case. We only need to
// escape it inside `${}` to disambiguate it from the ending token of the syntax, but after we
// escape every occurrence of `$`, we wouldn't have `${}` in the first place.
//
// This will break if the text contains snippet or it will be included in a snippet (hence doc
// comment). Compare `fn escape(buf)` in `render_snippet()` above, where the escaped text is
// included in a snippet.
stdx::replace(text, '\\', r"\\");
stdx::replace(text, '$', r"\$");
}
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))