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                }
318            })
319            .collect()
320    }
321
322    fn completion_for_mention(mat: &StringMatch) -> (String, CodeLabel) {
323        let label = CodeLabel {
324            filter_range: 1..mat.string.len() + 1,
325            text: format!("@{}", mat.string),
326            runs: Vec::new(),
327        };
328        (mat.string.clone(), label)
329    }
330
331    fn completion_for_emoji(mat: &StringMatch) -> (String, CodeLabel) {
332        let emoji = emojis::get_by_shortcode(&mat.string).unwrap();
333        let label = CodeLabel {
334            filter_range: 1..mat.string.len() + 1,
335            text: format!(":{}: {}", mat.string, emoji),
336            runs: Vec::new(),
337        };
338        (emoji.to_string(), label)
339    }
340
341    fn collect_mention_candidates(
342        &mut self,
343        buffer: &Model<Buffer>,
344        end_anchor: Anchor,
345        cx: &mut ViewContext<Self>,
346    ) -> Option<(Anchor, String, Vec<StringMatchCandidate>)> {
347        let end_offset = end_anchor.to_offset(buffer.read(cx));
348
349        let Some(query) = buffer.update(cx, |buffer, _| {
350            let mut query = String::new();
351            for ch in buffer.reversed_chars_at(end_offset).take(100) {
352                if ch == '@' {
353                    return Some(query.chars().rev().collect::<String>());
354                }
355                if ch.is_whitespace() || !ch.is_ascii() {
356                    break;
357                }
358                query.push(ch);
359            }
360            None
361        }) else {
362            return None;
363        };
364
365        let start_offset = end_offset - query.len();
366        let start_anchor = buffer.read(cx).anchor_before(start_offset);
367
368        let mut names = HashSet::default();
369        if let Some(chat) = self.channel_chat.as_ref() {
370            let chat = chat.read(cx);
371            for participant in ChannelStore::global(cx)
372                .read(cx)
373                .channel_participants(chat.channel_id)
374            {
375                names.insert(participant.github_login.clone());
376            }
377            for message in chat
378                .messages_in_range(chat.message_count().saturating_sub(100)..chat.message_count())
379            {
380                names.insert(message.sender.github_login.clone());
381            }
382        }
383
384        let candidates = names
385            .into_iter()
386            .map(|user| StringMatchCandidate {
387                id: 0,
388                string: user.clone(),
389                char_bag: user.chars().collect(),
390            })
391            .collect::<Vec<_>>();
392
393        Some((start_anchor, query, candidates))
394    }
395
396    fn collect_emoji_candidates(
397        &mut self,
398        buffer: &Model<Buffer>,
399        end_anchor: Anchor,
400        cx: &mut ViewContext<Self>,
401    ) -> Option<(Anchor, String, &'static [StringMatchCandidate])> {
402        lazy_static! {
403            static ref EMOJI_FUZZY_MATCH_CANDIDATES: Vec<StringMatchCandidate> = {
404                let emojis = emojis::iter()
405                    .flat_map(|s| s.shortcodes())
406                    .map(|emoji| StringMatchCandidate {
407                        id: 0,
408                        string: emoji.to_string(),
409                        char_bag: emoji.chars().collect(),
410                    })
411                    .collect::<Vec<_>>();
412                emojis
413            };
414        }
415
416        let end_offset = end_anchor.to_offset(buffer.read(cx));
417
418        let Some(query) = buffer.update(cx, |buffer, _| {
419            let mut query = String::new();
420            for ch in buffer.reversed_chars_at(end_offset).take(100) {
421                if ch == ':' {
422                    let next_char = buffer
423                        .reversed_chars_at(end_offset - query.len() - 1)
424                        .next();
425                    // Ensure we are at the start of the message or that the previous character is a whitespace
426                    if next_char.is_none() || next_char.unwrap().is_whitespace() {
427                        return Some(query.chars().rev().collect::<String>());
428                    }
429
430                    // If the previous character is not a whitespace, we are in the middle of a word
431                    // and we only want to complete the shortcode if the word is made up of other emojis
432                    let mut containing_word = String::new();
433                    for ch in buffer
434                        .reversed_chars_at(end_offset - query.len() - 1)
435                        .take(100)
436                    {
437                        if ch.is_whitespace() {
438                            break;
439                        }
440                        containing_word.push(ch);
441                    }
442                    let containing_word = containing_word.chars().rev().collect::<String>();
443                    if util::word_consists_of_emojis(containing_word.as_str()) {
444                        return Some(query.chars().rev().collect::<String>());
445                    }
446                    break;
447                }
448                if ch.is_whitespace() || !ch.is_ascii() {
449                    break;
450                }
451                query.push(ch);
452            }
453            None
454        }) else {
455            return None;
456        };
457
458        let start_offset = end_offset - query.len() - 1;
459        let start_anchor = buffer.read(cx).anchor_before(start_offset);
460
461        Some((start_anchor, query, &EMOJI_FUZZY_MATCH_CANDIDATES))
462    }
463
464    async fn find_mentions(
465        this: WeakView<MessageEditor>,
466        buffer: BufferSnapshot,
467        mut cx: AsyncWindowContext,
468    ) {
469        let (buffer, ranges) = cx
470            .background_executor()
471            .spawn(async move {
472                let ranges = MENTIONS_SEARCH.search(&buffer, None).await;
473                (buffer, ranges)
474            })
475            .await;
476
477        this.update(&mut cx, |this, cx| {
478            let mut anchor_ranges = Vec::new();
479            let mut mentioned_user_ids = Vec::new();
480            let mut text = String::new();
481
482            this.editor.update(cx, |editor, cx| {
483                let multi_buffer = editor.buffer().read(cx).snapshot(cx);
484                for range in ranges {
485                    text.clear();
486                    text.extend(buffer.text_for_range(range.clone()));
487                    if let Some(username) = text.strip_prefix('@') {
488                        if let Some(user) = this
489                            .user_store
490                            .read(cx)
491                            .cached_user_by_github_login(username)
492                        {
493                            let start = multi_buffer.anchor_after(range.start);
494                            let end = multi_buffer.anchor_after(range.end);
495
496                            mentioned_user_ids.push(user.id);
497                            anchor_ranges.push(start..end);
498                        }
499                    }
500                }
501
502                editor.clear_highlights::<Self>(cx);
503                editor.highlight_text::<Self>(
504                    anchor_ranges,
505                    HighlightStyle {
506                        font_weight: Some(FontWeight::BOLD),
507                        ..Default::default()
508                    },
509                    cx,
510                )
511            });
512
513            this.mentions = mentioned_user_ids;
514            this.mentions_task.take();
515        })
516        .ok();
517    }
518
519    pub(crate) fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
520        self.editor.read(cx).focus_handle(cx)
521    }
522}
523
524impl Render for MessageEditor {
525    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
526        let settings = ThemeSettings::get_global(cx);
527        let text_style = TextStyle {
528            color: if self.editor.read(cx).read_only(cx) {
529                cx.theme().colors().text_disabled
530            } else {
531                cx.theme().colors().text
532            },
533            font_family: settings.ui_font.family.clone(),
534            font_features: settings.ui_font.features.clone(),
535            font_fallbacks: settings.ui_font.fallbacks.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}