Skip to content

Commit 509bc25

Browse files
committed
notebook editor worker service.
1 parent 065fde6 commit 509bc25

7 files changed

Lines changed: 509 additions & 81 deletions

File tree

.eslintrc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,20 @@
653653
"**/vs/workbench/services/**/{common,browser}/**"
654654
]
655655
},
656+
{
657+
"target": "**/vs/workbench/contrib/notebook/common/**",
658+
"restrictions": [
659+
"vs/nls",
660+
"vs/css!./**/*",
661+
"**/vs/base/**/{common,worker}/**",
662+
"**/vs/platform/**/common/**",
663+
"**/vs/editor/**",
664+
"**/vs/workbench/common/**",
665+
"**/vs/workbench/api/common/**",
666+
"**/vs/workbench/services/**/common/**",
667+
"**/vs/workbench/contrib/**/common/**"
668+
]
669+
},
656670
{
657671
"target": "**/vs/workbench/contrib/**/common/**",
658672
"restrictions": [

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
4242
import { INotebookEditorModelResolverService, NotebookModelResolverService } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverService';
4343
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
4444
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
45-
import { NotebookDiffEditorInput } from './notebookDiffEditorInput';
46-
import { NotebookDiffEditor } from './notebookDiffEditor';
45+
import { NotebookDiffEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookDiffEditorInput';
46+
import { NotebookDiffEditor } from 'vs/workbench/contrib/notebook/browser/notebookDiffEditor';
47+
import { INotebookEditorWorkerService } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerService';
48+
import { NotebookEditorWorkerServiceImpl } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerServiceImpl';
4749

4850
// Editor Contribution
4951

@@ -438,6 +440,7 @@ workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContributio
438440
workbenchContributionsRegistry.registerWorkbenchContribution(CellContentProvider, LifecyclePhase.Starting);
439441

440442
registerSingleton(INotebookService, NotebookService);
443+
registerSingleton(INotebookEditorWorkerService, NotebookEditorWorkerServiceImpl);
441444
registerSingleton(INotebookEditorModelResolverService, NotebookModelResolverService, true);
442445

443446
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);

src/vs/workbench/contrib/notebook/browser/notebookDiffEditor.ts

Lines changed: 33 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@ import { NotebookEditorWidget } from 'vs/workbench/contrib/notebook/browser/note
1616
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
1717
import { NotebookDiffEditorInput } from './notebookDiffEditorInput';
1818
import { CancellationToken } from 'vs/base/common/cancellation';
19-
import { IDiffResult, LcsDiff } from 'vs/base/common/diff/diff';
20-
import { CellSequence, INotebookDiffEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon';
19+
import { INotebookDiffEditorModel, INotebookDiffResult } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2120
import { INotebookDeltaDecoration } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
2221
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
2322
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
2423
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
2524
import { FileService } from 'vs/platform/files/common/fileService';
2625
import { IFileService } from 'vs/platform/files/common/files';
27-
import { DiffComputer } from 'vs/editor/common/diff/diffComputer';
2826
import { createDecoration, DECORATIONS, isChangeOrDelete, isChangeOrInsert } from 'vs/editor/browser/widget/diffEditorWidget';
2927
import * as editorCommon from 'vs/editor/common/editorCommon';
3028
import { Color } from 'vs/base/common/color';
3129
import { IModelDeltaDecoration, IReadonlyTextBuffer } from 'vs/editor/common/model';
3230
import { Range } from 'vs/editor/common/core/range';
3331
import { Constants } from 'vs/base/common/uint';
3432
import { defaultInsertColor, defaultRemoveColor, diffInserted, diffRemoved } from 'vs/platform/theme/common/colorRegistry';
33+
import { INotebookEditorWorkerService } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerService';
3534

3635
export class NotebookDiffEditor extends BaseEditor {
3736
static readonly ID: string = 'workbench.editor.notebookDiffEditor';
@@ -52,6 +51,7 @@ export class NotebookDiffEditor extends BaseEditor {
5251
@ITelemetryService telemetryService: ITelemetryService,
5352
@IInstantiationService private readonly instantiationService: IInstantiationService,
5453
@IStorageService storageService: IStorageService,
54+
@INotebookEditorWorkerService private readonly notebookEditorWorkerService: INotebookEditorWorkerService
5555
) {
5656
super(NotebookDiffEditor.ID, telemetryService, themeService, storageService);
5757

@@ -149,10 +149,9 @@ export class NotebookDiffEditor extends BaseEditor {
149149
}
150150

151151
private _update(model: INotebookDiffEditorModel) {
152-
const diff = new LcsDiff(new CellSequence(model.original.notebook), new CellSequence(model.modified.notebook));
153-
const diffResult = diff.ComputeDiff(false);
154-
155-
this._adjustHeight(diffResult);
152+
this.notebookEditorWorkerService.computeDiff(model.original.notebook.uri, model.modified.notebook.uri).then(diffResult => {
153+
this._adjustHeight(diffResult);
154+
});
156155
}
157156

158157
private _setStrategy(newStrategy: DiffEditorWidgetSideBySide): void {
@@ -162,13 +161,12 @@ export class NotebookDiffEditor extends BaseEditor {
162161

163162
this._strategy = newStrategy;
164163
newStrategy.applyColors(this.themeService.getColorTheme());
165-
166-
// if (this._diffComputationResult) {
167-
// this._updateDecorations();
168-
// }
169164
}
170165

171-
private _adjustHeight(diffResult: IDiffResult) {
166+
private _adjustHeight(notebookDiffResult: INotebookDiffResult) {
167+
const diffResult = notebookDiffResult.cellsDiff;
168+
const linesDiffResult = notebookDiffResult.linesDiff;
169+
172170
if (!this._widget || !this._originalWidget) {
173171
return;
174172
}
@@ -211,67 +209,37 @@ export class NotebookDiffEditor extends BaseEditor {
211209
};
212210

213211
viewLayoutUpdateDisposables.push(...[
214-
...originalCells.map(cell => (cell instanceof CodeCellViewModel) ? cell.onDidChangeLayout(e => update()) : (cell as MarkdownCellViewModel).onDidChangeLayout(e => update())),
215-
...modifiedCells.map(cell => (cell instanceof CodeCellViewModel) ? cell.onDidChangeLayout(e => update()) : (cell as MarkdownCellViewModel).onDidChangeLayout(e => update())),
212+
...originalCells.map(cell => (cell instanceof CodeCellViewModel) ? cell.onDidChangeLayout(() => update()) : (cell as MarkdownCellViewModel).onDidChangeLayout(() => update())),
213+
...modifiedCells.map(cell => (cell instanceof CodeCellViewModel) ? cell.onDidChangeLayout(() => update()) : (cell as MarkdownCellViewModel).onDidChangeLayout(() => update())),
216214
]);
215+
216+
update();
217217
});
218218

219-
// console.log(diffResult);
220219

221-
diffResult.changes.forEach(change => {
222-
if (change.modifiedLength === 0) {
223-
// deletion ...
224-
return;
225-
}
220+
linesDiffResult.forEach(diff => {
221+
const originalCell = this._originalWidget!.viewModel!.viewCells.find(cell => cell.handle === diff.originalCellhandle);
222+
const modifiedCell = this._widget!.viewModel!.viewCells.find(cell => cell.handle === diff.modifiedCellhandle);
226223

227-
if (change.originalLength === 0) {
228-
// insertion
224+
if (!originalCell || !modifiedCell) {
229225
return;
230226
}
231227

232-
for (let i = 0, len = Math.min(change.modifiedLength, change.originalLength); i < len; i++) {
233-
let originalIndex = change.originalStart + i;
234-
let modifiedIndex = change.modifiedStart + i;
235-
236-
const originalCell = this._originalWidget!.viewModel!.viewCells[originalIndex];
237-
const modifiedCell = this._widget!.viewModel!.viewCells[modifiedIndex];
238-
239-
if (originalCell.getText() !== modifiedCell.getText()) {
240-
// console.log(`original cell ${originalIndex} content change`);
241-
const originalLines = originalCell.textBuffer.getLinesContent();
242-
const modifiedLines = modifiedCell.textBuffer.getLinesContent();
243-
const diffComputer = new DiffComputer(originalLines, modifiedLines, {
244-
shouldComputeCharChanges: true,
245-
shouldPostProcessCharChanges: true,
246-
shouldIgnoreTrimWhitespace: false,
247-
shouldMakePrettyDiff: true,
248-
maxComputationTime: 5000
249-
});
250-
251-
const lineChanges = diffComputer.computeDiff().changes;
252-
const lineDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, false, false, originalCell.textBuffer, modifiedCell.textBuffer);
253-
254-
this._originalWidget?.changeModelDecorations(accessor => {
255-
accessor.deltaDecorations([], [{
256-
ownerId: originalCell.handle,
257-
decorations: lineDecorations.original.decorations
258-
}]);
259-
});
260-
261-
this._widget?.changeModelDecorations(accessor => {
262-
accessor.deltaDecorations([], [{
263-
ownerId: modifiedCell.handle,
264-
decorations: lineDecorations.modified.decorations
265-
}]);
266-
});
267-
268-
// console.log(lineDecorations);
269-
270-
} else {
271-
// console.log(`original cell ${originalIndex} metadata change`);
272-
}
273-
274-
}
228+
const lineDecorations = this._strategy.getEditorsDiffDecorations(diff.lineChanges, false, false, originalCell.textBuffer, modifiedCell.textBuffer);
229+
230+
this._originalWidget?.changeModelDecorations(accessor => {
231+
accessor.deltaDecorations([], [{
232+
ownerId: diff.originalCellhandle,
233+
decorations: lineDecorations.original.decorations
234+
}]);
235+
});
236+
237+
this._widget?.changeModelDecorations(accessor => {
238+
accessor.deltaDecorations([], [{
239+
ownerId: diff.modifiedCellhandle,
240+
decorations: lineDecorations.modified.decorations
241+
}]);
242+
});
275243
});
276244

277245
this._originalCellDecorations = this._originalWidget.deltaCellDecorations(this._originalCellDecorations, originalDecorations);
@@ -362,29 +330,16 @@ export class DiffEditorWidgetSideBySide extends Disposable {
362330
}
363331

364332
public getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalModel: IReadonlyTextBuffer, modifiedModel: IReadonlyTextBuffer): IEditorsDiffDecorationsWithZones {
365-
// Get view zones
366-
// modifiedWhitespaces = modifiedWhitespaces.sort((a, b) => {
367-
// return a.afterLineNumber - b.afterLineNumber;
368-
// });
369-
// originalWhitespaces = originalWhitespaces.sort((a, b) => {
370-
// return a.afterLineNumber - b.afterLineNumber;
371-
// });
372-
// let zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor, renderIndicators);
373-
374333
// Get decorations & overview ruler zones
375334
let originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalModel);
376335
let modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, modifiedModel);
377336

378337
return {
379338
original: {
380339
decorations: originalDecorations.decorations,
381-
// overviewZones: originalDecorations.overviewZones,
382-
// zones: zones.original
383340
},
384341
modified: {
385342
decorations: modifiedDecorations.decorations,
386-
// overviewZones: modifiedDecorations.overviewZones,
387-
// zones: zones.modified
388343
}
389344
};
390345
}

src/vs/workbench/contrib/notebook/common/notebookCommon.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';
1818
import { Schemas } from 'vs/base/common/network';
1919
import { IRevertOptions } from 'vs/workbench/common/editor';
2020
import { basename } from 'vs/base/common/path';
21-
import { ISequence } from 'vs/base/common/diff/diff';
21+
import { IDiffResult, ISequence } from 'vs/base/common/diff/diff';
2222

2323
export enum CellKind {
2424
Markdown = 1,
@@ -715,3 +715,8 @@ export class CellSequence implements ISequence {
715715
return hashValue;
716716
}
717717
}
718+
719+
export interface INotebookDiffResult {
720+
cellsDiff: IDiffResult,
721+
linesDiff: { originalCellhandle: number, modifiedCellhandle: number, lineChanges: editorCommon.ILineChange[] }[];
722+
}

0 commit comments

Comments
 (0)