Skip to content

feat(assists): Make raw string unescaped #1922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ra_assists/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ join_to_string = "0.1.3"
itertools = "0.8.0"
arrayvec = "0.4.10"
rustc-hash = "1.0.1"
rustc_lexer = "0.1.0"

ra_syntax = { path = "../ra_syntax" }
ra_text_edit = { path = "../ra_text_edit" }
Expand Down
110 changes: 92 additions & 18 deletions crates/ra_assists/src/assists/raw_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use hir::db::HirDatabase;
use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit};
use rustc_lexer;

use crate::{Assist, AssistCtx, AssistId};

Expand All @@ -8,13 +9,51 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
if literal.token().kind() != ra_syntax::SyntaxKind::STRING {
return None;
}
let token = literal.token();
let text = token.text().as_str();
let usual_string_range = find_usual_string_range(text)?;
let start_of_inside = usual_string_range.start().to_usize() + 1;
let end_of_inside = usual_string_range.end().to_usize();
let inside_str = &text[start_of_inside..end_of_inside];
let mut unescaped = String::with_capacity(inside_str.len());
let mut error = Ok(());
rustc_lexer::unescape::unescape_str(
inside_str,
&mut |_, unescaped_char| match unescaped_char {
Ok(c) => unescaped.push(c),
Err(_) => error = Err(()),
},
);
if error.is_err() {
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");
let max_hash_streak = count_hashes(&unescaped);
let mut hashes = String::with_capacity(max_hash_streak + 1);
for _ in 0..hashes.capacity() {
hashes.push('#');
}
edit.replace(
literal.syntax().text_range(),
format!("r{}\"{}\"{}", hashes, unescaped, hashes),
);
});
ctx.build()
}

fn count_hashes(s: &str) -> usize {
let mut max_hash_streak = 0usize;
for idx in s.match_indices("\"#").map(|(i, _)| i) {
let (_, sub) = s.split_at(idx + 1);
let nb_hash = sub.chars().take_while(|c| *c == '#').count();
if nb_hash > max_hash_streak {
max_hash_streak = nb_hash;
}
}
max_hash_streak
}

fn find_usual_string_range(s: &str) -> Option<TextRange> {
Some(TextRange::from_to(
TextUnit::from(s.find('"')? as u32),
Expand Down Expand Up @@ -92,10 +131,10 @@ mod test {
make_raw_string,
r#"
fn f() {
let s = <|>"random string";
let s = <|>"random\nstring";
}
"#,
r#""random string""#,
r#""random\nstring""#,
);
}

Expand All @@ -105,44 +144,69 @@ 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_hashes_inside_works() {
check_assist(
make_raw_string,
r#"
r###"
fn f() {
let s = <|>"random\nstring";
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(
fn make_raw_string_closing_hashes_inside_works() {
check_assist(
make_raw_string,
r###"
fn f() {
let s = <|>"#random\"##\nstring";
}
"###,
r####"
fn f() {
let s = <|>r###"#random"##
string"###;
}
"####,
)
}

#[test]
fn make_raw_string_nothing_to_unescape_works() {
check_assist(
make_raw_string,
r#"
fn f() {
let s = <|>r"random string";
let s = <|>"random string";
}
"#,
);
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
)
}

#[test]
Expand Down Expand Up @@ -367,4 +431,14 @@ mod test {
"#,
);
}

#[test]
fn count_hashes_test() {
assert_eq!(0, count_hashes("abc"));
assert_eq!(0, count_hashes("###"));
assert_eq!(1, count_hashes("\"#abc"));
assert_eq!(0, count_hashes("#abc"));
assert_eq!(2, count_hashes("#ab\"##c"));
assert_eq!(4, count_hashes("#ab\"##\"####c"));
}
}
1 change: 0 additions & 1 deletion crates/ra_assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,4 @@ mod tests {
assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable");
assert_eq!(assists.next().expect("expected assist").0.label, "replace with match");
}

}
7 changes: 4 additions & 3 deletions docs/user/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,18 @@ fn foo<T: u32, F: FnOnce(T) -> T>() {}
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
```

- Make raw string
- Make raw string unescaped

```rust
// before:
fn f() {
let s = <|>"abcd";
let s = <|>"ab\ncd";
}

// after:
fn f() {
let s = <|>r"abcd";
let s = <|>r#"ab
cd"#;
}
```

Expand Down