tab_command.rs

  1use anyhow::{Context as _, Result};
  2use assistant_slash_command::{
  3    ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
  4    SlashCommandResult,
  5};
  6use collections::{HashMap, HashSet};
  7use editor::Editor;
  8use futures::future::join_all;
  9use gpui::{Task, WeakEntity};
 10use language::{BufferSnapshot, CodeLabel, HighlightId, LspAdapterDelegate};
 11use std::{
 12    path::PathBuf,
 13    sync::{Arc, atomic::AtomicBool},
 14};
 15use ui::{ActiveTheme, App, Window, prelude::*};
 16use util::ResultExt;
 17use workspace::Workspace;
 18
 19use crate::file_command::append_buffer_to_output;
 20
 21pub struct TabSlashCommand;
 22
 23const ALL_TABS_COMPLETION_ITEM: &str = "all";
 24
 25impl SlashCommand for TabSlashCommand {
 26    fn name(&self) -> String {
 27        "tab".into()
 28    }
 29
 30    fn description(&self) -> String {
 31        "Insert open tabs (active tab by default)".to_owned()
 32    }
 33
 34    fn icon(&self) -> IconName {
 35        IconName::FileTree
 36    }
 37
 38    fn menu_text(&self) -> String {
 39        self.description()
 40    }
 41
 42    fn requires_argument(&self) -> bool {
 43        false
 44    }
 45
 46    fn accepts_arguments(&self) -> bool {
 47        true
 48    }
 49
 50    fn complete_argument(
 51        self: Arc<Self>,
 52        arguments: &[String],
 53        cancel: Arc<AtomicBool>,
 54        workspace: Option<WeakEntity<Workspace>>,
 55        window: &mut Window,
 56        cx: &mut App,
 57    ) -> Task<Result<Vec<ArgumentCompletion>>> {
 58        let mut has_all_tabs_completion_item = false;
 59        let argument_set = arguments
 60            .iter()
 61            .filter(|argument| {
 62                if has_all_tabs_completion_item || ALL_TABS_COMPLETION_ITEM == argument.as_str() {
 63                    has_all_tabs_completion_item = true;
 64                    false
 65                } else {
 66                    true
 67                }
 68            })
 69            .cloned()
 70            .collect::<HashSet<_>>();
 71        if has_all_tabs_completion_item {
 72            return Task::ready(Ok(Vec::new()));
 73        }
 74
 75        let active_item_path = workspace.as_ref().and_then(|workspace| {
 76            workspace
 77                .update(cx, |workspace, cx| {
 78                    let snapshot = active_item_buffer(workspace, cx).ok()?;
 79                    snapshot.resolve_file_path(cx, true)
 80                })
 81                .ok()
 82                .flatten()
 83        });
 84        let current_query = arguments.last().cloned().unwrap_or_default();
 85        let tab_items_search =
 86            tab_items_for_queries(workspace, &[current_query], cancel, false, window, cx);
 87
 88        let comment_id = cx.theme().syntax().highlight_id("comment").map(HighlightId);
 89        window.spawn(cx, async move |_| {
 90            let tab_items = tab_items_search.await?;
 91            let run_command = tab_items.len() == 1;
 92            let tab_completion_items = tab_items.into_iter().filter_map(|(path, ..)| {
 93                let path_string = path.as_deref()?.to_string_lossy().to_string();
 94                if argument_set.contains(&path_string) {
 95                    return None;
 96                }
 97                if active_item_path.is_some() && active_item_path == path {
 98                    return None;
 99                }
100                let label = create_tab_completion_label(path.as_ref()?, comment_id);
101                Some(ArgumentCompletion {
102                    label,
103                    new_text: path_string,
104                    replace_previous_arguments: false,
105                    after_completion: run_command.into(),
106                })
107            });
108
109            let active_item_completion = active_item_path
110                .as_deref()
111                .map(|active_item_path| {
112                    let path_string = active_item_path.to_string_lossy().to_string();
113                    let label = create_tab_completion_label(active_item_path, comment_id);
114                    ArgumentCompletion {
115                        label,
116                        new_text: path_string,
117                        replace_previous_arguments: false,
118                        after_completion: run_command.into(),
119                    }
120                })
121                .filter(|completion| !argument_set.contains(&completion.new_text));
122
123            Ok(active_item_completion
124                .into_iter()
125                .chain(Some(ArgumentCompletion {
126                    label: ALL_TABS_COMPLETION_ITEM.into(),
127                    new_text: ALL_TABS_COMPLETION_ITEM.to_owned(),
128                    replace_previous_arguments: false,
129                    after_completion: true.into(),
130                }))
131                .chain(tab_completion_items)
132                .collect())
133        })
134    }
135
136    fn run(
137        self: Arc<Self>,
138        arguments: &[String],
139        _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
140        _context_buffer: BufferSnapshot,
141        workspace: WeakEntity<Workspace>,
142        _delegate: Option<Arc<dyn LspAdapterDelegate>>,
143        window: &mut Window,
144        cx: &mut App,
145    ) -> Task<SlashCommandResult> {
146        let tab_items_search = tab_items_for_queries(
147            Some(workspace),
148            arguments,
149            Arc::new(AtomicBool::new(false)),
150            true,
151            window,
152            cx,
153        );
154
155        cx.background_spawn(async move {
156            let mut output = SlashCommandOutput::default();
157            for (full_path, buffer, _) in tab_items_search.await? {
158                append_buffer_to_output(&buffer, full_path.as_deref(), &mut output).log_err();
159            }
160            Ok(output.to_event_stream())
161        })
162    }
163}
164
165fn tab_items_for_queries(
166    workspace: Option<WeakEntity<Workspace>>,
167    queries: &[String],
168    cancel: Arc<AtomicBool>,
169    strict_match: bool,
170    window: &mut Window,
171    cx: &mut App,
172) -> Task<anyhow::Result<Vec<(Option<PathBuf>, BufferSnapshot, usize)>>> {
173    let empty_query = queries.is_empty() || queries.iter().all(|query| query.trim().is_empty());
174    let queries = queries.to_owned();
175    window.spawn(cx, async move |cx| {
176        let mut open_buffers =
177            workspace
178                .context("no workspace")?
179                .update(cx, |workspace, cx| {
180                    if strict_match && empty_query {
181                        let snapshot = active_item_buffer(workspace, cx)?;
182                        let full_path = snapshot.resolve_file_path(cx, true);
183                        return anyhow::Ok(vec![(full_path, snapshot, 0)]);
184                    }
185
186                    let mut timestamps_by_entity_id = HashMap::default();
187                    let mut visited_buffers = HashSet::default();
188                    let mut open_buffers = Vec::new();
189
190                    for pane in workspace.panes() {
191                        let pane = pane.read(cx);
192                        for entry in pane.activation_history() {
193                            timestamps_by_entity_id.insert(entry.entity_id, entry.timestamp);
194                        }
195                    }
196
197                    for editor in workspace.items_of_type::<Editor>(cx) {
198                        if let Some(buffer) = editor.read(cx).buffer().read(cx).as_singleton()
199                            && let Some(timestamp) =
200                                timestamps_by_entity_id.get(&editor.entity_id())
201                                && visited_buffers.insert(buffer.read(cx).remote_id()) {
202                                    let snapshot = buffer.read(cx).snapshot();
203                                    let full_path = snapshot.resolve_file_path(cx, true);
204                                    open_buffers.push((full_path, snapshot, *timestamp));
205                                }
206                    }
207
208                    Ok(open_buffers)
209                })??;
210
211        let background_executor = cx.background_executor().clone();
212        cx.background_spawn(async move {
213            open_buffers.sort_by_key(|(_, _, timestamp)| *timestamp);
214            if empty_query
215                || queries
216                    .iter()
217                    .any(|query| query == ALL_TABS_COMPLETION_ITEM)
218            {
219                return Ok(open_buffers);
220            }
221
222            let matched_items = if strict_match {
223                let match_candidates = open_buffers
224                    .iter()
225                    .enumerate()
226                    .filter_map(|(id, (full_path, ..))| {
227                        let path_string = full_path.as_deref()?.to_string_lossy().to_string();
228                        Some((id, path_string))
229                    })
230                    .fold(HashMap::default(), |mut candidates, (id, path_string)| {
231                        candidates
232                            .entry(path_string)
233                            .or_insert_with(Vec::new)
234                            .push(id);
235                        candidates
236                    });
237
238                queries
239                    .iter()
240                    .filter_map(|query| match_candidates.get(query))
241                    .flatten()
242                    .copied()
243                    .filter_map(|id| open_buffers.get(id))
244                    .cloned()
245                    .collect()
246            } else {
247                let match_candidates = open_buffers
248                    .iter()
249                    .enumerate()
250                    .filter_map(|(id, (full_path, ..))| {
251                        let path_string = full_path.as_deref()?.to_string_lossy().to_string();
252                        Some(fuzzy::StringMatchCandidate::new(id, &path_string))
253                    })
254                    .collect::<Vec<_>>();
255                let mut processed_matches = HashSet::default();
256                let file_queries = queries.iter().map(|query| {
257                    fuzzy::match_strings(
258                        &match_candidates,
259                        query,
260                        true,
261                        true,
262                        usize::MAX,
263                        &cancel,
264                        background_executor.clone(),
265                    )
266                });
267
268                join_all(file_queries)
269                    .await
270                    .into_iter()
271                    .flatten()
272                    .filter(|string_match| processed_matches.insert(string_match.candidate_id))
273                    .filter_map(|string_match| open_buffers.get(string_match.candidate_id))
274                    .cloned()
275                    .collect()
276            };
277            Ok(matched_items)
278        })
279        .await
280    })
281}
282
283fn active_item_buffer(
284    workspace: &mut Workspace,
285    cx: &mut Context<Workspace>,
286) -> anyhow::Result<BufferSnapshot> {
287    let active_editor = workspace
288        .active_item(cx)
289        .context("no active item")?
290        .downcast::<Editor>()
291        .context("active item is not an editor")?;
292    let snapshot = active_editor
293        .read(cx)
294        .buffer()
295        .read(cx)
296        .as_singleton()
297        .context("active editor is not a singleton buffer")?
298        .read(cx)
299        .snapshot();
300    Ok(snapshot)
301}
302
303fn create_tab_completion_label(
304    path: &std::path::Path,
305    comment_id: Option<HighlightId>,
306) -> CodeLabel {
307    let file_name = path
308        .file_name()
309        .map(|f| f.to_string_lossy())
310        .unwrap_or_default();
311    let parent_path = path
312        .parent()
313        .map(|p| p.to_string_lossy())
314        .unwrap_or_default();
315    let mut label = CodeLabel::default();
316    label.push_str(&file_name, None);
317    label.push_str(" ", None);
318    label.push_str(&parent_path, comment_id);
319    label.filter_range = 0..file_name.len();
320    label
321}