From e293c34e85d2343cd8f16842634133a2dee4e8ad Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Sun, 29 Sep 2019 19:14:19 +0100 Subject: [PATCH] feat(assists): Keep only one version of make_raw_string --- crates/ra_assists/src/assists/raw_string.rs | 97 +++------------------ crates/ra_assists/src/lib.rs | 1 - docs/user/features.md | 18 +--- 3 files changed, 16 insertions(+), 100 deletions(-) diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index fe396806bc..e57f24e7c8 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,10 +111,10 @@ mod test { make_raw_string, r#" fn f() { - let s = <|>"random string"; + let s = <|>"random\nstring"; } "#, - r#""random string""#, + r#""random\nstring""#, ); } @@ -139,86 +124,32 @@ mod test { make_raw_string, r#" fn f() { - let s = <|>"random string"; + let s = <|>"random\nstring"; } "#, - r#" + r##" fn f() { - let s = <|>r"random string"; + let s = <|>r#"random +string"#; } - "#, + "##, ) } #[test] - fn make_raw_string_with_escaped_works() { + fn make_raw_string_nothing_to_unescape_works() { check_assist( make_raw_string, r#" - fn f() { - let s = <|>"random\nstring"; - } - "#, - r#" - fn f() { - let s = <|>r"random\nstring"; - } - "#, - ) - } - - #[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() { - check_assist( - make_raw_string_unescaped, - r#" - fn f() { - let s = <|>"random\nstring"; - } - "#, - r#" - fn f() { - let s = <|>r"random -string"; - } - "#, - ) - } - - #[test] - fn make_raw_string_unescaped_dont_works() { - check_assist_not_applicable( - make_raw_string_unescaped, - r#" fn f() { let s = <|>"random string"; } "#, + r##" + fn f() { + let s = <|>r#"random string"#; + } + "##, ) } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index d1e2d32514..3ca3320f7a 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, ] diff --git a/docs/user/features.md b/docs/user/features.md index fadd4cdf17..23842f4092 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -445,20 +445,6 @@ fn foo T>() {} fn foo() where T: u32, F: FnOnce(T) -> T {} ``` -- Make raw string - -```rust -// before: -fn f() { - let s = <|>"abcd"; -} - -// after: -fn f() { - let s = <|>r"abcd"; -} -``` - - Make raw string unescaped ```rust @@ -469,8 +455,8 @@ fn f() { // after: fn f() { - let s = <|>r"ab -cd"; + let s = <|>r#"ab +cd"#; } ```