Visual Studio Code, rebuilt in Rust
Rust
//! Several code editors in one group (a diff's sides, a multi diff's file sides, a notebook's cells, the playground's
//! snippets), each its own
//! `OpenEditor`. The focused one owns the group's viewport and text area cells; the others keep theirs here and draw
//! swapped in (`with_code_editor`) before the tab's body, which places them with `code_editor_area`.
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use gpui::{AnyElement, Bounds, Context, IntoElement, Window};
use super::Workbench;
use super::find_widget::FindWidget;
use crate::editor::minimap::MinimapLayoutInfo;
use crate::editor::{OpenEditor, Viewport};
#[derive(Default)]
pub(super) struct CodeEditors {
/// The code editor of each group that owns the group's viewport and text area cells.
owners: HashMap<u32, u32>,
/// Those of the other code editors, by group and model id.
parked: HashMap<(u32, u32), Area>,
/// The unfocused code editors the bodies asked for, by group and model id: each frame draws those of the last.
wanted: RefCell<HashSet<(u32, u32)>>,
/// The ones drawn for the body that draws now, by model id.
drawn: RefCell<HashMap<u32, AnyElement>>,
/// The code editor whose find widget is `Workbench::find`, and the other code editors' widgets (findController.ts:
/// a controller and widget per editor).
find_owner: Option<(u32, Instance)>,
finds: HashMap<(u32, Instance), FindWidget>,
}
/// A code editor of a group: the one of its text editor pane, whose models change with the tab; a diff or a merge
/// editor pane's; or one a tab stacks (a multi diff file side, a notebook cell, a playground snippet), by model id.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Instance {
Text,
DiffOriginal,
DiffModified,
MergeResult,
MergeInput(crate::merge_editor::Input),
Stacked(u32),
}
struct Area {
viewport: Viewport,
left: Rc<Cell<f32>>,
bounds: Rc<Cell<Bounds<f32>>>,
minimap: Option<(MinimapLayoutInfo, f64)>,
}
impl Workbench {
/// The group's viewport and text area cells go to the code editor that has the focus now (codeEditorWidget.ts: each
/// editor keeps its own layout).
pub(super) fn code_editor_focus_moved(&mut self) {
let group = self.editors.id;
let Some(focused) = self.editors.active().map(|editor| editor.model_id) else { return };
let Some(owner) = self.code_editors.owners.insert(group, focused).filter(|owner| *owner != focused) else { return };
let incoming = self.code_editors.parked.remove(&(group, focused)).unwrap_or_else(|| self.new_area());
let outgoing = self.swap_area(incoming);
self.code_editors.parked.insert((group, owner), outgoing);
}
/// A press on an unfocused code editor of the active tab focuses it (codeEditorWidget.ts: the DOM focus moves).
pub(super) fn focus_code_editor(&mut self, model_id: u32, window: &mut Window, cx: &mut Context<Self>) {
if !self.zone_editor_focus_moved(model_id, window, cx) && !self.editors.focus_multi_diff_side(model_id) && !self.editors.focus_merge_side(model_id) {
let original = self.editors.active_diff().and_then(|diff| diff.original_editor.as_ref()).is_some_and(|editor| editor.model_id == model_id);
self.editors.focus_diff_side(original);
}
self.code_editor_focus_moved();
self.find_editor_changed();
// mainThreadDocumentsAndEditors.ts: the editor with the text focus is the host's active one.
self.send_host_changes();
window.focus(&self.editor_focus, cx);
cx.notify();
}
/// A closed code editor's area goes; the focus moves on first if it had it.
pub(super) fn drop_code_editor(&mut self, model_id: u32) {
self.code_editor_focus_moved();
self.code_editors.parked.remove(&(self.editors.id, model_id));
}
/// Runs `run` with `model_id`, a code editor of the group without the focus, as the active editor in its own area.
pub(super) fn with_code_editor<R>(&mut self, model_id: u32, run: impl FnOnce(&mut Self) -> R) -> R {
self.code_editor_focus_moved();
if self.editors.drawn.is_some() || self.editors.active().is_some_and(|editor| editor.model_id == model_id) {
return run(self);
}
let group = self.editors.id;
let area = self.code_editors.parked.remove(&(group, model_id)).unwrap_or_else(|| self.new_area());
let focused = self.swap_area(area);
self.editors.drawn = Some(model_id);
self.find_editor_changed();
let result = run(self);
self.editors.drawn = None;
self.find_editor_changed();
let area = self.swap_area(focused);
self.code_editors.parked.insert((group, model_id), area);
result
}
/// Parks the find widget of the code editor that was active and brings in the active one's; the find and replace
/// histories stay (`FindWidgetSearchHistory.getOrCreate`: one per window).
pub(super) fn bind_find_widget(&mut self) {
let owner = self.active_code_editor_instance();
if owner == self.code_editors.find_owner {
return;
}
let previous = std::mem::replace(&mut self.code_editors.find_owner, owner);
let incoming = match owner.and_then(|owner| self.code_editors.finds.remove(&owner)) {
Some(parked) => parked,
// The widget no code editor had yet (none open) becomes this one's.
None if previous.is_none() => return,
None => self.find.sibling(),
};
let mut outgoing = std::mem::replace(&mut self.find, incoming);
std::mem::swap(&mut self.find.find_history, &mut outgoing.find_history);
std::mem::swap(&mut self.find.replace_history, &mut outgoing.replace_history);
if let Some(previous) = previous {
self.code_editors.finds.insert(previous, outgoing);
}
// A closed group's and a closed stacked editor's widgets go (`FindController.dispose`).
let group = self.editors.id;
let (editors, groups) = (&self.editors, &mut self.code_editors.finds);
groups.retain(|(owner_group, instance), _| match *owner_group == group {
true => !matches!(instance, Instance::Stacked(id) if editors.editor_by_id(*id).is_none()),
false => editors.group(*owner_group).is_some(),
});
}
fn active_code_editor_instance(&self) -> Option<(u32, Instance)> {
let tab = self.editors.active_tab()?;
let editor = self.editors.active()?;
let instance = if let Some(diff) = self.editors.diffs.get(&tab.model_id) {
match diff.original_editor.as_ref().is_some_and(|original| original.model_id == editor.model_id) {
true => Instance::DiffOriginal,
false => Instance::DiffModified,
}
} else if let Some(merge) = self.editors.merges.get(&tab.model_id) {
merge.focused_input.map_or(Instance::MergeResult, Instance::MergeInput)
} else if editor.model_id == tab.model_id {
Instance::Text
} else {
Instance::Stacked(editor.model_id)
};
Some((self.editors.id, instance))
}
/// The viewport unfocused code editor `model_id` of the group was last laid out in.
pub(super) fn code_editor_viewport(&self, model_id: u32) -> Viewport {
self.code_editors.parked.get(&(self.editors.id, model_id)).map_or(self.first_viewport(), |area| area.viewport)
}
/// The viewport of the active tab's pane editor, whether it has the focus, draws now or is parked.
pub(super) fn pane_editor_viewport(&self) -> Viewport {
let Some(pane) = self.editors.pane_editor().map(|editor| editor.model_id) else { return self.editors.viewport };
let owns = self.editors.drawn.map_or(self.code_editors.owners.get(&self.editors.id) == Some(&pane), |drawn| drawn == pane);
if owns { self.editors.viewport } else { self.code_editor_viewport(pane) }
}
/// A first layout: the focused editor's size until its own is measured, without the space its find widget keeps.
fn first_viewport(&self) -> Viewport {
Viewport { top_whitespace: 0., ..self.editors.viewport }
}
fn new_area(&self) -> Area {
Area { viewport: self.first_viewport(), left: Rc::new(Cell::new(self.text_area_left.get())), bounds: Rc::new(Cell::new(self.text_area_bounds.get())), minimap: None }
}
fn swap_area(&mut self, area: Area) -> Area {
Area {
viewport: std::mem::replace(&mut self.editors.viewport, area.viewport),
left: std::mem::replace(&mut self.text_area_left, area.left),
bounds: std::mem::replace(&mut self.text_area_bounds, area.bounds),
minimap: std::mem::replace(&mut self.minimap.info, area.minimap),
}
}
/// Draws the unfocused code editors of the active group that its body asked for last frame, as an inactive group's
/// editor draws: no focus, no input handler, no overlay widgets.
pub(super) fn draw_unfocused_code_editors(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.code_editor_focus_moved();
let group = self.editors.id;
let mut ids = Vec::new();
self.code_editors.wanted.borrow_mut().retain(|&(wanted_group, id)| {
if wanted_group == group {
ids.push(id);
}
wanted_group != group
});
// A focused comment editor or peek preview draws in a view zone of its tab's code editor, so before it, which then
// draws unfocused.
if let Some(focused) = self.editors.focused_zone_editor().and_then(|id| self.editors.editor_by_id(id)) {
let area = self.text_area(focused, window, cx).into_any_element();
self.code_editors.drawn.borrow_mut().insert(focused.model_id, area);
let pane = self.editors.pane_editor().map(|editor| editor.model_id);
ids.extend(pane.filter(|id| !ids.contains(id)));
}
let was_inactive = std::mem::replace(&mut self.editor_groups.drawing_inactive, true);
for id in ids {
if self.editors.editor_by_id(id).is_none() || self.editors.active().is_some_and(|editor| editor.model_id == id) {
continue;
}
let area = self.with_code_editor(id, |workbench| {
workbench.detect_colors_of_drawn_editor(cx);
let editor = workbench.editors.active().expect("drawn above");
workbench.text_area(editor, window, cx).into_any_element()
});
self.code_editors.drawn.borrow_mut().insert(id, area);
}
self.editor_groups.drawing_inactive = was_inactive;
}
/// The body drew: what it did not place is dropped.
pub(super) fn unfocused_code_editors_placed(&self) {
self.code_editors.drawn.borrow_mut().clear();
}
/// `editor`'s code editor: the focused one's text area, another's as drawn before the body. On an unfocused one's
/// first frame `None`, and the next frame draws it.
pub(super) fn code_editor_area(&self, editor: &OpenEditor, window: &mut Window, cx: &mut Context<Self>) -> Option<AnyElement> {
if self.editors.active().is_some_and(|active| std::ptr::eq(active, editor)) {
return Some(self.text_area(editor, window, cx).into_any_element());
}
self.code_editors.wanted.borrow_mut().insert((self.editors.id, editor.model_id));
let area = self.code_editors.drawn.borrow_mut().remove(&editor.model_id);
if area.is_none() {
window.request_animation_frame();
}
area
}
}