message_editor.rs

  1use anyhow::{Context, Result};
  2use channel::{ChannelChat, ChannelStore, MessageParams};
  3use client::{UserId, UserStore};
  4use collections::HashSet;
  5use editor::{AnchorRangeExt, CompletionProvider, Editor, EditorElement, EditorStyle};
  6use fuzzy::{StringMatch, StringMatchCandidate};
  7use gpui::{
  8    AsyncWindowContext, FocusableView, FontStyle, FontWeight, HighlightStyle, IntoElement, Model,
  9    Render, Task, TextStyle, View, ViewContext, WeakView,
 10};
 11use language::{
 12    language_settings::SoftWrap, Anchor, Buffer, BufferSnapshot, CodeLabel, LanguageRegistry,
 13    LanguageServerId, ToOffset,
 14};
 15use parking_lot::RwLock;
 16use project::{search::SearchQuery, Completion};
 17use settings::Settings;
 18use std::{ops::Range, sync::Arc, sync::LazyLock, time::Duration};
 19use theme::ThemeSettings;
 20use ui::{prelude::*, TextSize};
 21
 22use crate::panel_settings::MessageEditorSettings;
 23
 24const MENTIONS_DEBOUNCE_INTERVAL: Duration = Duration::from_millis(50);
 25
 26static MENTIONS_SEARCH: LazyLock<SearchQuery> = LazyLock::new(|| {
 27    SearchQuery::regex(
 28        "@[-_\\w]+",
 29        false,
 30        false,
 31        false,
 32        Default::default(),
 33        Default::default(),
 34        None,
 35    )
 36    .unwrap()
 37});
 38
 39pub struct MessageEditor {
 40    pub editor: View<Editor>,
 41    user_store: Model<UserStore>,
 42    channel_chat: Option<Model<ChannelChat>>,
 43    mentions: Vec<UserId>,
 44    mentions_task: Option<Task<()>>,
 45    reply_to_message_id: Option<u64>,
 46    edit_message_id: Option<u64>,
 47}
 48
 49struct MessageEditorCompletionProvider(WeakView<MessageEditor>);
 50
 51impl CompletionProvider for MessageEditorCompletionProvider {
 52    fn completions(
 53        &self,
 54        buffer: &Model<Buffer>,
 55        buffer_position: language::Anchor,
 56        _: editor::CompletionContext,
 57        cx: &mut ViewContext<Editor>,
 58    ) -> Task<anyhow::Result<Vec<Completion>>> {
 59        let Some(handle) = self.0.upgrade() else {
 60            return Task::ready(Ok(Vec::new()));
 61        };
 62        handle.update(cx, |message_editor, cx| {
 63            message_editor.completions(buffer, buffer_position, cx)
 64        })
 65    }
 66
 67    fn resolve_completions(
 68        &self,
 69        _buffer: Model<Buffer>,
 70        _completion_indices: Vec<usize>,
 71        _completions: Arc<RwLock<Box<[Completion]>>>,
 72        _cx: &mut ViewContext<Editor>,
 73    ) -> Task<anyhow::Result<bool>> {
 74        Task::ready(Ok(false))
 75    }
 76
 77    fn apply_additional_edits_for_completion(
 78        &self,
 79        _buffer: Model<Buffer>,
 80        _completion: Completion,
 81        _push_to_history: bool,
 82        _cx: &mut ViewContext<Editor>,
 83    ) -> Task<Result<Option<language::Transaction>>> {
 84        Task::ready(Ok(None))
 85    }
 86
 87    fn is_completion_trigger(
 88        &self,
 89        _buffer: &Model<Buffer>,
 90        _position: language::Anchor,
 91        text: &str,
 92        _trigger_in_words: bool,
 93        _cx: &mut ViewContext<Editor>,
 94    ) -> bool {
 95        text == "@"
 96    }
 97}
 98
 99impl MessageEditor {
100    pub fn new(
101        language_registry: Arc<LanguageRegistry>,
102        user_store: Model<UserStore>,
103        channel_chat: Option<Model<ChannelChat>>,
104        editor: View<Editor>,
105        cx: &mut ViewContext<Self>,
106    ) -> Self {
107        let this = cx.view().downgrade();
108        editor.update(cx, |editor, cx| {
109            editor.set_soft_wrap_mode(SoftWrap::EditorWidth, cx);
110            editor.set_use_autoclose(false);
111            editor.set_show_gutter(false, cx);
112            editor.set_show_wrap_guides(false, cx);
113            editor.set_show_indent_guides(false, cx);
114            editor.set_completion_provider(Box::new(MessageEditorCompletionProvider(this)));
115            editor.set_auto_replace_emoji_shortcode(
116                MessageEditorSettings::get_global(cx).auto_replace_emoji_shortcode,
117            );
118        });
119
120        let buffer = editor
121            .read(cx)
122            .buffer()
123            .read(cx)
124            .as_singleton()
125            .expect("message editor must be singleton");
126
127        cx.subscribe(&buffer, Self::on_buffer_event).detach();
128        cx.observe_global::<settings::SettingsStore>(|view, cx| {
129            view.editor.update(cx, |editor, cx| {
130                editor.set_auto_replace_emoji_shortcode(
131                    MessageEditorSettings::get_global(cx).auto_replace_emoji_shortcode,
132                )
133            })
134        })
135        .detach();
136
137        let markdown = language_registry.language_for_name("Markdown");
138        cx.spawn(|_, mut cx| async move {
139            let markdown = markdown.await.context("failed to load Markdown language")?;
140            buffer.update(&mut cx, |buffer, cx| {
141                buffer.set_language(Some(markdown), cx)
142            })
143        })
144        .detach_and_log_err(cx);
145
146        Self {
147            editor,
148            user_store,
149            channel_chat,
150            mentions: Vec::new(),
151            mentions_task: None,
152            reply_to_message_id: None,
153            edit_message_id: None,
154        }
155    }
156
157    pub fn reply_to_message_id(&self) -> Option<u64> {
158        self.reply_to_message_id
159    }
160
161    pub fn set_reply_to_message_id(&mut self, reply_to_message_id: u64) {
162        self.reply_to_message_id = Some(reply_to_message_id);
163    }
164
165    pub fn clear_reply_to_message_id(&mut self) {
166        self.reply_to_message_id = None;
167    }
168
169    pub fn edit_message_id(&self) -> Option<u64> {
170        self.edit_message_id
171    }
172
173    pub fn set_edit_message_id(&mut self, edit_message_id: u64) {
174        self.edit_message_id = Some(edit_message_id);
175    }
176
177    pub fn clear_edit_message_id(&mut self) {
178        self.edit_message_id = None;
179    }
180
181    pub fn set_channel_chat(&mut self, chat: Model<ChannelChat>, cx: &mut ViewContext<Self>) {
182        let channel_id = chat.read(cx).channel_id;
183        self.channel_chat = Some(chat);
184        let channel_name = ChannelStore::global(cx)
185            .read(cx)
186            .channel_for_id(channel_id)
187            .map(|channel| channel.name.clone());
188        self.editor.update(cx, |editor, cx| {
189            if let Some(channel_name) = channel_name {
190                editor.set_placeholder_text(format!("Message #{channel_name}"), cx);
191            } else {
192                editor.set_placeholder_text("Message Channel", cx);
193            }
194        });
195    }
196
197    pub fn take_message(&mut self, cx: &mut ViewContext<Self>) -> MessageParams {
198        self.editor.update(cx, |editor, cx| {
199            let highlights = editor.text_highlights::<Self>(cx);
200            let text = editor.text(cx);
201            let snapshot = editor.buffer().read(cx).snapshot(cx);
202            let mentions = if let Some((_, ranges)) = highlights {
203                ranges
204                    .iter()
205                    .map(|range| range.to_offset(&snapshot))
206                    .zip(self.mentions.iter().copied())
207                    .collect()
208            } else {
209                Vec::new()
210            };
211
212            editor.clear(cx);
213            self.mentions.clear();
214            let reply_to_message_id = std::mem::take(&mut self.reply_to_message_id);
215
216            MessageParams {
217                text,
218                mentions,
219                reply_to_message_id,
220            }
221        })
222    }
223
224    fn on_buffer_event(
225        &mut self,
226        buffer: Model<Buffer>,
227        event: &language::Event,
228        cx: &mut ViewContext<Self>,
229    ) {
230        if let language::Event::Reparsed | language::Event::Edited = event {
231            let buffer = buffer.read(cx).snapshot();
232            self.mentions_task = Some(cx.spawn(|this, cx| async move {
233                cx.background_executor()
234                    .timer(MENTIONS_DEBOUNCE_INTERVAL)
235                    .await;
236                Self::find_mentions(this, buffer, cx).await;
237            }));
238        }
239    }
240
241    fn completions(
242        &mut self,
243        buffer: &Model<Buffer>,
244        end_anchor: Anchor,
245        cx: &mut ViewContext<Self>,
246    ) -> Task<Result<Vec<Completion>>> {
247        if let Some((start_anchor, query, candidates)) =
248            self.collect_mention_candidates(buffer, end_anchor, cx)
249        {
250            if !candidates.is_empty() {
251                return cx.spawn(|_, cx| async move {
252                    Ok(Self::resolve_completions_for_candidates(
253                        &cx,
254                        query.as_str(),
255                        &candidates,
256                        start_anchor..end_anchor,
257                        Self::completion_for_mention,
258                    )
259                    .await)
260                });
261            }
262        }
263
264        if let Some((start_anchor, query, candidates)) =
265            self.collect_emoji_candidates(buffer, end_anchor, cx)
266        {
267            if !candidates.is_empty() {
268                return cx.spawn(|_, cx| async move {
269                    Ok(Self::resolve_completions_for_candidates(
270                        &cx,
271                        query.as_str(),
272                        candidates,
273                        start_anchor..end_anchor,
274                        Self::completion_for_emoji,
275                    )
276                    .await)
277                });
278            }
279        }
280
281        Task::ready(Ok(vec![]))
282    }
283
284    async fn resolve_completions_for_candidates(
285        cx: &AsyncWindowContext,
286        query: &str,
287        candidates: &[StringMatchCandidate],
288        range: Range<Anchor>,
289        completion_fn: impl Fn(&StringMatch) -> (String, CodeLabel),
290    ) -> Vec<Completion> {
291        let matches = fuzzy::match_strings(
292            candidates,
293            query,
294            true,
295            10,
296            &Default::default(),
297            cx.background_executor().clone(),
298        )
299        .await;
300
301        matches
302            .into_iter()
303            .map(|mat| {
304                let (new_text, label) = completion_fn(&mat);
305                Completion {
306                    old_range: range.clone(),
307                    new_text,
308                    label,
309                    documentation: None,
310                    server_id: LanguageServerId(0), // TODO: Make this optional or something?
311                    lsp_completion: Default::default(), // TODO: Make this optional or something?
312                    confirm: None,
313                }
314            })
315            .collect()
316    }
317
318    fn completion_for_mention(mat: &StringMatch) -> (String, CodeLabel) {
319        let label = CodeLabel {
320            filter_range: 1..mat.string.len() + 1,
321            text: format!("@{}", mat.string),
322            runs: Vec::new(),
323        };
324        (mat.string.clone(), label)
325    }
326
327    fn completion_for_emoji(mat: &StringMatch) -> (String, CodeLabel) {
328        let emoji = emojis::get_by_shortcode(&mat.string).unwrap();
329        let label = CodeLabel {
330            filter_range: 1..mat.string.len() + 1,
331            text: format!(":{}: {}", mat.string, emoji),
332            runs: Vec::new(),
333        };
334        (emoji.to_string(), label)
335    }
336
337    fn collect_mention_candidates(
338        &mut self,
339        buffer: &Model<Buffer>,
340        end_anchor: Anchor,
341        cx: &mut ViewContext<Self>,
342    ) -> Option<(Anchor, String, Vec<StringMatchCandidate>)> {
343        let end_offset = end_anchor.to_offset(buffer.read(cx));
344
345        let query = buffer.update(cx, |buffer, _| {
346            let mut query = String::new();
347            for ch in buffer.reversed_chars_at(end_offset).take(100) {
348                if ch == '@' {
349                    return Some(query.chars().rev().collect::<String>());
350                }
351                if ch.is_whitespace() || !ch.is_ascii() {
352                    break;
353                }
354                query.push(ch);
355            }
356            None
357        })?;
358
359        let start_offset = end_offset - query.len();
360        let start_anchor = buffer.read(cx).anchor_before(start_offset);
361
362        let mut names = HashSet::default();
363        if let Some(chat) = self.channel_chat.as_ref() {
364            let chat = chat.read(cx);
365            for participant in ChannelStore::global(cx)
366                .read(cx)
367                .channel_participants(chat.channel_id)
368            {
369                names.insert(participant.github_login.clone());
370            }
371            for message in chat
372                .messages_in_range(chat.message_count().saturating_sub(100)..chat.message_count())
373            {
374                names.insert(message.sender.github_login.clone());
375            }
376        }
377
378        let candidates = names
379            .into_iter()
380            .map(|user| StringMatchCandidate {
381                id: 0,
382                string: user.clone(),
383                char_bag: user.chars().collect(),
384            })
385            .collect::<Vec<_>>();
386
387        Some((start_anchor, query, candidates))
388    }
389
390    fn collect_emoji_candidates(
391        &mut self,
392        buffer: &Model<Buffer>,
393        end_anchor: Anchor,
394        cx: &mut ViewContext<Self>,
395    ) -> Option<(Anchor, String, &'static [StringMatchCandidate])> {
396        static EMOJI_FUZZY_MATCH_CANDIDATES: LazyLock<Vec<StringMatchCandidate>> =
397            LazyLock::new(|| {
398                let emojis = emojis::iter()
399                    .flat_map(|s| s.shortcodes())
400                    .map(|emoji| StringMatchCandidate {
401                        id: 0,
402                        string: emoji.to_string(),
403                        char_bag: emoji.chars().collect(),
404                    })
405                    .collect::<Vec<_>>();
406                emojis
407            });
408
409        let end_offset = end_anchor.to_offset(buffer.read(cx));
410
411        let query = buffer.update(cx, |buffer, _| {
412            let mut query = String::new();
413            for ch in buffer.reversed_chars_at(end_offset).take(100) {
414                if ch == ':' {
415                    let next_char = buffer
416                        .reversed_chars_at(end_offset - query.len() - 1)
417                        .next();
418                    // Ensure we are at the start of the message or that the previous character is a whitespace
419                    if next_char.is_none() || next_char.unwrap().is_whitespace() {
420                        return Some(query.chars().rev().collect::<String>());
421                    }
422
423                    // If the previous character is not a whitespace, we are in the middle of a word
424                    // and we only want to complete the shortcode if the word is made up of other emojis
425                    let mut containing_word = String::new();
426                    for ch in buffer
427                        .reversed_chars_at(end_offset - query.len() - 1)
428                        .take(100)
429                    {
430                        if ch.is_whitespace() {
431                            break;
432                        }
433                        containing_word.push(ch);
434                    }
435                    let containing_word = containing_word.chars().rev().collect::<String>();
436                    if util::word_consists_of_emojis(containing_word.as_str()) {
437                        return Some(query.chars().rev().collect::<String>());
438                    }
439                    break;
440                }
441                if ch.is_whitespace() || !ch.is_ascii() {
442                    break;
443                }
444                query.push(ch);
445            }
446            None
447        })?;
448
449        let start_offset = end_offset - query.len() - 1;
450        let start_anchor = buffer.read(cx).anchor_before(start_offset);
451
452        Some((start_anchor, query, &EMOJI_FUZZY_MATCH_CANDIDATES))
453    }
454
455    async fn find_mentions(
456        this: WeakView<MessageEditor>,
457        buffer: BufferSnapshot,
458        mut cx: AsyncWindowContext,
459    ) {
460        let (buffer, ranges) = cx
461            .background_executor()
462            .spawn(async move {
463                let ranges = MENTIONS_SEARCH.search(&buffer, None).await;
464                (buffer, ranges)
465            })
466            .await;
467
468        this.update(&mut cx, |this, cx| {
469            let mut anchor_ranges = Vec::new();
470            let mut mentioned_user_ids = Vec::new();
471            let mut text = String::new();
472
473            this.editor.update(cx, |editor, cx| {
474                let multi_buffer = editor.buffer().read(cx).snapshot(cx);
475                for range in ranges {
476                    text.clear();
477                    text.extend(buffer.text_for_range(range.clone()));
478                    if let Some(username) = text.strip_prefix('@') {
479                        if let Some(user) = this
480                            .user_store
481                            .read(cx)
482                            .cached_user_by_github_login(username)
483                        {
484                            let start = multi_buffer.anchor_after(range.start);
485                            let end = multi_buffer.anchor_after(range.end);
486
487                            mentioned_user_ids.push(user.id);
488                            anchor_ranges.push(start..end);
489                        }
490                    }
491                }
492
493                editor.clear_highlights::<Self>(cx);
494                editor.highlight_text::<Self>(
495                    anchor_ranges,
496                    HighlightStyle {
497                        font_weight: Some(FontWeight::BOLD),
498                        ..Default::default()
499                    },
500                    cx,
501                )
502            });
503
504            this.mentions = mentioned_user_ids;
505            this.mentions_task.take();
506        })
507        .ok();
508    }
509
510    pub(crate) fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
511        self.editor.read(cx).focus_handle(cx)
512    }
513}
514
515impl Render for MessageEditor {
516    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
517        let settings = ThemeSettings::get_global(cx);
518        let text_style = TextStyle {
519            color: if self.editor.read(cx).read_only(cx) {
520                cx.theme().colors().text_disabled
521            } else {
522                cx.theme().colors().text
523            },
524            font_family: settings.ui_font.family.clone(),
525            font_features: settings.ui_font.features.clone(),
526            font_fallbacks: settings.ui_font.fallbacks.clone(),
527            font_size: TextSize::Small.rems(cx).into(),
528            font_weight: settings.ui_font.weight,
529            font_style: FontStyle::Normal,
530            line_height: relative(1.3),
531            ..Default::default()
532        };
533
534        div()
535            .w_full()
536            .px_2()
537            .py_1()
538            .bg(cx.theme().colors().editor_background)
539            .rounded_md()
540            .child(EditorElement::new(
541                &self.editor,
542                EditorStyle {
543                    local_player: cx.theme().players().local(),
544                    text: text_style,
545                    ..Default::default()
546                },
547            ))
548    }
549}