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 ++++------------------------- crates/ra_assists/src/lib.rs | 1 - 2 files changed, 13 insertions(+), 83 deletions(-) (limited to 'crates') 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"#; } - "#, + "##, ) } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index d1e2d3251..3ca3320f7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -132,7 +132,6 @@ mod assists { move_bounds::move_bounds_to_where_clause, raw_string::add_hash, raw_string::make_raw_string, - raw_string::make_raw_string_unescaped, raw_string::make_usual_string, raw_string::remove_hash, ] -- cgit v1.2.3