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(Some(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::BufferEvent,
232 cx: &mut ViewContext<Self>,
233 ) {
234 if let language::BufferEvent::Reparsed | language::BufferEvent::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 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 })?;
362
363 let start_offset = end_offset - query.len();
364 let start_anchor = buffer.read(cx).anchor_before(start_offset);
365
366 let mut names = HashSet::default();
367 if let Some(chat) = self.channel_chat.as_ref() {
368 let chat = chat.read(cx);
369 for participant in ChannelStore::global(cx)
370 .read(cx)
371 .channel_participants(chat.channel_id)
372 {
373 names.insert(participant.github_login.clone());
374 }
375 for message in chat
376 .messages_in_range(chat.message_count().saturating_sub(100)..chat.message_count())
377 {
378 names.insert(message.sender.github_login.clone());
379 }
380 }
381
382 let candidates = names
383 .into_iter()
384 .map(|user| StringMatchCandidate {
385 id: 0,
386 string: user.clone(),
387 char_bag: user.chars().collect(),
388 })
389 .collect::<Vec<_>>();
390
391 Some((start_anchor, query, candidates))
392 }
393
394 fn collect_emoji_candidates(
395 &mut self,
396 buffer: &Model<Buffer>,
397 end_anchor: Anchor,
398 cx: &mut ViewContext<Self>,
399 ) -> Option<(Anchor, String, &'static [StringMatchCandidate])> {
400 static EMOJI_FUZZY_MATCH_CANDIDATES: LazyLock<Vec<StringMatchCandidate>> =
401 LazyLock::new(|| {
402 let emojis = emojis::iter()
403 .flat_map(|s| s.shortcodes())
404 .map(|emoji| StringMatchCandidate {
405 id: 0,
406 string: emoji.to_string(),
407 char_bag: emoji.chars().collect(),
408 })
409 .collect::<Vec<_>>();
410 emojis
411 });
412
413 let end_offset = end_anchor.to_offset(buffer.read(cx));
414
415 let query = buffer.update(cx, |buffer, _| {
416 let mut query = String::new();
417 for ch in buffer.reversed_chars_at(end_offset).take(100) {
418 if ch == ':' {
419 let next_char = buffer
420 .reversed_chars_at(end_offset - query.len() - 1)
421 .next();
422 // Ensure we are at the start of the message or that the previous character is a whitespace
423 if next_char.is_none() || next_char.unwrap().is_whitespace() {
424 return Some(query.chars().rev().collect::<String>());
425 }
426
427 // If the previous character is not a whitespace, we are in the middle of a word
428 // and we only want to complete the shortcode if the word is made up of other emojis
429 let mut containing_word = String::new();
430 for ch in buffer
431 .reversed_chars_at(end_offset - query.len() - 1)
432 .take(100)
433 {
434 if ch.is_whitespace() {
435 break;
436 }
437 containing_word.push(ch);
438 }
439 let containing_word = containing_word.chars().rev().collect::<String>();
440 if util::word_consists_of_emojis(containing_word.as_str()) {
441 return Some(query.chars().rev().collect::<String>());
442 }
443 break;
444 }
445 if ch.is_whitespace() || !ch.is_ascii() {
446 break;
447 }
448 query.push(ch);
449 }
450 None
451 })?;
452
453 let start_offset = end_offset - query.len() - 1;
454 let start_anchor = buffer.read(cx).anchor_before(start_offset);
455
456 Some((start_anchor, query, &EMOJI_FUZZY_MATCH_CANDIDATES))
457 }
458
459 async fn find_mentions(
460 this: WeakView<MessageEditor>,
461 buffer: BufferSnapshot,
462 mut cx: AsyncWindowContext,
463 ) {
464 let (buffer, ranges) = cx
465 .background_executor()
466 .spawn(async move {
467 let ranges = MENTIONS_SEARCH.search(&buffer, None).await;
468 (buffer, ranges)
469 })
470 .await;
471
472 this.update(&mut cx, |this, cx| {
473 let mut anchor_ranges = Vec::new();
474 let mut mentioned_user_ids = Vec::new();
475 let mut text = String::new();
476
477 this.editor.update(cx, |editor, cx| {
478 let multi_buffer = editor.buffer().read(cx).snapshot(cx);
479 for range in ranges {
480 text.clear();
481 text.extend(buffer.text_for_range(range.clone()));
482 if let Some(username) = text.strip_prefix('@') {
483 if let Some(user) = this
484 .user_store
485 .read(cx)
486 .cached_user_by_github_login(username)
487 {
488 let start = multi_buffer.anchor_after(range.start);
489 let end = multi_buffer.anchor_after(range.end);
490
491 mentioned_user_ids.push(user.id);
492 anchor_ranges.push(start..end);
493 }
494 }
495 }
496
497 editor.clear_highlights::<Self>(cx);
498 editor.highlight_text::<Self>(
499 anchor_ranges,
500 HighlightStyle {
501 font_weight: Some(FontWeight::BOLD),
502 ..Default::default()
503 },
504 cx,
505 )
506 });
507
508 this.mentions = mentioned_user_ids;
509 this.mentions_task.take();
510 })
511 .ok();
512 }
513
514 pub(crate) fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
515 self.editor.read(cx).focus_handle(cx)
516 }
517}
518
519impl Render for MessageEditor {
520 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
521 let settings = ThemeSettings::get_global(cx);
522 let text_style = TextStyle {
523 color: if self.editor.read(cx).read_only(cx) {
524 cx.theme().colors().text_disabled
525 } else {
526 cx.theme().colors().text
527 },
528 font_family: settings.ui_font.family.clone(),
529 font_features: settings.ui_font.features.clone(),
530 font_fallbacks: settings.ui_font.fallbacks.clone(),
531 font_size: TextSize::Small.rems(cx).into(),
532 font_weight: settings.ui_font.weight,
533 font_style: FontStyle::Normal,
534 line_height: relative(1.3),
535 ..Default::default()
536 };
537
538 div()
539 .w_full()
540 .px_2()
541 .py_1()
542 .bg(cx.theme().colors().editor_background)
543 .rounded_md()
544 .child(EditorElement::new(
545 &self.editor,
546 EditorStyle {
547 local_player: cx.theme().players().local(),
548 text: text_style,
549 ..Default::default()
550 },
551 ))
552 }
553}