Visual Studio Code, rebuilt in Rust
Rust
//! editorQuickAccess.ts: `edt ` lists every group's editors in grid order, `edt mru ` every editor most recently
//! active first, `edt active ` the active group's by recency; each pick has a Close Editor button.
use gpui::{Context, Window};
use super::Workbench;
use super::quick_access::Accept;
use crate::commands::Outcome;
use crate::fuzzy_scorer::{self, FuzzyScorerCache, ScorerItem};
use crate::quick_input::{Button, Entry, Item};
/// `AllEditorsByAppearanceQuickAccess.PREFIX`.
pub const ALL_PREFIX: &str = "edt ";
/// `AllEditorsByMostRecentlyUsedQuickAccess.PREFIX`.
pub const MRU_PREFIX: &str = "edt mru ";
/// `ActiveGroupEditorsByMostRecentlyUsedQuickAccess.PREFIX`.
pub const ACTIVE_GROUP_PREFIX: &str = "edt active ";
pub const PLACEHOLDER: &str = "Type the name of an editor to open it.";
struct EditorPick {
group: u32,
model_id: u32,
item: Item,
}
impl Workbench {
pub(super) fn register_editor_quick_access_commands(&mut self) {
let commands = [
("workbench.action.showAllEditors", ALL_PREFIX),
("workbench.action.showAllEditorsByMostRecentlyUsed", MRU_PREFIX),
("workbench.action.showEditorsInActiveGroup", ACTIVE_GROUP_PREFIX),
// editorCommands.ts `SHOW_EDITORS_IN_GROUP`: the group of the command's context first.
("workbench.action.showEditorsInGroup", ACTIVE_GROUP_PREFIX),
];
for (id, prefix) in commands {
self.commands.register(id, move |workbench, _, window, cx| {
workbench.leave_panel_group();
workbench.show_quick_access(prefix, false, false, window, cx);
Outcome::done()
});
}
}
/// `BaseEditorQuickAccessProvider._getPicks`: the editors the query matches, sorted by group then score while it
/// filters, under a separator per group when there are several groups.
pub(super) fn editor_picks(&self, prefix: &str, filter: &str) -> (Vec<Entry>, Vec<Accept>) {
let query = fuzzy_scorer::prepare_query(filter);
let mut cache = FuzzyScorerCache::new();
let mut picks = Vec::new();
for pick in self.editor_pick_items(prefix) {
if query.piece.normalized.is_empty() {
picks.push(pick);
continue;
}
let scored = ScorerItem { label: &pick.item.label, description: pick.item.description.as_deref(), path: None };
let score = fuzzy_scorer::score_item_fuzzy(Some(&scored), &query, true, &mut cache);
if score.score == 0 {
continue;
}
let mut pick = pick;
pick.item.label_highlights = score.label_match.unwrap_or_default();
pick.item.description_highlights = score.description_match.unwrap_or_default();
picks.push(pick);
}
if !query.piece.normalized.is_empty() {
let groups = self.groups_in_grid_order();
let position = |group: u32| groups.iter().position(|candidate| *candidate == group);
picks.sort_by(|a, b| {
let item = |pick: &EditorPick| (pick.item.label.clone(), pick.item.description.clone());
let ((label_a, description_a), (label_b, description_b)) = (item(a), item(b));
position(a.group).cmp(&position(b.group)).then_with(|| {
let a = ScorerItem { label: &label_a, description: description_a.as_deref(), path: None };
let b = ScorerItem { label: &label_b, description: description_b.as_deref(), path: None };
fuzzy_scorer::compare_items_by_fuzzy_score(&a, &b, &query, true, &mut cache)
})
});
}
let several_groups = self.groups_in_grid_order().len() > 1;
let mut entries = Vec::new();
let mut accepts = Vec::new();
let mut last_group = None;
for mut pick in picks {
if several_groups && last_group != Some(pick.group) {
entries.push(Entry::Separator(Some(self.editor_group_label(pick.group))));
last_group = Some(pick.group);
}
pick.item.handle = accepts.len() as i64;
entries.push(Entry::Item(pick.item));
accepts.push(Accept::Editor { group: pick.group, model_id: pick.model_id });
}
// `noResultsPick`.
if accepts.is_empty() {
entries.push(Entry::Item(Item { handle: 0, label: "No matching editors".into(), pickable: true, ..Item::default() }));
accepts.push(Accept::Nothing);
}
(entries, accepts)
}
/// `doGetEditors` of the provider of `prefix`, as picks: name, folder, file icon, italic while in preview, and the
/// Close Editor button, kept visible as a dirty dot.
fn editor_pick_items(&self, prefix: &str) -> Vec<EditorPick> {
let groups = match prefix {
ACTIVE_GROUP_PREFIX => vec![self.editor_group_id()],
MRU_PREFIX => self.editors.group_ids_mru().into_iter().filter(|group| self.part_of_group(*group).is_some()).collect(),
_ => self.groups_in_grid_order(),
};
let mut picks = Vec::new();
for group in groups {
let editors = match prefix {
ALL_PREFIX => self.editors.group_tabs(group).0.iter().collect(),
_ => self.editors.group_editors_by_recency(group),
};
for editor in editors {
let dirty = self.editor_shows_dirty(editor);
let description = match editor.document().scheme.as_str() {
"file" => editor.document().path.parent().map(|folder| crate::quick_input::path_label(folder, self.workspace())).filter(|label| !label.is_empty()),
_ => self.untitled_description(editor),
};
let file_icon = matches!(editor.document().scheme.as_str(), "file" | "untitled").then(|| editor.document().path.clone());
let close = Button { handle: 0, codicon: Some(if dirty { "close-dirty" } else { "close" }.into()), tooltip: Some("Close Editor".into()), always_visible: dirty, ..Button::default() };
let item = Item { label: self.editor_name(editor), description, pickable: true, file_icon, italic: editor.preview, buttons: vec![close], ..Item::default() };
picks.push(EditorPick { group, model_id: editor.model_id, item });
}
}
picks
}
/// editorGroupView.ts `label`: `Group <n>` by the group's place in its part, `Window <n>: Group <n>` in an
/// editor window (editorParts.ts `getGroupsLabel`).
fn editor_group_label(&self, group: u32) -> String {
let part = self.part_of_group(group).unwrap_or(super::editor_groups::MAIN_PART);
let index = self.part_groups(part).iter().position(|candidate| *candidate == group).unwrap_or(0) + 1;
match self.auxiliary_windows.part_ids().position(|candidate| candidate == part) {
Some(window) => format!("Window {}: Group {index}", window + 2),
None => format!("Group {index}"),
}
}
/// The pick's `accept`: the group opens the editor, focused unless in the background.
pub(super) fn accept_editor_pick(&mut self, group: u32, model_id: u32, in_background: bool, window: &mut Window, cx: &mut Context<Self>) {
let Some(index) = self.editors.group_tabs(group).0.iter().position(|editor| editor.model_id == model_id) else { return };
match in_background {
true => self.activate_group(group),
false => self.focus_group(group, window, cx),
}
self.editors.activate(index);
self.editors_changed(cx);
}
/// The pick's `trigger`: Close Editor without moving the focus. Returns whether the editor went.
pub(super) fn trigger_editor_pick_close(&mut self, group: u32, model_id: u32, window: &mut Window, cx: &mut Context<Self>) -> bool {
let Some(index) = self.editors.group_tabs(group).0.iter().position(|editor| editor.model_id == model_id) else { return true };
// A dirty editor asks first, which waits on the group being the active one.
let outcome = match self.editor_shows_dirty(&self.editors.group_tabs(group).0[index]) {
true => {
self.activate_group(group);
self.close_editors(vec![index], window, cx)
}
false => self.with_group(group, |workbench| workbench.close_editors(vec![index], window, cx)),
};
self.editors_changed(cx);
matches!(outcome, Outcome::Ready(_)) && !self.editors.group_tabs(group).0.iter().any(|editor| editor.model_id == model_id)
}
}