Skip to content

Commit

Permalink
do not need isUntitled.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 9, 2020
1 parent 9753398 commit eb05b79
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 85 deletions.
14 changes: 10 additions & 4 deletions src/vs/workbench/api/browser/mainThreadNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { combinedDisposable, Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { Schemas } from 'vs/base/common/network';
import { URI, UriComponents } from 'vs/base/common/uri';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
Expand Down Expand Up @@ -456,9 +457,14 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo

if (data.cells.length) {
textModel.initialize(data!.cells);
} else {
const mainCell = textModel.createCellTextModel('', textModel.resolvedLanguages.length ? textModel.resolvedLanguages[0] : '', CellKind.Code, [], undefined);
textModel.insertTemplateCell(mainCell);
} else if (textModel.uri.scheme === Schemas.untitled) {
textModel.initialize([{
cellKind: CellKind.Code,
language: textModel.resolvedLanguages.length ? textModel.resolvedLanguages[0] : '',
outputs: [],
metadata: undefined,
source: ''
}]);
}

this._proxy.$acceptDocumentPropertiesChanged(textModel.uri, { metadata: textModel.metadata });
Expand Down Expand Up @@ -604,7 +610,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
const textModel = this._notebookService.getNotebookTextModel(URI.from(resource));

if (textModel) {
textModel.handleEdit(label, () => {
textModel.handleUnknownEdit(label, () => {
return this._proxy.$undoNotebook(textModel.viewType, textModel.uri, editId, textModel.isDirty);
}, () => {
return this._proxy.$redoNotebook(textModel.viewType, textModel.uri, editId, textModel.isDirty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ export class ListTopCellToolbar extends Disposable {
this._modelDisposables.add(this.notebookEditor.viewModel.onDidChangeViewCells(() => {
this.updateClass();
}));

this.updateClass();
}
}));

Expand Down
106 changes: 25 additions & 81 deletions src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ class DelayedEmitter {

emit(data: {
triggerDirty: { value: boolean } | undefined,
modelContentChange: { value: NotebookTextModelChangedEvent } | undefined,
contentChange: { value: NotebookTextModelChangedEvent } | undefined,
}) {
this._increaseVersion();

if (data.triggerDirty) {
this._textModel.setDirty(data.triggerDirty.value);
}

if (data.modelContentChange) {
if (data.contentChange) {
this._onDidChangeContent.fire(
{
...data.modelContentChange.value,
...data.contentChange.value,
versionId: this._textModel.versionId
}
);
Expand Down Expand Up @@ -164,7 +164,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel

metadata: NotebookDocumentMetadata = notebookDocumentMetadataDefaults;
transientOptions: TransientOptions = { transientMetadata: {}, transientOutputs: false };
private _isUntitled: boolean | undefined = undefined;
private _versionId = 0;

//#region selection TODO@rebornix this shouldn't be here
Expand Down Expand Up @@ -251,14 +250,15 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.cellKind, cell.outputs || [], cell.metadata, this.transientOptions, this._modelService);
});

this._isUntitled = false;

for (let i = 0; i < mainCells.length; i++) {
this._mapping.set(mainCells[i].handle, mainCells[i]);
const dirtyStateListener = mainCells[i].onDidChangeContent(() => {
this.setDirty(true);
this._increaseVersionId();
this._onDidChangeContent.fire({ kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true });
this._eventEmitter.emit({
triggerDirty: { value: true },
contentChange: {
value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true }
}
});
});

this._cellListeners.set(mainCells[i].handle, dirtyStateListener);
Expand Down Expand Up @@ -325,7 +325,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
return;
}

this._isUntitled = false; //TODO@rebornix fishy?
const oldViewCells = this._cells.slice(0);
const oldMap = new Map(this._mapping);

Expand All @@ -348,7 +347,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
const dirtyStateListener = cell.onDidChangeContent(() => {
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true } }
contentChange: { value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true } }
});
});
this._cellListeners.set(cell.handle, dirtyStateListener);
Expand Down Expand Up @@ -378,7 +377,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
// should be deferred
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: {
contentChange: {
value: {
kind: NotebookCellsChangeType.ModelChange,
versionId: this._versionId,
Expand All @@ -389,7 +388,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
});
}

handleEdit(label: string | undefined, undo: () => void, redo: () => void): void {
handleUnknownEdit(label: string | undefined, undo: () => void, redo: () => void): void {
this._operationManager.pushEditOperation({
type: UndoRedoElementType.Resource,
resource: this.uri,
Expand All @@ -404,6 +403,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this.setDirty(true);
}

handleUnknownChange() {
this.setDirty(true);
}

createSnapshot(preserveBOM?: boolean): ITextSnapshot {
return new NotebookTextModelSnapshot(this);
}
Expand All @@ -412,17 +415,13 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._versionId = this._versionId + 1;
}

handleUnknownChange() {
this.setDirty(true);
}

updateLanguages(languages: string[]) {
const allLanguages = languages.find(lan => lan === '*');
this._allLanguages = allLanguages !== undefined;
this._languages = languages;

const resolvedLanguages = this.resolvedLanguages;
if (this._isUntitled && resolvedLanguages.length && this._cells.length) {
if (resolvedLanguages.length && this._cells.length) {
this._cells[0].language = resolvedLanguages[0];
}
}
Expand All @@ -431,70 +430,17 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this.metadata = metadata;
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.ChangeDocumentMetadata, versionId: this.versionId, metadata: this.metadata, synchronous: true } }
contentChange: { value: { kind: NotebookCellsChangeType.ChangeDocumentMetadata, versionId: this.versionId, metadata: this.metadata, synchronous: true } }
});
}

insertTemplateCell(cell: NotebookCellTextModel) {
if (this._cells.length > 0 || this._isUntitled !== undefined) {
return;
}

this._isUntitled = true;
this._cells = [cell];
this._mapping.set(cell.handle, cell);

const dirtyStateListener = cell.onDidChangeContent(() => {
this._isUntitled = false;
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true } }
});
});

this._cellListeners.set(cell.handle, dirtyStateListener);
this.setDirty(false);

// this._eventEmitter.emit({
// triggerDirty: undefined,
// modelContentChange: { value: {
// kind: NotebookCellsChangeType.ModelChange,
// versionId: this._versionId, changes:
// [[
// 0,
// 0,
// [cell]
// ]],
// synchronous: true
// } }
// });

this.setDirty(false);
// this._onDidChangeContent.fire(NotebookCellsChangeType.ModelChange);

this._onDidChangeContent.fire({
kind: NotebookCellsChangeType.ModelChange,
versionId: this._versionId, changes:
[[
0,
0,
[cell]
]],
synchronous: true
});

return;
}

private _insertNewCell(index: number, cells: NotebookCellTextModel[], synchronous: boolean, endSelections?: number[]): void {
this._isUntitled = false;

for (let i = 0; i < cells.length; i++) {
this._mapping.set(cells[i].handle, cells[i]);
const dirtyStateListener = cells[i].onDidChangeContent(() => {
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true } }
contentChange: { value: { kind: NotebookCellsChangeType.ChangeCellContent, versionId: this.versionId, synchronous: true } }
});
});

Expand All @@ -504,7 +450,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._cells.splice(index, 0, ...cells);
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: {
contentChange: {
value: {
kind: NotebookCellsChangeType.ModelChange,
versionId: this._versionId, changes:
Expand All @@ -523,8 +469,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
}

private _removeCell(index: number, count: number, synchronous: boolean, endSelections?: number[]) {
this._isUntitled = false;

for (let i = index; i < index + count; i++) {
const cell = this._cells[i];
this._cellListeners.get(cell.handle)?.dispose();
Expand All @@ -533,7 +477,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._cells.splice(index, count);
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.ModelChange, versionId: this._versionId, changes: [[index, count, []]], synchronous, endSelections } }
contentChange: { value: { kind: NotebookCellsChangeType.ModelChange, versionId: this._versionId, changes: [[index, count, []]], synchronous, endSelections } }
});
}

Expand Down Expand Up @@ -582,7 +526,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel

this._eventEmitter.emit({
triggerDirty: { value: triggerDirtyChange },
modelContentChange: {
contentChange: {
value: { kind: NotebookCellsChangeType.ChangeCellMetadata, versionId: this._versionId, index: this._cells.indexOf(cell), metadata: cell.metadata, synchronous: true }
}
});
Expand All @@ -596,7 +540,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel

this._eventEmitter.emit({
triggerDirty: { value: !this.transientOptions.transientOutputs },
modelContentChange: {
contentChange: {
value: {
kind: NotebookCellsChangeType.Output,
versionId: this.versionId,
Expand Down Expand Up @@ -626,7 +570,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel

this._eventEmitter.emit({
triggerDirty: undefined,
modelContentChange: { value: { kind: NotebookCellsChangeType.ChangeLanguage, versionId: this._versionId, index: this._cells.indexOf(cell), language: languageId, synchronous: true } }
contentChange: { value: { kind: NotebookCellsChangeType.ChangeLanguage, versionId: this._versionId, index: this._cells.indexOf(cell), language: languageId, synchronous: true } }
});
}
}
Expand Down Expand Up @@ -671,7 +615,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
this._cells.splice(newIdx, 0, ...cells);
this._eventEmitter.emit({
triggerDirty: { value: true },
modelContentChange: { value: { kind: NotebookCellsChangeType.Move, versionId: this._versionId, index, length, newIdx, cells, synchronous, endSelections } }
contentChange: { value: { kind: NotebookCellsChangeType.Move, versionId: this._versionId, index, length, newIdx, cells, synchronous, endSelections } }
});
}

Expand Down

0 comments on commit eb05b79

Please sign in to comment.