mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
replace_string_with_char #6252
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
parent
89aad020c8
commit
62192cede3
2 changed files with 129 additions and 0 deletions
127
crates/assists/src/handlers/replace_string_with_char.rs
Normal file
127
crates/assists/src/handlers/replace_string_with_char.rs
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use syntax::{
|
||||||
|
ast::{self, HasQuotes, HasStringValue},
|
||||||
|
AstToken,
|
||||||
|
SyntaxKind::{RAW_STRING, STRING},
|
||||||
|
TextRange, TextSize,
|
||||||
|
};
|
||||||
|
use test_utils::mark;
|
||||||
|
|
||||||
|
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.len() > 1 || value.is_empty() {
|
||||||
|
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_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');
|
||||||
|
}
|
||||||
|
"##,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -159,6 +159,7 @@ mod handlers {
|
||||||
mod replace_impl_trait_with_generic;
|
mod replace_impl_trait_with_generic;
|
||||||
mod replace_let_with_if_let;
|
mod replace_let_with_if_let;
|
||||||
mod replace_qualified_name_with_use;
|
mod replace_qualified_name_with_use;
|
||||||
|
mod replace_string_with_char;
|
||||||
mod replace_unwrap_with_match;
|
mod replace_unwrap_with_match;
|
||||||
mod split_import;
|
mod split_import;
|
||||||
mod unwrap_block;
|
mod unwrap_block;
|
||||||
|
@ -208,6 +209,7 @@ mod handlers {
|
||||||
replace_impl_trait_with_generic::replace_impl_trait_with_generic,
|
replace_impl_trait_with_generic::replace_impl_trait_with_generic,
|
||||||
replace_let_with_if_let::replace_let_with_if_let,
|
replace_let_with_if_let::replace_let_with_if_let,
|
||||||
replace_qualified_name_with_use::replace_qualified_name_with_use,
|
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,
|
replace_unwrap_with_match::replace_unwrap_with_match,
|
||||||
split_import::split_import,
|
split_import::split_import,
|
||||||
unwrap_block::unwrap_block,
|
unwrap_block::unwrap_block,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue