feat(assists): Keep only one version of make_raw_string

This commit is contained in:
Geobert Quach 2019-09-29 19:14:19 +01:00
parent 281e107155
commit e293c34e85
3 changed files with 16 additions and 100 deletions

View file

@ -5,27 +5,12 @@ use rustc_lexer;
use crate::{Assist, AssistCtx, AssistId}; use crate::{Assist, AssistCtx, AssistId};
pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let literal = ctx.node_at_offset::<Literal>()?;
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<impl HirDatabase>) -> Option<Assist> {
let literal = ctx.node_at_offset::<Literal>()?; let literal = ctx.node_at_offset::<Literal>()?;
if literal.token().kind() != ra_syntax::SyntaxKind::STRING { if literal.token().kind() != ra_syntax::SyntaxKind::STRING {
return None; return None;
} }
let token = literal.token(); let token = literal.token();
let text = token.text().as_str(); let text = token.text().as_str();
if !text.contains(&['\\', '\r'][..]) {
return None;
}
let usual_string_range = find_usual_string_range(text)?; let usual_string_range = find_usual_string_range(text)?;
ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| {
edit.target(literal.syntax().text_range()); edit.target(literal.syntax().text_range());
@ -43,7 +28,7 @@ pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx<impl HirDatabase>) ->
if error.is_err() { if error.is_err() {
eprintln!("Error unescaping string"); eprintln!("Error unescaping string");
} else { } else {
edit.replace(literal.syntax().text_range(), format!("r\"{}\"", unescaped)); edit.replace(literal.syntax().text_range(), format!("r#\"{}\"#", unescaped));
} }
}); });
ctx.build() ctx.build()
@ -126,10 +111,10 @@ mod test {
make_raw_string, make_raw_string,
r#" r#"
fn f() { 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, make_raw_string,
r#" r#"
fn f() { fn f() {
let s = <|>"random string"; let s = <|>"random\nstring";
} }
"#, "#,
r#" r##"
fn f() { fn f() {
let s = <|>r"random string"; let s = <|>r#"random
string"#;
} }
"#, "##,
) )
} }
#[test] #[test]
fn make_raw_string_with_escaped_works() { fn make_raw_string_nothing_to_unescape_works() {
check_assist( check_assist(
make_raw_string, make_raw_string,
r#" 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() { fn f() {
let s = <|>"random string"; let s = <|>"random string";
} }
"#, "#,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
) )
} }

View file

@ -132,7 +132,6 @@ mod assists {
move_bounds::move_bounds_to_where_clause, move_bounds::move_bounds_to_where_clause,
raw_string::add_hash, raw_string::add_hash,
raw_string::make_raw_string, raw_string::make_raw_string,
raw_string::make_raw_string_unescaped,
raw_string::make_usual_string, raw_string::make_usual_string,
raw_string::remove_hash, raw_string::remove_hash,
] ]

View file

@ -445,20 +445,6 @@ fn foo<T: u32, F: FnOnce(T) -> T>() {}
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {} fn foo<T, F>() 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 - Make raw string unescaped
```rust ```rust
@ -469,8 +455,8 @@ fn f() {
// after: // after:
fn f() { fn f() {
let s = <|>r"ab let s = <|>r#"ab
cd"; cd"#;
} }
``` ```