Skip to content

Commit

Permalink
gradually move towards cellAt index asset.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 9, 2021
1 parent 277f6ef commit aa93eef
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/vs/workbench/api/browser/mainThreadNotebookEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ export class MainThreadNotebookEditors implements MainThreadNotebookEditorsShape
if (!notebookEditor.hasModel()) {
return;
}
const cell = notebookEditor.cellAt(range.start);
if (!cell) {

if (range.start >= notebookEditor.getLength()) {
return;
}

const cell = notebookEditor.cellAt(range.start);

switch (revealType) {
case NotebookEditorRevealType.Default:
return notebookEditor.revealCellRangeInView(range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function runCutCells(accessor: ServicesAccessor, editor: INotebookEditor,

if (!containingSelection) {
// focus is out of any selection, we should only cut this cell
const targetCell = editor.cellAt(focus.start)!;
const targetCell = editor.cellAt(focus.start);
clipboardService.writeText(targetCell.getText());
const newFocus = focus.end === editor.getLength() ? { start: focus.start - 1, end: focus.end - 1 } : focus;
const newSelections = selections.map(selection => (selection.end <= focus.start ? selection : { start: selection.start - 1, end: selection.end - 1 }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ export class FindModel extends Disposable {

if (oldCurrMatchCellIndex < 0) {
// the cell containing the active match is deleted
const focusedCell = this._notebookEditor.cellAt(this._notebookEditor.getFocus().start);

if (!focusedCell) {
if (this._notebookEditor.getLength() === 0) {
this.set(findMatches, false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ registerAction2(class extends Action2 {
return;
}

if (!editor.hasModel()) {
return;
}

const levels = args && args.levels || 1;
const direction = args && args.direction === 'up' ? 'up' : 'down';
let index: number | undefined = undefined;
Expand All @@ -227,7 +231,7 @@ registerAction2(class extends Action2 {

const controller = editor.getContribution<FoldingController>(FoldingController.id);
if (index !== undefined) {
const targetCell = editor.cellAt(index);
const targetCell = (index < 0 || index >= editor.getLength()) ? undefined : editor.cellAt(index);
if (targetCell?.cellKind === CellKind.Code && direction === 'down') {
return;
}
Expand All @@ -238,8 +242,8 @@ registerAction2(class extends Action2 {
controller.setFoldingStateDown(index, CellFoldingState.Collapsed, levels);
}

const viewIndex = editor._getViewModel()!.getNearestVisibleCellIndexUpwards(index);
editor.focusElement(editor.cellAt(viewIndex)!);
const viewIndex = editor._getViewModel().getNearestVisibleCellIndexUpwards(index);
editor.focusElement(editor.cellAt(viewIndex));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ registerAction2(class extends NotebookCellAction {
return;
}

const newCell = editor.cellAt(idx + 1);

if (!newCell) {
if (idx >= editor.getLength()) {
// last one
return;
}

const newCell = editor.cellAt(idx + 1);
const newFocusMode = newCell.cellKind === CellKind.Markup && newCell.getEditState() === CellEditState.Preview ? 'container' : 'editor';
editor.focusNotebookCell(newCell, newFocusMode);
editor.cursorNavigationMode = true;
Expand Down Expand Up @@ -105,17 +105,12 @@ registerAction2(class extends NotebookCellAction {
return;
}

if (idx < 1) {
if (idx < 1 || editor.getLength() === 0) {
// we don't do loop
return;
}

const newCell = editor.cellAt(idx - 1);

if (!newCell) {
return;
}

const newFocusMode = newCell.cellKind === CellKind.Markup && newCell.getEditState() === CellEditState.Preview ? 'container' : 'editor';
editor.focusNotebookCell(newCell, newFocusMode);
editor.cursorNavigationMode = true;
Expand All @@ -139,14 +134,12 @@ registerAction2(class extends NotebookAction {

async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
const editor = context.notebookEditor;
if (!editor.getLength()) {
if (editor.getLength() === 0) {
return;
}

const firstCell = editor.cellAt(0);
if (firstCell) {
editor.focusNotebookCell(firstCell, 'container');
}
editor.focusNotebookCell(firstCell, 'container');
}
});

Expand All @@ -166,7 +159,7 @@ registerAction2(class extends NotebookAction {

async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
const editor = context.notebookEditor;
if (!editor.hasModel() || !editor.getLength()) {
if (!editor.hasModel() || editor.getLength() === 0) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Codicon } from 'vs/base/common/codicons';
import { Emitter, Event } from 'vs/base/common/event';
import { combinedDisposable, IDisposable, Disposable, DisposableStore, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IThemeService, ThemeIcon } from 'vs/platform/theme/common/themeService';
import { ICellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { IActiveNotebookEditor, ICellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { IOutline, IOutlineComparator, IOutlineCreator, IOutlineListConfig, IOutlineService, IQuickPickDataSource, IQuickPickOutlineElement, OutlineChangeEvent, OutlineConfigKeys, OutlineTarget } from 'vs/workbench/services/outline/browser/outline';
Expand Down Expand Up @@ -371,13 +371,19 @@ export class NotebookCellOutline extends Disposable implements IOutline<OutlineE
this._activeEntry = undefined;
this._entries.length = 0;

const notebookEditorWidget = this._editor.getControl();
const notebookEditorControl = this._editor.getControl();

if (!notebookEditorControl) {
return;
}

if (!notebookEditorWidget) {
if (!notebookEditorControl.hasModel()) {
return;
}

if (!notebookEditorWidget.hasModel()) {
const notebookEditorWidget: IActiveNotebookEditor = notebookEditorControl;

if (notebookEditorWidget.getLength() === 0) {
return;
}

Expand All @@ -394,10 +400,6 @@ export class NotebookCellOutline extends Disposable implements IOutline<OutlineE

for (let i = 0; i < notebookEditorWidget.getLength(); i++) {
const cell = notebookEditorWidget.cellAt(i);
if (!cell) {
continue;
}

const isMarkdown = cell.cellKind === CellKind.Markup;
if (!isMarkdown && !includeCodeCells) {
continue;
Expand Down Expand Up @@ -515,12 +517,14 @@ export class NotebookCellOutline extends Disposable implements IOutline<OutlineE
const notebookEditorWidget = this._editor.getControl();

if (notebookEditorWidget) {
const cell = notebookEditorWidget.cellAt(notebookEditorWidget.getFocus().start);
if (cell) {
for (let entry of this._entries) {
newActive = entry.find(cell, []);
if (newActive) {
break;
if (notebookEditorWidget.hasModel() && notebookEditorWidget.getLength() > 0) {
const cell = notebookEditorWidget.cellAt(notebookEditorWidget.getFocus().start);
if (cell) {
for (let entry of this._entries) {
newActive = entry.find(cell, []);
if (newActive) {
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ export async function changeCellToKind(kind: CellKind, context: INotebookActionC
};
}, undefined, true);
const newCell = notebookEditor.cellAt(idx);

if (!newCell) {
return;
}

notebookEditor.focusNotebookCell(newCell, cell.getEditState() === CellEditState.Editing ? 'editor' : 'container');
} else if (context.selectedCells) {
const selectedCells = context.selectedCells;
Expand Down Expand Up @@ -131,7 +126,7 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo
editType: CellEditType.Replace, index: selection.start, count: selection.end - selection.start, cells: []
}));

const nextCellAfterContainingSelection = editor.cellAt(containingSelection.end);
const nextCellAfterContainingSelection = containingSelection.end >= editor.getLength() ? undefined : editor.cellAt(containingSelection.end);

textModel.applyEdits(edits, true, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => {
if (nextCellAfterContainingSelection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
private get selectionHandles() {
const handlesSet = new Set<number>();
const handles: number[] = [];
cellRangesToIndexes(this._selectionCollection.selections).map(index => this.cellAt(index)).forEach(cell => {
cellRangesToIndexes(this._selectionCollection.selections).map(index => index < this.length ? this.cellAt(index) : undefined).forEach(cell => {
if (cell && !handlesSet.has(cell.handle)) {
handles.push(cell.handle);
}
Expand Down

0 comments on commit aa93eef

Please sign in to comment.