mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
feat(assists): Make raw string unescaped
This commit is contained in:
parent
53a30d9e69
commit
281e107155
5 changed files with 95 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -904,6 +904,7 @@ dependencies = [
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"ra_text_edit 0.1.0",
|
"ra_text_edit 0.1.0",
|
||||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"rustc_lexer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"test_utils 0.1.0",
|
"test_utils 0.1.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ join_to_string = "0.1.3"
|
||||||
itertools = "0.8.0"
|
itertools = "0.8.0"
|
||||||
arrayvec = "0.4.10"
|
arrayvec = "0.4.10"
|
||||||
rustc-hash = "1.0.1"
|
rustc-hash = "1.0.1"
|
||||||
|
rustc_lexer = "0.1.0"
|
||||||
|
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_text_edit = { path = "../ra_text_edit" }
|
ra_text_edit = { path = "../ra_text_edit" }
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use hir::db::HirDatabase;
|
use hir::db::HirDatabase;
|
||||||
use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit};
|
use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit};
|
||||||
|
use rustc_lexer;
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
|
@ -15,6 +16,39 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
||||||
ctx.build()
|
ctx.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn make_raw_string_unescaped(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
|
let literal = ctx.node_at_offset::<Literal>()?;
|
||||||
|
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());
|
||||||
|
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() {
|
||||||
|
eprintln!("Error unescaping string");
|
||||||
|
} else {
|
||||||
|
edit.replace(literal.syntax().text_range(), format!("r\"{}\"", unescaped));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ctx.build()
|
||||||
|
}
|
||||||
|
|
||||||
fn find_usual_string_range(s: &str) -> Option<TextRange> {
|
fn find_usual_string_range(s: &str) -> Option<TextRange> {
|
||||||
Some(TextRange::from_to(
|
Some(TextRange::from_to(
|
||||||
TextUnit::from(s.find('"')? as u32),
|
TextUnit::from(s.find('"')? as u32),
|
||||||
|
@ -145,6 +179,49 @@ mod test {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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";
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_hash_target() {
|
fn add_hash_target() {
|
||||||
check_assist_target(
|
check_assist_target(
|
||||||
|
|
|
@ -132,6 +132,7 @@ 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,
|
||||||
]
|
]
|
||||||
|
@ -340,5 +341,4 @@ mod tests {
|
||||||
assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable");
|
assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable");
|
||||||
assert_eq!(assists.next().expect("expected assist").0.label, "replace with match");
|
assert_eq!(assists.next().expect("expected assist").0.label, "replace with match");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -459,6 +459,21 @@ fn f() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- Make raw string unescaped
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// before:
|
||||||
|
fn f() {
|
||||||
|
let s = <|>"ab\ncd";
|
||||||
|
}
|
||||||
|
|
||||||
|
// after:
|
||||||
|
fn f() {
|
||||||
|
let s = <|>r"ab
|
||||||
|
cd";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
- Make usual string
|
- Make usual string
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|
Loading…
Reference in a new issue