Commit caa2f10e authored by Phil Hughes's avatar Phil Hughes

change how diffing works

[ci skip]
parent eb8a6095
/* global monaco */ /* global monaco */
import Disposable from '../common/disposable';
import DirtyDiffWorker from './worker'; import DirtyDiffWorker from './worker';
import Disposable from '../common/disposable';
import decorationsController from '../decorations/controller'; import decorationsController from '../decorations/controller';
export const getDiffChangeType = (change) => { export const getDiffChangeType = (change) => {
if (change.originalEndLineNumber === 0) { if (change.modified) {
return 'modified';
} else if (change.added) {
return 'added'; return 'added';
} else if (change.modifiedEndLineNumber === 0) { } else if (change.removed) {
return 'removed'; return 'removed';
} }
return 'modified'; return '';
}; };
export const getDecorator = change => ({ export const getDecorator = change => ({
range: new monaco.Range( range: new monaco.Range(
change.modifiedStartLineNumber, change.lineNumber,
1, 1,
!change.modifiedEndLineNumber ? change.endLineNumber,
change.modifiedStartLineNumber : change.modifiedEndLineNumber,
1, 1,
), ),
options: { options: {
...@@ -37,22 +38,15 @@ export default class DirtyDiffController { ...@@ -37,22 +38,15 @@ export default class DirtyDiffController {
this.disposable = new Disposable(); this.disposable = new Disposable();
this.editorSimpleWorker = null; this.editorSimpleWorker = null;
this.modelManager = modelManager; this.modelManager = modelManager;
this.disposable.add(this.worker = new DirtyDiffWorker()); this.dirtyDiffWorker = new DirtyDiffWorker();
} }
attachModel(model) { attachModel(model) {
if (model.attachedToWorker) return; model.onChange(() => this.computeDiff(model));
[model.getModel(), model.getOriginalModel()].forEach(() => {
this.worker.attachModel(model);
});
model.onChange((_, e) => this.computeDiff(model, e));
} }
computeDiff(model, e) { computeDiff(model) {
this.worker.modelChanged(model, e); decorate(model, this.dirtyDiffWorker.compute(model));
this.worker.compute(model, changes => decorate(model, changes));
} }
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
......
/* global monaco */ import { diffLines } from 'diff';
import Disposable from '../common/disposable';
export default class DirtyDiffWorker { export default class DirtyDiffWorker {
constructor() { // eslint-disable-next-line class-methods-use-this
this.editorSimpleWorker = null; compute(model) {
this.disposable = new Disposable(); const originalContent = model.getOriginalModel().getValue();
this.actions = new Set(); const newContent = model.getModel().getValue();
const changes = diffLines(originalContent, newContent);
// eslint-disable-next-line promise/catch-or-return
monaco.editor.createWebWorker({ let lineNumber = 1;
moduleId: 'vs/editor/common/services/editorSimpleWorker', return changes.reduce((acc, change) => {
}).getProxy().then((editorSimpleWorker) => { const findOnLine = acc.find(c => c.lineNumber === lineNumber);
this.disposable.add(this.editorSimpleWorker = editorSimpleWorker);
this.ready(); if (findOnLine) {
}); Object.assign(findOnLine, change, {
} modified: true,
endLineNumber: change.count > 1 ? lineNumber + change.count : lineNumber,
// loop through all the previous cached actions });
// this way we don't block the user from editing the file } else if ('added' in change || 'removed' in change) {
ready() { acc.push(Object.assign({}, change, {
this.actions.forEach((action) => { lineNumber,
const methodName = Object.keys(action)[0]; modified: undefined,
this[methodName](...action[methodName]); endLineNumber: change.count > 1 ? lineNumber + change.count : lineNumber,
}); }));
}
this.actions.clear();
} if (!change.removed) {
lineNumber += change.count;
attachModel(model) { }
if (this.editorSimpleWorker && !model.attachedToWorker) {
this.editorSimpleWorker.acceptNewModel(model.diffModel); return acc;
this.editorSimpleWorker.acceptNewModel(model.originalDiffModel); }, []);
model.setAttachedToWorker(true);
} else if (!this.editorSimpleWorker) {
this.actions.add({
attachModel: [model],
});
}
}
modelChanged(model, e) {
if (this.editorSimpleWorker) {
this.editorSimpleWorker.acceptModelChanged(
model.url,
e,
);
} else {
this.actions.add({
modelChanged: [model, e],
});
}
}
compute(model, cb) {
if (this.editorSimpleWorker) {
return this.editorSimpleWorker.computeDiff(
model.originalUrl,
model.url,
).then(cb);
}
this.actions.add({
compute: [model, cb],
});
return null;
}
dispose() {
this.actions.clear();
this.disposable.dispose();
this.editorSimpleWorker = null;
} }
} }
...@@ -11,28 +11,27 @@ export default class Editor { ...@@ -11,28 +11,27 @@ export default class Editor {
} }
constructor() { constructor() {
this.diffComputers = new Map();
this.currentModel = null; this.currentModel = null;
this.instance = null; this.instance = null;
this.dirtyDiffController = null; this.dirtyDiffController = null;
this.modelManager = new ModelManager();
this.disposable = new Disposable(); this.disposable = new Disposable();
this.disposable.add(this.modelManager); this.disposable.add(
this.modelManager = new ModelManager(),
);
} }
createInstance(domElement) { createInstance(domElement) {
if (!this.instance) { if (!this.instance) {
this.instance = monaco.editor.create(domElement, { this.disposable.add(
model: null, this.instance = monaco.editor.create(domElement, {
readOnly: false, model: null,
contextmenu: true, readOnly: false,
scrollBeyondLastLine: false, contextmenu: true,
}); scrollBeyondLastLine: false,
}),
this.dirtyDiffController = new DirtyDiffController(this.modelManager); this.dirtyDiffController = new DirtyDiffController(this.modelManager),
);
this.disposable.add(this.dirtyDiffController, this.instance);
} }
} }
......
...@@ -1895,6 +1895,10 @@ di@^0.0.1: ...@@ -1895,6 +1895,10 @@ di@^0.0.1:
version "0.0.1" version "0.0.1"
resolved "https://registry.yarnpkg.com/di/-/di-0.0.1.tgz#806649326ceaa7caa3306d75d985ea2748ba913c" resolved "https://registry.yarnpkg.com/di/-/di-0.0.1.tgz#806649326ceaa7caa3306d75d985ea2748ba913c"
diff@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"
diffie-hellman@^5.0.0: diffie-hellman@^5.0.0:
version "5.0.2" version "5.0.2"
resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.2.tgz#b5835739270cfe26acf632099fded2a07f209e5e" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.2.tgz#b5835739270cfe26acf632099fded2a07f209e5e"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment