From 62192cede3567df632c819656cc28ec3b2d2f5d6 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Fri, 16 Oct 2020 20:21:16 +0200 Subject: replace_string_with_char #6252 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- .../src/handlers/replace_string_with_char.rs | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 crates/assists/src/handlers/replace_string_with_char.rs (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/replace_string_with_char.rs b/crates/assists/src/handlers/replace_string_with_char.rs new file mode 100644 index 000000000..3dc19431c --- /dev/null +++ b/crates/assists/src/handlers/replace_string_with_char.rs @@ -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'); + } + "##, + ) + } +} -- cgit v1.2.3 From c22c0395780c548d7b7fb557b1879e998921b01d Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sat, 17 Oct 2020 16:08:25 +0200 Subject: Assist: replace string with char Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- .../src/handlers/replace_string_with_char.rs | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/replace_string_with_char.rs b/crates/assists/src/handlers/replace_string_with_char.rs index 3dc19431c..8408e4463 100644 --- a/crates/assists/src/handlers/replace_string_with_char.rs +++ b/crates/assists/src/handlers/replace_string_with_char.rs @@ -1,18 +1,14 @@ -use std::borrow::Cow; - use syntax::{ - ast::{self, HasQuotes, HasStringValue}, + ast::{self, HasStringValue}, AstToken, - SyntaxKind::{RAW_STRING, STRING}, - TextRange, TextSize, + SyntaxKind::STRING, }; -use test_utils::mark; use crate::{AssistContext, AssistId, AssistKind, Assists}; // Assist: replace_string_with_char // -// Replace string with char +// Replace string with char. // // ``` // fn main() { @@ -29,7 +25,8 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) - 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() { + + if value.is_empty() || value.chars().count() > 1 { return None; } @@ -79,6 +76,23 @@ mod tests { ) } + #[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( -- cgit v1.2.3 From 9883435e4e7f96a3e33ef3e38665189339db71eb Mon Sep 17 00:00:00 2001 From: Coenen Benjamin Date: Tue, 20 Oct 2020 20:07:39 +0200 Subject: Update crates/assists/src/handlers/replace_string_with_char.rs Co-authored-by: Aleksey Kladov --- crates/assists/src/handlers/replace_string_with_char.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/replace_string_with_char.rs b/crates/assists/src/handlers/replace_string_with_char.rs index 8408e4463..4ca87a8ec 100644 --- a/crates/assists/src/handlers/replace_string_with_char.rs +++ b/crates/assists/src/handlers/replace_string_with_char.rs @@ -26,7 +26,7 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) - let value = token.value()?; let target = token.syntax().text_range(); - if value.is_empty() || value.chars().count() > 1 { + if value.chars().take(2).count() != 1 { return None; } -- cgit v1.2.3