Skip to content

Commit c22c039

Browse files
committed
Assist: replace string with char
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent 62192ce commit c22c039

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

crates/assists/src/handlers/replace_string_with_char.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
use std::borrow::Cow;
2-
31
use syntax::{
4-
ast::{self, HasQuotes, HasStringValue},
2+
ast::{self, HasStringValue},
53
AstToken,
6-
SyntaxKind::{RAW_STRING, STRING},
7-
TextRange, TextSize,
4+
SyntaxKind::STRING,
85
};
9-
use test_utils::mark;
106

117
use crate::{AssistContext, AssistId, AssistKind, Assists};
128

139
// Assist: replace_string_with_char
1410
//
15-
// Replace string with char
11+
// Replace string with char.
1612
//
1713
// ```
1814
// fn main() {
@@ -29,7 +25,8 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
2925
let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
3026
let value = token.value()?;
3127
let target = token.syntax().text_range();
32-
if value.len() > 1 || value.is_empty() {
28+
29+
if value.is_empty() || value.chars().count() > 1 {
3330
return None;
3431
}
3532

@@ -79,6 +76,23 @@ mod tests {
7976
)
8077
}
8178

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+
8296
#[test]
8397
fn replace_string_with_char_assist_not_applicable() {
8498
check_assist_not_applicable(

crates/assists/src/tests/generated.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,23 @@ fn process(map: HashMap<String, String>) {}
881881
)
882882
}
883883

884+
#[test]
885+
fn doctest_replace_string_with_char() {
886+
check_doc_test(
887+
"replace_string_with_char",
888+
r#####"
889+
fn main() {
890+
find("{<|>");
891+
}
892+
"#####,
893+
r#####"
894+
fn main() {
895+
find('{');
896+
}
897+
"#####,
898+
)
899+
}
900+
884901
#[test]
885902
fn doctest_replace_unwrap_with_match() {
886903
check_doc_test(

0 commit comments

Comments
 (0)