Skip to content

Assist: replace string with char #6256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions crates/assists/src/handlers/replace_string_with_char.rs
Original file line number Diff line number Diff line change
@@ -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.is_empty() || value.chars().count() > 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if value.is_empty() || value.chars().count() > 1 {
if value.chars().count() != 1 {

but its probably best to avoid premature pessimization and call .next twice manually instead of O(N) count

other then this, lgmt!

bors d+

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');
}
"##,
)
}
}
2 changes: 2 additions & 0 deletions crates/assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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;
Expand Down Expand Up @@ -208,6 +209,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,
Expand Down
17 changes: 17 additions & 0 deletions crates/assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,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(
Expand Down