Merge #6256
6256: Assist: replace string with char r=bnjjj a=bnjjj close #6252 Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Co-authored-by: Coenen Benjamin <benjamin.coenen@hotmail.com>
This commit is contained in:
commit
c00339509d
141
crates/assists/src/handlers/replace_string_with_char.rs
Normal file
141
crates/assists/src/handlers/replace_string_with_char.rs
Normal file
@ -0,0 +1,141 @@
|
||||
use syntax::{
|
||||
ast::{self, HasStringValue},
|
||||
AstToken,
|
||||
SyntaxKind::STRING,
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
|
||||
// Assist: replace_string_with_char
|
||||
//
|
||||
// Replace string with char.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// find("{<|>");
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// find('{');
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
|
||||
let value = token.value()?;
|
||||
let target = token.syntax().text_range();
|
||||
|
||||
if value.chars().take(2).count() != 1 {
|
||||
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';
|
||||
}
|
||||
"##,
|
||||
)
|
||||
}
|
||||
|
||||
#[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 = '😀';
|
||||
}
|
||||
"##,
|
||||
)
|
||||
}
|
||||
|
||||
#[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');
|
||||
}
|
||||
"##,
|
||||
)
|
||||
}
|
||||
}
|
@ -160,6 +160,7 @@ mod handlers {
|
||||
mod replace_impl_trait_with_generic;
|
||||
mod replace_let_with_if_let;
|
||||
mod replace_qualified_name_with_use;
|
||||
mod replace_string_with_char;
|
||||
mod replace_unwrap_with_match;
|
||||
mod split_import;
|
||||
mod unwrap_block;
|
||||
@ -210,6 +211,7 @@ mod handlers {
|
||||
replace_impl_trait_with_generic::replace_impl_trait_with_generic,
|
||||
replace_let_with_if_let::replace_let_with_if_let,
|
||||
replace_qualified_name_with_use::replace_qualified_name_with_use,
|
||||
replace_string_with_char::replace_string_with_char,
|
||||
replace_unwrap_with_match::replace_unwrap_with_match,
|
||||
split_import::split_import,
|
||||
unwrap_block::unwrap_block,
|
||||
|
@ -900,6 +900,23 @@ fn process(map: HashMap<String, String>) {}
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_replace_string_with_char() {
|
||||
check_doc_test(
|
||||
"replace_string_with_char",
|
||||
r#####"
|
||||
fn main() {
|
||||
find("{<|>");
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
find('{');
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_replace_unwrap_with_match() {
|
||||
check_doc_test(
|
||||
|
Loading…
x
Reference in New Issue
Block a user