2020-10-16 13:21:16 -05:00
|
|
|
use syntax::{
|
2020-10-17 09:08:25 -05:00
|
|
|
ast::{self, HasStringValue},
|
2020-10-16 13:21:16 -05:00
|
|
|
AstToken,
|
2020-10-17 09:08:25 -05:00
|
|
|
SyntaxKind::STRING,
|
2020-10-16 13:21:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
|
|
|
// Assist: replace_string_with_char
|
|
|
|
//
|
2020-10-17 09:08:25 -05:00
|
|
|
// Replace string with char.
|
2020-10-16 13:21:16 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// find("{<|>");
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// find('{');
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-11-06 15:21:56 -06:00
|
|
|
let token = ctx.find_token_syntax_at_offset(STRING).and_then(ast::String::cast)?;
|
2020-10-16 13:21:16 -05:00
|
|
|
let value = token.value()?;
|
|
|
|
let target = token.syntax().text_range();
|
2020-10-17 09:08:25 -05:00
|
|
|
|
2020-10-20 13:07:39 -05:00
|
|
|
if value.chars().take(2).count() != 1 {
|
2020-10-16 13:21:16 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
|
|
|
|
"Replace string with char",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
edit.replace(token.syntax().text_range(), format!("'{}'", value));
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_target() {
|
|
|
|
check_assist_target(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "<|>c";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#""c""#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_assist() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "<|>c";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
let s = 'c';
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-17 09:08:25 -05:00
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_assist_with_emoji() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "<|>😀";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
let s = '😀';
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:21:16 -05:00
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_assist_not_applicable() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "<|>test";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_works_inside_macros() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
format!(<|>"x", 92)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
format!('x', 92)
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_works_func_args() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find(<|>"x");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
find('x');
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|