feat(assists): raw string <-> usual string manipulation

Fixes #1730
This commit is contained in:
Geobert Quach 2019-09-15 18:51:34 +01:00
parent 58dc3b1649
commit b6d55290a1
3 changed files with 387 additions and 0 deletions

View File

@ -96,6 +96,7 @@ pub fn assists<H>(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction)>
mod merge_match_arms;
mod introduce_variable;
mod inline_local_variable;
mod raw_string;
mod replace_if_let_with_match;
mod split_import;
mod remove_dbg;
@ -125,6 +126,10 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
move_guard::move_guard_to_arm_body,
move_guard::move_arm_cond_to_match_guard,
move_bounds::move_bounds_to_where_clause,
raw_string::add_hash,
raw_string::make_raw_string,
raw_string::make_usual_string,
raw_string::remove_hash,
]
}

View File

@ -0,0 +1,326 @@
use hir::db::HirDatabase;
use ra_syntax::{ast::AstNode, ast::Literal, TextRange, TextUnit};
use crate::{Assist, AssistCtx, AssistId};
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 {
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()
} else {
None
}
}
pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let literal = ctx.node_at_offset::<Literal>()?;
if literal.token().kind() == ra_syntax::SyntaxKind::RAW_STRING {
ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| {
let text = literal.syntax().text();
let usual_start_pos = text.find_char('"').unwrap(); // we have a RAW_STRING
let end = literal.syntax().text_range().end();
dbg!(&end);
let mut i = 0;
let mut pos = 0;
let mut c = text.char_at(end - TextUnit::from(i));
while c != Some('"') {
if c != None {
pos += 1;
}
i += 1;
c = text.char_at(end - TextUnit::from(i));
}
edit.target(literal.syntax().text_range());
edit.delete(TextRange::from_to(
literal.syntax().text_range().start(),
literal.syntax().text_range().start() + usual_start_pos,
));
edit.delete(TextRange::from_to(
literal.syntax().text_range().end() - TextUnit::from(pos),
literal.syntax().text_range().end(),
));
});
ctx.build()
} else {
None
}
}
pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let literal = ctx.node_at_offset::<Literal>()?;
if literal.token().kind() == ra_syntax::SyntaxKind::RAW_STRING {
ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| {
edit.target(literal.syntax().text_range());
edit.insert(literal.syntax().text_range().start() + TextUnit::from(1), "#");
edit.insert(literal.syntax().text_range().end(), "#");
});
ctx.build()
} else {
None
}
}
pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let literal = ctx.node_at_offset::<Literal>()?;
if literal.token().kind() == ra_syntax::SyntaxKind::RAW_STRING {
if !literal.syntax().text().contains_char('#') {
return None;
}
ctx.add_action(AssistId("remove_hash"), "remove hash from raw string", |edit| {
edit.target(literal.syntax().text_range());
edit.delete(TextRange::from_to(
literal.syntax().text_range().start() + TextUnit::from(1),
literal.syntax().text_range().start() + TextUnit::from(2),
));
edit.delete(TextRange::from_to(
literal.syntax().text_range().end() - TextUnit::from(1),
literal.syntax().text_range().end(),
));
});
ctx.build()
} else {
None
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
#[test]
fn make_raw_string_target() {
check_assist_target(
make_raw_string,
r#"
fn f() {
let s = <|>"random string";
}
"#,
r#""random string""#,
);
}
#[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_not_works() {
check_assist_not_applicable(
make_raw_string,
r#"
fn f() {
let s = <|>r"random string";
}
"#,
);
}
#[test]
fn add_hash_target() {
check_assist_target(
add_hash,
r#"
fn f() {
let s = <|>r"random string";
}
"#,
r#"r"random string""#,
);
}
#[test]
fn add_hash_works() {
check_assist(
add_hash,
r#"
fn f() {
let s = <|>r"random string";
}
"#,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
)
}
#[test]
fn add_more_hash_works() {
check_assist(
add_hash,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
r###"
fn f() {
let s = <|>r##"random string"##;
}
"###,
)
}
#[test]
fn add_hash_not_works() {
check_assist_not_applicable(
add_hash,
r#"
fn f() {
let s = <|>"random string";
}
"#,
);
}
#[test]
fn remove_hash_target() {
check_assist_target(
remove_hash,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
r##"r#"random string"#"##,
);
}
#[test]
fn remove_hash_works() {
check_assist(
remove_hash,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
r#"
fn f() {
let s = <|>r"random string";
}
"#,
)
}
#[test]
fn remove_more_hash_works() {
check_assist(
remove_hash,
r###"
fn f() {
let s = <|>r##"random string"##;
}
"###,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
)
}
#[test]
fn remove_hash_not_works() {
check_assist_not_applicable(
remove_hash,
r#"
fn f() {
let s = <|>"random string";
}
"#,
);
}
#[test]
fn remove_hash_no_hash_not_works() {
check_assist_not_applicable(
remove_hash,
r#"
fn f() {
let s = <|>r"random string";
}
"#,
);
}
#[test]
fn make_usual_string_target() {
check_assist_target(
make_usual_string,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
r##"r#"random string"#"##,
);
}
#[test]
fn make_usual_string_works() {
check_assist(
make_usual_string,
r##"
fn f() {
let s = <|>r#"random string"#;
}
"##,
r#"
fn f() {
let s = <|>"random string";
}
"#,
)
}
#[test]
fn make_usual_string_more_hash_works() {
check_assist(
make_usual_string,
r###"
fn f() {
let s = <|>r##"random string"##;
}
"###,
r##"
fn f() {
let s = <|>"random string";
}
"##,
)
}
#[test]
fn make_usual_string_not_works() {
check_assist_not_applicable(
make_usual_string,
r#"
fn f() {
let s = <|>"random string";
}
"#,
);
}
}

View File

@ -445,6 +445,62 @@ fn foo<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 usual string
```rust
// before:
fn f() {
let s = <|>r#"abcd"#;
}
// after:
fn f() {
let s = <|>"abcd";
}
```
- Add hash
```rust
// before:
fn f() {
let s = <|>r"abcd";
}
// after:
fn f() {
let s = <|>r#"abcd"#;
}
```
- Remove hash
```rust
// before:
fn f() {
let s = <|>r#"abcd"#;
}
// after:
fn f() {
let s = <|>r"abcd";
}
```
### Magic Completions
In addition to usual reference completion, rust-analyzer provides some ✨magic✨