Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookViewZones.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/456import assert from 'assert';7import { DisposableStore } from '../../../../../base/common/lifecycle.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';9import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';10import { TestConfigurationService } from '../../../../../platform/configuration/test/common/testConfigurationService.js';11import { TestInstantiationService } from '../../../../../platform/instantiation/test/common/instantiationServiceMock.js';12import { NotebookCellsLayout } from '../../browser/view/notebookCellListView.js';13import { FoldingModel } from '../../browser/viewModel/foldingModel.js';14import { CellEditType, CellKind } from '../../common/notebookCommon.js';15import { createNotebookCellList, setupInstantiationService, withTestNotebook } from './testNotebookEditor.js';1617suite('NotebookRangeMap', () => {1819ensureNoDisposablesAreLeakedInTestSuite();2021test('empty', () => {22const rangeMap = new NotebookCellsLayout();23assert.strictEqual(rangeMap.size, 0);24assert.strictEqual(rangeMap.count, 0);25});2627const one = { size: 1 };28const two = { size: 2 };29const three = { size: 3 };30const five = { size: 5 };31const ten = { size: 10 };3233test('length & count', () => {34const rangeMap = new NotebookCellsLayout();35rangeMap.splice(0, 0, [one]);36assert.strictEqual(rangeMap.size, 1);37assert.strictEqual(rangeMap.count, 1);38});3940test('length & count #2', () => {41const rangeMap = new NotebookCellsLayout();42rangeMap.splice(0, 0, [one, one, one, one, one]);43assert.strictEqual(rangeMap.size, 5);44assert.strictEqual(rangeMap.count, 5);45});4647test('length & count #3', () => {48const rangeMap = new NotebookCellsLayout();49rangeMap.splice(0, 0, [five]);50assert.strictEqual(rangeMap.size, 5);51assert.strictEqual(rangeMap.count, 1);52});5354test('length & count #4', () => {55const rangeMap = new NotebookCellsLayout();56rangeMap.splice(0, 0, [five, five, five, five, five]);57assert.strictEqual(rangeMap.size, 25);58assert.strictEqual(rangeMap.count, 5);59});6061test('insert', () => {62const rangeMap = new NotebookCellsLayout();63rangeMap.splice(0, 0, [five, five, five, five, five]);64assert.strictEqual(rangeMap.size, 25);65assert.strictEqual(rangeMap.count, 5);6667rangeMap.splice(0, 0, [five, five, five, five, five]);68assert.strictEqual(rangeMap.size, 50);69assert.strictEqual(rangeMap.count, 10);7071rangeMap.splice(5, 0, [ten, ten]);72assert.strictEqual(rangeMap.size, 70);73assert.strictEqual(rangeMap.count, 12);7475rangeMap.splice(12, 0, [{ size: 200 }]);76assert.strictEqual(rangeMap.size, 270);77assert.strictEqual(rangeMap.count, 13);78});7980test('delete', () => {81const rangeMap = new NotebookCellsLayout();82rangeMap.splice(0, 0, [five, five, five, five, five,83five, five, five, five, five,84five, five, five, five, five,85five, five, five, five, five]);86assert.strictEqual(rangeMap.size, 100);87assert.strictEqual(rangeMap.count, 20);8889rangeMap.splice(10, 5);90assert.strictEqual(rangeMap.size, 75);91assert.strictEqual(rangeMap.count, 15);9293rangeMap.splice(0, 1);94assert.strictEqual(rangeMap.size, 70);95assert.strictEqual(rangeMap.count, 14);9697rangeMap.splice(1, 13);98assert.strictEqual(rangeMap.size, 5);99assert.strictEqual(rangeMap.count, 1);100101rangeMap.splice(1, 1);102assert.strictEqual(rangeMap.size, 5);103assert.strictEqual(rangeMap.count, 1);104});105106test('insert & delete', () => {107const rangeMap = new NotebookCellsLayout();108assert.strictEqual(rangeMap.size, 0);109assert.strictEqual(rangeMap.count, 0);110111rangeMap.splice(0, 0, [one]);112assert.strictEqual(rangeMap.size, 1);113assert.strictEqual(rangeMap.count, 1);114115rangeMap.splice(0, 1);116assert.strictEqual(rangeMap.size, 0);117assert.strictEqual(rangeMap.count, 0);118});119120test('insert & delete #2', () => {121const rangeMap = new NotebookCellsLayout();122rangeMap.splice(0, 0, [one, one, one, one, one,123one, one, one, one, one]);124rangeMap.splice(2, 6);125assert.strictEqual(rangeMap.count, 4);126assert.strictEqual(rangeMap.size, 4);127});128129test('insert & delete #3', () => {130const rangeMap = new NotebookCellsLayout();131rangeMap.splice(0, 0, [one, one, one, one, one,132one, one, one, one, one,133two, two, two, two, two,134two, two, two, two, two]);135rangeMap.splice(8, 4);136assert.strictEqual(rangeMap.count, 16);137assert.strictEqual(rangeMap.size, 24);138});139140test('insert & delete #4', () => {141const rangeMap = new NotebookCellsLayout();142rangeMap.splice(0, 0, [one, one, one, one, one,143one, one, one, one, one,144two, two, two, two, two,145two, two, two, two, two]);146rangeMap.splice(5, 0, [three, three, three, three, three]);147assert.strictEqual(rangeMap.count, 25);148assert.strictEqual(rangeMap.size, 45);149150rangeMap.splice(4, 7);151assert.strictEqual(rangeMap.count, 18);152assert.strictEqual(rangeMap.size, 28);153});154155suite('indexAt, positionAt', () => {156test('empty', () => {157const rangeMap = new NotebookCellsLayout();158assert.strictEqual(rangeMap.indexAt(0), 0);159assert.strictEqual(rangeMap.indexAt(10), 0);160assert.strictEqual(rangeMap.indexAt(-1), -1);161assert.strictEqual(rangeMap.positionAt(0), -1);162assert.strictEqual(rangeMap.positionAt(10), -1);163assert.strictEqual(rangeMap.positionAt(-1), -1);164});165166test('simple', () => {167const rangeMap = new NotebookCellsLayout();168rangeMap.splice(0, 0, [one]);169assert.strictEqual(rangeMap.indexAt(0), 0);170assert.strictEqual(rangeMap.indexAt(1), 1);171assert.strictEqual(rangeMap.positionAt(0), 0);172assert.strictEqual(rangeMap.positionAt(1), -1);173});174175test('simple #2', () => {176const rangeMap = new NotebookCellsLayout();177rangeMap.splice(0, 0, [ten]);178assert.strictEqual(rangeMap.indexAt(0), 0);179assert.strictEqual(rangeMap.indexAt(5), 0);180assert.strictEqual(rangeMap.indexAt(9), 0);181assert.strictEqual(rangeMap.indexAt(10), 1);182assert.strictEqual(rangeMap.positionAt(0), 0);183assert.strictEqual(rangeMap.positionAt(1), -1);184});185186test('insert', () => {187const rangeMap = new NotebookCellsLayout();188rangeMap.splice(0, 0, [one, one, one, one, one, one, one, one, one, one]);189assert.strictEqual(rangeMap.indexAt(0), 0);190assert.strictEqual(rangeMap.indexAt(1), 1);191assert.strictEqual(rangeMap.indexAt(5), 5);192assert.strictEqual(rangeMap.indexAt(9), 9);193assert.strictEqual(rangeMap.indexAt(10), 10);194assert.strictEqual(rangeMap.indexAt(11), 10);195196rangeMap.splice(10, 0, [one, one, one, one, one, one, one, one, one, one]);197assert.strictEqual(rangeMap.indexAt(10), 10);198assert.strictEqual(rangeMap.indexAt(19), 19);199assert.strictEqual(rangeMap.indexAt(20), 20);200assert.strictEqual(rangeMap.indexAt(21), 20);201assert.strictEqual(rangeMap.positionAt(0), 0);202assert.strictEqual(rangeMap.positionAt(1), 1);203assert.strictEqual(rangeMap.positionAt(19), 19);204assert.strictEqual(rangeMap.positionAt(20), -1);205});206207test('delete', () => {208const rangeMap = new NotebookCellsLayout();209rangeMap.splice(0, 0, [one, one, one, one, one, one, one, one, one, one]);210rangeMap.splice(2, 6);211212assert.strictEqual(rangeMap.indexAt(0), 0);213assert.strictEqual(rangeMap.indexAt(1), 1);214assert.strictEqual(rangeMap.indexAt(3), 3);215assert.strictEqual(rangeMap.indexAt(4), 4);216assert.strictEqual(rangeMap.indexAt(5), 4);217assert.strictEqual(rangeMap.positionAt(0), 0);218assert.strictEqual(rangeMap.positionAt(1), 1);219assert.strictEqual(rangeMap.positionAt(3), 3);220assert.strictEqual(rangeMap.positionAt(4), -1);221});222223test('delete #2', () => {224const rangeMap = new NotebookCellsLayout();225rangeMap.splice(0, 0, [ten, ten, ten, ten, ten, ten, ten, ten, ten, ten]);226rangeMap.splice(2, 6);227228assert.strictEqual(rangeMap.indexAt(0), 0);229assert.strictEqual(rangeMap.indexAt(1), 0);230assert.strictEqual(rangeMap.indexAt(30), 3);231assert.strictEqual(rangeMap.indexAt(40), 4);232assert.strictEqual(rangeMap.indexAt(50), 4);233assert.strictEqual(rangeMap.positionAt(0), 0);234assert.strictEqual(rangeMap.positionAt(1), 10);235assert.strictEqual(rangeMap.positionAt(2), 20);236assert.strictEqual(rangeMap.positionAt(3), 30);237assert.strictEqual(rangeMap.positionAt(4), -1);238});239});240});241242suite('NotebookRangeMap with top padding', () => {243244ensureNoDisposablesAreLeakedInTestSuite();245246test('empty', () => {247const rangeMap = new NotebookCellsLayout(10);248assert.strictEqual(rangeMap.size, 10);249assert.strictEqual(rangeMap.count, 0);250});251252const one = { size: 1 };253const five = { size: 5 };254const ten = { size: 10 };255256test('length & count', () => {257const rangeMap = new NotebookCellsLayout(10);258rangeMap.splice(0, 0, [one]);259assert.strictEqual(rangeMap.size, 11);260assert.strictEqual(rangeMap.count, 1);261});262263test('length & count #2', () => {264const rangeMap = new NotebookCellsLayout(10);265rangeMap.splice(0, 0, [one, one, one, one, one]);266assert.strictEqual(rangeMap.size, 15);267assert.strictEqual(rangeMap.count, 5);268});269270test('length & count #3', () => {271const rangeMap = new NotebookCellsLayout(10);272rangeMap.splice(0, 0, [five]);273assert.strictEqual(rangeMap.size, 15);274assert.strictEqual(rangeMap.count, 1);275});276277test('length & count #4', () => {278const rangeMap = new NotebookCellsLayout(10);279rangeMap.splice(0, 0, [five, five, five, five, five]);280assert.strictEqual(rangeMap.size, 35);281assert.strictEqual(rangeMap.count, 5);282});283284test('insert', () => {285const rangeMap = new NotebookCellsLayout(10);286rangeMap.splice(0, 0, [five, five, five, five, five]);287assert.strictEqual(rangeMap.size, 35);288assert.strictEqual(rangeMap.count, 5);289290rangeMap.splice(0, 0, [five, five, five, five, five]);291assert.strictEqual(rangeMap.size, 60);292assert.strictEqual(rangeMap.count, 10);293294rangeMap.splice(5, 0, [ten, ten]);295assert.strictEqual(rangeMap.size, 80);296assert.strictEqual(rangeMap.count, 12);297298rangeMap.splice(12, 0, [{ size: 200 }]);299assert.strictEqual(rangeMap.size, 280);300assert.strictEqual(rangeMap.count, 13);301});302303suite('indexAt, positionAt', () => {304test('empty', () => {305const rangeMap = new NotebookCellsLayout(10);306assert.strictEqual(rangeMap.indexAt(0), 0);307assert.strictEqual(rangeMap.indexAt(10), 0);308assert.strictEqual(rangeMap.indexAt(-1), -1);309assert.strictEqual(rangeMap.positionAt(0), -1);310assert.strictEqual(rangeMap.positionAt(10), -1);311assert.strictEqual(rangeMap.positionAt(-1), -1);312});313314test('simple', () => {315const rangeMap = new NotebookCellsLayout(10);316rangeMap.splice(0, 0, [one]);317assert.strictEqual(rangeMap.indexAt(0), 0);318assert.strictEqual(rangeMap.indexAt(1), 0);319assert.strictEqual(rangeMap.indexAt(10), 0);320assert.strictEqual(rangeMap.indexAt(11), 1);321assert.strictEqual(rangeMap.positionAt(0), 10);322assert.strictEqual(rangeMap.positionAt(1), -1);323});324});325});326327suite('NotebookRangeMap with whitesspaces', () => {328let testDisposables: DisposableStore;329let instantiationService: TestInstantiationService;330let config: TestConfigurationService;331332teardown(() => {333testDisposables.dispose();334});335336ensureNoDisposablesAreLeakedInTestSuite();337338setup(() => {339testDisposables = new DisposableStore();340instantiationService = setupInstantiationService(testDisposables);341config = new TestConfigurationService();342instantiationService.stub(IConfigurationService, config);343});344345test('simple', () => {346const rangeMap = new NotebookCellsLayout(0);347rangeMap.splice(0, 0, [{ size: 479 }, { size: 163 }, { size: 182 }, { size: 106 }, { size: 106 }, { size: 106 }, { size: 87 }]);348349const start = rangeMap.indexAt(650);350const end = rangeMap.indexAfter(650 + 890 - 1);351assert.strictEqual(start, 2);352assert.strictEqual(end, 7);353354rangeMap.insertWhitespace('1', 0, 18);355assert.strictEqual(rangeMap.indexAt(650), 1);356});357358test('Whitespace CRUD', async function () {359const twenty = { size: 20 };360361const rangeMap = new NotebookCellsLayout(0);362rangeMap.splice(0, 0, [twenty, twenty, twenty]);363rangeMap.insertWhitespace('0', 0, 5);364rangeMap.insertWhitespace('1', 0, 5);365assert.strictEqual(rangeMap.indexAt(0), 0);366assert.strictEqual(rangeMap.indexAt(1), 0);367assert.strictEqual(rangeMap.indexAt(10), 0);368assert.strictEqual(rangeMap.indexAt(11), 0);369assert.strictEqual(rangeMap.indexAt(21), 0);370assert.strictEqual(rangeMap.indexAt(31), 1);371assert.strictEqual(rangeMap.positionAt(0), 10);372373assert.strictEqual(rangeMap.getWhitespacePosition('0'), 0);374assert.strictEqual(rangeMap.getWhitespacePosition('1'), 5);375376assert.strictEqual(rangeMap.positionAt(0), 10);377assert.strictEqual(rangeMap.positionAt(1), 30);378379rangeMap.changeOneWhitespace('0', 0, 10);380assert.strictEqual(rangeMap.getWhitespacePosition('0'), 0);381assert.strictEqual(rangeMap.getWhitespacePosition('1'), 10);382383assert.strictEqual(rangeMap.positionAt(0), 15);384assert.strictEqual(rangeMap.positionAt(1), 35);385386rangeMap.removeWhitespace('1');387assert.strictEqual(rangeMap.getWhitespacePosition('0'), 0);388389assert.strictEqual(rangeMap.positionAt(0), 10);390assert.strictEqual(rangeMap.positionAt(1), 30);391});392393test('Whitespace with editing', async function () {394await withTestNotebook(395[396['# header a', 'markdown', CellKind.Markup, [], {}],397['var b = 1;', 'javascript', CellKind.Code, [], {}],398['# header b', 'markdown', CellKind.Markup, [], {}],399['var b = 2;', 'javascript', CellKind.Code, [], {}],400['# header c', 'markdown', CellKind.Markup, [], {}]401],402async (editor, viewModel, disposables) => {403viewModel.restoreEditorViewState({404editingCells: [false, false, false, false, false],405cellLineNumberStates: {},406editorViewStates: [null, null, null, null, null],407cellTotalHeights: [50, 100, 50, 100, 50],408collapsedInputCells: {},409collapsedOutputCells: {},410});411412const cellList = createNotebookCellList(instantiationService, disposables);413disposables.add(cellList);414cellList.attachViewModel(viewModel);415416// render height 210, it can render 3 full cells and 1 partial cell417cellList.layout(210, 100);418assert.strictEqual(cellList.scrollHeight, 350);419420cellList.changeViewZones(accessor => {421const id = accessor.addZone({422afterModelPosition: 1,423heightInPx: 20,424domNode: document.createElement('div')425});426427accessor.layoutZone(id);428assert.strictEqual(cellList.scrollHeight, 370);429430assert.strictEqual(cellList.getElementTop(0), 0);431assert.strictEqual(cellList.getElementTop(1), 70);432assert.strictEqual(cellList.getElementTop(2), 170);433434const textModel = editor.textModel;435textModel.applyEdits([436{ editType: CellEditType.Replace, index: 0, count: 1, cells: [] },437], true, undefined, () => undefined, undefined, true);438439assert.strictEqual(cellList.getElementTop(0), 20);440assert.strictEqual(cellList.getElementTop(1), 120);441assert.strictEqual(cellList.getElementTop(2), 170);442443accessor.removeZone(id);444});445});446});447448test('Multiple Whitespaces', async function () {449await withTestNotebook(450[451['# header a', 'markdown', CellKind.Markup, [], {}],452['var b = 1;', 'javascript', CellKind.Code, [], {}],453['# header b', 'markdown', CellKind.Markup, [], {}],454['var b = 2;', 'javascript', CellKind.Code, [], {}],455['# header c', 'markdown', CellKind.Markup, [], {}]456],457async (editor, viewModel, disposables) => {458viewModel.restoreEditorViewState({459editingCells: [false, false, false, false, false],460cellLineNumberStates: {},461editorViewStates: [null, null, null, null, null],462cellTotalHeights: [50, 100, 50, 100, 50],463collapsedInputCells: {},464collapsedOutputCells: {},465});466467const cellList = createNotebookCellList(instantiationService, disposables);468disposables.add(cellList);469cellList.attachViewModel(viewModel);470471// render height 210, it can render 3 full cells and 1 partial cell472cellList.layout(210, 100);473assert.strictEqual(cellList.scrollHeight, 350);474475cellList.changeViewZones(accessor => {476const first = accessor.addZone({477afterModelPosition: 0,478heightInPx: 20,479domNode: document.createElement('div')480});481accessor.layoutZone(first);482483const second = accessor.addZone({484afterModelPosition: 3,485heightInPx: 20,486domNode: document.createElement('div')487});488accessor.layoutZone(second);489490assert.strictEqual(cellList.scrollHeight, 390);491492assert.strictEqual(cellList.getElementTop(0), 20);493assert.strictEqual(cellList.getElementTop(1), 70);494assert.strictEqual(cellList.getElementTop(2), 170);495assert.strictEqual(cellList.getElementTop(3), 240);496497accessor.removeZone(first);498499assert.strictEqual(cellList.scrollHeight, 370);500assert.strictEqual(cellList.getElementTop(0), 0);501assert.strictEqual(cellList.getElementTop(1), 50);502assert.strictEqual(cellList.getElementTop(2), 150);503assert.strictEqual(cellList.getElementTop(3), 220);504505accessor.removeZone(second);506507assert.strictEqual(cellList.scrollHeight, 350);508assert.strictEqual(cellList.getElementTop(3), 200);509});510});511});512513test('Multiple Whitespaces 2', async function () {514await withTestNotebook(515[516['# header a', 'markdown', CellKind.Markup, [], {}],517['var b = 1;', 'javascript', CellKind.Code, [], {}],518['# header b', 'markdown', CellKind.Markup, [], {}],519['var b = 2;', 'javascript', CellKind.Code, [], {}],520['# header c', 'markdown', CellKind.Markup, [], {}]521],522async (editor, viewModel, disposables) => {523viewModel.restoreEditorViewState({524editingCells: [false, false, false, false, false],525cellLineNumberStates: {},526editorViewStates: [null, null, null, null, null],527cellTotalHeights: [50, 100, 50, 100, 50],528collapsedInputCells: {},529collapsedOutputCells: {},530});531532const cellList = createNotebookCellList(instantiationService, disposables);533disposables.add(cellList);534cellList.attachViewModel(viewModel);535536// render height 210, it can render 3 full cells and 1 partial cell537cellList.layout(210, 100);538assert.strictEqual(cellList.scrollHeight, 350);539540cellList.changeViewZones(accessor => {541const first = accessor.addZone({542afterModelPosition: 0,543heightInPx: 20,544domNode: document.createElement('div')545});546accessor.layoutZone(first);547548const second = accessor.addZone({549afterModelPosition: 1,550heightInPx: 20,551domNode: document.createElement('div')552});553accessor.layoutZone(second);554555assert.strictEqual(cellList.scrollHeight, 390);556assert.strictEqual(cellList._getView().getWhitespacePosition(first), 0);557assert.strictEqual(cellList._getView().getWhitespacePosition(second), 70);558559accessor.removeZone(first);560accessor.removeZone(second);561});562});563});564565test('Multiple Whitespaces 3', async function () {566await withTestNotebook(567[568['# header a', 'markdown', CellKind.Markup, [], {}],569['var b = 1;', 'javascript', CellKind.Code, [], {}],570['# header b', 'markdown', CellKind.Markup, [], {}],571['var b = 2;', 'javascript', CellKind.Code, [], {}],572['# header c', 'markdown', CellKind.Markup, [], {}]573],574async (editor, viewModel, disposables) => {575viewModel.restoreEditorViewState({576editingCells: [false, false, false, false, false],577cellLineNumberStates: {},578editorViewStates: [null, null, null, null, null],579cellTotalHeights: [50, 100, 50, 100, 50],580collapsedInputCells: {},581collapsedOutputCells: {},582});583584const cellList = createNotebookCellList(instantiationService, disposables);585disposables.add(cellList);586cellList.attachViewModel(viewModel);587588// render height 210, it can render 3 full cells and 1 partial cell589cellList.layout(210, 100);590assert.strictEqual(cellList.scrollHeight, 350);591592cellList.changeViewZones(accessor => {593const first = accessor.addZone({594afterModelPosition: 1,595heightInPx: 20,596domNode: document.createElement('div')597});598accessor.layoutZone(first);599600const second = accessor.addZone({601afterModelPosition: 2,602heightInPx: 20,603domNode: document.createElement('div')604});605accessor.layoutZone(second);606607assert.strictEqual(cellList.scrollHeight, 390);608assert.strictEqual(cellList._getView().getWhitespacePosition(first), 50);609assert.strictEqual(cellList._getView().getWhitespacePosition(second), 170);610611accessor.removeZone(first);612accessor.removeZone(second);613});614});615});616617// test('Multiple Whitespaces 4', async function () {618// await withTestNotebook(619// [620// ['# header a', 'markdown', CellKind.Markup, [], {}],621// ['var b = 1;', 'javascript', CellKind.Code, [], {}],622// ['# header b', 'markdown', CellKind.Markup, [], {}],623// ['var b = 2;', 'javascript', CellKind.Code, [], {}],624// ['# header c', 'markdown', CellKind.Markup, [], {}]625// ],626// async (editor, viewModel, disposables) => {627// viewModel.restoreEditorViewState({628// editingCells: [false, false, false, false, false],629// cellLineNumberStates: {},630// editorViewStates: [null, null, null, null, null],631// cellTotalHeights: [50, 100, 50, 100, 50],632// collapsedInputCells: {},633// collapsedOutputCells: {},634// });635636// const cellList = createNotebookCellList(instantiationService, disposables);637// disposables.add(cellList);638// cellList.attachViewModel(viewModel);639640// // render height 210, it can render 3 full cells and 1 partial cell641// cellList.layout(210, 100);642// assert.strictEqual(cellList.scrollHeight, 350);643644// cellList.changeViewZones(accessor => {645// const first = accessor.addZone({646// afterModelPosition: 1,647// heightInPx: 20,648// domNode: document.createElement('div')649// });650// accessor.layoutZone(first);651652// const second = accessor.addZone({653// afterModelPosition: 1,654// heightInPx: 20,655// domNode: document.createElement('div')656// });657// accessor.layoutZone(second);658659// const third = accessor.addZone({660// afterModelPosition: 2,661// heightInPx: 20,662// domNode: document.createElement('div')663// });664// accessor.layoutZone(second);665666// assert.strictEqual(cellList.scrollHeight, 410);667// assert.strictEqual(cellList._getView().getWhitespacePosition(first), 50);668// assert.strictEqual(cellList._getView().getWhitespacePosition(second), 70);669// assert.strictEqual(cellList._getView().getWhitespacePosition(third), 190);670671// accessor.removeZone(first);672// accessor.removeZone(second);673// accessor.removeZone(third);674// });675// });676// });677678test('Whitespace with folding support', async function () {679await withTestNotebook(680[681['# header a', 'markdown', CellKind.Markup, [], {}],682['var b = 1;', 'javascript', CellKind.Code, [], {}],683['# header b', 'markdown', CellKind.Markup, [], {}],684['var b = 2;', 'javascript', CellKind.Code, [], {}],685['# header c', 'markdown', CellKind.Markup, [], {}]686],687async (editor, viewModel, disposables) => {688viewModel.restoreEditorViewState({689editingCells: [false, false, false, false, false],690cellLineNumberStates: {},691editorViewStates: [null, null, null, null, null],692cellTotalHeights: [50, 100, 50, 100, 50],693collapsedInputCells: {},694collapsedOutputCells: {},695});696697const cellList = createNotebookCellList(instantiationService, disposables);698disposables.add(cellList);699cellList.attachViewModel(viewModel);700701// render height 210, it can render 3 full cells and 1 partial cell702cellList.layout(210, 100);703assert.strictEqual(cellList.scrollHeight, 350);704705cellList.changeViewZones(accessor => {706const id = accessor.addZone({707afterModelPosition: 0,708heightInPx: 20,709domNode: document.createElement('div')710});711712accessor.layoutZone(id);713assert.strictEqual(cellList.scrollHeight, 370);714715assert.strictEqual(cellList.getElementTop(0), 20);716assert.strictEqual(cellList.getElementTop(1), 70);717assert.strictEqual(cellList.getElementTop(2), 170);718assert.strictEqual(cellList.getElementTop(3), 220);719assert.strictEqual(cellList.getElementTop(4), 320);720721accessor.removeZone(id);722assert.strictEqual(cellList.scrollHeight, 350);723});724725cellList.changeViewZones(accessor => {726const id = accessor.addZone({727afterModelPosition: 1,728heightInPx: 20,729domNode: document.createElement('div')730});731732accessor.layoutZone(id);733assert.strictEqual(cellList.scrollHeight, 370);734735assert.strictEqual(cellList.getElementTop(0), 0);736assert.strictEqual(cellList.getElementTop(1), 70);737assert.strictEqual(cellList.getElementTop(2), 170);738assert.strictEqual(cellList.getElementTop(3), 220);739assert.strictEqual(cellList.getElementTop(4), 320);740741accessor.removeZone(id);742assert.strictEqual(cellList.scrollHeight, 350);743});744745// Whitespace should be hidden if it's after the header in a folding region746cellList.changeViewZones(accessor => {747const id = accessor.addZone({748afterModelPosition: 3,749heightInPx: 20,750domNode: document.createElement('div')751});752753accessor.layoutZone(id);754assert.strictEqual(cellList.scrollHeight, 370);755756const foldingModel = disposables.add(new FoldingModel());757foldingModel.attachViewModel(viewModel);758foldingModel.applyMemento([{ start: 2, end: 3 }]);759viewModel.updateFoldingRanges(foldingModel.regions);760assert.deepStrictEqual(viewModel.getHiddenRanges(), [761{ start: 3, end: 3 }762]);763cellList.setHiddenAreas(viewModel.getHiddenRanges(), true);764assert.strictEqual(cellList.scrollHeight, 250);765766assert.strictEqual(cellList.getElementTop(0), 0);767assert.strictEqual(cellList.getElementTop(1), 50);768assert.strictEqual(cellList.getElementTop(2), 150);769assert.strictEqual(cellList.getElementTop(3), 200);770771cellList.setHiddenAreas([], true);772assert.strictEqual(cellList.scrollHeight, 370);773accessor.removeZone(id);774assert.strictEqual(cellList.scrollHeight, 350);775});776777// Whitespace should not be hidden if it's after the last cell in a folding region778cellList.changeViewZones(accessor => {779const id = accessor.addZone({780afterModelPosition: 4,781heightInPx: 20,782domNode: document.createElement('div')783});784785accessor.layoutZone(id);786assert.strictEqual(cellList.scrollHeight, 370);787788const foldingModel = disposables.add(new FoldingModel());789foldingModel.attachViewModel(viewModel);790foldingModel.applyMemento([{ start: 2, end: 3 }]);791viewModel.updateFoldingRanges(foldingModel.regions);792assert.deepStrictEqual(viewModel.getHiddenRanges(), [793{ start: 3, end: 3 }794]);795cellList.setHiddenAreas(viewModel.getHiddenRanges(), true);796assert.strictEqual(cellList.scrollHeight, 270);797798assert.strictEqual(cellList.getElementTop(0), 0);799assert.strictEqual(cellList.getElementTop(1), 50);800assert.strictEqual(cellList.getElementTop(2), 150);801assert.strictEqual(cellList.getElementTop(3), 220);802803cellList.setHiddenAreas([], true);804assert.strictEqual(cellList.scrollHeight, 370);805accessor.removeZone(id);806assert.strictEqual(cellList.scrollHeight, 350);807});808809// Whitespace move when previous folding regions fold810cellList.changeViewZones(accessor => {811const id = accessor.addZone({812afterModelPosition: 4,813heightInPx: 20,814domNode: document.createElement('div')815});816817accessor.layoutZone(id);818assert.strictEqual(cellList.scrollHeight, 370);819820const foldingModel = disposables.add(new FoldingModel());821foldingModel.attachViewModel(viewModel);822foldingModel.applyMemento([{ start: 0, end: 1 }]);823viewModel.updateFoldingRanges(foldingModel.regions);824assert.deepStrictEqual(viewModel.getHiddenRanges(), [825{ start: 1, end: 1 }826]);827cellList.setHiddenAreas(viewModel.getHiddenRanges(), true);828assert.strictEqual(cellList.scrollHeight, 270);829830assert.strictEqual(cellList.getElementTop(0), 0);831assert.strictEqual(cellList.getElementTop(1), 50);832assert.strictEqual(cellList.getElementTop(2), 100);833assert.strictEqual(cellList.getElementTop(3), 220);834835cellList.setHiddenAreas([], true);836assert.strictEqual(cellList.scrollHeight, 370);837accessor.removeZone(id);838assert.strictEqual(cellList.scrollHeight, 350);839});840});841});842843test('Whitespace with multiple viewzones at same position', async function () {844await withTestNotebook(845[846['# header a', 'markdown', CellKind.Markup, [], {}],847['var b = 1;', 'javascript', CellKind.Code, [], {}],848['# header b', 'markdown', CellKind.Markup, [], {}],849['var b = 2;', 'javascript', CellKind.Code, [], {}],850['# header c', 'markdown', CellKind.Markup, [], {}]851],852async (editor, viewModel, disposables) => {853viewModel.restoreEditorViewState({854editingCells: [false, false, false, false, false],855cellLineNumberStates: {},856editorViewStates: [null, null, null, null, null],857cellTotalHeights: [50, 100, 50, 100, 50],858collapsedInputCells: {},859collapsedOutputCells: {},860});861862const cellList = createNotebookCellList(instantiationService, disposables);863disposables.add(cellList);864cellList.attachViewModel(viewModel);865866// render height 210, it can render 3 full cells and 1 partial cell867cellList.layout(210, 100);868assert.strictEqual(cellList.scrollHeight, 350);869870cellList.changeViewZones(accessor => {871const first = accessor.addZone({872afterModelPosition: 0,873heightInPx: 20,874domNode: document.createElement('div')875});876877accessor.layoutZone(first);878assert.strictEqual(cellList.scrollHeight, 370);879880const second = accessor.addZone({881afterModelPosition: 0,882heightInPx: 20,883domNode: document.createElement('div')884});885accessor.layoutZone(second);886assert.strictEqual(cellList.scrollHeight, 390);887888assert.strictEqual(cellList.getElementTop(0), 40);889assert.strictEqual(cellList.getElementTop(1), 90);890assert.strictEqual(cellList.getElementTop(2), 190);891assert.strictEqual(cellList.getElementTop(3), 240);892assert.strictEqual(cellList.getElementTop(4), 340);893894895accessor.removeZone(first);896assert.strictEqual(cellList.scrollHeight, 370);897accessor.removeZone(second);898assert.strictEqual(cellList.scrollHeight, 350);899});900});901});902});903904905