1use crate::{
2 assistant_settings::AssistantSettings, humanize_token_count, prompts::generate_content_prompt,
3 AssistantPanel, AssistantPanelEvent, CompletionProvider, Hunk, StreamingDiff,
4};
5use anyhow::{anyhow, Context as _, Result};
6use client::telemetry::Telemetry;
7use collections::{hash_map, HashMap, HashSet, VecDeque};
8use editor::{
9 actions::{MoveDown, MoveUp, SelectAll},
10 display_map::{
11 BlockContext, BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, RenderBlock,
12 ToDisplayPoint,
13 },
14 Anchor, AnchorRangeExt, Editor, EditorElement, EditorEvent, EditorMode, EditorStyle,
15 ExcerptRange, GutterDimensions, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
16};
17use fs::Fs;
18use futures::{
19 channel::mpsc,
20 future::LocalBoxFuture,
21 stream::{self, BoxStream},
22 SinkExt, Stream, StreamExt,
23};
24use gpui::{
25 point, AppContext, EventEmitter, FocusHandle, FocusableView, Global, HighlightStyle, Model,
26 ModelContext, Subscription, Task, TextStyle, UpdateGlobal, View, ViewContext, WeakView,
27 WindowContext,
28};
29use language::{Buffer, Point, Selection, TransactionId};
30use language_model::{LanguageModelRequest, LanguageModelRequestMessage, Role};
31use multi_buffer::MultiBufferRow;
32use parking_lot::Mutex;
33use rope::Rope;
34use settings::{update_settings_file, Settings};
35use similar::TextDiff;
36use smol::future::FutureExt;
37use std::{
38 cmp,
39 future::Future,
40 mem,
41 ops::{Range, RangeInclusive},
42 pin::Pin,
43 sync::Arc,
44 task::{self, Poll},
45 time::{Duration, Instant},
46};
47use theme::ThemeSettings;
48use ui::{prelude::*, ContextMenu, IconButtonShape, PopoverMenu, Tooltip};
49use util::RangeExt;
50use workspace::{notifications::NotificationId, Toast, Workspace};
51
52pub fn init(fs: Arc<dyn Fs>, telemetry: Arc<Telemetry>, cx: &mut AppContext) {
53 cx.set_global(InlineAssistant::new(fs, telemetry));
54}
55
56const PROMPT_HISTORY_MAX_LEN: usize = 20;
57
58pub struct InlineAssistant {
59 next_assist_id: InlineAssistId,
60 next_assist_group_id: InlineAssistGroupId,
61 assists: HashMap<InlineAssistId, InlineAssist>,
62 assists_by_editor: HashMap<WeakView<Editor>, EditorInlineAssists>,
63 assist_groups: HashMap<InlineAssistGroupId, InlineAssistGroup>,
64 prompt_history: VecDeque<String>,
65 telemetry: Option<Arc<Telemetry>>,
66 fs: Arc<dyn Fs>,
67}
68
69impl Global for InlineAssistant {}
70
71impl InlineAssistant {
72 pub fn new(fs: Arc<dyn Fs>, telemetry: Arc<Telemetry>) -> Self {
73 Self {
74 next_assist_id: InlineAssistId::default(),
75 next_assist_group_id: InlineAssistGroupId::default(),
76 assists: HashMap::default(),
77 assists_by_editor: HashMap::default(),
78 assist_groups: HashMap::default(),
79 prompt_history: VecDeque::default(),
80 telemetry: Some(telemetry),
81 fs,
82 }
83 }
84
85 pub fn assist(
86 &mut self,
87 editor: &View<Editor>,
88 workspace: Option<WeakView<Workspace>>,
89 assistant_panel: Option<&View<AssistantPanel>>,
90 initial_prompt: Option<String>,
91 cx: &mut WindowContext,
92 ) {
93 let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
94
95 let mut selections = Vec::<Selection<Point>>::new();
96 let mut newest_selection = None;
97 for mut selection in editor.read(cx).selections.all::<Point>(cx) {
98 if selection.end > selection.start {
99 selection.start.column = 0;
100 // If the selection ends at the start of the line, we don't want to include it.
101 if selection.end.column == 0 {
102 selection.end.row -= 1;
103 }
104 selection.end.column = snapshot.line_len(MultiBufferRow(selection.end.row));
105 }
106
107 if let Some(prev_selection) = selections.last_mut() {
108 if selection.start <= prev_selection.end {
109 prev_selection.end = selection.end;
110 continue;
111 }
112 }
113
114 let latest_selection = newest_selection.get_or_insert_with(|| selection.clone());
115 if selection.id > latest_selection.id {
116 *latest_selection = selection.clone();
117 }
118 selections.push(selection);
119 }
120 let newest_selection = newest_selection.unwrap();
121
122 let mut codegen_ranges = Vec::new();
123 for (excerpt_id, buffer, buffer_range) in
124 snapshot.excerpts_in_ranges(selections.iter().map(|selection| {
125 snapshot.anchor_before(selection.start)..snapshot.anchor_after(selection.end)
126 }))
127 {
128 let start = Anchor {
129 buffer_id: Some(buffer.remote_id()),
130 excerpt_id,
131 text_anchor: buffer.anchor_before(buffer_range.start),
132 };
133 let end = Anchor {
134 buffer_id: Some(buffer.remote_id()),
135 excerpt_id,
136 text_anchor: buffer.anchor_after(buffer_range.end),
137 };
138 codegen_ranges.push(start..end);
139 }
140
141 let assist_group_id = self.next_assist_group_id.post_inc();
142 let prompt_buffer =
143 cx.new_model(|cx| Buffer::local(initial_prompt.unwrap_or_default(), cx));
144 let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
145
146 let mut assists = Vec::new();
147 let mut assist_to_focus = None;
148 for range in codegen_ranges {
149 let assist_id = self.next_assist_id.post_inc();
150 let codegen = cx.new_model(|cx| {
151 Codegen::new(
152 editor.read(cx).buffer().clone(),
153 range.clone(),
154 None,
155 self.telemetry.clone(),
156 cx,
157 )
158 });
159
160 let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
161 let prompt_editor = cx.new_view(|cx| {
162 PromptEditor::new(
163 assist_id,
164 gutter_dimensions.clone(),
165 self.prompt_history.clone(),
166 prompt_buffer.clone(),
167 codegen.clone(),
168 editor,
169 assistant_panel,
170 workspace.clone(),
171 self.fs.clone(),
172 cx,
173 )
174 });
175
176 if assist_to_focus.is_none() {
177 let focus_assist = if newest_selection.reversed {
178 range.start.to_point(&snapshot) == newest_selection.start
179 } else {
180 range.end.to_point(&snapshot) == newest_selection.end
181 };
182 if focus_assist {
183 assist_to_focus = Some(assist_id);
184 }
185 }
186
187 let [prompt_block_id, end_block_id] =
188 self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
189
190 assists.push((assist_id, prompt_editor, prompt_block_id, end_block_id));
191 }
192
193 let editor_assists = self
194 .assists_by_editor
195 .entry(editor.downgrade())
196 .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
197 let mut assist_group = InlineAssistGroup::new();
198 for (assist_id, prompt_editor, prompt_block_id, end_block_id) in assists {
199 self.assists.insert(
200 assist_id,
201 InlineAssist::new(
202 assist_id,
203 assist_group_id,
204 assistant_panel.is_some(),
205 editor,
206 &prompt_editor,
207 prompt_block_id,
208 end_block_id,
209 prompt_editor.read(cx).codegen.clone(),
210 workspace.clone(),
211 cx,
212 ),
213 );
214 assist_group.assist_ids.push(assist_id);
215 editor_assists.assist_ids.push(assist_id);
216 }
217 self.assist_groups.insert(assist_group_id, assist_group);
218
219 if let Some(assist_id) = assist_to_focus {
220 self.focus_assist(assist_id, cx);
221 }
222 }
223
224 #[allow(clippy::too_many_arguments)]
225 pub fn suggest_assist(
226 &mut self,
227 editor: &View<Editor>,
228 mut range: Range<Anchor>,
229 initial_prompt: String,
230 initial_insertion: Option<String>,
231 workspace: Option<WeakView<Workspace>>,
232 assistant_panel: Option<&View<AssistantPanel>>,
233 cx: &mut WindowContext,
234 ) -> InlineAssistId {
235 let assist_group_id = self.next_assist_group_id.post_inc();
236 let prompt_buffer = cx.new_model(|cx| Buffer::local(&initial_prompt, cx));
237 let prompt_buffer = cx.new_model(|cx| MultiBuffer::singleton(prompt_buffer, cx));
238
239 let assist_id = self.next_assist_id.post_inc();
240
241 let buffer = editor.read(cx).buffer().clone();
242 let prepend_transaction_id = initial_insertion.and_then(|initial_insertion| {
243 buffer.update(cx, |buffer, cx| {
244 buffer.start_transaction(cx);
245 buffer.edit([(range.start..range.start, initial_insertion)], None, cx);
246 buffer.end_transaction(cx)
247 })
248 });
249
250 range.start = range.start.bias_left(&buffer.read(cx).read(cx));
251 range.end = range.end.bias_right(&buffer.read(cx).read(cx));
252
253 let codegen = cx.new_model(|cx| {
254 Codegen::new(
255 editor.read(cx).buffer().clone(),
256 range.clone(),
257 prepend_transaction_id,
258 self.telemetry.clone(),
259 cx,
260 )
261 });
262
263 let gutter_dimensions = Arc::new(Mutex::new(GutterDimensions::default()));
264 let prompt_editor = cx.new_view(|cx| {
265 PromptEditor::new(
266 assist_id,
267 gutter_dimensions.clone(),
268 self.prompt_history.clone(),
269 prompt_buffer.clone(),
270 codegen.clone(),
271 editor,
272 assistant_panel,
273 workspace.clone(),
274 self.fs.clone(),
275 cx,
276 )
277 });
278
279 let [prompt_block_id, end_block_id] =
280 self.insert_assist_blocks(editor, &range, &prompt_editor, cx);
281
282 let editor_assists = self
283 .assists_by_editor
284 .entry(editor.downgrade())
285 .or_insert_with(|| EditorInlineAssists::new(&editor, cx));
286
287 let mut assist_group = InlineAssistGroup::new();
288 self.assists.insert(
289 assist_id,
290 InlineAssist::new(
291 assist_id,
292 assist_group_id,
293 assistant_panel.is_some(),
294 editor,
295 &prompt_editor,
296 prompt_block_id,
297 end_block_id,
298 prompt_editor.read(cx).codegen.clone(),
299 workspace.clone(),
300 cx,
301 ),
302 );
303 assist_group.assist_ids.push(assist_id);
304 editor_assists.assist_ids.push(assist_id);
305 self.assist_groups.insert(assist_group_id, assist_group);
306 assist_id
307 }
308
309 fn insert_assist_blocks(
310 &self,
311 editor: &View<Editor>,
312 range: &Range<Anchor>,
313 prompt_editor: &View<PromptEditor>,
314 cx: &mut WindowContext,
315 ) -> [CustomBlockId; 2] {
316 let assist_blocks = vec![
317 BlockProperties {
318 style: BlockStyle::Sticky,
319 position: range.start,
320 height: prompt_editor.read(cx).height_in_lines,
321 render: build_assist_editor_renderer(prompt_editor),
322 disposition: BlockDisposition::Above,
323 },
324 BlockProperties {
325 style: BlockStyle::Sticky,
326 position: range.end,
327 height: 1,
328 render: Box::new(|cx| {
329 v_flex()
330 .h_full()
331 .w_full()
332 .border_t_1()
333 .border_color(cx.theme().status().info_border)
334 .into_any_element()
335 }),
336 disposition: BlockDisposition::Below,
337 },
338 ];
339
340 editor.update(cx, |editor, cx| {
341 let block_ids = editor.insert_blocks(assist_blocks, None, cx);
342 [block_ids[0], block_ids[1]]
343 })
344 }
345
346 fn handle_prompt_editor_focus_in(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
347 let assist = &self.assists[&assist_id];
348 let Some(decorations) = assist.decorations.as_ref() else {
349 return;
350 };
351 let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
352 let editor_assists = self.assists_by_editor.get_mut(&assist.editor).unwrap();
353
354 assist_group.active_assist_id = Some(assist_id);
355 if assist_group.linked {
356 for assist_id in &assist_group.assist_ids {
357 if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
358 decorations.prompt_editor.update(cx, |prompt_editor, cx| {
359 prompt_editor.set_show_cursor_when_unfocused(true, cx)
360 });
361 }
362 }
363 }
364
365 assist
366 .editor
367 .update(cx, |editor, cx| {
368 let scroll_top = editor.scroll_position(cx).y;
369 let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
370 let prompt_row = editor
371 .row_for_block(decorations.prompt_block_id, cx)
372 .unwrap()
373 .0 as f32;
374
375 if (scroll_top..scroll_bottom).contains(&prompt_row) {
376 editor_assists.scroll_lock = Some(InlineAssistScrollLock {
377 assist_id,
378 distance_from_top: prompt_row - scroll_top,
379 });
380 } else {
381 editor_assists.scroll_lock = None;
382 }
383 })
384 .ok();
385 }
386
387 fn handle_prompt_editor_focus_out(
388 &mut self,
389 assist_id: InlineAssistId,
390 cx: &mut WindowContext,
391 ) {
392 let assist = &self.assists[&assist_id];
393 let assist_group = self.assist_groups.get_mut(&assist.group_id).unwrap();
394 if assist_group.active_assist_id == Some(assist_id) {
395 assist_group.active_assist_id = None;
396 if assist_group.linked {
397 for assist_id in &assist_group.assist_ids {
398 if let Some(decorations) = self.assists[assist_id].decorations.as_ref() {
399 decorations.prompt_editor.update(cx, |prompt_editor, cx| {
400 prompt_editor.set_show_cursor_when_unfocused(false, cx)
401 });
402 }
403 }
404 }
405 }
406 }
407
408 fn handle_prompt_editor_event(
409 &mut self,
410 prompt_editor: View<PromptEditor>,
411 event: &PromptEditorEvent,
412 cx: &mut WindowContext,
413 ) {
414 let assist_id = prompt_editor.read(cx).id;
415 match event {
416 PromptEditorEvent::StartRequested => {
417 self.start_assist(assist_id, cx);
418 }
419 PromptEditorEvent::StopRequested => {
420 self.stop_assist(assist_id, cx);
421 }
422 PromptEditorEvent::ConfirmRequested => {
423 self.finish_assist(assist_id, false, cx);
424 }
425 PromptEditorEvent::CancelRequested => {
426 self.finish_assist(assist_id, true, cx);
427 }
428 PromptEditorEvent::DismissRequested => {
429 self.dismiss_assist(assist_id, cx);
430 }
431 PromptEditorEvent::Resized { height_in_lines } => {
432 self.resize_assist(assist_id, *height_in_lines, cx);
433 }
434 }
435 }
436
437 fn handle_editor_newline(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
438 let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
439 return;
440 };
441
442 let editor = editor.read(cx);
443 if editor.selections.count() == 1 {
444 let selection = editor.selections.newest::<usize>(cx);
445 let buffer = editor.buffer().read(cx).snapshot(cx);
446 for assist_id in &editor_assists.assist_ids {
447 let assist = &self.assists[assist_id];
448 let assist_range = assist.codegen.read(cx).range.to_offset(&buffer);
449 if assist_range.contains(&selection.start) && assist_range.contains(&selection.end)
450 {
451 if matches!(assist.codegen.read(cx).status, CodegenStatus::Pending) {
452 self.dismiss_assist(*assist_id, cx);
453 } else {
454 self.finish_assist(*assist_id, false, cx);
455 }
456
457 return;
458 }
459 }
460 }
461
462 cx.propagate();
463 }
464
465 fn handle_editor_cancel(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
466 let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
467 return;
468 };
469
470 let editor = editor.read(cx);
471 if editor.selections.count() == 1 {
472 let selection = editor.selections.newest::<usize>(cx);
473 let buffer = editor.buffer().read(cx).snapshot(cx);
474 for assist_id in &editor_assists.assist_ids {
475 let assist = &self.assists[assist_id];
476 let assist_range = assist.codegen.read(cx).range.to_offset(&buffer);
477 if assist.decorations.is_some()
478 && assist_range.contains(&selection.start)
479 && assist_range.contains(&selection.end)
480 {
481 self.focus_assist(*assist_id, cx);
482 return;
483 }
484 }
485 }
486
487 cx.propagate();
488 }
489
490 fn handle_editor_release(&mut self, editor: WeakView<Editor>, cx: &mut WindowContext) {
491 if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor) {
492 for assist_id in editor_assists.assist_ids.clone() {
493 self.finish_assist(assist_id, true, cx);
494 }
495 }
496 }
497
498 fn handle_editor_change(&mut self, editor: View<Editor>, cx: &mut WindowContext) {
499 let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) else {
500 return;
501 };
502 let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() else {
503 return;
504 };
505 let assist = &self.assists[&scroll_lock.assist_id];
506 let Some(decorations) = assist.decorations.as_ref() else {
507 return;
508 };
509
510 editor.update(cx, |editor, cx| {
511 let scroll_position = editor.scroll_position(cx);
512 let target_scroll_top = editor
513 .row_for_block(decorations.prompt_block_id, cx)
514 .unwrap()
515 .0 as f32
516 - scroll_lock.distance_from_top;
517 if target_scroll_top != scroll_position.y {
518 editor.set_scroll_position(point(scroll_position.x, target_scroll_top), cx);
519 }
520 });
521 }
522
523 fn handle_editor_event(
524 &mut self,
525 editor: View<Editor>,
526 event: &EditorEvent,
527 cx: &mut WindowContext,
528 ) {
529 let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) else {
530 return;
531 };
532
533 match event {
534 EditorEvent::Saved => {
535 for assist_id in editor_assists.assist_ids.clone() {
536 let assist = &self.assists[&assist_id];
537 if let CodegenStatus::Done = &assist.codegen.read(cx).status {
538 self.finish_assist(assist_id, false, cx)
539 }
540 }
541 }
542 EditorEvent::Edited { transaction_id } => {
543 let buffer = editor.read(cx).buffer().read(cx);
544 let edited_ranges =
545 buffer.edited_ranges_for_transaction::<usize>(*transaction_id, cx);
546 let snapshot = buffer.snapshot(cx);
547
548 for assist_id in editor_assists.assist_ids.clone() {
549 let assist = &self.assists[&assist_id];
550 if matches!(
551 assist.codegen.read(cx).status,
552 CodegenStatus::Error(_) | CodegenStatus::Done
553 ) {
554 let assist_range = assist.codegen.read(cx).range.to_offset(&snapshot);
555 if edited_ranges
556 .iter()
557 .any(|range| range.overlaps(&assist_range))
558 {
559 self.finish_assist(assist_id, false, cx);
560 }
561 }
562 }
563 }
564 EditorEvent::ScrollPositionChanged { .. } => {
565 if let Some(scroll_lock) = editor_assists.scroll_lock.as_ref() {
566 let assist = &self.assists[&scroll_lock.assist_id];
567 if let Some(decorations) = assist.decorations.as_ref() {
568 let distance_from_top = editor.update(cx, |editor, cx| {
569 let scroll_top = editor.scroll_position(cx).y;
570 let prompt_row = editor
571 .row_for_block(decorations.prompt_block_id, cx)
572 .unwrap()
573 .0 as f32;
574 prompt_row - scroll_top
575 });
576
577 if distance_from_top != scroll_lock.distance_from_top {
578 editor_assists.scroll_lock = None;
579 }
580 }
581 }
582 }
583 EditorEvent::SelectionsChanged { .. } => {
584 for assist_id in editor_assists.assist_ids.clone() {
585 let assist = &self.assists[&assist_id];
586 if let Some(decorations) = assist.decorations.as_ref() {
587 if decorations.prompt_editor.focus_handle(cx).is_focused(cx) {
588 return;
589 }
590 }
591 }
592
593 editor_assists.scroll_lock = None;
594 }
595 _ => {}
596 }
597 }
598
599 fn finish_assist(&mut self, assist_id: InlineAssistId, undo: bool, cx: &mut WindowContext) {
600 if let Some(assist) = self.assists.get(&assist_id) {
601 let assist_group_id = assist.group_id;
602 if self.assist_groups[&assist_group_id].linked {
603 for assist_id in self.unlink_assist_group(assist_group_id, cx) {
604 self.finish_assist(assist_id, undo, cx);
605 }
606 return;
607 }
608 }
609
610 self.dismiss_assist(assist_id, cx);
611
612 if let Some(assist) = self.assists.remove(&assist_id) {
613 if let hash_map::Entry::Occupied(mut entry) = self.assist_groups.entry(assist.group_id)
614 {
615 entry.get_mut().assist_ids.retain(|id| *id != assist_id);
616 if entry.get().assist_ids.is_empty() {
617 entry.remove();
618 }
619 }
620
621 if let hash_map::Entry::Occupied(mut entry) =
622 self.assists_by_editor.entry(assist.editor.clone())
623 {
624 entry.get_mut().assist_ids.retain(|id| *id != assist_id);
625 if entry.get().assist_ids.is_empty() {
626 entry.remove();
627 if let Some(editor) = assist.editor.upgrade() {
628 self.update_editor_highlights(&editor, cx);
629 }
630 } else {
631 entry.get().highlight_updates.send(()).ok();
632 }
633 }
634
635 if undo {
636 assist.codegen.update(cx, |codegen, cx| codegen.undo(cx));
637 }
638 }
639 }
640
641 fn dismiss_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) -> bool {
642 let Some(assist) = self.assists.get_mut(&assist_id) else {
643 return false;
644 };
645 let Some(editor) = assist.editor.upgrade() else {
646 return false;
647 };
648 let Some(decorations) = assist.decorations.take() else {
649 return false;
650 };
651
652 editor.update(cx, |editor, cx| {
653 let mut to_remove = decorations.removed_line_block_ids;
654 to_remove.insert(decorations.prompt_block_id);
655 to_remove.insert(decorations.end_block_id);
656 editor.remove_blocks(to_remove, None, cx);
657 });
658
659 if decorations
660 .prompt_editor
661 .focus_handle(cx)
662 .contains_focused(cx)
663 {
664 self.focus_next_assist(assist_id, cx);
665 }
666
667 if let Some(editor_assists) = self.assists_by_editor.get_mut(&editor.downgrade()) {
668 if editor_assists
669 .scroll_lock
670 .as_ref()
671 .map_or(false, |lock| lock.assist_id == assist_id)
672 {
673 editor_assists.scroll_lock = None;
674 }
675 editor_assists.highlight_updates.send(()).ok();
676 }
677
678 true
679 }
680
681 fn focus_next_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
682 let Some(assist) = self.assists.get(&assist_id) else {
683 return;
684 };
685
686 let assist_group = &self.assist_groups[&assist.group_id];
687 let assist_ix = assist_group
688 .assist_ids
689 .iter()
690 .position(|id| *id == assist_id)
691 .unwrap();
692 let assist_ids = assist_group
693 .assist_ids
694 .iter()
695 .skip(assist_ix + 1)
696 .chain(assist_group.assist_ids.iter().take(assist_ix));
697
698 for assist_id in assist_ids {
699 let assist = &self.assists[assist_id];
700 if assist.decorations.is_some() {
701 self.focus_assist(*assist_id, cx);
702 return;
703 }
704 }
705
706 assist.editor.update(cx, |editor, cx| editor.focus(cx)).ok();
707 }
708
709 fn focus_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
710 let assist = &self.assists[&assist_id];
711 let Some(editor) = assist.editor.upgrade() else {
712 return;
713 };
714
715 if let Some(decorations) = assist.decorations.as_ref() {
716 decorations.prompt_editor.update(cx, |prompt_editor, cx| {
717 prompt_editor.editor.update(cx, |editor, cx| {
718 editor.focus(cx);
719 editor.select_all(&SelectAll, cx);
720 })
721 });
722 }
723
724 let position = assist.codegen.read(cx).range.start;
725 editor.update(cx, |editor, cx| {
726 editor.change_selections(None, cx, |selections| {
727 selections.select_anchor_ranges([position..position])
728 });
729
730 let mut scroll_target_top;
731 let mut scroll_target_bottom;
732 if let Some(decorations) = assist.decorations.as_ref() {
733 scroll_target_top = editor
734 .row_for_block(decorations.prompt_block_id, cx)
735 .unwrap()
736 .0 as f32;
737 scroll_target_bottom = editor
738 .row_for_block(decorations.end_block_id, cx)
739 .unwrap()
740 .0 as f32;
741 } else {
742 let snapshot = editor.snapshot(cx);
743 let codegen = assist.codegen.read(cx);
744 let start_row = codegen
745 .range
746 .start
747 .to_display_point(&snapshot.display_snapshot)
748 .row();
749 scroll_target_top = start_row.0 as f32;
750 scroll_target_bottom = scroll_target_top + 1.;
751 }
752 scroll_target_top -= editor.vertical_scroll_margin() as f32;
753 scroll_target_bottom += editor.vertical_scroll_margin() as f32;
754
755 let height_in_lines = editor.visible_line_count().unwrap_or(0.);
756 let scroll_top = editor.scroll_position(cx).y;
757 let scroll_bottom = scroll_top + height_in_lines;
758
759 if scroll_target_top < scroll_top {
760 editor.set_scroll_position(point(0., scroll_target_top), cx);
761 } else if scroll_target_bottom > scroll_bottom {
762 if (scroll_target_bottom - scroll_target_top) <= height_in_lines {
763 editor
764 .set_scroll_position(point(0., scroll_target_bottom - height_in_lines), cx);
765 } else {
766 editor.set_scroll_position(point(0., scroll_target_top), cx);
767 }
768 }
769 });
770 }
771
772 fn resize_assist(
773 &mut self,
774 assist_id: InlineAssistId,
775 height_in_lines: u8,
776 cx: &mut WindowContext,
777 ) {
778 if let Some(assist) = self.assists.get_mut(&assist_id) {
779 if let Some(editor) = assist.editor.upgrade() {
780 if let Some(decorations) = assist.decorations.as_ref() {
781 let mut new_blocks = HashMap::default();
782 new_blocks.insert(
783 decorations.prompt_block_id,
784 (
785 Some(height_in_lines),
786 build_assist_editor_renderer(&decorations.prompt_editor),
787 ),
788 );
789 editor.update(cx, |editor, cx| {
790 editor
791 .display_map
792 .update(cx, |map, cx| map.replace_blocks(new_blocks, cx))
793 });
794 }
795 }
796 }
797 }
798
799 fn unlink_assist_group(
800 &mut self,
801 assist_group_id: InlineAssistGroupId,
802 cx: &mut WindowContext,
803 ) -> Vec<InlineAssistId> {
804 let assist_group = self.assist_groups.get_mut(&assist_group_id).unwrap();
805 assist_group.linked = false;
806 for assist_id in &assist_group.assist_ids {
807 let assist = self.assists.get_mut(assist_id).unwrap();
808 if let Some(editor_decorations) = assist.decorations.as_ref() {
809 editor_decorations
810 .prompt_editor
811 .update(cx, |prompt_editor, cx| prompt_editor.unlink(cx));
812 }
813 }
814 assist_group.assist_ids.clone()
815 }
816
817 pub fn start_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
818 let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
819 assist
820 } else {
821 return;
822 };
823
824 let assist_group_id = assist.group_id;
825 if self.assist_groups[&assist_group_id].linked {
826 for assist_id in self.unlink_assist_group(assist_group_id, cx) {
827 self.start_assist(assist_id, cx);
828 }
829 return;
830 }
831
832 let Some(user_prompt) = assist
833 .decorations
834 .as_ref()
835 .map(|decorations| decorations.prompt_editor.read(cx).prompt(cx))
836 else {
837 return;
838 };
839
840 self.prompt_history.retain(|prompt| *prompt != user_prompt);
841 self.prompt_history.push_back(user_prompt.clone());
842 if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
843 self.prompt_history.pop_front();
844 }
845
846 let codegen = assist.codegen.clone();
847 let telemetry_id = CompletionProvider::global(cx).model().telemetry_id();
848 let chunks: LocalBoxFuture<Result<BoxStream<Result<String>>>> =
849 if user_prompt.trim().to_lowercase() == "delete" {
850 async { Ok(stream::empty().boxed()) }.boxed_local()
851 } else {
852 let request = self.request_for_inline_assist(assist_id, cx);
853 let mut cx = cx.to_async();
854 async move {
855 let request = request.await?;
856 let chunks = cx
857 .update(|cx| CompletionProvider::global(cx).stream_completion(request, cx))?
858 .await?;
859 Ok(chunks.boxed())
860 }
861 .boxed_local()
862 };
863 codegen.update(cx, |codegen, cx| {
864 codegen.start(telemetry_id, chunks, cx);
865 });
866 }
867
868 fn request_for_inline_assist(
869 &self,
870 assist_id: InlineAssistId,
871 cx: &mut WindowContext,
872 ) -> Task<Result<LanguageModelRequest>> {
873 cx.spawn(|mut cx| async move {
874 let (user_prompt, context_request, project_name, buffer, range, model) = cx
875 .read_global(|this: &InlineAssistant, cx: &WindowContext| {
876 let assist = this.assists.get(&assist_id).context("invalid assist")?;
877 let decorations = assist.decorations.as_ref().context("invalid assist")?;
878 let editor = assist.editor.upgrade().context("invalid assist")?;
879 let user_prompt = decorations.prompt_editor.read(cx).prompt(cx);
880 let context_request = if assist.include_context {
881 assist.workspace.as_ref().and_then(|workspace| {
882 let workspace = workspace.upgrade()?.read(cx);
883 let assistant_panel = workspace.panel::<AssistantPanel>(cx)?;
884 Some(
885 assistant_panel
886 .read(cx)
887 .active_context(cx)?
888 .read(cx)
889 .to_completion_request(cx),
890 )
891 })
892 } else {
893 None
894 };
895 let project_name = assist.workspace.as_ref().and_then(|workspace| {
896 let workspace = workspace.upgrade()?;
897 Some(
898 workspace
899 .read(cx)
900 .project()
901 .read(cx)
902 .worktree_root_names(cx)
903 .collect::<Vec<&str>>()
904 .join("/"),
905 )
906 });
907 let buffer = editor.read(cx).buffer().read(cx).snapshot(cx);
908 let range = assist.codegen.read(cx).range.clone();
909 let model = CompletionProvider::global(cx).model();
910 anyhow::Ok((
911 user_prompt,
912 context_request,
913 project_name,
914 buffer,
915 range,
916 model,
917 ))
918 })??;
919
920 let language = buffer.language_at(range.start);
921 let language_name = if let Some(language) = language.as_ref() {
922 if Arc::ptr_eq(language, &language::PLAIN_TEXT) {
923 None
924 } else {
925 Some(language.name())
926 }
927 } else {
928 None
929 };
930
931 // Higher Temperature increases the randomness of model outputs.
932 // If Markdown or No Language is Known, increase the randomness for more creative output
933 // If Code, decrease temperature to get more deterministic outputs
934 let temperature = if let Some(language) = language_name.clone() {
935 if language.as_ref() == "Markdown" {
936 1.0
937 } else {
938 0.5
939 }
940 } else {
941 1.0
942 };
943
944 let prompt = cx
945 .background_executor()
946 .spawn(async move {
947 let language_name = language_name.as_deref();
948 let start = buffer.point_to_buffer_offset(range.start);
949 let end = buffer.point_to_buffer_offset(range.end);
950 let (buffer, range) = if let Some((start, end)) = start.zip(end) {
951 let (start_buffer, start_buffer_offset) = start;
952 let (end_buffer, end_buffer_offset) = end;
953 if start_buffer.remote_id() == end_buffer.remote_id() {
954 (start_buffer.clone(), start_buffer_offset..end_buffer_offset)
955 } else {
956 return Err(anyhow!("invalid transformation range"));
957 }
958 } else {
959 return Err(anyhow!("invalid transformation range"));
960 };
961 generate_content_prompt(user_prompt, language_name, buffer, range, project_name)
962 })
963 .await?;
964
965 let mut messages = Vec::new();
966 if let Some(context_request) = context_request {
967 messages = context_request.messages;
968 }
969
970 messages.push(LanguageModelRequestMessage {
971 role: Role::User,
972 content: prompt,
973 });
974
975 Ok(LanguageModelRequest {
976 model,
977 messages,
978 stop: vec!["|END|>".to_string()],
979 temperature,
980 })
981 })
982 }
983
984 pub fn stop_assist(&mut self, assist_id: InlineAssistId, cx: &mut WindowContext) {
985 let assist = if let Some(assist) = self.assists.get_mut(&assist_id) {
986 assist
987 } else {
988 return;
989 };
990
991 assist.codegen.update(cx, |codegen, cx| codegen.stop(cx));
992 }
993
994 fn update_editor_highlights(&self, editor: &View<Editor>, cx: &mut WindowContext) {
995 let mut gutter_pending_ranges = Vec::new();
996 let mut gutter_transformed_ranges = Vec::new();
997 let mut foreground_ranges = Vec::new();
998 let mut inserted_row_ranges = Vec::new();
999 let empty_assist_ids = Vec::new();
1000 let assist_ids = self
1001 .assists_by_editor
1002 .get(&editor.downgrade())
1003 .map_or(&empty_assist_ids, |editor_assists| {
1004 &editor_assists.assist_ids
1005 });
1006
1007 for assist_id in assist_ids {
1008 if let Some(assist) = self.assists.get(assist_id) {
1009 let codegen = assist.codegen.read(cx);
1010 foreground_ranges.extend(codegen.last_equal_ranges().iter().cloned());
1011
1012 if codegen.edit_position != codegen.range.end {
1013 gutter_pending_ranges.push(codegen.edit_position..codegen.range.end);
1014 }
1015
1016 if codegen.range.start != codegen.edit_position {
1017 gutter_transformed_ranges.push(codegen.range.start..codegen.edit_position);
1018 }
1019
1020 if assist.decorations.is_some() {
1021 inserted_row_ranges.extend(codegen.diff.inserted_row_ranges.iter().cloned());
1022 }
1023 }
1024 }
1025
1026 let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
1027 merge_ranges(&mut foreground_ranges, &snapshot);
1028 merge_ranges(&mut gutter_pending_ranges, &snapshot);
1029 merge_ranges(&mut gutter_transformed_ranges, &snapshot);
1030 editor.update(cx, |editor, cx| {
1031 enum GutterPendingRange {}
1032 if gutter_pending_ranges.is_empty() {
1033 editor.clear_gutter_highlights::<GutterPendingRange>(cx);
1034 } else {
1035 editor.highlight_gutter::<GutterPendingRange>(
1036 &gutter_pending_ranges,
1037 |cx| cx.theme().status().info_background,
1038 cx,
1039 )
1040 }
1041
1042 enum GutterTransformedRange {}
1043 if gutter_transformed_ranges.is_empty() {
1044 editor.clear_gutter_highlights::<GutterTransformedRange>(cx);
1045 } else {
1046 editor.highlight_gutter::<GutterTransformedRange>(
1047 &gutter_transformed_ranges,
1048 |cx| cx.theme().status().info,
1049 cx,
1050 )
1051 }
1052
1053 if foreground_ranges.is_empty() {
1054 editor.clear_highlights::<InlineAssist>(cx);
1055 } else {
1056 editor.highlight_text::<InlineAssist>(
1057 foreground_ranges,
1058 HighlightStyle {
1059 fade_out: Some(0.6),
1060 ..Default::default()
1061 },
1062 cx,
1063 );
1064 }
1065
1066 editor.clear_row_highlights::<InlineAssist>();
1067 for row_range in inserted_row_ranges {
1068 editor.highlight_rows::<InlineAssist>(
1069 row_range,
1070 Some(cx.theme().status().info_background),
1071 false,
1072 cx,
1073 );
1074 }
1075 });
1076 }
1077
1078 fn update_editor_blocks(
1079 &mut self,
1080 editor: &View<Editor>,
1081 assist_id: InlineAssistId,
1082 cx: &mut WindowContext,
1083 ) {
1084 let Some(assist) = self.assists.get_mut(&assist_id) else {
1085 return;
1086 };
1087 let Some(decorations) = assist.decorations.as_mut() else {
1088 return;
1089 };
1090
1091 let codegen = assist.codegen.read(cx);
1092 let old_snapshot = codegen.snapshot.clone();
1093 let old_buffer = codegen.old_buffer.clone();
1094 let deleted_row_ranges = codegen.diff.deleted_row_ranges.clone();
1095
1096 editor.update(cx, |editor, cx| {
1097 let old_blocks = mem::take(&mut decorations.removed_line_block_ids);
1098 editor.remove_blocks(old_blocks, None, cx);
1099
1100 let mut new_blocks = Vec::new();
1101 for (new_row, old_row_range) in deleted_row_ranges {
1102 let (_, buffer_start) = old_snapshot
1103 .point_to_buffer_offset(Point::new(*old_row_range.start(), 0))
1104 .unwrap();
1105 let (_, buffer_end) = old_snapshot
1106 .point_to_buffer_offset(Point::new(
1107 *old_row_range.end(),
1108 old_snapshot.line_len(MultiBufferRow(*old_row_range.end())),
1109 ))
1110 .unwrap();
1111
1112 let deleted_lines_editor = cx.new_view(|cx| {
1113 let multi_buffer = cx.new_model(|_| {
1114 MultiBuffer::without_headers(0, language::Capability::ReadOnly)
1115 });
1116 multi_buffer.update(cx, |multi_buffer, cx| {
1117 multi_buffer.push_excerpts(
1118 old_buffer.clone(),
1119 Some(ExcerptRange {
1120 context: buffer_start..buffer_end,
1121 primary: None,
1122 }),
1123 cx,
1124 );
1125 });
1126
1127 enum DeletedLines {}
1128 let mut editor = Editor::for_multibuffer(multi_buffer, None, true, cx);
1129 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
1130 editor.set_show_wrap_guides(false, cx);
1131 editor.set_show_gutter(false, cx);
1132 editor.scroll_manager.set_forbid_vertical_scroll(true);
1133 editor.set_read_only(true);
1134 editor.highlight_rows::<DeletedLines>(
1135 Anchor::min()..=Anchor::max(),
1136 Some(cx.theme().status().deleted_background),
1137 false,
1138 cx,
1139 );
1140 editor
1141 });
1142
1143 let height = deleted_lines_editor
1144 .update(cx, |editor, cx| editor.max_point(cx).row().0 as u8 + 1);
1145 new_blocks.push(BlockProperties {
1146 position: new_row,
1147 height,
1148 style: BlockStyle::Flex,
1149 render: Box::new(move |cx| {
1150 div()
1151 .bg(cx.theme().status().deleted_background)
1152 .size_full()
1153 .pl(cx.gutter_dimensions.full_width())
1154 .child(deleted_lines_editor.clone())
1155 .into_any_element()
1156 }),
1157 disposition: BlockDisposition::Above,
1158 });
1159 }
1160
1161 decorations.removed_line_block_ids = editor
1162 .insert_blocks(new_blocks, None, cx)
1163 .into_iter()
1164 .collect();
1165 })
1166 }
1167}
1168
1169struct EditorInlineAssists {
1170 assist_ids: Vec<InlineAssistId>,
1171 scroll_lock: Option<InlineAssistScrollLock>,
1172 highlight_updates: async_watch::Sender<()>,
1173 _update_highlights: Task<Result<()>>,
1174 _subscriptions: Vec<gpui::Subscription>,
1175}
1176
1177struct InlineAssistScrollLock {
1178 assist_id: InlineAssistId,
1179 distance_from_top: f32,
1180}
1181
1182impl EditorInlineAssists {
1183 #[allow(clippy::too_many_arguments)]
1184 fn new(editor: &View<Editor>, cx: &mut WindowContext) -> Self {
1185 let (highlight_updates_tx, mut highlight_updates_rx) = async_watch::channel(());
1186 Self {
1187 assist_ids: Vec::new(),
1188 scroll_lock: None,
1189 highlight_updates: highlight_updates_tx,
1190 _update_highlights: cx.spawn(|mut cx| {
1191 let editor = editor.downgrade();
1192 async move {
1193 while let Ok(()) = highlight_updates_rx.changed().await {
1194 let editor = editor.upgrade().context("editor was dropped")?;
1195 cx.update_global(|assistant: &mut InlineAssistant, cx| {
1196 assistant.update_editor_highlights(&editor, cx);
1197 })?;
1198 }
1199 Ok(())
1200 }
1201 }),
1202 _subscriptions: vec![
1203 cx.observe_release(editor, {
1204 let editor = editor.downgrade();
1205 |_, cx| {
1206 InlineAssistant::update_global(cx, |this, cx| {
1207 this.handle_editor_release(editor, cx);
1208 })
1209 }
1210 }),
1211 cx.observe(editor, move |editor, cx| {
1212 InlineAssistant::update_global(cx, |this, cx| {
1213 this.handle_editor_change(editor, cx)
1214 })
1215 }),
1216 cx.subscribe(editor, move |editor, event, cx| {
1217 InlineAssistant::update_global(cx, |this, cx| {
1218 this.handle_editor_event(editor, event, cx)
1219 })
1220 }),
1221 editor.update(cx, |editor, cx| {
1222 let editor_handle = cx.view().downgrade();
1223 editor.register_action(
1224 move |_: &editor::actions::Newline, cx: &mut WindowContext| {
1225 InlineAssistant::update_global(cx, |this, cx| {
1226 if let Some(editor) = editor_handle.upgrade() {
1227 this.handle_editor_newline(editor, cx)
1228 }
1229 })
1230 },
1231 )
1232 }),
1233 editor.update(cx, |editor, cx| {
1234 let editor_handle = cx.view().downgrade();
1235 editor.register_action(
1236 move |_: &editor::actions::Cancel, cx: &mut WindowContext| {
1237 InlineAssistant::update_global(cx, |this, cx| {
1238 if let Some(editor) = editor_handle.upgrade() {
1239 this.handle_editor_cancel(editor, cx)
1240 }
1241 })
1242 },
1243 )
1244 }),
1245 ],
1246 }
1247 }
1248}
1249
1250struct InlineAssistGroup {
1251 assist_ids: Vec<InlineAssistId>,
1252 linked: bool,
1253 active_assist_id: Option<InlineAssistId>,
1254}
1255
1256impl InlineAssistGroup {
1257 fn new() -> Self {
1258 Self {
1259 assist_ids: Vec::new(),
1260 linked: true,
1261 active_assist_id: None,
1262 }
1263 }
1264}
1265
1266fn build_assist_editor_renderer(editor: &View<PromptEditor>) -> RenderBlock {
1267 let editor = editor.clone();
1268 Box::new(move |cx: &mut BlockContext| {
1269 *editor.read(cx).gutter_dimensions.lock() = *cx.gutter_dimensions;
1270 editor.clone().into_any_element()
1271 })
1272}
1273
1274#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1275pub struct InlineAssistId(usize);
1276
1277impl InlineAssistId {
1278 fn post_inc(&mut self) -> InlineAssistId {
1279 let id = *self;
1280 self.0 += 1;
1281 id
1282 }
1283}
1284
1285#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
1286struct InlineAssistGroupId(usize);
1287
1288impl InlineAssistGroupId {
1289 fn post_inc(&mut self) -> InlineAssistGroupId {
1290 let id = *self;
1291 self.0 += 1;
1292 id
1293 }
1294}
1295
1296enum PromptEditorEvent {
1297 StartRequested,
1298 StopRequested,
1299 ConfirmRequested,
1300 CancelRequested,
1301 DismissRequested,
1302 Resized { height_in_lines: u8 },
1303}
1304
1305struct PromptEditor {
1306 id: InlineAssistId,
1307 fs: Arc<dyn Fs>,
1308 height_in_lines: u8,
1309 editor: View<Editor>,
1310 edited_since_done: bool,
1311 gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1312 prompt_history: VecDeque<String>,
1313 prompt_history_ix: Option<usize>,
1314 pending_prompt: String,
1315 codegen: Model<Codegen>,
1316 _codegen_subscription: Subscription,
1317 editor_subscriptions: Vec<Subscription>,
1318 pending_token_count: Task<Result<()>>,
1319 token_count: Option<usize>,
1320 _token_count_subscriptions: Vec<Subscription>,
1321 workspace: Option<WeakView<Workspace>>,
1322}
1323
1324impl EventEmitter<PromptEditorEvent> for PromptEditor {}
1325
1326impl Render for PromptEditor {
1327 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1328 let gutter_dimensions = *self.gutter_dimensions.lock();
1329 let fs = self.fs.clone();
1330
1331 let buttons = match &self.codegen.read(cx).status {
1332 CodegenStatus::Idle => {
1333 vec![
1334 IconButton::new("cancel", IconName::Close)
1335 .icon_color(Color::Muted)
1336 .size(ButtonSize::None)
1337 .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1338 .on_click(
1339 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1340 ),
1341 IconButton::new("start", IconName::Sparkle)
1342 .icon_color(Color::Muted)
1343 .size(ButtonSize::None)
1344 .icon_size(IconSize::XSmall)
1345 .tooltip(|cx| Tooltip::for_action("Transform", &menu::Confirm, cx))
1346 .on_click(
1347 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StartRequested)),
1348 ),
1349 ]
1350 }
1351 CodegenStatus::Pending => {
1352 vec![
1353 IconButton::new("cancel", IconName::Close)
1354 .icon_color(Color::Muted)
1355 .size(ButtonSize::None)
1356 .tooltip(|cx| Tooltip::text("Cancel Assist", cx))
1357 .on_click(
1358 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1359 ),
1360 IconButton::new("stop", IconName::Stop)
1361 .icon_color(Color::Error)
1362 .size(ButtonSize::None)
1363 .icon_size(IconSize::XSmall)
1364 .tooltip(|cx| {
1365 Tooltip::with_meta(
1366 "Interrupt Transformation",
1367 Some(&menu::Cancel),
1368 "Changes won't be discarded",
1369 cx,
1370 )
1371 })
1372 .on_click(
1373 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::StopRequested)),
1374 ),
1375 ]
1376 }
1377 CodegenStatus::Error(_) | CodegenStatus::Done => {
1378 vec![
1379 IconButton::new("cancel", IconName::Close)
1380 .icon_color(Color::Muted)
1381 .size(ButtonSize::None)
1382 .tooltip(|cx| Tooltip::for_action("Cancel Assist", &menu::Cancel, cx))
1383 .on_click(
1384 cx.listener(|_, _, cx| cx.emit(PromptEditorEvent::CancelRequested)),
1385 ),
1386 if self.edited_since_done {
1387 IconButton::new("restart", IconName::RotateCw)
1388 .icon_color(Color::Info)
1389 .icon_size(IconSize::XSmall)
1390 .size(ButtonSize::None)
1391 .tooltip(|cx| {
1392 Tooltip::with_meta(
1393 "Restart Transformation",
1394 Some(&menu::Confirm),
1395 "Changes will be discarded",
1396 cx,
1397 )
1398 })
1399 .on_click(cx.listener(|_, _, cx| {
1400 cx.emit(PromptEditorEvent::StartRequested);
1401 }))
1402 } else {
1403 IconButton::new("confirm", IconName::Check)
1404 .icon_color(Color::Info)
1405 .size(ButtonSize::None)
1406 .tooltip(|cx| Tooltip::for_action("Confirm Assist", &menu::Confirm, cx))
1407 .on_click(cx.listener(|_, _, cx| {
1408 cx.emit(PromptEditorEvent::ConfirmRequested);
1409 }))
1410 },
1411 ]
1412 }
1413 };
1414
1415 h_flex()
1416 .bg(cx.theme().colors().editor_background)
1417 .border_y_1()
1418 .border_color(cx.theme().status().info_border)
1419 .py_1p5()
1420 .h_full()
1421 .w_full()
1422 .on_action(cx.listener(Self::confirm))
1423 .on_action(cx.listener(Self::cancel))
1424 .on_action(cx.listener(Self::move_up))
1425 .on_action(cx.listener(Self::move_down))
1426 .child(
1427 h_flex()
1428 .w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0))
1429 .justify_center()
1430 .gap_2()
1431 .child(
1432 PopoverMenu::new("model-switcher")
1433 .menu(move |cx| {
1434 ContextMenu::build(cx, |mut menu, cx| {
1435 for model in CompletionProvider::global(cx).available_models() {
1436 menu = menu.custom_entry(
1437 {
1438 let model = model.clone();
1439 move |_| {
1440 Label::new(model.display_name())
1441 .into_any_element()
1442 }
1443 },
1444 {
1445 let fs = fs.clone();
1446 let model = model.clone();
1447 move |cx| {
1448 let model = model.clone();
1449 update_settings_file::<AssistantSettings>(
1450 fs.clone(),
1451 cx,
1452 move |settings| settings.set_model(model),
1453 );
1454 }
1455 },
1456 );
1457 }
1458 menu
1459 })
1460 .into()
1461 })
1462 .trigger(
1463 IconButton::new("context", IconName::Settings)
1464 .shape(IconButtonShape::Square)
1465 .icon_size(IconSize::Small)
1466 .icon_color(Color::Muted)
1467 .tooltip(move |cx| {
1468 Tooltip::with_meta(
1469 format!(
1470 "Using {}",
1471 CompletionProvider::global(cx)
1472 .model()
1473 .display_name()
1474 ),
1475 None,
1476 "Change Model",
1477 cx,
1478 )
1479 }),
1480 )
1481 .anchor(gpui::AnchorCorner::BottomRight),
1482 )
1483 .children(
1484 if let CodegenStatus::Error(error) = &self.codegen.read(cx).status {
1485 let error_message = SharedString::from(error.to_string());
1486 Some(
1487 div()
1488 .id("error")
1489 .tooltip(move |cx| Tooltip::text(error_message.clone(), cx))
1490 .child(
1491 Icon::new(IconName::XCircle)
1492 .size(IconSize::Small)
1493 .color(Color::Error),
1494 ),
1495 )
1496 } else {
1497 None
1498 },
1499 ),
1500 )
1501 .child(div().flex_1().child(self.render_prompt_editor(cx)))
1502 .child(
1503 h_flex()
1504 .gap_2()
1505 .pr_4()
1506 .children(self.render_token_count(cx))
1507 .children(buttons),
1508 )
1509 }
1510}
1511
1512impl FocusableView for PromptEditor {
1513 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
1514 self.editor.focus_handle(cx)
1515 }
1516}
1517
1518impl PromptEditor {
1519 const MAX_LINES: u8 = 8;
1520
1521 #[allow(clippy::too_many_arguments)]
1522 fn new(
1523 id: InlineAssistId,
1524 gutter_dimensions: Arc<Mutex<GutterDimensions>>,
1525 prompt_history: VecDeque<String>,
1526 prompt_buffer: Model<MultiBuffer>,
1527 codegen: Model<Codegen>,
1528 parent_editor: &View<Editor>,
1529 assistant_panel: Option<&View<AssistantPanel>>,
1530 workspace: Option<WeakView<Workspace>>,
1531 fs: Arc<dyn Fs>,
1532 cx: &mut ViewContext<Self>,
1533 ) -> Self {
1534 let prompt_editor = cx.new_view(|cx| {
1535 let mut editor = Editor::new(
1536 EditorMode::AutoHeight {
1537 max_lines: Self::MAX_LINES as usize,
1538 },
1539 prompt_buffer,
1540 None,
1541 false,
1542 cx,
1543 );
1544 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1545 // Since the prompt editors for all inline assistants are linked,
1546 // always show the cursor (even when it isn't focused) because
1547 // typing in one will make what you typed appear in all of them.
1548 editor.set_show_cursor_when_unfocused(true, cx);
1549 editor.set_placeholder_text("Add a prompt…", cx);
1550 editor
1551 });
1552
1553 let mut token_count_subscriptions = Vec::new();
1554 token_count_subscriptions
1555 .push(cx.subscribe(parent_editor, Self::handle_parent_editor_event));
1556 if let Some(assistant_panel) = assistant_panel {
1557 token_count_subscriptions
1558 .push(cx.subscribe(assistant_panel, Self::handle_assistant_panel_event));
1559 }
1560
1561 let mut this = Self {
1562 id,
1563 height_in_lines: 1,
1564 editor: prompt_editor,
1565 edited_since_done: false,
1566 gutter_dimensions,
1567 prompt_history,
1568 prompt_history_ix: None,
1569 pending_prompt: String::new(),
1570 _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
1571 editor_subscriptions: Vec::new(),
1572 codegen,
1573 fs,
1574 pending_token_count: Task::ready(Ok(())),
1575 token_count: None,
1576 _token_count_subscriptions: token_count_subscriptions,
1577 workspace,
1578 };
1579 this.count_lines(cx);
1580 this.count_tokens(cx);
1581 this.subscribe_to_editor(cx);
1582 this
1583 }
1584
1585 fn subscribe_to_editor(&mut self, cx: &mut ViewContext<Self>) {
1586 self.editor_subscriptions.clear();
1587 self.editor_subscriptions
1588 .push(cx.observe(&self.editor, Self::handle_prompt_editor_changed));
1589 self.editor_subscriptions
1590 .push(cx.subscribe(&self.editor, Self::handle_prompt_editor_events));
1591 }
1592
1593 fn set_show_cursor_when_unfocused(
1594 &mut self,
1595 show_cursor_when_unfocused: bool,
1596 cx: &mut ViewContext<Self>,
1597 ) {
1598 self.editor.update(cx, |editor, cx| {
1599 editor.set_show_cursor_when_unfocused(show_cursor_when_unfocused, cx)
1600 });
1601 }
1602
1603 fn unlink(&mut self, cx: &mut ViewContext<Self>) {
1604 let prompt = self.prompt(cx);
1605 let focus = self.editor.focus_handle(cx).contains_focused(cx);
1606 self.editor = cx.new_view(|cx| {
1607 let mut editor = Editor::auto_height(Self::MAX_LINES as usize, cx);
1608 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
1609 editor.set_placeholder_text("Add a prompt…", cx);
1610 editor.set_text(prompt, cx);
1611 if focus {
1612 editor.focus(cx);
1613 }
1614 editor
1615 });
1616 self.subscribe_to_editor(cx);
1617 }
1618
1619 fn prompt(&self, cx: &AppContext) -> String {
1620 self.editor.read(cx).text(cx)
1621 }
1622
1623 fn count_lines(&mut self, cx: &mut ViewContext<Self>) {
1624 let height_in_lines = cmp::max(
1625 2, // Make the editor at least two lines tall, to account for padding and buttons.
1626 cmp::min(
1627 self.editor
1628 .update(cx, |editor, cx| editor.max_point(cx).row().0 + 1),
1629 Self::MAX_LINES as u32,
1630 ),
1631 ) as u8;
1632
1633 if height_in_lines != self.height_in_lines {
1634 self.height_in_lines = height_in_lines;
1635 cx.emit(PromptEditorEvent::Resized { height_in_lines });
1636 }
1637 }
1638
1639 fn handle_parent_editor_event(
1640 &mut self,
1641 _: View<Editor>,
1642 event: &EditorEvent,
1643 cx: &mut ViewContext<Self>,
1644 ) {
1645 if let EditorEvent::BufferEdited { .. } = event {
1646 self.count_tokens(cx);
1647 }
1648 }
1649
1650 fn handle_assistant_panel_event(
1651 &mut self,
1652 _: View<AssistantPanel>,
1653 event: &AssistantPanelEvent,
1654 cx: &mut ViewContext<Self>,
1655 ) {
1656 let AssistantPanelEvent::ContextEdited { .. } = event;
1657 self.count_tokens(cx);
1658 }
1659
1660 fn count_tokens(&mut self, cx: &mut ViewContext<Self>) {
1661 let assist_id = self.id;
1662 self.pending_token_count = cx.spawn(|this, mut cx| async move {
1663 cx.background_executor().timer(Duration::from_secs(1)).await;
1664 let request = cx
1665 .update_global(|inline_assistant: &mut InlineAssistant, cx| {
1666 inline_assistant.request_for_inline_assist(assist_id, cx)
1667 })?
1668 .await?;
1669
1670 let token_count = cx
1671 .update(|cx| CompletionProvider::global(cx).count_tokens(request, cx))?
1672 .await?;
1673 this.update(&mut cx, |this, cx| {
1674 this.token_count = Some(token_count);
1675 cx.notify();
1676 })
1677 })
1678 }
1679
1680 fn handle_prompt_editor_changed(&mut self, _: View<Editor>, cx: &mut ViewContext<Self>) {
1681 self.count_lines(cx);
1682 }
1683
1684 fn handle_prompt_editor_events(
1685 &mut self,
1686 _: View<Editor>,
1687 event: &EditorEvent,
1688 cx: &mut ViewContext<Self>,
1689 ) {
1690 match event {
1691 EditorEvent::Edited { .. } => {
1692 let prompt = self.editor.read(cx).text(cx);
1693 if self
1694 .prompt_history_ix
1695 .map_or(true, |ix| self.prompt_history[ix] != prompt)
1696 {
1697 self.prompt_history_ix.take();
1698 self.pending_prompt = prompt;
1699 }
1700
1701 self.edited_since_done = true;
1702 cx.notify();
1703 }
1704 EditorEvent::BufferEdited => {
1705 self.count_tokens(cx);
1706 }
1707 _ => {}
1708 }
1709 }
1710
1711 fn handle_codegen_changed(&mut self, _: Model<Codegen>, cx: &mut ViewContext<Self>) {
1712 match &self.codegen.read(cx).status {
1713 CodegenStatus::Idle => {
1714 self.editor
1715 .update(cx, |editor, _| editor.set_read_only(false));
1716 }
1717 CodegenStatus::Pending => {
1718 self.editor
1719 .update(cx, |editor, _| editor.set_read_only(true));
1720 }
1721 CodegenStatus::Done | CodegenStatus::Error(_) => {
1722 self.edited_since_done = false;
1723 self.editor
1724 .update(cx, |editor, _| editor.set_read_only(false));
1725 }
1726 }
1727 }
1728
1729 fn cancel(&mut self, _: &editor::actions::Cancel, cx: &mut ViewContext<Self>) {
1730 match &self.codegen.read(cx).status {
1731 CodegenStatus::Idle | CodegenStatus::Done | CodegenStatus::Error(_) => {
1732 cx.emit(PromptEditorEvent::CancelRequested);
1733 }
1734 CodegenStatus::Pending => {
1735 cx.emit(PromptEditorEvent::StopRequested);
1736 }
1737 }
1738 }
1739
1740 fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
1741 match &self.codegen.read(cx).status {
1742 CodegenStatus::Idle => {
1743 cx.emit(PromptEditorEvent::StartRequested);
1744 }
1745 CodegenStatus::Pending => {
1746 cx.emit(PromptEditorEvent::DismissRequested);
1747 }
1748 CodegenStatus::Done | CodegenStatus::Error(_) => {
1749 if self.edited_since_done {
1750 cx.emit(PromptEditorEvent::StartRequested);
1751 } else {
1752 cx.emit(PromptEditorEvent::ConfirmRequested);
1753 }
1754 }
1755 }
1756 }
1757
1758 fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
1759 if let Some(ix) = self.prompt_history_ix {
1760 if ix > 0 {
1761 self.prompt_history_ix = Some(ix - 1);
1762 let prompt = self.prompt_history[ix - 1].as_str();
1763 self.editor.update(cx, |editor, cx| {
1764 editor.set_text(prompt, cx);
1765 editor.move_to_beginning(&Default::default(), cx);
1766 });
1767 }
1768 } else if !self.prompt_history.is_empty() {
1769 self.prompt_history_ix = Some(self.prompt_history.len() - 1);
1770 let prompt = self.prompt_history[self.prompt_history.len() - 1].as_str();
1771 self.editor.update(cx, |editor, cx| {
1772 editor.set_text(prompt, cx);
1773 editor.move_to_beginning(&Default::default(), cx);
1774 });
1775 }
1776 }
1777
1778 fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
1779 if let Some(ix) = self.prompt_history_ix {
1780 if ix < self.prompt_history.len() - 1 {
1781 self.prompt_history_ix = Some(ix + 1);
1782 let prompt = self.prompt_history[ix + 1].as_str();
1783 self.editor.update(cx, |editor, cx| {
1784 editor.set_text(prompt, cx);
1785 editor.move_to_end(&Default::default(), cx)
1786 });
1787 } else {
1788 self.prompt_history_ix = None;
1789 let prompt = self.pending_prompt.as_str();
1790 self.editor.update(cx, |editor, cx| {
1791 editor.set_text(prompt, cx);
1792 editor.move_to_end(&Default::default(), cx)
1793 });
1794 }
1795 }
1796 }
1797
1798 fn render_token_count(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
1799 let model = CompletionProvider::global(cx).model();
1800 let token_count = self.token_count?;
1801 let max_token_count = model.max_token_count();
1802
1803 let remaining_tokens = max_token_count as isize - token_count as isize;
1804 let token_count_color = if remaining_tokens <= 0 {
1805 Color::Error
1806 } else if token_count as f32 / max_token_count as f32 >= 0.8 {
1807 Color::Warning
1808 } else {
1809 Color::Muted
1810 };
1811
1812 let mut token_count = h_flex()
1813 .id("token_count")
1814 .gap_0p5()
1815 .child(
1816 Label::new(humanize_token_count(token_count))
1817 .size(LabelSize::Small)
1818 .color(token_count_color),
1819 )
1820 .child(Label::new("/").size(LabelSize::Small).color(Color::Muted))
1821 .child(
1822 Label::new(humanize_token_count(max_token_count))
1823 .size(LabelSize::Small)
1824 .color(Color::Muted),
1825 );
1826 if let Some(workspace) = self.workspace.clone() {
1827 token_count = token_count
1828 .tooltip(|cx| {
1829 Tooltip::with_meta(
1830 "Tokens Used by Inline Assistant",
1831 None,
1832 "Click to Open Assistant Panel",
1833 cx,
1834 )
1835 })
1836 .cursor_pointer()
1837 .on_mouse_down(gpui::MouseButton::Left, |_, cx| cx.stop_propagation())
1838 .on_click(move |_, cx| {
1839 cx.stop_propagation();
1840 workspace
1841 .update(cx, |workspace, cx| {
1842 workspace.focus_panel::<AssistantPanel>(cx)
1843 })
1844 .ok();
1845 });
1846 } else {
1847 token_count = token_count
1848 .cursor_default()
1849 .tooltip(|cx| Tooltip::text("Tokens Used by Inline Assistant", cx));
1850 }
1851
1852 Some(token_count)
1853 }
1854
1855 fn render_prompt_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
1856 let settings = ThemeSettings::get_global(cx);
1857 let text_style = TextStyle {
1858 color: if self.editor.read(cx).read_only(cx) {
1859 cx.theme().colors().text_disabled
1860 } else {
1861 cx.theme().colors().text
1862 },
1863 font_family: settings.ui_font.family.clone(),
1864 font_features: settings.ui_font.features.clone(),
1865 font_size: rems(0.875).into(),
1866 font_weight: settings.ui_font.weight,
1867 line_height: relative(1.3),
1868 ..Default::default()
1869 };
1870 EditorElement::new(
1871 &self.editor,
1872 EditorStyle {
1873 background: cx.theme().colors().editor_background,
1874 local_player: cx.theme().players().local(),
1875 text: text_style,
1876 ..Default::default()
1877 },
1878 )
1879 }
1880}
1881
1882struct InlineAssist {
1883 group_id: InlineAssistGroupId,
1884 editor: WeakView<Editor>,
1885 decorations: Option<InlineAssistDecorations>,
1886 codegen: Model<Codegen>,
1887 _subscriptions: Vec<Subscription>,
1888 workspace: Option<WeakView<Workspace>>,
1889 include_context: bool,
1890}
1891
1892impl InlineAssist {
1893 #[allow(clippy::too_many_arguments)]
1894 fn new(
1895 assist_id: InlineAssistId,
1896 group_id: InlineAssistGroupId,
1897 include_context: bool,
1898 editor: &View<Editor>,
1899 prompt_editor: &View<PromptEditor>,
1900 prompt_block_id: CustomBlockId,
1901 end_block_id: CustomBlockId,
1902 codegen: Model<Codegen>,
1903 workspace: Option<WeakView<Workspace>>,
1904 cx: &mut WindowContext,
1905 ) -> Self {
1906 let prompt_editor_focus_handle = prompt_editor.focus_handle(cx);
1907 InlineAssist {
1908 group_id,
1909 include_context,
1910 editor: editor.downgrade(),
1911 decorations: Some(InlineAssistDecorations {
1912 prompt_block_id,
1913 prompt_editor: prompt_editor.clone(),
1914 removed_line_block_ids: HashSet::default(),
1915 end_block_id,
1916 }),
1917 codegen: codegen.clone(),
1918 workspace: workspace.clone(),
1919 _subscriptions: vec![
1920 cx.on_focus_in(&prompt_editor_focus_handle, move |cx| {
1921 InlineAssistant::update_global(cx, |this, cx| {
1922 this.handle_prompt_editor_focus_in(assist_id, cx)
1923 })
1924 }),
1925 cx.on_focus_out(&prompt_editor_focus_handle, move |_, cx| {
1926 InlineAssistant::update_global(cx, |this, cx| {
1927 this.handle_prompt_editor_focus_out(assist_id, cx)
1928 })
1929 }),
1930 cx.subscribe(prompt_editor, |prompt_editor, event, cx| {
1931 InlineAssistant::update_global(cx, |this, cx| {
1932 this.handle_prompt_editor_event(prompt_editor, event, cx)
1933 })
1934 }),
1935 cx.observe(&codegen, {
1936 let editor = editor.downgrade();
1937 move |_, cx| {
1938 if let Some(editor) = editor.upgrade() {
1939 InlineAssistant::update_global(cx, |this, cx| {
1940 if let Some(editor_assists) =
1941 this.assists_by_editor.get(&editor.downgrade())
1942 {
1943 editor_assists.highlight_updates.send(()).ok();
1944 }
1945
1946 this.update_editor_blocks(&editor, assist_id, cx);
1947 })
1948 }
1949 }
1950 }),
1951 cx.subscribe(&codegen, move |codegen, event, cx| {
1952 InlineAssistant::update_global(cx, |this, cx| match event {
1953 CodegenEvent::Undone => this.finish_assist(assist_id, false, cx),
1954 CodegenEvent::Finished => {
1955 let assist = if let Some(assist) = this.assists.get(&assist_id) {
1956 assist
1957 } else {
1958 return;
1959 };
1960
1961 if let CodegenStatus::Error(error) = &codegen.read(cx).status {
1962 if assist.decorations.is_none() {
1963 if let Some(workspace) = assist
1964 .workspace
1965 .as_ref()
1966 .and_then(|workspace| workspace.upgrade())
1967 {
1968 let error = format!("Inline assistant error: {}", error);
1969 workspace.update(cx, |workspace, cx| {
1970 struct InlineAssistantError;
1971
1972 let id =
1973 NotificationId::identified::<InlineAssistantError>(
1974 assist_id.0,
1975 );
1976
1977 workspace.show_toast(Toast::new(id, error), cx);
1978 })
1979 }
1980 }
1981 }
1982
1983 if assist.decorations.is_none() {
1984 this.finish_assist(assist_id, false, cx);
1985 }
1986 }
1987 })
1988 }),
1989 ],
1990 }
1991 }
1992}
1993
1994struct InlineAssistDecorations {
1995 prompt_block_id: CustomBlockId,
1996 prompt_editor: View<PromptEditor>,
1997 removed_line_block_ids: HashSet<CustomBlockId>,
1998 end_block_id: CustomBlockId,
1999}
2000
2001#[derive(Debug)]
2002pub enum CodegenEvent {
2003 Finished,
2004 Undone,
2005}
2006
2007pub struct Codegen {
2008 buffer: Model<MultiBuffer>,
2009 old_buffer: Model<Buffer>,
2010 snapshot: MultiBufferSnapshot,
2011 range: Range<Anchor>,
2012 edit_position: Anchor,
2013 last_equal_ranges: Vec<Range<Anchor>>,
2014 prepend_transaction_id: Option<TransactionId>,
2015 generation_transaction_id: Option<TransactionId>,
2016 status: CodegenStatus,
2017 generation: Task<()>,
2018 diff: Diff,
2019 telemetry: Option<Arc<Telemetry>>,
2020 _subscription: gpui::Subscription,
2021}
2022
2023enum CodegenStatus {
2024 Idle,
2025 Pending,
2026 Done,
2027 Error(anyhow::Error),
2028}
2029
2030#[derive(Default)]
2031struct Diff {
2032 task: Option<Task<()>>,
2033 should_update: bool,
2034 deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)>,
2035 inserted_row_ranges: Vec<RangeInclusive<Anchor>>,
2036}
2037
2038impl EventEmitter<CodegenEvent> for Codegen {}
2039
2040impl Codegen {
2041 pub fn new(
2042 buffer: Model<MultiBuffer>,
2043 range: Range<Anchor>,
2044 prepend_transaction_id: Option<TransactionId>,
2045 telemetry: Option<Arc<Telemetry>>,
2046 cx: &mut ModelContext<Self>,
2047 ) -> Self {
2048 let snapshot = buffer.read(cx).snapshot(cx);
2049
2050 let (old_buffer, _, _) = buffer
2051 .read(cx)
2052 .range_to_buffer_ranges(range.clone(), cx)
2053 .pop()
2054 .unwrap();
2055 let old_buffer = cx.new_model(|cx| {
2056 let old_buffer = old_buffer.read(cx);
2057 let text = old_buffer.as_rope().clone();
2058 let line_ending = old_buffer.line_ending();
2059 let language = old_buffer.language().cloned();
2060 let language_registry = old_buffer.language_registry();
2061
2062 let mut buffer = Buffer::local_normalized(text, line_ending, cx);
2063 buffer.set_language(language, cx);
2064 if let Some(language_registry) = language_registry {
2065 buffer.set_language_registry(language_registry)
2066 }
2067 buffer
2068 });
2069
2070 Self {
2071 buffer: buffer.clone(),
2072 old_buffer,
2073 edit_position: range.start,
2074 range,
2075 snapshot,
2076 last_equal_ranges: Default::default(),
2077 prepend_transaction_id,
2078 generation_transaction_id: None,
2079 status: CodegenStatus::Idle,
2080 generation: Task::ready(()),
2081 diff: Diff::default(),
2082 telemetry,
2083 _subscription: cx.subscribe(&buffer, Self::handle_buffer_event),
2084 }
2085 }
2086
2087 fn handle_buffer_event(
2088 &mut self,
2089 _buffer: Model<MultiBuffer>,
2090 event: &multi_buffer::Event,
2091 cx: &mut ModelContext<Self>,
2092 ) {
2093 if let multi_buffer::Event::TransactionUndone { transaction_id } = event {
2094 if self.generation_transaction_id == Some(*transaction_id) {
2095 self.generation_transaction_id = None;
2096 self.generation = Task::ready(());
2097 cx.emit(CodegenEvent::Undone);
2098 } else if self.prepend_transaction_id == Some(*transaction_id) {
2099 self.prepend_transaction_id = None;
2100 self.generation_transaction_id = None;
2101 self.generation = Task::ready(());
2102 cx.emit(CodegenEvent::Undone);
2103 }
2104 }
2105 }
2106
2107 pub fn last_equal_ranges(&self) -> &[Range<Anchor>] {
2108 &self.last_equal_ranges
2109 }
2110
2111 pub fn start(
2112 &mut self,
2113 telemetry_id: String,
2114 stream: impl 'static + Future<Output = Result<BoxStream<'static, Result<String>>>>,
2115 cx: &mut ModelContext<Self>,
2116 ) {
2117 let range = self.range.clone();
2118 let snapshot = self.snapshot.clone();
2119 let selected_text = snapshot
2120 .text_for_range(range.start..range.end)
2121 .collect::<Rope>();
2122
2123 let selection_start = range.start.to_point(&snapshot);
2124 let suggested_line_indent = snapshot
2125 .suggested_indents(selection_start.row..selection_start.row + 1, cx)
2126 .into_values()
2127 .next()
2128 .unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));
2129
2130 let telemetry = self.telemetry.clone();
2131 self.edit_position = range.start;
2132 self.diff = Diff::default();
2133 self.status = CodegenStatus::Pending;
2134 if let Some(transaction_id) = self.generation_transaction_id.take() {
2135 self.buffer
2136 .update(cx, |buffer, cx| buffer.undo_transaction(transaction_id, cx));
2137 }
2138 self.generation = cx.spawn(|this, mut cx| {
2139 async move {
2140 let chunks = stream.await;
2141 let generate = async {
2142 let mut edit_start = range.start.to_offset(&snapshot);
2143
2144 let (mut hunks_tx, mut hunks_rx) = mpsc::channel(1);
2145 let diff: Task<anyhow::Result<()>> =
2146 cx.background_executor().spawn(async move {
2147 let mut response_latency = None;
2148 let request_start = Instant::now();
2149 let diff = async {
2150 let chunks = StripInvalidSpans::new(chunks?);
2151 futures::pin_mut!(chunks);
2152 let mut diff = StreamingDiff::new(selected_text.to_string());
2153
2154 let mut new_text = String::new();
2155 let mut base_indent = None;
2156 let mut line_indent = None;
2157 let mut first_line = true;
2158
2159 while let Some(chunk) = chunks.next().await {
2160 if response_latency.is_none() {
2161 response_latency = Some(request_start.elapsed());
2162 }
2163 let chunk = chunk?;
2164
2165 let mut lines = chunk.split('\n').peekable();
2166 while let Some(line) = lines.next() {
2167 new_text.push_str(line);
2168 if line_indent.is_none() {
2169 if let Some(non_whitespace_ch_ix) =
2170 new_text.find(|ch: char| !ch.is_whitespace())
2171 {
2172 line_indent = Some(non_whitespace_ch_ix);
2173 base_indent = base_indent.or(line_indent);
2174
2175 let line_indent = line_indent.unwrap();
2176 let base_indent = base_indent.unwrap();
2177 let indent_delta =
2178 line_indent as i32 - base_indent as i32;
2179 let mut corrected_indent_len = cmp::max(
2180 0,
2181 suggested_line_indent.len as i32 + indent_delta,
2182 )
2183 as usize;
2184 if first_line {
2185 corrected_indent_len = corrected_indent_len
2186 .saturating_sub(
2187 selection_start.column as usize,
2188 );
2189 }
2190
2191 let indent_char = suggested_line_indent.char();
2192 let mut indent_buffer = [0; 4];
2193 let indent_str =
2194 indent_char.encode_utf8(&mut indent_buffer);
2195 new_text.replace_range(
2196 ..line_indent,
2197 &indent_str.repeat(corrected_indent_len),
2198 );
2199 }
2200 }
2201
2202 if line_indent.is_some() {
2203 hunks_tx.send(diff.push_new(&new_text)).await?;
2204 new_text.clear();
2205 }
2206
2207 if lines.peek().is_some() {
2208 hunks_tx.send(diff.push_new("\n")).await?;
2209 if line_indent.is_none() {
2210 // Don't write out the leading indentation in empty lines on the next line
2211 // This is the case where the above if statement didn't clear the buffer
2212 new_text.clear();
2213 }
2214 line_indent = None;
2215 first_line = false;
2216 }
2217 }
2218 }
2219 hunks_tx.send(diff.push_new(&new_text)).await?;
2220 hunks_tx.send(diff.finish()).await?;
2221
2222 anyhow::Ok(())
2223 };
2224
2225 let result = diff.await;
2226
2227 let error_message =
2228 result.as_ref().err().map(|error| error.to_string());
2229 if let Some(telemetry) = telemetry {
2230 telemetry.report_assistant_event(
2231 None,
2232 telemetry_events::AssistantKind::Inline,
2233 telemetry_id,
2234 response_latency,
2235 error_message,
2236 );
2237 }
2238
2239 result?;
2240 Ok(())
2241 });
2242
2243 while let Some(hunks) = hunks_rx.next().await {
2244 this.update(&mut cx, |this, cx| {
2245 this.last_equal_ranges.clear();
2246
2247 let transaction = this.buffer.update(cx, |buffer, cx| {
2248 // Avoid grouping assistant edits with user edits.
2249 buffer.finalize_last_transaction(cx);
2250
2251 buffer.start_transaction(cx);
2252 buffer.edit(
2253 hunks.into_iter().filter_map(|hunk| match hunk {
2254 Hunk::Insert { text } => {
2255 let edit_start = snapshot.anchor_after(edit_start);
2256 Some((edit_start..edit_start, text))
2257 }
2258 Hunk::Remove { len } => {
2259 let edit_end = edit_start + len;
2260 let edit_range = snapshot.anchor_after(edit_start)
2261 ..snapshot.anchor_before(edit_end);
2262 edit_start = edit_end;
2263 Some((edit_range, String::new()))
2264 }
2265 Hunk::Keep { len } => {
2266 let edit_end = edit_start + len;
2267 let edit_range = snapshot.anchor_after(edit_start)
2268 ..snapshot.anchor_before(edit_end);
2269 edit_start = edit_end;
2270 this.last_equal_ranges.push(edit_range);
2271 None
2272 }
2273 }),
2274 None,
2275 cx,
2276 );
2277 this.edit_position = snapshot.anchor_after(edit_start);
2278
2279 buffer.end_transaction(cx)
2280 });
2281
2282 if let Some(transaction) = transaction {
2283 if let Some(first_transaction) = this.generation_transaction_id {
2284 // Group all assistant edits into the first transaction.
2285 this.buffer.update(cx, |buffer, cx| {
2286 buffer.merge_transactions(
2287 transaction,
2288 first_transaction,
2289 cx,
2290 )
2291 });
2292 } else {
2293 this.generation_transaction_id = Some(transaction);
2294 this.buffer.update(cx, |buffer, cx| {
2295 buffer.finalize_last_transaction(cx)
2296 });
2297 }
2298 }
2299
2300 this.update_diff(cx);
2301 cx.notify();
2302 })?;
2303 }
2304
2305 diff.await?;
2306
2307 anyhow::Ok(())
2308 };
2309
2310 let result = generate.await;
2311 this.update(&mut cx, |this, cx| {
2312 this.last_equal_ranges.clear();
2313 if let Err(error) = result {
2314 this.status = CodegenStatus::Error(error);
2315 } else {
2316 this.status = CodegenStatus::Done;
2317 }
2318 cx.emit(CodegenEvent::Finished);
2319 cx.notify();
2320 })
2321 .ok();
2322 }
2323 });
2324 cx.notify();
2325 }
2326
2327 pub fn stop(&mut self, cx: &mut ModelContext<Self>) {
2328 self.last_equal_ranges.clear();
2329 self.status = CodegenStatus::Done;
2330 self.generation = Task::ready(());
2331 cx.emit(CodegenEvent::Finished);
2332 cx.notify();
2333 }
2334
2335 pub fn undo(&mut self, cx: &mut ModelContext<Self>) {
2336 if let Some(transaction_id) = self.prepend_transaction_id.take() {
2337 self.buffer
2338 .update(cx, |buffer, cx| buffer.undo_transaction(transaction_id, cx));
2339 }
2340
2341 if let Some(transaction_id) = self.generation_transaction_id.take() {
2342 self.buffer
2343 .update(cx, |buffer, cx| buffer.undo_transaction(transaction_id, cx));
2344 }
2345 }
2346
2347 fn update_diff(&mut self, cx: &mut ModelContext<Self>) {
2348 if self.diff.task.is_some() {
2349 self.diff.should_update = true;
2350 } else {
2351 self.diff.should_update = false;
2352
2353 let old_snapshot = self.snapshot.clone();
2354 let old_range = self.range.to_point(&old_snapshot);
2355 let new_snapshot = self.buffer.read(cx).snapshot(cx);
2356 let new_range = self.range.to_point(&new_snapshot);
2357
2358 self.diff.task = Some(cx.spawn(|this, mut cx| async move {
2359 let (deleted_row_ranges, inserted_row_ranges) = cx
2360 .background_executor()
2361 .spawn(async move {
2362 let old_text = old_snapshot
2363 .text_for_range(
2364 Point::new(old_range.start.row, 0)
2365 ..Point::new(
2366 old_range.end.row,
2367 old_snapshot.line_len(MultiBufferRow(old_range.end.row)),
2368 ),
2369 )
2370 .collect::<String>();
2371 let new_text = new_snapshot
2372 .text_for_range(
2373 Point::new(new_range.start.row, 0)
2374 ..Point::new(
2375 new_range.end.row,
2376 new_snapshot.line_len(MultiBufferRow(new_range.end.row)),
2377 ),
2378 )
2379 .collect::<String>();
2380
2381 let mut old_row = old_range.start.row;
2382 let mut new_row = new_range.start.row;
2383 let diff = TextDiff::from_lines(old_text.as_str(), new_text.as_str());
2384
2385 let mut deleted_row_ranges: Vec<(Anchor, RangeInclusive<u32>)> = Vec::new();
2386 let mut inserted_row_ranges = Vec::new();
2387 for change in diff.iter_all_changes() {
2388 let line_count = change.value().lines().count() as u32;
2389 match change.tag() {
2390 similar::ChangeTag::Equal => {
2391 old_row += line_count;
2392 new_row += line_count;
2393 }
2394 similar::ChangeTag::Delete => {
2395 let old_end_row = old_row + line_count - 1;
2396 let new_row =
2397 new_snapshot.anchor_before(Point::new(new_row, 0));
2398
2399 if let Some((_, last_deleted_row_range)) =
2400 deleted_row_ranges.last_mut()
2401 {
2402 if *last_deleted_row_range.end() + 1 == old_row {
2403 *last_deleted_row_range =
2404 *last_deleted_row_range.start()..=old_end_row;
2405 } else {
2406 deleted_row_ranges
2407 .push((new_row, old_row..=old_end_row));
2408 }
2409 } else {
2410 deleted_row_ranges.push((new_row, old_row..=old_end_row));
2411 }
2412
2413 old_row += line_count;
2414 }
2415 similar::ChangeTag::Insert => {
2416 let new_end_row = new_row + line_count - 1;
2417 let start = new_snapshot.anchor_before(Point::new(new_row, 0));
2418 let end = new_snapshot.anchor_before(Point::new(
2419 new_end_row,
2420 new_snapshot.line_len(MultiBufferRow(new_end_row)),
2421 ));
2422 inserted_row_ranges.push(start..=end);
2423 new_row += line_count;
2424 }
2425 }
2426 }
2427
2428 (deleted_row_ranges, inserted_row_ranges)
2429 })
2430 .await;
2431
2432 this.update(&mut cx, |this, cx| {
2433 this.diff.deleted_row_ranges = deleted_row_ranges;
2434 this.diff.inserted_row_ranges = inserted_row_ranges;
2435 this.diff.task = None;
2436 if this.diff.should_update {
2437 this.update_diff(cx);
2438 }
2439 cx.notify();
2440 })
2441 .ok();
2442 }));
2443 }
2444 }
2445}
2446
2447struct StripInvalidSpans<T> {
2448 stream: T,
2449 stream_done: bool,
2450 buffer: String,
2451 first_line: bool,
2452 line_end: bool,
2453 starts_with_code_block: bool,
2454}
2455
2456impl<T> StripInvalidSpans<T>
2457where
2458 T: Stream<Item = Result<String>>,
2459{
2460 fn new(stream: T) -> Self {
2461 Self {
2462 stream,
2463 stream_done: false,
2464 buffer: String::new(),
2465 first_line: true,
2466 line_end: false,
2467 starts_with_code_block: false,
2468 }
2469 }
2470}
2471
2472impl<T> Stream for StripInvalidSpans<T>
2473where
2474 T: Stream<Item = Result<String>>,
2475{
2476 type Item = Result<String>;
2477
2478 fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Option<Self::Item>> {
2479 const CODE_BLOCK_DELIMITER: &str = "```";
2480 const CURSOR_SPAN: &str = "<|CURSOR|>";
2481
2482 let this = unsafe { self.get_unchecked_mut() };
2483 loop {
2484 if !this.stream_done {
2485 let mut stream = unsafe { Pin::new_unchecked(&mut this.stream) };
2486 match stream.as_mut().poll_next(cx) {
2487 Poll::Ready(Some(Ok(chunk))) => {
2488 this.buffer.push_str(&chunk);
2489 }
2490 Poll::Ready(Some(Err(error))) => return Poll::Ready(Some(Err(error))),
2491 Poll::Ready(None) => {
2492 this.stream_done = true;
2493 }
2494 Poll::Pending => return Poll::Pending,
2495 }
2496 }
2497
2498 let mut chunk = String::new();
2499 let mut consumed = 0;
2500 if !this.buffer.is_empty() {
2501 let mut lines = this.buffer.split('\n').enumerate().peekable();
2502 while let Some((line_ix, line)) = lines.next() {
2503 if line_ix > 0 {
2504 this.first_line = false;
2505 }
2506
2507 if this.first_line {
2508 let trimmed_line = line.trim();
2509 if lines.peek().is_some() {
2510 if trimmed_line.starts_with(CODE_BLOCK_DELIMITER) {
2511 consumed += line.len() + 1;
2512 this.starts_with_code_block = true;
2513 continue;
2514 }
2515 } else if trimmed_line.is_empty()
2516 || prefixes(CODE_BLOCK_DELIMITER)
2517 .any(|prefix| trimmed_line.starts_with(prefix))
2518 {
2519 break;
2520 }
2521 }
2522
2523 let line_without_cursor = line.replace(CURSOR_SPAN, "");
2524 if lines.peek().is_some() {
2525 if this.line_end {
2526 chunk.push('\n');
2527 }
2528
2529 chunk.push_str(&line_without_cursor);
2530 this.line_end = true;
2531 consumed += line.len() + 1;
2532 } else if this.stream_done {
2533 if !this.starts_with_code_block
2534 || !line_without_cursor.trim().ends_with(CODE_BLOCK_DELIMITER)
2535 {
2536 if this.line_end {
2537 chunk.push('\n');
2538 }
2539
2540 chunk.push_str(&line);
2541 }
2542
2543 consumed += line.len();
2544 } else {
2545 let trimmed_line = line.trim();
2546 if trimmed_line.is_empty()
2547 || prefixes(CURSOR_SPAN).any(|prefix| trimmed_line.ends_with(prefix))
2548 || prefixes(CODE_BLOCK_DELIMITER)
2549 .any(|prefix| trimmed_line.ends_with(prefix))
2550 {
2551 break;
2552 } else {
2553 if this.line_end {
2554 chunk.push('\n');
2555 this.line_end = false;
2556 }
2557
2558 chunk.push_str(&line_without_cursor);
2559 consumed += line.len();
2560 }
2561 }
2562 }
2563 }
2564
2565 this.buffer = this.buffer.split_off(consumed);
2566 if !chunk.is_empty() {
2567 return Poll::Ready(Some(Ok(chunk)));
2568 } else if this.stream_done {
2569 return Poll::Ready(None);
2570 }
2571 }
2572 }
2573}
2574
2575fn prefixes(text: &str) -> impl Iterator<Item = &str> {
2576 (0..text.len() - 1).map(|ix| &text[..ix + 1])
2577}
2578
2579fn merge_ranges(ranges: &mut Vec<Range<Anchor>>, buffer: &MultiBufferSnapshot) {
2580 ranges.sort_unstable_by(|a, b| {
2581 a.start
2582 .cmp(&b.start, buffer)
2583 .then_with(|| b.end.cmp(&a.end, buffer))
2584 });
2585
2586 let mut ix = 0;
2587 while ix + 1 < ranges.len() {
2588 let b = ranges[ix + 1].clone();
2589 let a = &mut ranges[ix];
2590 if a.end.cmp(&b.start, buffer).is_gt() {
2591 if a.end.cmp(&b.end, buffer).is_lt() {
2592 a.end = b.end;
2593 }
2594 ranges.remove(ix + 1);
2595 } else {
2596 ix += 1;
2597 }
2598 }
2599}
2600
2601#[cfg(test)]
2602mod tests {
2603 use super::*;
2604 use completion::FakeCompletionProvider;
2605 use futures::stream::{self};
2606 use gpui::{Context, TestAppContext};
2607 use indoc::indoc;
2608 use language::{
2609 language_settings, tree_sitter_rust, Buffer, Language, LanguageConfig, LanguageMatcher,
2610 Point,
2611 };
2612 use rand::prelude::*;
2613 use serde::Serialize;
2614 use settings::SettingsStore;
2615 use std::{future, sync::Arc};
2616
2617 #[derive(Serialize)]
2618 pub struct DummyCompletionRequest {
2619 pub name: String,
2620 }
2621
2622 #[gpui::test(iterations = 10)]
2623 async fn test_transform_autoindent(cx: &mut TestAppContext, mut rng: StdRng) {
2624 cx.set_global(cx.update(SettingsStore::test));
2625 cx.update(|cx| FakeCompletionProvider::setup_test(cx));
2626 cx.update(language_settings::init);
2627
2628 let text = indoc! {"
2629 fn main() {
2630 let x = 0;
2631 for _ in 0..10 {
2632 x += 1;
2633 }
2634 }
2635 "};
2636 let buffer =
2637 cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
2638 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
2639 let range = buffer.read_with(cx, |buffer, cx| {
2640 let snapshot = buffer.snapshot(cx);
2641 snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(4, 5))
2642 });
2643 let codegen = cx.new_model(|cx| Codegen::new(buffer.clone(), range, None, None, cx));
2644
2645 let (chunks_tx, chunks_rx) = mpsc::unbounded();
2646 codegen.update(cx, |codegen, cx| {
2647 codegen.start(
2648 String::new(),
2649 future::ready(Ok(chunks_rx.map(|chunk| Ok(chunk)).boxed())),
2650 cx,
2651 )
2652 });
2653
2654 let mut new_text = concat!(
2655 " let mut x = 0;\n",
2656 " while x < 10 {\n",
2657 " x += 1;\n",
2658 " }",
2659 );
2660 while !new_text.is_empty() {
2661 let max_len = cmp::min(new_text.len(), 10);
2662 let len = rng.gen_range(1..=max_len);
2663 let (chunk, suffix) = new_text.split_at(len);
2664 chunks_tx.unbounded_send(chunk.to_string()).unwrap();
2665 new_text = suffix;
2666 cx.background_executor.run_until_parked();
2667 }
2668 drop(chunks_tx);
2669 cx.background_executor.run_until_parked();
2670
2671 assert_eq!(
2672 buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
2673 indoc! {"
2674 fn main() {
2675 let mut x = 0;
2676 while x < 10 {
2677 x += 1;
2678 }
2679 }
2680 "}
2681 );
2682 }
2683
2684 #[gpui::test(iterations = 10)]
2685 async fn test_autoindent_when_generating_past_indentation(
2686 cx: &mut TestAppContext,
2687 mut rng: StdRng,
2688 ) {
2689 cx.set_global(cx.update(SettingsStore::test));
2690 cx.update(language_settings::init);
2691
2692 let text = indoc! {"
2693 fn main() {
2694 le
2695 }
2696 "};
2697 let buffer =
2698 cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
2699 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
2700 let range = buffer.read_with(cx, |buffer, cx| {
2701 let snapshot = buffer.snapshot(cx);
2702 snapshot.anchor_before(Point::new(1, 6))..snapshot.anchor_after(Point::new(1, 6))
2703 });
2704 let codegen = cx.new_model(|cx| Codegen::new(buffer.clone(), range, None, None, cx));
2705
2706 let (chunks_tx, chunks_rx) = mpsc::unbounded();
2707 codegen.update(cx, |codegen, cx| {
2708 codegen.start(
2709 String::new(),
2710 future::ready(Ok(chunks_rx.map(|chunk| Ok(chunk)).boxed())),
2711 cx,
2712 )
2713 });
2714
2715 cx.background_executor.run_until_parked();
2716
2717 let mut new_text = concat!(
2718 "t mut x = 0;\n",
2719 "while x < 10 {\n",
2720 " x += 1;\n",
2721 "}", //
2722 );
2723 while !new_text.is_empty() {
2724 let max_len = cmp::min(new_text.len(), 10);
2725 let len = rng.gen_range(1..=max_len);
2726 let (chunk, suffix) = new_text.split_at(len);
2727 chunks_tx.unbounded_send(chunk.to_string()).unwrap();
2728 new_text = suffix;
2729 cx.background_executor.run_until_parked();
2730 }
2731 drop(chunks_tx);
2732 cx.background_executor.run_until_parked();
2733
2734 assert_eq!(
2735 buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
2736 indoc! {"
2737 fn main() {
2738 let mut x = 0;
2739 while x < 10 {
2740 x += 1;
2741 }
2742 }
2743 "}
2744 );
2745 }
2746
2747 #[gpui::test(iterations = 10)]
2748 async fn test_autoindent_when_generating_before_indentation(
2749 cx: &mut TestAppContext,
2750 mut rng: StdRng,
2751 ) {
2752 cx.update(|cx| FakeCompletionProvider::setup_test(cx));
2753 cx.set_global(cx.update(SettingsStore::test));
2754 cx.update(language_settings::init);
2755
2756 let text = concat!(
2757 "fn main() {\n",
2758 " \n",
2759 "}\n" //
2760 );
2761 let buffer =
2762 cx.new_model(|cx| Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx));
2763 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
2764 let range = buffer.read_with(cx, |buffer, cx| {
2765 let snapshot = buffer.snapshot(cx);
2766 snapshot.anchor_before(Point::new(1, 2))..snapshot.anchor_after(Point::new(1, 2))
2767 });
2768 let codegen = cx.new_model(|cx| Codegen::new(buffer.clone(), range, None, None, cx));
2769
2770 let (chunks_tx, chunks_rx) = mpsc::unbounded();
2771 codegen.update(cx, |codegen, cx| {
2772 codegen.start(
2773 String::new(),
2774 future::ready(Ok(chunks_rx.map(|chunk| Ok(chunk)).boxed())),
2775 cx,
2776 )
2777 });
2778
2779 cx.background_executor.run_until_parked();
2780
2781 let mut new_text = concat!(
2782 "let mut x = 0;\n",
2783 "while x < 10 {\n",
2784 " x += 1;\n",
2785 "}", //
2786 );
2787 while !new_text.is_empty() {
2788 let max_len = cmp::min(new_text.len(), 10);
2789 let len = rng.gen_range(1..=max_len);
2790 let (chunk, suffix) = new_text.split_at(len);
2791 chunks_tx.unbounded_send(chunk.to_string()).unwrap();
2792 new_text = suffix;
2793 cx.background_executor.run_until_parked();
2794 }
2795 drop(chunks_tx);
2796 cx.background_executor.run_until_parked();
2797
2798 assert_eq!(
2799 buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx).text()),
2800 indoc! {"
2801 fn main() {
2802 let mut x = 0;
2803 while x < 10 {
2804 x += 1;
2805 }
2806 }
2807 "}
2808 );
2809 }
2810
2811 #[gpui::test]
2812 async fn test_strip_invalid_spans_from_codeblock() {
2813 assert_chunks("Lorem ipsum dolor", "Lorem ipsum dolor").await;
2814 assert_chunks("```\nLorem ipsum dolor", "Lorem ipsum dolor").await;
2815 assert_chunks("```\nLorem ipsum dolor\n```", "Lorem ipsum dolor").await;
2816 assert_chunks(
2817 "```html\n```js\nLorem ipsum dolor\n```\n```",
2818 "```js\nLorem ipsum dolor\n```",
2819 )
2820 .await;
2821 assert_chunks("``\nLorem ipsum dolor\n```", "``\nLorem ipsum dolor\n```").await;
2822 assert_chunks("Lorem<|CURSOR|> ipsum", "Lorem ipsum").await;
2823 assert_chunks("Lorem ipsum", "Lorem ipsum").await;
2824 assert_chunks("```\n<|CURSOR|>Lorem ipsum\n```", "Lorem ipsum").await;
2825
2826 async fn assert_chunks(text: &str, expected_text: &str) {
2827 for chunk_size in 1..=text.len() {
2828 let actual_text = StripInvalidSpans::new(chunks(text, chunk_size))
2829 .map(|chunk| chunk.unwrap())
2830 .collect::<String>()
2831 .await;
2832 assert_eq!(
2833 actual_text, expected_text,
2834 "failed to strip invalid spans, chunk size: {}",
2835 chunk_size
2836 );
2837 }
2838 }
2839
2840 fn chunks(text: &str, size: usize) -> impl Stream<Item = Result<String>> {
2841 stream::iter(
2842 text.chars()
2843 .collect::<Vec<_>>()
2844 .chunks(size)
2845 .map(|chunk| Ok(chunk.iter().collect::<String>()))
2846 .collect::<Vec<_>>(),
2847 )
2848 }
2849 }
2850
2851 fn rust_lang() -> Language {
2852 Language::new(
2853 LanguageConfig {
2854 name: "Rust".into(),
2855 matcher: LanguageMatcher {
2856 path_suffixes: vec!["rs".to_string()],
2857 ..Default::default()
2858 },
2859 ..Default::default()
2860 },
2861 Some(tree_sitter_rust::language()),
2862 )
2863 .with_indents_query(
2864 r#"
2865 (call_expression) @indent
2866 (field_expression) @indent
2867 (_ "(" ")" @end) @indent
2868 (_ "{" "}" @end) @indent
2869 "#,
2870 )
2871 .unwrap()
2872 }
2873}