Skip to content

Commit

Permalink
re #108464.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Nov 23, 2020
1 parent 1732cb9 commit 2a1c8bb
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,17 +1354,49 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
return new Promise(resolve => { r = resolve; });
}

private _nearestCodeCellIndex(index: number /* exclusive */, direction: 'above' | 'below') {
const nearest = this._notebookViewModel!.viewCells.slice(0, index).reverse().findIndex(cell => cell.cellKind === CellKind.Code);
if (nearest > -1) {
return index - nearest - 1;
} else {
const nearestCellTheOtherDirection = this._notebookViewModel!.viewCells.slice(index + 1).findIndex(cell => cell.cellKind === CellKind.Code);
if (nearestCellTheOtherDirection > -1) {
return index + nearestCellTheOtherDirection;
}
return -1;
}
}

insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction: 'above' | 'below' = 'above', initialText: string = '', ui: boolean = false): CellViewModel | null {
if (!this._notebookViewModel!.metadata.editable) {
return null;
}

const index = cell ? this._notebookViewModel!.getCellIndex(cell) : 0;
const nextIndex = ui ? this._notebookViewModel!.getNextVisibleCellIndex(index) : index + 1;
const newLanguages = this._notebookViewModel!.resolvedLanguages;
const language = (cell?.cellKind === CellKind.Code && type === CellKind.Code)
? cell.language
: ((type === CellKind.Code && newLanguages && newLanguages.length) ? newLanguages[0] : 'markdown');
let language;
if (type === CellKind.Code) {
if (cell?.cellKind === CellKind.Code) {
language = cell.language;
} else if (cell?.cellKind === CellKind.Markdown) {
const nearestCodeCellIndex = this._nearestCodeCellIndex(index, direction);
if (nearestCodeCellIndex > -1) {
language = this._notebookViewModel!.viewCells[nearestCodeCellIndex].language;
} else {
language = this._notebookViewModel!.resolvedLanguages[0] || 'plaintext';
}
} else {
if (cell === undefined && direction === 'above') {
// insert cell at the very top
language = this._notebookViewModel!.viewCells.find(cell => cell.cellKind === CellKind.Code)?.language || this._notebookViewModel!.resolvedLanguages[0] || 'plaintext';
} else {
language = this._notebookViewModel!.resolvedLanguages[0] || 'plaintext';
}
}
} else {
language = 'markdown';
}

const insertIndex = cell ?
(direction === 'above' ? index : nextIndex) :
index;
Expand Down

0 comments on commit 2a1c8bb

Please sign in to comment.