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