reduce use of handles when possible.

This commit is contained in:
rebornix 2021-02-22 13:58:41 -08:00
parent 5b943120a1
commit 5e067109a3
3 changed files with 13 additions and 10 deletions

View file

@ -389,7 +389,8 @@ class NotebookCellOutline implements IOutline<OutlineEntry> {
includeCodeCells = this._configurationService.getValue<boolean>('notebook.breadcrumbs.showCodeCells');
}
const selected = viewModel.primarySelectionHandle;
const selectedCellIndex = viewModel.getSelection().start;
const selected = viewModel.getCellByIndex(selectedCellIndex)?.handle;
const entries: OutlineEntry[] = [];
for (let i = 0; i < viewModel.viewCells.length; i++) {
@ -510,9 +511,8 @@ class NotebookCellOutline implements IOutline<OutlineEntry> {
let newActive: OutlineEntry | undefined;
const { viewModel } = this._editor;
if (viewModel && viewModel.primarySelectionHandle !== null) {
const selected = viewModel.primarySelectionHandle;
const cell = viewModel.getCellByHandle(selected);
if (viewModel) {
const cell = viewModel.getCellByIndex(viewModel.getSelection()?.start);
if (cell) {
for (let entry of this._entries) {
newActive = entry.find(cell, []);

View file

@ -21,7 +21,7 @@ import { IListService, IWorkbenchListOptions, WorkbenchList } from 'vs/platform/
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { CellRevealPosition, CellRevealType, CursorAtBoundary, getVisibleCells, ICellViewModel, INotebookCellList, reduceCellRanges, CellEditState, CellFocusMode, BaseCellRenderTemplate, NOTEBOOK_CELL_LIST_FOCUSED, cellRangesEqual, ICellOutputViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellViewModel, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { diff, NOTEBOOK_EDITOR_CURSOR_BOUNDARY, CellKind, ICellRange, NOTEBOOK_EDITOR_CURSOR_BEGIN_END, cellRangesToIndexes } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { diff, NOTEBOOK_EDITOR_CURSOR_BOUNDARY, CellKind, ICellRange, NOTEBOOK_EDITOR_CURSOR_BEGIN_END, cellRangesToIndexes, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { clamp } from 'vs/base/common/numbers';
import { SCROLLABLE_ELEMENT_PADDING_TOP } from 'vs/workbench/contrib/notebook/browser/constants';
@ -509,8 +509,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
if (!selectionsLeft.length && this._viewModel!.viewCells.length) {
// after splice, the selected cells are deleted
this._viewModel!.selectionHandles = [this._viewModel!.viewCells[0].handle];
this._viewModel!.primarySelectionHandle = this._viewModel!.viewCells[0].handle;
this._viewModel!.updateSelectionsFromEdits({ kind: SelectionStateType.Index, selections: [{ start: 0, end: 1 }] });
}
}
@ -600,6 +599,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
setFocus(indexes: number[], browserEvent?: UIEvent, ignoreTextModelUpdate?: boolean): void {
if (ignoreTextModelUpdate) {
super.setFocus(indexes, browserEvent);
return;
}
@ -619,6 +619,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
setSelection(indexes: number[], browserEvent?: UIEvent | undefined, ignoreTextModelUpdate?: boolean) {
if (ignoreTextModelUpdate) {
super.setSelection(indexes, browserEvent);
return;
}

View file

@ -386,13 +386,15 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
updateSelectionsFromView(primary: number | null, selections: number[]) {
const primaryIndex = primary !== null ? this.getCellIndexByHandle(primary) : null;
const selectionIndexes = selections.map(sel => this.getCellIndexByHandle(sel));
this._selectionCollection.setState2(primaryIndex !== null ? { start: primaryIndex, end: primaryIndex + 1 } : null, cellIndexesToRanges(selectionIndexes), true);
this._selectionCollection.setState2(primaryIndex !== null ? { start: primaryIndex, end: primaryIndex + 1 } : null, cellIndexesToRanges(selectionIndexes), false);
}
updateSelectionsFromEdits(state: ISelectionState) {
if (this._focused) {
if (state.kind === SelectionStateType.Handle) {
this._selectionCollection.setState(state.primary, state.selections);
const primaryIndex = state.primary !== null ? this.getCellIndexByHandle(state.primary) : null;
const selectionIndexes = state.selections.map(sel => this.getCellIndexByHandle(sel));
this._selectionCollection.setState2(primaryIndex !== null ? { start: primaryIndex, end: primaryIndex + 1 } : null, cellIndexesToRanges(selectionIndexes), true);
} else {
this._selectionCollection.setState2(state.selections[0] ?? null, state.selections, true);
}
@ -728,7 +730,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
cells: []
}],
synchronous,
{ kind: SelectionStateType.Handle, primary: this.primarySelectionHandle, selections: this.selectionHandles },
{ kind: SelectionStateType.Index, selections: this.getSelections() },
() => ({ kind: SelectionStateType.Handle, primary: endPrimarySelection, selections: endSelections }),
undefined,
pushUndoStop