From e293c34e85d2343cd8f16842634133a2dee4e8ad Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Sun, 29 Sep 2019 19:14:19 +0100 Subject: feat(assists): Keep only one version of make_raw_string --- crates/ra_assists/src/assists/raw_string.rs | 95 ++++------------------------- 1 file changed, 13 insertions(+), 82 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index fe396806b..e57f24e7c 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs @@ -5,27 +5,12 @@ use rustc_lexer; use crate::{Assist, AssistCtx, AssistId}; pub(crate) fn make_raw_string(mut ctx: AssistCtx) -> Option { - let literal = ctx.node_at_offset::()?; - if literal.token().kind() != ra_syntax::SyntaxKind::STRING { - return None; - } - ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { - edit.target(literal.syntax().text_range()); - edit.insert(literal.syntax().text_range().start(), "r"); - }); - ctx.build() -} - -pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx) -> Option { let literal = ctx.node_at_offset::()?; if literal.token().kind() != ra_syntax::SyntaxKind::STRING { return None; } let token = literal.token(); let text = token.text().as_str(); - if !text.contains(&['\\', '\r'][..]) { - return None; - } let usual_string_range = find_usual_string_range(text)?; ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { edit.target(literal.syntax().text_range()); @@ -43,7 +28,7 @@ pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx) -> if error.is_err() { eprintln!("Error unescaping string"); } else { - edit.replace(literal.syntax().text_range(), format!("r\"{}\"", unescaped)); + edit.replace(literal.syntax().text_range(), format!("r#\"{}\"#", unescaped)); } }); ctx.build() @@ -126,32 +111,15 @@ mod test { make_raw_string, r#" fn f() { - let s = <|>"random string"; + let s = <|>"random\nstring"; } "#, - r#""random string""#, + r#""random\nstring""#, ); } #[test] fn make_raw_string_works() { - check_assist( - make_raw_string, - r#" - fn f() { - let s = <|>"random string"; - } - "#, - r#" - fn f() { - let s = <|>r"random string"; - } - "#, - ) - } - - #[test] - fn make_raw_string_with_escaped_works() { check_assist( make_raw_string, r#" @@ -159,66 +127,29 @@ mod test { let s = <|>"random\nstring"; } "#, - r#" + r##" fn f() { - let s = <|>r"random\nstring"; + let s = <|>r#"random +string"#; } - "#, + "##, ) } #[test] - fn make_raw_string_not_works() { - check_assist_not_applicable( - make_raw_string, - r#" - fn f() { - let s = <|>r"random string"; - } - "#, - ); - } - - #[test] - fn make_raw_string_unescaped_target() { - check_assist_target( - make_raw_string_unescaped, - r#" - fn f() { - let s = <|>"random\nstring"; - } - "#, - r#""random\nstring""#, - ); - } - - #[test] - fn make_raw_string_unescaped_works() { + fn make_raw_string_nothing_to_unescape_works() { check_assist( - make_raw_string_unescaped, - r#" - fn f() { - let s = <|>"random\nstring"; - } - "#, + make_raw_string, r#" fn f() { - let s = <|>r"random -string"; + let s = <|>"random string"; } "#, - ) - } - - #[test] - fn make_raw_string_unescaped_dont_works() { - check_assist_not_applicable( - make_raw_string_unescaped, - r#" + r##" fn f() { - let s = <|>"random string"; + let s = <|>r#"random string"#; } - "#, + "##, ) } -- cgit v1.2.3