chat: fix emoji completions when word consists of emojis (#9107)

https://github.com/zed-industries/zed/assets/53836821/f4b31c47-d306-43f5-b971-0969f64a48f9

Fix for #9096 @JosephTLyons 

Release Notes:
- Fixed emoji completion not showing up when word contains only emojis
(#9096)
This commit is contained in:
Bennet Bo Fenner 2024-03-11 16:08:18 +01:00 committed by GitHub
parent eb5e18c66d
commit a8fa1f7363
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 9 deletions

1
Cargo.lock generated
View file

@ -11083,6 +11083,7 @@ dependencies = [
"log", "log",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"rand 0.8.5", "rand 0.8.5",
"regex",
"rust-embed", "rust-embed",
"serde", "serde",
"serde_json", "serde_json",

View file

@ -407,6 +407,23 @@ impl MessageEditor {
if next_char.is_none() || next_char.unwrap().is_whitespace() { if next_char.is_none() || next_char.unwrap().is_whitespace() {
return Some(query.chars().rev().collect::<String>()); return Some(query.chars().rev().collect::<String>());
} }
// If the previous character is not a whitespace, we are in the middle of a word
// and we only want to complete the shortcode if the word is made up of other emojis
let mut containing_word = String::new();
for ch in buffer
.reversed_chars_at(end_offset - query.len() - 1)
.take(100)
{
if ch.is_whitespace() {
break;
}
containing_word.push(ch);
}
let containing_word = containing_word.chars().rev().collect::<String>();
if util::word_consists_of_emojis(containing_word.as_str()) {
return Some(query.chars().rev().collect::<String>());
}
break; break;
} }
if ch.is_whitespace() || !ch.is_ascii() { if ch.is_whitespace() || !ch.is_ascii() {

View file

@ -2653,10 +2653,31 @@ impl Editor {
let mut found_colon = false; let mut found_colon = false;
for char in snapshot.reversed_chars_at(position).take(100) { for char in snapshot.reversed_chars_at(position).take(100) {
// Found a possible emoji shortcode in the middle of the buffer // Found a possible emoji shortcode in the middle of the buffer
if found_colon && char.is_whitespace() { if found_colon {
chars.reverse(); if char.is_whitespace() {
return Some(chars.iter().collect()); chars.reverse();
return Some(chars.iter().collect());
}
// If the previous character is not a whitespace, we are in the middle of a word
// and we only want to complete the shortcode if the word is made up of other emojis
let mut containing_word = String::new();
for ch in snapshot
.reversed_chars_at(position)
.skip(chars.len() + 1)
.take(100)
{
if ch.is_whitespace() {
break;
}
containing_word.push(ch);
}
let containing_word = containing_word.chars().rev().collect::<String>();
if util::word_consists_of_emojis(containing_word.as_str()) {
chars.reverse();
return Some(chars.iter().collect());
}
} }
if char.is_whitespace() || !char.is_ascii() { if char.is_whitespace() || !char.is_ascii() {
return None; return None;
} }

View file

@ -5206,20 +5206,39 @@ async fn test_auto_replace_emoji_shortcode(cx: &mut gpui::TestAppContext) {
editor.handle_input(":", cx); editor.handle_input(":", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄".unindent()); assert_eq!(editor.text(cx), "Hello 👋 😄".unindent());
editor.handle_input(":1:", cx); // Ensure shortcode gets replaced when it is part of a word that only consists of emojis
assert_eq!(editor.text(cx), "Hello 👋 😄:1:".unindent()); editor.handle_input(":wave", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄:wave".unindent());
editor.handle_input(":", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄👋".unindent());
editor.handle_input(":1", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄👋:1".unindent());
editor.handle_input(":", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄👋:1:".unindent());
// Ensure shortcode does not get replaced when it is part of a word // Ensure shortcode does not get replaced when it is part of a word
editor.handle_input(" Test:wave:", cx); editor.handle_input(" Test:wave", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄:1: Test:wave:".unindent()); assert_eq!(editor.text(cx), "Hello 👋 😄👋:1: Test:wave".unindent());
editor.handle_input(":", cx);
assert_eq!(editor.text(cx), "Hello 👋 😄👋:1: Test:wave:".unindent());
editor.set_auto_replace_emoji_shortcode(false); editor.set_auto_replace_emoji_shortcode(false);
// Ensure shortcode does not get replaced when auto replace is off // Ensure shortcode does not get replaced when auto replace is off
editor.handle_input(" :wave:", cx); editor.handle_input(" :wave", cx);
assert_eq!( assert_eq!(
editor.text(cx), editor.text(cx),
"Hello 👋 😄:1: Test:wave: :wave:".unindent() "Hello 👋 😄👋:1: Test:wave: :wave".unindent()
);
editor.handle_input(":", cx);
assert_eq!(
editor.text(cx),
"Hello 👋 😄👋:1: Test:wave: :wave:".unindent()
); );
}); });
} }

View file

@ -28,6 +28,7 @@ lazy_static.workspace = true
log.workspace = true log.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
rand.workspace = true rand.workspace = true
regex.workspace = true
rust-embed.workspace = true rust-embed.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true

View file

@ -524,6 +524,22 @@ impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
lazy_static! {
static ref EMOJI_REGEX: regex::Regex = regex::Regex::new("(\\p{Emoji}|\u{200D})").unwrap();
}
/// Returns true if the given string consists of emojis only.
/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
pub fn word_consists_of_emojis(s: &str) -> bool {
let mut prev_end = 0;
for capture in EMOJI_REGEX.find_iter(s) {
if capture.start() != prev_end {
return false;
}
prev_end = capture.end();
}
prev_end == s.len()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -583,4 +599,21 @@ mod tests {
) )
} }
} }
#[test]
fn test_word_consists_of_emojis() {
let words_to_test = vec![
("👨‍👩‍👧‍👧👋🥒", true),
("👋", true),
("!👋", false),
("👋!", false),
("👋 ", false),
(" 👋", false),
("Test", false),
];
for (text, expected_result) in words_to_test {
assert_eq!(word_consists_of_emojis(text), expected_result);
}
}
} }