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