delta_command.rs

  1use crate::slash_command::file_command::{FileCommandMetadata, FileSlashCommand};
  2use anyhow::{anyhow, Result};
  3use assistant_slash_command::{
  4    ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
  5    SlashCommandResult,
  6};
  7use collections::HashSet;
  8use futures::future;
  9use gpui::{Task, WeakView, WindowContext};
 10use language::{BufferSnapshot, LspAdapterDelegate};
 11use std::sync::{atomic::AtomicBool, Arc};
 12use text::OffsetRangeExt;
 13use ui::prelude::*;
 14use workspace::Workspace;
 15
 16pub(crate) struct DeltaSlashCommand;
 17
 18impl SlashCommand for DeltaSlashCommand {
 19    fn name(&self) -> String {
 20        "delta".into()
 21    }
 22
 23    fn description(&self) -> String {
 24        "Re-insert changed files".into()
 25    }
 26
 27    fn menu_text(&self) -> String {
 28        self.description()
 29    }
 30
 31    fn icon(&self) -> IconName {
 32        IconName::Diff
 33    }
 34
 35    fn requires_argument(&self) -> bool {
 36        false
 37    }
 38
 39    fn complete_argument(
 40        self: Arc<Self>,
 41        _arguments: &[String],
 42        _cancellation_flag: Arc<AtomicBool>,
 43        _workspace: Option<WeakView<Workspace>>,
 44        _cx: &mut WindowContext,
 45    ) -> Task<Result<Vec<ArgumentCompletion>>> {
 46        Task::ready(Err(anyhow!("this command does not require argument")))
 47    }
 48
 49    fn run(
 50        self: Arc<Self>,
 51        _arguments: &[String],
 52        context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
 53        context_buffer: BufferSnapshot,
 54        workspace: WeakView<Workspace>,
 55        delegate: Option<Arc<dyn LspAdapterDelegate>>,
 56        cx: &mut WindowContext,
 57    ) -> Task<SlashCommandResult> {
 58        let mut paths = HashSet::default();
 59        let mut file_command_old_outputs = Vec::new();
 60        let mut file_command_new_outputs = Vec::new();
 61        for section in context_slash_command_output_sections.iter().rev() {
 62            if let Some(metadata) = section
 63                .metadata
 64                .as_ref()
 65                .and_then(|value| serde_json::from_value::<FileCommandMetadata>(value.clone()).ok())
 66            {
 67                if paths.insert(metadata.path.clone()) {
 68                    file_command_old_outputs.push(
 69                        context_buffer
 70                            .as_rope()
 71                            .slice(section.range.to_offset(&context_buffer)),
 72                    );
 73                    file_command_new_outputs.push(Arc::new(FileSlashCommand).run(
 74                        &[metadata.path.clone()],
 75                        context_slash_command_output_sections,
 76                        context_buffer.clone(),
 77                        workspace.clone(),
 78                        delegate.clone(),
 79                        cx,
 80                    ));
 81                }
 82            }
 83        }
 84
 85        cx.background_executor().spawn(async move {
 86            let mut output = SlashCommandOutput::default();
 87
 88            let file_command_new_outputs = future::join_all(file_command_new_outputs).await;
 89            for (old_text, new_output) in file_command_old_outputs
 90                .into_iter()
 91                .zip(file_command_new_outputs)
 92            {
 93                if let Ok(new_output) = new_output {
 94                    if let Ok(new_output) = SlashCommandOutput::from_event_stream(new_output).await
 95                    {
 96                        if let Some(file_command_range) = new_output.sections.first() {
 97                            let new_text = &new_output.text[file_command_range.range.clone()];
 98                            if old_text.chars().ne(new_text.chars()) {
 99                                output.sections.extend(new_output.sections.into_iter().map(
100                                    |section| SlashCommandOutputSection {
101                                        range: output.text.len() + section.range.start
102                                            ..output.text.len() + section.range.end,
103                                        icon: section.icon,
104                                        label: section.label,
105                                        metadata: section.metadata,
106                                    },
107                                ));
108                                output.text.push_str(&new_output.text);
109                            }
110                        }
111                    }
112                }
113            }
114
115            Ok(output.to_event_stream())
116        })
117    }
118}