current_file_command.rs

  1use std::{borrow::Cow, cell::Cell, rc::Rc};
  2
  3use anyhow::{anyhow, Result};
  4use collections::HashMap;
  5use editor::Editor;
  6use futures::channel::oneshot;
  7use gpui::{AppContext, Entity, Subscription, Task, WindowHandle};
  8use workspace::{Event as WorkspaceEvent, Workspace};
  9
 10use super::{SlashCommand, SlashCommandCleanup, SlashCommandInvocation};
 11
 12pub(crate) struct CurrentFileSlashCommand {
 13    workspace: WindowHandle<Workspace>,
 14}
 15
 16impl CurrentFileSlashCommand {
 17    pub fn new(workspace: WindowHandle<Workspace>) -> Self {
 18        Self { workspace }
 19    }
 20}
 21
 22impl SlashCommand for CurrentFileSlashCommand {
 23    fn name(&self) -> String {
 24        "current_file".into()
 25    }
 26
 27    fn description(&self) -> String {
 28        "insert the current file".into()
 29    }
 30
 31    fn complete_argument(
 32        &self,
 33        _query: String,
 34        _cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
 35        _cx: &mut AppContext,
 36    ) -> Task<Result<Vec<String>>> {
 37        Task::ready(Err(anyhow!("this command does not require argument")))
 38    }
 39
 40    fn requires_argument(&self) -> bool {
 41        false
 42    }
 43
 44    fn run(&self, _argument: Option<&str>, cx: &mut AppContext) -> SlashCommandInvocation {
 45        let (invalidate_tx, invalidate_rx) = oneshot::channel();
 46        let invalidate_tx = Rc::new(Cell::new(Some(invalidate_tx)));
 47        let mut subscriptions: Vec<Subscription> = Vec::new();
 48        let output = self.workspace.update(cx, |workspace, cx| {
 49            let mut timestamps_by_entity_id = HashMap::default();
 50            for pane in workspace.panes() {
 51                let pane = pane.read(cx);
 52                for entry in pane.activation_history() {
 53                    timestamps_by_entity_id.insert(entry.entity_id, entry.timestamp);
 54                }
 55            }
 56
 57            let mut most_recent_buffer = None;
 58            for editor in workspace.items_of_type::<Editor>(cx) {
 59                let Some(buffer) = editor.read(cx).buffer().read(cx).as_singleton() else {
 60                    continue;
 61                };
 62
 63                let timestamp = timestamps_by_entity_id
 64                    .get(&editor.entity_id())
 65                    .copied()
 66                    .unwrap_or_default();
 67                if most_recent_buffer
 68                    .as_ref()
 69                    .map_or(true, |(_, prev_timestamp)| timestamp > *prev_timestamp)
 70                {
 71                    most_recent_buffer = Some((buffer, timestamp));
 72                }
 73            }
 74
 75            subscriptions.push({
 76                let workspace_view = cx.view().clone();
 77                let invalidate_tx = invalidate_tx.clone();
 78                cx.window_context()
 79                    .subscribe(&workspace_view, move |_workspace, event, _cx| match event {
 80                        WorkspaceEvent::ActiveItemChanged
 81                        | WorkspaceEvent::ItemAdded
 82                        | WorkspaceEvent::ItemRemoved
 83                        | WorkspaceEvent::PaneAdded(_)
 84                        | WorkspaceEvent::PaneRemoved => {
 85                            if let Some(invalidate_tx) = invalidate_tx.take() {
 86                                _ = invalidate_tx.send(());
 87                            }
 88                        }
 89                        _ => {}
 90                    })
 91            });
 92
 93            if let Some((buffer, _)) = most_recent_buffer {
 94                subscriptions.push({
 95                    let invalidate_tx = invalidate_tx.clone();
 96                    cx.window_context().observe(&buffer, move |_buffer, _cx| {
 97                        if let Some(invalidate_tx) = invalidate_tx.take() {
 98                            _ = invalidate_tx.send(());
 99                        }
100                    })
101                });
102
103                let snapshot = buffer.read(cx).snapshot();
104                let path = snapshot.resolve_file_path(cx, true);
105                cx.background_executor().spawn(async move {
106                    let path = path
107                        .as_ref()
108                        .map(|path| path.to_string_lossy())
109                        .unwrap_or_else(|| Cow::Borrowed("untitled"));
110
111                    let mut output = String::with_capacity(path.len() + snapshot.len() + 9);
112                    output.push_str("```");
113                    output.push_str(&path);
114                    output.push('\n');
115                    for chunk in snapshot.as_rope().chunks() {
116                        output.push_str(chunk);
117                    }
118                    if !output.ends_with('\n') {
119                        output.push('\n');
120                    }
121                    output.push_str("```");
122                    Ok(output)
123                })
124            } else {
125                Task::ready(Err(anyhow!("no recent buffer found")))
126            }
127        });
128
129        SlashCommandInvocation {
130            output: output.unwrap_or_else(|error| Task::ready(Err(error))),
131            invalidated: invalidate_rx,
132            cleanup: SlashCommandCleanup::new(move || drop(subscriptions)),
133        }
134    }
135}