2021-07-30 09:46:06 -05:00
|
|
|
use syntax::{
|
|
|
|
ast,
|
|
|
|
ast::IsString,
|
|
|
|
AstToken,
|
|
|
|
SyntaxKind::{CHAR, STRING},
|
|
|
|
TextRange, TextSize,
|
|
|
|
};
|
2020-10-16 13:21:16 -05:00
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
|
|
|
// Assist: replace_string_with_char
|
|
|
|
//
|
2021-07-30 09:46:06 -05:00
|
|
|
// Replace string literal with char literal.
|
2020-10-16 13:21:16 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// find("{$0");
|
2020-10-16 13:21:16 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// find('{');
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
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;
|
|
|
|
}
|
2021-03-06 14:21:18 -06:00
|
|
|
let quote_offets = token.quote_offsets()?;
|
2020-10-16 13:21:16 -05:00
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
|
|
|
|
"Replace string with char",
|
|
|
|
target,
|
|
|
|
|edit| {
|
2021-03-06 14:21:18 -06:00
|
|
|
let (left, right) = quote_offets.quotes;
|
2021-07-30 09:46:06 -05:00
|
|
|
edit.replace(left, '\'');
|
|
|
|
edit.replace(right, '\'');
|
|
|
|
if value == "'" {
|
|
|
|
edit.insert(left.end(), '\\');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assist: replace_char_with_string
|
|
|
|
//
|
|
|
|
// Replace a char literal with a string literal.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// find('{$0');
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// find("{");
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn replace_char_with_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-07-30 09:46:06 -05:00
|
|
|
let token = ctx.find_token_syntax_at_offset(CHAR)?;
|
|
|
|
let target = token.text_range();
|
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("replace_char_with_string", AssistKind::RefactorRewrite),
|
|
|
|
"Replace char with string",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
if token.text() == "'\"'" {
|
|
|
|
edit.replace(token.text_range(), r#""\"""#);
|
|
|
|
} else {
|
|
|
|
let len = TextSize::of('\'');
|
|
|
|
edit.replace(TextRange::at(target.start(), len), '"');
|
|
|
|
edit.replace(TextRange::at(target.end() - len, len), '"');
|
|
|
|
}
|
2020-10-16 13:21:16 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-07-30 09:46:06 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-10-16 13:21:16 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_assist() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
let s = "$0c";
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-16 13:21:16 -05:00
|
|
|
r##"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
let s = 'c';
|
|
|
|
}
|
|
|
|
"##,
|
2020-10-16 13:21:16 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-17 09:08:25 -05:00
|
|
|
#[test]
|
2021-07-30 09:46:06 -05:00
|
|
|
fn replace_string_with_char_assist_with_multi_byte_char() {
|
2020-10-17 09:08:25 -05:00
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
let s = "$0😀";
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-17 09:08:25 -05:00
|
|
|
r##"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
let s = '😀';
|
|
|
|
}
|
|
|
|
"##,
|
2020-10-17 09:08:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:21:16 -05:00
|
|
|
#[test]
|
2021-07-30 09:46:06 -05:00
|
|
|
fn replace_string_with_char_multiple_chars() {
|
2020-10-16 13:21:16 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
let s = "$0test";
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-16 13:21:16 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_works_inside_macros() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
format!($0"x", 92)
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-16 13:21:16 -05:00
|
|
|
r##"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
format!('x', 92)
|
|
|
|
}
|
|
|
|
"##,
|
2020-10-16 13:21:16 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:20:18 -06:00
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_newline() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
find($0"\n");
|
|
|
|
}
|
|
|
|
"#,
|
2021-03-03 15:20:18 -06:00
|
|
|
r##"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
find('\n');
|
|
|
|
}
|
|
|
|
"##,
|
2021-03-03 15:20:18 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_unicode_escape() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
find($0"\u{7FFF}");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
find('\u{7FFF}');
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_raw_string_with_char() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
2021-03-03 15:20:18 -06:00
|
|
|
r##"
|
2021-03-06 14:21:18 -06:00
|
|
|
fn f() {
|
|
|
|
$0r#"X"#
|
|
|
|
}
|
|
|
|
"##,
|
|
|
|
r##"
|
|
|
|
fn f() {
|
|
|
|
'X'
|
|
|
|
}
|
|
|
|
"##,
|
2021-03-03 15:20:18 -06:00
|
|
|
)
|
|
|
|
}
|
2021-07-30 09:46:06 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_char_with_string_assist() {
|
|
|
|
check_assist(
|
|
|
|
replace_char_with_string,
|
|
|
|
r"
|
|
|
|
fn f() {
|
|
|
|
let s = '$0c';
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "c";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_char_with_string_assist_with_multi_byte_char() {
|
|
|
|
check_assist(
|
|
|
|
replace_char_with_string,
|
|
|
|
r"
|
|
|
|
fn f() {
|
|
|
|
let s = '$0😀';
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let s = "😀";
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_char_with_string_newline() {
|
|
|
|
check_assist(
|
|
|
|
replace_char_with_string,
|
|
|
|
r"
|
|
|
|
fn f() {
|
|
|
|
find($0'\n');
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find("\n");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_char_with_string_unicode_escape() {
|
|
|
|
check_assist(
|
|
|
|
replace_char_with_string,
|
|
|
|
r"
|
|
|
|
fn f() {
|
|
|
|
find($0'\u{7FFF}');
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find("\u{7FFF}");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_char_with_string_quote() {
|
|
|
|
check_assist(
|
|
|
|
replace_char_with_string,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find($0'"');
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find("\"");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_string_with_char_quote() {
|
|
|
|
check_assist(
|
|
|
|
replace_string_with_char,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find($0"'");
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
find('\'');
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2020-10-16 13:21:16 -05:00
|
|
|
}
|