Visual Studio Code, rebuilt in Rust
Rust
//! notebookOutline.ts in the Outline view: `NotebookOutlinePaneProvider` rows of the active notebook editor (markdown
//! headers by default, `notebook.outline.*`), the active cell's entry selected, a click reveals its cell at the top.
use gpui::{AnyElement, ClickEvent, Context, Window, div, prelude::*, px};
use super::Workbench;
use super::notebook_editor::CellReveal;
use crate::notebook::outline::{self, Entry};
/// `NotebookOutlineVirtualDelegate.getHeight`.
const ROW_HEIGHT: f32 = 22.;
/// A shown entry: its index in the outline, its depth and whether it has shown children.
struct Row {
entry: usize,
depth: usize,
has_children: bool,
}
impl Workbench {
/// The Outline view's body while a notebook editor is active.
pub(super) fn notebook_outline_pane_body(&self, window: &Window, cx: &mut Context<Self>) -> Option<AnyElement> {
let editor = self.active_notebook_editor()?;
let tab = editor.tab_model_id;
let model = self.host.notebooks.model(&editor.notebook)?;
let entries = self.notebook_outline(model);
let (headers_only, code_cells) = self.notebook_outline_filters();
let rows = self.notebook_outline_rows(tab, &entries, headers_only, code_cells);
let selected = self.outline_pane.view_state.follow_cursor.then(|| active_shown_entry(&entries, editor.focus, headers_only, code_cells)).flatten();
let focused = self.outline_pane.focus.is_focused(window);
let (selection, selection_foreground) = match focused {
true => (self.color("list.activeSelectionBackground"), self.theme.color("list.activeSelectionForeground")),
false => (self.color("list.inactiveSelectionBackground"), self.theme.color("list.inactiveSelectionForeground")),
};
let indent = self.tree_indent();
let render_row = |index: usize, cx: &mut Context<Self>| {
let row = &rows[index];
let entry = &entries[row.entry];
let is_selected = selected == Some(row.entry);
let icon_color = match focused && is_selected {
true => self.list_selection_icon_color(),
false => self.color("icon.foreground"),
};
let id = row.entry.to_string();
let twistie = match row.has_children {
true => {
let chevron = if self.outline_row_expanded(tab, &id) { "chevron-down" } else { "chevron-right" };
let toggled = id.clone();
self.icon(chevron, 16., 16., self.foreground_or("icon.foreground")).id(("notebook-outline-twistie", index)).cursor_pointer().on_click(cx.listener(move |workbench, _: &ClickEvent, _, cx| {
cx.stop_propagation();
let expanded = workbench.outline_row_expanded(tab, &toggled);
workbench.set_outline_row_expanded(tab, &toggled, !expanded);
cx.notify();
}))
}
false => div().id(("notebook-outline-twistie", index)).size(px(16.)).flex_none(),
};
let icon = (self.setting("outline.icons") != false).then(|| self.outline_entry_icon(model, entry, icon_color, ROW_HEIGHT)).flatten();
let cell = entry.cell;
div()
.id(("notebook-outline-row", index))
.h(px(ROW_HEIGHT))
.flex_none()
.flex()
.items_center()
.cursor_pointer()
.mx(px(self.pane_row_inset()))
.rounded(px(4.))
.pl(px(11. + indent * row.depth as f32))
.when(!is_selected, |row| row.hover(|row| row.bg(self.color("list.hoverBackground"))))
.when(is_selected, |row| row.bg(selection).when_some(selection_foreground, |row, color| row.text_color(color)))
.child(twistie)
.child(
div()
// tree.css: the twistie's 6px padding-right less its translateX(3px).
.ml(px(3.))
.flex_1()
.min_w_0()
.flex()
.items_center()
.overflow_hidden()
.whitespace_nowrap()
.children(icon)
.child(format!(" {}", entry.label.replace('\n', "\u{23ce}"))),
)
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
window.focus(&workbench.outline_pane.focus, cx);
workbench.reveal_notebook_outline_entry(tab, cell, cx);
}))
.into_any_element()
};
let elements: Vec<AnyElement> = (0..rows.len()).map(|index| render_row(index, cx)).collect();
let tree = div().id("notebook-outline-tree").track_focus(&self.outline_pane.focus).size_full().overflow_y_scroll().track_scroll(&self.outline_pane.scroll).line_height(px(ROW_HEIGHT)).children(elements);
let list = super::list_scroll::ListId::Outline;
let sticky = self.tree_sticky_rows(list, &rows.iter().map(|row| row.depth).collect::<Vec<_>>());
let sticky_elements = sticky.iter().map(|sticky_row| render_row(sticky_row.index, cx)).collect();
let sticky_container = self.tree_sticky_container(list, &sticky, sticky_elements, cx);
Some(div().relative().size_full().child(tree).child(self.list_scroll_layer(list, cx)).children(sticky_container).into_any_element())
}
/// outlinePane.ts `updateAllCollapsedCtx` while a notebook editor is active: every top entry is a leaf or collapsed.
pub(super) fn notebook_outline_all_collapsed(&self) -> Option<bool> {
let editor = self.active_notebook_editor()?;
let model = self.host.notebooks.model(&editor.notebook)?;
let (headers_only, code_cells) = self.notebook_outline_filters();
let rows = self.notebook_outline_rows(editor.tab_model_id, &self.notebook_outline(model), headers_only, code_cells);
Some(rows.iter().filter(|row| row.depth == 0).all(|row| !row.has_children || !self.outline_row_expanded(editor.tab_model_id, &row.entry.to_string())))
}
/// The shown rows' depths while a notebook editor is active, for the sticky rows.
pub(super) fn notebook_outline_depths(&self) -> Option<Vec<usize>> {
let editor = self.active_notebook_editor()?;
let model = self.host.notebooks.model(&editor.notebook)?;
let (headers_only, code_cells) = self.notebook_outline_filters();
Some(self.notebook_outline_rows(editor.tab_model_id, &self.notebook_outline(model), headers_only, code_cells).iter().map(|row| row.depth).collect())
}
fn notebook_outline_filters(&self) -> (bool, bool) {
(self.setting("notebook.outline.showMarkdownHeadersOnly") != false, self.setting("notebook.outline.showCodeCells") == true)
}
/// The active notebook editor's outline tab and the ids of its entries that have children.
pub(super) fn notebook_outline_parents(&self) -> Option<(u32, Vec<String>)> {
let editor = self.active_notebook_editor()?;
let entries = self.notebook_outline(self.host.notebooks.model(&editor.notebook)?);
let parents = (0..entries.len()).filter(|index| entries.iter().any(|entry| entry.parent == Some(*index))).map(|index| index.to_string()).collect();
Some((editor.tab_model_id, parents))
}
/// `getChildren` from the top, each shown entry followed by its shown children when expanded.
fn notebook_outline_rows(&self, tab: u32, entries: &[Entry], headers_only: bool, code_cells: bool) -> Vec<Row> {
let shown = |index: usize| !outline::filtered(&entries[index], headers_only, code_cells);
let mut rows = Vec::new();
push_rows(&mut rows, entries, None, 0, &shown, &|id: &str| self.outline_row_expanded(tab, id));
rows
}
/// `NotebookCellOutline.reveal`: the entry's cell is focused and at the top of the list.
fn reveal_notebook_outline_entry(&mut self, tab: u32, cell: usize, cx: &mut Context<Self>) {
let Some(editor) = self.host.notebooks.editors.iter_mut().find(|editor| editor.tab_model_id == tab) else { return };
editor.focus = cell;
editor.selections = vec![(cell, cell + 1)];
self.reveal_notebook_cells(tab, cell, cell + 1, CellReveal::Top, cx);
self.editors_changed(cx);
cx.notify();
}
}
fn push_rows(rows: &mut Vec<Row>, entries: &[Entry], parent: Option<usize>, depth: usize, shown: &impl Fn(usize) -> bool, expanded: &impl Fn(&str) -> bool) {
for index in (0..entries.len()).filter(|index| entries[*index].parent == parent && shown(*index)) {
let has_children = entries.iter().enumerate().any(|(child, entry)| entry.parent == Some(index) && shown(child));
rows.push(Row { entry: index, depth, has_children });
if has_children && expanded(&index.to_string()) {
push_rows(rows, entries, Some(index), depth + 1, shown, expanded);
}
}
}
/// `NotebookOutlinePaneProvider.getActiveEntry`: the focused cell's entry, else its nearest shown parent.
fn active_shown_entry(entries: &[Entry], focus: usize, headers_only: bool, code_cells: bool) -> Option<usize> {
let mut candidate = outline::active(entries, focus);
while let Some(index) = candidate {
if !outline::filtered(&entries[index], headers_only, code_cells) {
return Some(index);
}
candidate = entries[index].parent;
}
None
}