Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix list view npe when inserting whitespaces at top #205311

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/vs/base/browser/ui/list/listView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export class ListView<T> implements IListView<T> {
protected rangeMap: IRangeMap;
private cache: RowCache<T>;
private renderers = new Map<string, IListRenderer<any /* TODO@joao */, any>>();
private lastRenderTop: number;
private lastRenderHeight: number;
protected lastRenderTop: number;
protected lastRenderHeight: number;
private renderWidth = 0;
private rowsContainer: HTMLElement;
private scrollable: Scrollable;
Expand Down Expand Up @@ -1400,7 +1400,7 @@ export class ListView<T> implements IListView<T> {
return undefined;
}

private getRenderRange(renderTop: number, renderHeight: number): IRange {
protected getRenderRange(renderTop: number, renderHeight: number): IRange {
return {
start: this.rangeMap.indexAt(renderTop),
end: this.rangeMap.indexAfter(renderTop + renderHeight - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID

if (modelIndex >= this.hiddenRangesPrefixSum.getTotalSum()) {
// it's already after the last hidden range
return this.hiddenRangesPrefixSum.getTotalSum();
return Math.min(this.length, this.hiddenRangesPrefixSum.getTotalSum());
}

return this.hiddenRangesPrefixSum.getIndexOf(modelIndex).index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,14 @@ export class NotebookCellListView<T> extends ListView<T> {
insertWhitespace(afterPosition: number, size: number): string {
const scrollTop = this.scrollTop;
const id = `${++this._lastWhitespaceId}`;
const previousRenderRange = this.getRenderRange(this.lastRenderTop, this.lastRenderHeight);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new getCurrentRenderRange could also be introduced since these parameters belong to the listview anyway.

const elementPosition = this.elementTop(afterPosition);
const aboveScrollTop = scrollTop > elementPosition;
this.notebookRangeMap.insertWhitespace(id, afterPosition, size);

this._rerender(scrollTop, this.renderHeight, false);
const newScrolltop = aboveScrollTop ? scrollTop + size : scrollTop;
this.render(previousRenderRange, newScrolltop, this.lastRenderHeight, undefined, undefined, false);
this._rerender(newScrolltop, this.renderHeight, false);
this.eventuallyUpdateScrollDimensions();

return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export class NotebookViewZones extends Disposable {
}

private _addZone(zone: INotebookViewZone): string {
const whitespaceId = this.listView.insertWhitespace(zone.afterModelPosition, zone.heightInPx);
const viewPosition = this.coordinator.convertModelIndexToViewIndex(zone.afterModelPosition);
const whitespaceId = this.listView.insertWhitespace(viewPosition, zone.heightInPx);
const isInHiddenArea = this._isInHiddenRanges(zone);
const myZone: IZoneWidget = {
whitespaceId: whitespaceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ suite('NotebookRangeMap with whitesspaces', () => {
instantiationService.stub(IConfigurationService, config);
});

test('simple', () => {
const rangeMap = new NotebookCellsLayout(0);
rangeMap.splice(0, 0, [{ size: 479 }, { size: 163 }, { size: 182 }, { size: 106 }, { size: 106 }, { size: 106 }, { size: 87 }]);

const start = rangeMap.indexAt(650);
const end = rangeMap.indexAfter(650 + 890 - 1);
assert.strictEqual(start, 2);
assert.strictEqual(end, 7);

rangeMap.insertWhitespace('1', 0, 18);
assert.strictEqual(rangeMap.indexAt(650), 1);
});

test('Whitespace CRUD', async function () {
const twenty = { size: 20 };

Expand Down