aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/assists/src/handlers/replace_string_with_char.rs30
-rw-r--r--crates/assists/src/tests/generated.rs17
2 files changed, 39 insertions, 8 deletions
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 @@
1use std::borrow::Cow;
2
3use syntax::{ 1use syntax::{
4 ast::{self, HasQuotes, HasStringValue}, 2 ast::{self, HasStringValue},
5 AstToken, 3 AstToken,
6 SyntaxKind::{RAW_STRING, STRING}, 4 SyntaxKind::STRING,
7 TextRange, TextSize,
8}; 5};
9use test_utils::mark;
10 6
11use crate::{AssistContext, AssistId, AssistKind, Assists}; 7use crate::{AssistContext, AssistId, AssistKind, Assists};
12 8
13// Assist: replace_string_with_char 9// Assist: replace_string_with_char
14// 10//
15// Replace string with char 11// Replace string with char.
16// 12//
17// ``` 13// ```
18// fn main() { 14// fn main() {
@@ -29,7 +25,8 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
29 let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?; 25 let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
30 let value = token.value()?; 26 let value = token.value()?;
31 let target = token.syntax().text_range(); 27 let target = token.syntax().text_range();
32 if value.len() > 1 || value.is_empty() { 28
29 if value.is_empty() || value.chars().count() > 1 {
33 return None; 30 return None;
34 } 31 }
35 32
@@ -80,6 +77,23 @@ mod tests {
80 } 77 }
81 78
82 #[test] 79 #[test]
80 fn replace_string_with_char_assist_with_emoji() {
81 check_assist(
82 replace_string_with_char,
83 r#"
84 fn f() {
85 let s = "<|>😀";
86 }
87 "#,
88 r##"
89 fn f() {
90 let s = '😀';
91 }
92 "##,
93 )
94 }
95
96 #[test]
83 fn replace_string_with_char_assist_not_applicable() { 97 fn replace_string_with_char_assist_not_applicable() {
84 check_assist_not_applicable( 98 check_assist_not_applicable(
85 replace_string_with_char, 99 replace_string_with_char,
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs
index 41f536574..6fb2a7cac 100644
--- a/crates/assists/src/tests/generated.rs
+++ b/crates/assists/src/tests/generated.rs
@@ -882,6 +882,23 @@ fn process(map: HashMap<String, String>) {}
882} 882}
883 883
884#[test] 884#[test]
885fn doctest_replace_string_with_char() {
886 check_doc_test(
887 "replace_string_with_char",
888 r#####"
889fn main() {
890 find("{<|>");
891}
892"#####,
893 r#####"
894fn main() {
895 find('{');
896}
897"#####,
898 )
899}
900
901#[test]
885fn doctest_replace_unwrap_with_match() { 902fn doctest_replace_unwrap_with_match() {
886 check_doc_test( 903 check_doc_test(
887 "replace_unwrap_with_match", 904 "replace_unwrap_with_match",