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)
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 static EMOJI_FUZZY_MATCH_CANDIDATES: LazyLock<Vec<StringMatchCandidate>> =
403 LazyLock::new(|| {
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 let end_offset = end_anchor.to_offset(buffer.read(cx));
416
417 let Some(query) = buffer.update(cx, |buffer, _| {
418 let mut query = String::new();
419 for ch in buffer.reversed_chars_at(end_offset).take(100) {
420 if ch == ':' {
421 let next_char = buffer
422 .reversed_chars_at(end_offset - query.len() - 1)
423 .next();
424 // Ensure we are at the start of the message or that the previous character is a whitespace
425 if next_char.is_none() || next_char.unwrap().is_whitespace() {
426 return Some(query.chars().rev().collect::<String>());
427 }
428
429 // If the previous character is not a whitespace, we are in the middle of a word
430 // and we only want to complete the shortcode if the word is made up of other emojis
431 let mut containing_word = String::new();
432 for ch in buffer
433 .reversed_chars_at(end_offset - query.len() - 1)
434 .take(100)
435 {
436 if ch.is_whitespace() {
437 break;
438 }
439 containing_word.push(ch);
440 }
441 let containing_word = containing_word.chars().rev().collect::<String>();
442 if util::word_consists_of_emojis(containing_word.as_str()) {
443 return Some(query.chars().rev().collect::<String>());
444 }
445 break;
446 }
447 if ch.is_whitespace() || !ch.is_ascii() {
448 break;
449 }
450 query.push(ch);
451 }
452 None
453 }) else {
454 return None;
455 };
456
457 let start_offset = end_offset - query.len() - 1;
458 let start_anchor = buffer.read(cx).anchor_before(start_offset);
459
460 Some((start_anchor, query, &EMOJI_FUZZY_MATCH_CANDIDATES))
461 }
462
463 async fn find_mentions(
464 this: WeakView<MessageEditor>,
465 buffer: BufferSnapshot,
466 mut cx: AsyncWindowContext,
467 ) {
468 let (buffer, ranges) = cx
469 .background_executor()
470 .spawn(async move {
471 let ranges = MENTIONS_SEARCH.search(&buffer, None).await;
472 (buffer, ranges)
473 })
474 .await;
475
476 this.update(&mut cx, |this, cx| {
477 let mut anchor_ranges = Vec::new();
478 let mut mentioned_user_ids = Vec::new();
479 let mut text = String::new();
480
481 this.editor.update(cx, |editor, cx| {
482 let multi_buffer = editor.buffer().read(cx).snapshot(cx);
483 for range in ranges {
484 text.clear();
485 text.extend(buffer.text_for_range(range.clone()));
486 if let Some(username) = text.strip_prefix('@') {
487 if let Some(user) = this
488 .user_store
489 .read(cx)
490 .cached_user_by_github_login(username)
491 {
492 let start = multi_buffer.anchor_after(range.start);
493 let end = multi_buffer.anchor_after(range.end);
494
495 mentioned_user_ids.push(user.id);
496 anchor_ranges.push(start..end);
497 }
498 }
499 }
500
501 editor.clear_highlights::<Self>(cx);
502 editor.highlight_text::<Self>(
503 anchor_ranges,
504 HighlightStyle {
505 font_weight: Some(FontWeight::BOLD),
506 ..Default::default()
507 },
508 cx,
509 )
510 });
511
512 this.mentions = mentioned_user_ids;
513 this.mentions_task.take();
514 })
515 .ok();
516 }
517
518 pub(crate) fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
519 self.editor.read(cx).focus_handle(cx)
520 }
521}
522
523impl Render for MessageEditor {
524 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
525 let settings = ThemeSettings::get_global(cx);
526 let text_style = TextStyle {
527 color: if self.editor.read(cx).read_only(cx) {
528 cx.theme().colors().text_disabled
529 } else {
530 cx.theme().colors().text
531 },
532 font_family: settings.ui_font.family.clone(),
533 font_features: settings.ui_font.features.clone(),
534 font_fallbacks: settings.ui_font.fallbacks.clone(),
535 font_size: TextSize::Small.rems(cx).into(),
536 font_weight: settings.ui_font.weight,
537 font_style: FontStyle::Normal,
538 line_height: relative(1.3),
539 ..Default::default()
540 };
541
542 div()
543 .w_full()
544 .px_2()
545 .py_1()
546 .bg(cx.theme().colors().editor_background)
547 .rounded_md()
548 .child(EditorElement::new(
549 &self.editor,
550 EditorStyle {
551 local_player: cx.theme().players().local(),
552 text: text_style,
553 ..Default::default()
554 },
555 ))
556 }
557}