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.into_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 {
203 let snapshot = buffer.read(cx).snapshot();
204 let full_path = snapshot.resolve_file_path(cx, true);
205 open_buffers.push((full_path, snapshot, *timestamp));
206 }
207 }
208
209 Ok(open_buffers)
210 })??;
211
212 let background_executor = cx.background_executor().clone();
213 cx.background_spawn(async move {
214 open_buffers.sort_by_key(|(_, _, timestamp)| *timestamp);
215 if empty_query
216 || queries
217 .iter()
218 .any(|query| query == ALL_TABS_COMPLETION_ITEM)
219 {
220 return Ok(open_buffers);
221 }
222
223 let matched_items = if strict_match {
224 let match_candidates = open_buffers
225 .iter()
226 .enumerate()
227 .filter_map(|(id, (full_path, ..))| {
228 let path_string = full_path.as_deref()?.to_string_lossy().to_string();
229 Some((id, path_string))
230 })
231 .fold(HashMap::default(), |mut candidates, (id, path_string)| {
232 candidates
233 .entry(path_string)
234 .or_insert_with(Vec::new)
235 .push(id);
236 candidates
237 });
238
239 queries
240 .iter()
241 .filter_map(|query| match_candidates.get(query))
242 .flatten()
243 .copied()
244 .filter_map(|id| open_buffers.get(id))
245 .cloned()
246 .collect()
247 } else {
248 let match_candidates = open_buffers
249 .iter()
250 .enumerate()
251 .filter_map(|(id, (full_path, ..))| {
252 let path_string = full_path.as_deref()?.to_string_lossy().to_string();
253 Some(fuzzy::StringMatchCandidate::new(id, &path_string))
254 })
255 .collect::<Vec<_>>();
256 let mut processed_matches = HashSet::default();
257 let file_queries = queries.iter().map(|query| {
258 fuzzy::match_strings(
259 &match_candidates,
260 query,
261 true,
262 true,
263 usize::MAX,
264 &cancel,
265 background_executor.clone(),
266 )
267 });
268
269 join_all(file_queries)
270 .await
271 .into_iter()
272 .flatten()
273 .filter(|string_match| processed_matches.insert(string_match.candidate_id))
274 .filter_map(|string_match| open_buffers.get(string_match.candidate_id))
275 .cloned()
276 .collect()
277 };
278 Ok(matched_items)
279 })
280 .await
281 })
282}
283
284fn active_item_buffer(
285 workspace: &mut Workspace,
286 cx: &mut Context<Workspace>,
287) -> anyhow::Result<BufferSnapshot> {
288 let active_editor = workspace
289 .active_item(cx)
290 .context("no active item")?
291 .downcast::<Editor>()
292 .context("active item is not an editor")?;
293 let snapshot = active_editor
294 .read(cx)
295 .buffer()
296 .read(cx)
297 .as_singleton()
298 .context("active editor is not a singleton buffer")?
299 .read(cx)
300 .snapshot();
301 Ok(snapshot)
302}
303
304fn create_tab_completion_label(
305 path: &std::path::Path,
306 comment_id: Option<HighlightId>,
307) -> CodeLabel {
308 let file_name = path
309 .file_name()
310 .map(|f| f.to_string_lossy())
311 .unwrap_or_default();
312 let parent_path = path
313 .parent()
314 .map(|p| p.to_string_lossy())
315 .unwrap_or_default();
316 let mut label = CodeLabel::default();
317 label.push_str(&file_name, None);
318 label.push_str(" ", None);
319 label.push_str(&parent_path, comment_id);
320 label.filter_range = 0..file_name.len();
321 label
322}