message_editor.rs

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