Visual Studio Code, rebuilt in Rust
Rust
fix(minimap): follow view lines of wrapped and folded lines
rokartur9e3864219362
diff --git a/native/crates/app/src/editor/word_wrap.rs b/native/crates/app/src/editor/word_wrap.rsindex d5e65fc560b..f0dfa3f8012 100644--- a/native/crates/app/src/editor/word_wrap.rs+++ b/native/crates/app/src/editor/word_wrap.rs@@ -452,7 +452,7 @@ impl OpenEditor {}/// The first view line of every model line and the view line count last; a hidden line has none.- fn view_starts(&self) -> Rc<Vec<usize>> {+ pub fn view_starts(&self) -> Rc<Vec<usize>> {let hidden = self.folding.hidden();if let Some((cached_hidden, starts)) = &*self.word_wrap.starts.borrow()&& cached_hidden == hiddendiff --git a/native/crates/app/src/workbench/minimap_view.rs b/native/crates/app/src/workbench/minimap_view.rsindex c33931a5c34..943343ef3b0 100644--- a/native/crates/app/src/workbench/minimap_view.rs+++ b/native/crates/app/src/workbench/minimap_view.rs@@ -3,6 +3,7 @@//! the shadow.use std::cell::{Cell, RefCell};+use std::ops::Range;use std::rc::Rc;use std::sync::Arc;use std::time::{Duration, Instant};@@ -19,7 +20,7 @@ use crate::editor::minimap::{CharRenderer, LinesColors, MINIMAP_GUTTER_WIDTH, MinimapLayout, MinimapLayoutInfo, MinimapLayoutInput, MinimapOptions, MinimapRange, MinimapSampling, MinimapSide, MinimapSize, RenderedLines,SliderOptions, compute_minimap_layout, contained_minimap_line_count, decoration_fills, render_lines,};-use crate::editor::folding::FoldDecoration;+use crate::editor::folding::{FoldDecoration, hidden_range};use crate::editor::section_headers::{FindOptions, MARK_SECTION_HEADER_REGEX, SectionHeader, find_section_headers, fit_section_header};use crate::editor::{OpenEditor, Position, TextModel, VERTICAL_SCROLLBAR_WIDTH, Viewport};use crate::theme::relative_luminance;@@ -43,8 +44,8 @@ pub(super) struct Minimap {pub info: Option<(MinimapLayoutInfo, f64)>,/// `_lastRenderData.renderedLayout` with the model it showed: the next layout keeps the lines from jumping back.rendered: Cell<Option<(u32, MinimapLayout)>>,- /// `_samplingState` with the model, its version and whether its view lines were its model lines.- sampling: RefCell<Option<(u32, u32, bool, MinimapSampling)>>,+ /// `_samplingState` with the model, its version and the view lines of its model lines then.+ sampling: RefCell<Option<(u32, u32, ViewLineCounts, MinimapSampling)>>,image: RefCell<Option<Arc<RenderImage>>>,/// The RGBA lines behind `image`, whose unchanged rows the next frame copies.lines: RefCell<Option<RenderedLines>>,@@ -173,26 +174,97 @@ fn minimap_sampling(editor: &OpenEditor, viewport: Viewport, pixel_ratio: f64, oMinimapSampling::compute(editor.view_line_count(), contained_minimap_line_count(&input).minimap_line_count as usize, old)}-/// `MinimapSamplingState.onLinesDeleted` and `onLinesInserted` for the model's replaces since `version`, as-/// textModel.ts `_doApplyEdits` reports them: the lines past the edited ones are deleted or inserted.-// shortcut: replayed only while view lines are model lines; with word wrap or folds the state is kept as after-// `onFlushed`, upgrade when the port keeps the view line events of wrapped and folded lines.-fn follow_edits(sampling: &mut MinimapSampling, model: &TextModel, version: u32) {+/// viewModelLines.ts `projectedModelLineLineCounts`: the view lines of each model line, none for a hidden one.+#[derive(Clone)]+struct ViewLineCounts {+ line_count: usize,+ hidden: Vec<Range<usize>>,+ wrapped_starts: Option<Rc<Vec<usize>>>,+}++impl ViewLineCounts {+ fn of(editor: &OpenEditor) -> Self {+ ViewLineCounts { line_count: editor.document().lines.len(), hidden: editor.folding.hidden().to_vec(), wrapped_starts: editor.wraps().then(|| editor.view_starts()) }+ }++ fn count(&self, line: usize) -> usize {+ match &self.wrapped_starts {+ Some(starts) => starts[line + 1] - starts[line],+ None => usize::from(hidden_range(&self.hidden, line).is_none()),+ }+ }+}++/// `MinimapSamplingState.onLinesDeleted` and `onLinesInserted` for the view line events of the model's replaces+/// since `old`'s version: textModel.ts `_doApplyEdits` reports each replace's changed lines (last first), its+/// deleted lines, then its inserted ones, and viewModelLines.ts turns them into view lines, a changed or inserted+/// line with its line breaks after the edits (`lineNumberPostEdit`).+fn follow_edits(sampling: &mut MinimapSampling, model: &TextModel, version: u32, old: &ViewLineCounts, new: &ViewLineCounts) {if !model.edits.first().is_some_and(|(first, ..)| *first <= version) {return;}- for &(_, start, end, new_end, _) in model.edits.iter().filter(|(edit_version, ..)| *edit_version >= version) {+ let first = model.edits.partition_point(|(edit_version, ..)| *edit_version < version);+ let edits = &model.edits[first..];+ // A replace the log does not hold (a reload) is a `Flush`: the state stays as `onFlushed` keeps it.+ let line_delta: isize = edits.iter().map(|(_, _, end, new_end, _)| new_end.line as isize - end.line as isize).sum();+ if old.line_count as isize + line_delta != new.line_count as isize {+ return;+ }+ let mut counts: Vec<_> = (0..old.line_count).map(|line| old.count(line)).collect();+ for (index, &(_, start, end, new_end, _)) in edits.iter().enumerate() {+ // shortcut: a line two versions between frames both edit counts its last line breaks at both, where upstream+ // breaks it per version; upgrade if the word wrap keeps the breaks of each version.+ let new_count = |line: usize| new.count(line_after(&edits[index + 1..], line));let (deleting, inserting) = (end.line - start.line, new_end.line - start.line);- let splice = start.line + 1 + deleting.min(inserting);- if deleting > inserting {- sampling.on_lines_deleted(splice + 1, start.line + 1 + deleting);+ let editing = deleting.min(inserting);+ let edit_start = counts[..start.line].iter().sum::<usize>() + 1;+ let mut line_start = edit_start + counts[start.line..=start.line + editing].iter().sum::<usize>();+ for line in (start.line..=start.line + editing).rev() {+ let (old_count, new_count) = (counts[line], new_count(line));+ line_start -= old_count;+ let kept_end = line_start + old_count.min(new_count);+ if old_count > new_count {+ sampling.on_lines_deleted(kept_end, kept_end + old_count - new_count - 1);+ }+ if new_count > old_count {+ sampling.on_lines_inserted(kept_end, kept_end + new_count - old_count - 1);+ }+ counts[line] = new_count;}- if inserting > deleting {- sampling.on_lines_inserted(splice + 1, start.line + 1 + inserting);+ let splice = start.line + editing + 1;+ let splice_view_line = edit_start + counts[start.line..splice].iter().sum::<usize>();+ if deleting > editing {+ let deleted: usize = counts.drain(splice..=start.line + deleting).sum();+ if deleted > 0 {+ sampling.on_lines_deleted(splice_view_line, splice_view_line + deleted - 1);+ }+ }+ if inserting > editing {+ let inserted: Vec<_> = (splice..=start.line + inserting).map(new_count).collect();+ let inserted_view_lines: usize = inserted.iter().sum();+ if inserted_view_lines > 0 {+ sampling.on_lines_inserted(splice_view_line, splice_view_line + inserted_view_lines - 1);+ }+ counts.splice(splice..splice, inserted);}}}+/// The 0-based model line `line` moved through `edits`; a line inside a replace keeps its offset in it, clamped.+fn line_after(edits: &[(u32, Position, Position, Position, usize)], line: usize) -> usize {+ let mut line = line;+ for &(_, start, end, new_end, _) in edits {+ line = if line < start.line {+ line+ } else if line > end.line {+ line - end.line + new_end.line+ } else {+ start.line + (line - start.line).min(new_end.line - start.line)+ };+ }+ line+}+/// The 1-based view line a 1-based minimap line shows.fn sampled_view_line(sampling: Option<&MinimapSampling>, minimap_line: usize) -> usize {sampling.map_or(minimap_line, |sampling| sampling.lines[minimap_line - 1])@@ -202,18 +274,16 @@ impl Workbench {/// `_recreateLineSampling` from the last frame's state, moved through the edits since.fn sticky_minimap_sampling(&self, editor: &OpenEditor, viewport: Viewport, pixel_ratio: f64, options: MinimapOptions) -> MinimapSampling {let model = editor.text_model();- let view_is_model = editor.view_line_count() == model.document.lines.len();+ let view_lines = ViewLineCounts::of(editor);let mut cached = self.minimap.sampling.borrow_mut();let mut old = cached.take().filter(|(model_id, ..)| *model_id == editor.model_id);- if let Some((_, version, old_view_is_model, sampling)) = &mut old+ if let Some((_, version, old_view_lines, sampling)) = &mut old&& *version != model.version_id- && *old_view_is_model- && view_is_model{- follow_edits(sampling, &model, *version);+ follow_edits(sampling, &model, *version, old_view_lines, &view_lines);}let sampling = minimap_sampling(editor, viewport, pixel_ratio, options, old.as_ref().map(|(.., sampling)| sampling));- *cached = Some((editor.model_id, model.version_id, view_is_model, sampling.clone()));+ *cached = Some((editor.model_id, model.version_id, view_lines, sampling.clone()));sampling}@@ -756,7 +826,7 @@ mod tests {#[test]fn sampling_follows_the_lines_each_edit_inserts_and_deletes() {let mut editor = test_editor("0\n1\n2\n3\n4\n5\n6\n7\n8\n9", "\n");- let version = editor.text_model().version_id;+ let (version, before) = (editor.text_model().version_id, ViewLineCounts::of(&editor));let sampled = MinimapSampling::compute(10, 4, None);editor.move_to(Position::new(1, 0), false);editor.type_text("\n");@@ -764,7 +834,7 @@ mod tests {editor.move_to(Position::new(7, 0), true);editor.type_text("x");let mut followed = sampled.clone();- follow_edits(&mut followed, &editor.text_model(), version);+ follow_edits(&mut followed, &editor.text_model(), version, &before, &ViewLineCounts::of(&editor));// textModel.ts `_doApplyEdits`: the newline inserts line 3, the two-line replace deletes lines 7 to 8.let mut expected = sampled.clone();expected.on_lines_inserted(3, 3);@@ -772,4 +842,35 @@ mod tests {assert_ne!(followed.lines, sampled.lines);assert_eq!(followed.lines, expected.lines);}++ #[test]+ fn sampling_follows_the_view_lines_of_wrapped_and_folded_lines() {+ use crate::editor::folding::{FoldRange, FoldingRegions};+ use crate::editor::word_wrap::{BREAK_AFTER, BREAK_BEFORE, WrapOptions, WrappingIndent};+ let mut editor = test_editor("a\nb\nc\nd\ne\nf\ng\nh\ni\nj", "\n");+ let options = WrapOptions { column: 10, viewport_wrapping: false, tab_size: 4, indent: WrappingIndent::None, keep_all: false, wrap_on_escaped_line_feeds: false, columns_for_full_width_char: 2., two_cell_full_width: false, break_before: BREAK_BEFORE.into(), break_after: BREAK_AFTER.into() };+ let lines = editor.document().lines.clone();+ editor.word_wrap.configure(Some(options), &lines, &[]);+ editor.folding.update_post(FoldingRegions::new(vec![FoldRange { collapsed: true, ..FoldRange::new(6, 8) }]));+ assert_eq!(editor.view_line_count(), 8);+ let (version, before) = (editor.text_model().version_id, ViewLineCounts::of(&editor));+ let sampled = MinimapSampling::compute(8, 8, None);+ editor.move_to(Position::new(2, 1), false);+ editor.type_text(" xxxx yyyy zzzz");+ assert_eq!(editor.view_line_count(), 9);+ editor.move_to(Position::new(3, 0), false);+ editor.move_to(Position::new(4, 0), true);+ editor.type_text("x");+ editor.move_to(Position::new(7, 1), false);+ editor.type_text("\n");+ let mut followed = sampled.clone();+ follow_edits(&mut followed, &editor.text_model(), version, &before, &ViewLineCounts::of(&editor));+ // viewModelLines.ts: line 3 wraps into view lines 3 and 4, line 5 (view line 6, past the wrapped line) is+ // deleted, and the line inserted after "i" is view line 8, past the two folded lines.+ let mut expected = sampled.clone();+ expected.on_lines_inserted(4, 4);+ expected.on_lines_deleted(6, 6);+ expected.on_lines_inserted(8, 8);+ assert_eq!(followed.lines, expected.lines);+ }}diff --git a/port/ledger.md b/port/ledger.mdindex d05d0b122e2..c271d994024 100644--- a/port/ledger.md+++ b/port/ledger.md@@ -340,7 +340,7 @@ One row per upstream unit, generated by `bun port/tools/gen-ledger.ts`; mileston| platform/windows | src/vs/platform | M11 | verified | | windows_state.rs: cargo test -p app windows_state; ui-pair hot-exit.json `restored` (port/results/windows.md, milestone 11 verification). The default size stays 1024x768, the capture size of port/tools/ref-screenshot, where upstream takes 1200x800 or 1440x900 || platform/workspace | src/vs/platform | M12 | verified | | virtual workspaces (non-file folders): milestone 25; config/src/workspace.rs: folders, getFolder, toWorkspaceFolders, workspace label; cargo test -p config workspace (workspace.test.ts cases); api-workspace 4/4; verified: api-tests MOCHA_GREP="vscode API - (workspace|file system|env|types|tasks)" at 44a309311e1: every workspace, workspace-fs, workspace events, workspace-watcher and file system test passes, api-workspace 4/0/0 (port/results/api-tests.md "M12 verification"), api-workspace 4/4 || platform/workspaces | src/vs/platform | M12 | verified | | session.rs, recently_opened.rs, config/src/workspace.rs, workbench/workspace_editing.rs: cargo test -p exthost (workspaces.test.ts stable id case); cargo test -p config workspace:: 5/5; cargo test -p app recently_opened workspace_editing; ui-pair port/fixtures/ui-pair/m12/workspace-folders.json (Remove Folder from Workspace enters Untitled (Workspace)) |-| editor/browser | src/vs/editor | M8 | ported | [3 S] minimap_view.rs:179: with word wrap or folds the sticky minimap sampling is not moved through edits, where upstream follows the view line events of wrapped and folded lines. | editor/view.rs, editor/folding.rs, editor/minimap.rs, editor/word_wrap.rs, editor/smooth_scroll.rs, editor/mouse_selection.rs, workbench/editor_view.rs, workbench/gutter_mouse.rs, workbench/editor_view/text_decorations.rs: cargo test -p app editor::view::tests editor::folding minimap section_headers editor_menus selection_corners whitespace control_characters gutter_presses word_wrap smooth_scroll diff_editor diff_gutter switch_side bracket_pair_guide wrapping_indent_repeats without_a_wrapping_indent file_compare char_ranges a_click_without_a_move mouse_selection text_clicks_select a_selecting_drag_below a_drag_into_the_vertical the_bottom_edge_hit a_drag_below_a_comment text_input:: outline_and_border_follow_the_css_cascade a_decoration_outline_is_drawn_outside_its_range decoration_types_set_and_remove_editor_decorations wrapping_a_padded_inlay_hint_skips_its_padding_box a_selection_over_changes_shows_the_selection_toolbar; ui-pair m12/compare.json, port/fixtures/ui-pair/menus/diff-gutter-selection.json, port/fixtures/ui-pair/m8/inlay-hint-wrap.json (port/notes.md milestones 8 and 14) |+| editor/browser | src/vs/editor | M8 | verified | | editor/view.rs, editor/folding.rs, editor/minimap.rs, editor/word_wrap.rs, editor/smooth_scroll.rs, editor/mouse_selection.rs, workbench/editor_view.rs, workbench/gutter_mouse.rs, workbench/editor_view/text_decorations.rs: cargo test -p app editor::view::tests editor::folding minimap section_headers editor_menus selection_corners whitespace control_characters gutter_presses word_wrap smooth_scroll diff_editor diff_gutter switch_side bracket_pair_guide wrapping_indent_repeats without_a_wrapping_indent file_compare char_ranges a_click_without_a_move mouse_selection text_clicks_select a_selecting_drag_below a_drag_into_the_vertical the_bottom_edge_hit a_drag_below_a_comment text_input:: outline_and_border_follow_the_css_cascade a_decoration_outline_is_drawn_outside_its_range decoration_types_set_and_remove_editor_decorations wrapping_a_padded_inlay_hint_skips_its_padding_box a_selection_over_changes_shows_the_selection_toolbar; ui-pair m12/compare.json, port/fixtures/ui-pair/menus/diff-gutter-selection.json, port/fixtures/ui-pair/m8/inlay-hint-wrap.json (port/notes.md milestones 8 and 14) || editor/common | src/vs/editor | M8 | verified | | app/src/editor, decorations.rs, link_computer.rs, syntax.rs, brackets/tree.rs, the diff crate: cargo test -p app editor:: decorations:: link_computer syntax:: auto_closing brackets lines_in_view_past_the_tokenized_ones the_word_at_a_column full_width_chars_take moving_down_keeps a_hyphen_next_to_a_class_escape_is_literal (13 ignored: upstream's `test.skip` cases); cargo test -p diff; typing on the 52k-line TS file under 16 ms per edit with the section headers and unicode highlights, trim and comment on it just opened under 16 ms, its bracket selection scan under 30 ms (port/notes.md "Scheduling differences"): cargo gc test --release -p app -- --ignored typing_latency_on_a_large_file. By design: the piece tree text buffer is a vector of lines (large files through workbench/large_file.rs) || editor/standalone | src/vs/editor | | removed | | Artur: internal infrastructure or developer tool with no 1:1 counterpart in the native port || editor/test | src/vs/editor | M6 | n/a | | upstream test infrastructure; the port tests with cargo test and port/tools |