Commit f0a8d67a authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'ide-switching-file-changes' into 'master'

Fixed the IDE not showing the correct changes

Closes #5091

See merge request gitlab-org/gitlab-ee!4805
parents 74fc618d dcbfa848
...@@ -19,9 +19,6 @@ export default { ...@@ -19,9 +19,6 @@ export default {
shouldHideEditor() { shouldHideEditor() {
return this.activeFile && this.activeFile.binary && !this.activeFile.raw; return this.activeFile && this.activeFile.binary && !this.activeFile.raw;
}, },
activeFileChanged() {
return this.activeFile && this.activeFile.changed;
},
}, },
watch: { watch: {
activeFile(oldVal, newVal) { activeFile(oldVal, newVal) {
...@@ -29,13 +26,6 @@ export default { ...@@ -29,13 +26,6 @@ export default {
this.initMonaco(); this.initMonaco();
} }
}, },
activeFileChanged(newVal) {
if (!this.editor) return;
if (!newVal && this.model) {
this.model.setValue(this.model.getOriginalModel().getValue());
}
},
leftPanelCollapsed() { leftPanelCollapsed() {
this.editor.updateDimensions(); this.editor.updateDimensions();
}, },
...@@ -92,11 +82,13 @@ export default { ...@@ -92,11 +82,13 @@ export default {
this.editor.attachModel(this.model); this.editor.attachModel(this.model);
this.model.onChange((m) => { this.model.onChange((model) => {
if (this.model.file.active) { const { file } = this.model;
if (file.active) {
this.changeFileContent({ this.changeFileContent({
file: this.model.file, file,
content: m.getValue(), content: model.getValue(),
}); });
} }
}); });
......
...@@ -12,9 +12,13 @@ export default class ModelManager { ...@@ -12,9 +12,13 @@ export default class ModelManager {
return this.models.has(path); return this.models.has(path);
} }
getModel(path) {
return this.models.get(path);
}
addModel(file) { addModel(file) {
if (this.hasCachedModel(file.path)) { if (this.hasCachedModel(file.path)) {
return this.models.get(file.path); return this.getModel(file.path);
} }
const model = new Model(this.monaco, file); const model = new Model(this.monaco, file);
......
...@@ -59,7 +59,8 @@ export default class DirtyDiffController { ...@@ -59,7 +59,8 @@ export default class DirtyDiffController {
decorate({ data }) { decorate({ data }) {
const decorations = data.changes.map(change => getDecorator(change)); const decorations = data.changes.map(change => getDecorator(change));
this.decorationsController.addDecorations(data.path, 'dirtyDiff', decorations); const model = this.modelManager.getModel(data.path);
this.decorationsController.addDecorations(model, 'dirtyDiff', decorations);
} }
dispose() { dispose() {
......
...@@ -9,6 +9,8 @@ import gitlabTheme from 'ee/ide/lib/themes/gl_theme'; // eslint-disable-line imp ...@@ -9,6 +9,8 @@ import gitlabTheme from 'ee/ide/lib/themes/gl_theme'; // eslint-disable-line imp
export default class Editor { export default class Editor {
static create(monaco) { static create(monaco) {
if (this.editorInstance) return this.editorInstance;
this.editorInstance = new Editor(monaco); this.editorInstance = new Editor(monaco);
return this.editorInstance; return this.editorInstance;
...@@ -20,18 +22,14 @@ export default class Editor { ...@@ -20,18 +22,14 @@ export default class Editor {
this.instance = null; this.instance = null;
this.dirtyDiffController = null; this.dirtyDiffController = null;
this.disposable = new Disposable(); this.disposable = new Disposable();
this.modelManager = new ModelManager(this.monaco);
this.disposable.add( this.decorationsController = new DecorationsController(this);
this.modelManager = new ModelManager(this.monaco),
this.decorationsController = new DecorationsController(this),
);
this.setupMonacoTheme(); this.setupMonacoTheme();
this.debouncedUpdate = _.debounce(() => { this.debouncedUpdate = _.debounce(() => {
this.updateDimensions(); this.updateDimensions();
}, 200); }, 200);
window.addEventListener('resize', this.debouncedUpdate, false);
} }
createInstance(domElement) { createInstance(domElement) {
...@@ -50,6 +48,8 @@ export default class Editor { ...@@ -50,6 +48,8 @@ export default class Editor {
this.modelManager, this.decorationsController, this.modelManager, this.decorationsController,
), ),
); );
window.addEventListener('resize', this.debouncedUpdate, false);
} }
} }
......
---
title: Fixed IDE not showing the correct changes and diff markers
merge_request:
author:
type: fixed
...@@ -2,6 +2,7 @@ import Vue from 'vue'; ...@@ -2,6 +2,7 @@ import Vue from 'vue';
import store from '~/ide/stores'; import store from '~/ide/stores';
import repoEditor from '~/ide/components/repo_editor.vue'; import repoEditor from '~/ide/components/repo_editor.vue';
import monacoLoader from '~/ide/monaco_loader'; import monacoLoader from '~/ide/monaco_loader';
import Editor from '~/ide/lib/editor';
import { file, resetStore } from '../helpers'; import { file, resetStore } from '../helpers';
describe('RepoEditor', () => { describe('RepoEditor', () => {
...@@ -32,6 +33,8 @@ describe('RepoEditor', () => { ...@@ -32,6 +33,8 @@ describe('RepoEditor', () => {
vm.$destroy(); vm.$destroy();
resetStore(vm.$store); resetStore(vm.$store);
Editor.editorInstance.modelManager.dispose();
}); });
it('renders an ide container', (done) => { it('renders an ide container', (done) => {
...@@ -58,16 +61,46 @@ describe('RepoEditor', () => { ...@@ -58,16 +61,46 @@ describe('RepoEditor', () => {
}); });
}); });
describe('computed', () => { describe('setupEditor', () => {
describe('activeFileChanged', () => { it('creates new model', () => {
it('returns false when file has no changes', () => { spyOn(vm.editor, 'createModel').and.callThrough();
expect(vm.activeFileChanged).toBeFalsy();
Editor.editorInstance.modelManager.dispose();
vm.setupEditor();
expect(vm.editor.createModel).toHaveBeenCalledWith(vm.$store.getters.activeFile);
expect(vm.model).not.toBeNull();
});
it('attaches model to editor', () => {
spyOn(vm.editor, 'attachModel').and.callThrough();
Editor.editorInstance.modelManager.dispose();
vm.setupEditor();
expect(vm.editor.attachModel).toHaveBeenCalledWith(vm.model);
});
it('adds callback methods', () => {
spyOn(vm.editor, 'onPositionChange').and.callThrough();
Editor.editorInstance.modelManager.dispose();
vm.setupEditor();
expect(vm.editor.onPositionChange).toHaveBeenCalled();
expect(vm.model.events.size).toBe(1);
}); });
it('returns true when file has changes', () => { it('updates state when model content changed', (done) => {
vm.$store.getters.activeFile.changed = true; vm.model.setValue('testing 123');
expect(vm.activeFileChanged).toBeTruthy(); setTimeout(() => {
expect(vm.$store.getters.activeFile.content).toBe('testing 123');
done();
}); });
}); });
}); });
......
...@@ -61,6 +61,14 @@ describe('Multi-file editor library model manager', () => { ...@@ -61,6 +61,14 @@ describe('Multi-file editor library model manager', () => {
}); });
}); });
describe('getModel', () => {
it('returns cached model', () => {
instance.addModel(file('path-name'));
expect(instance.getModel('path-name')).not.toBeNull();
});
});
describe('dispose', () => { describe('dispose', () => {
it('clears cached models', () => { it('clears cached models', () => {
instance.addModel(file()); instance.addModel(file());
......
...@@ -22,7 +22,7 @@ describe('Multi-file editor library dirty diff controller', () => { ...@@ -22,7 +22,7 @@ describe('Multi-file editor library dirty diff controller', () => {
modelManager = new ModelManager(monaco); modelManager = new ModelManager(monaco);
decorationsController = new DecorationsController(editorInstance); decorationsController = new DecorationsController(editorInstance);
model = modelManager.addModel(file()); model = modelManager.addModel(file('path'));
controller = new DirtyDiffController(modelManager, decorationsController); controller = new DirtyDiffController(modelManager, decorationsController);
...@@ -128,7 +128,7 @@ describe('Multi-file editor library dirty diff controller', () => { ...@@ -128,7 +128,7 @@ describe('Multi-file editor library dirty diff controller', () => {
controller.decorate({ data: { changes: [], path: 'path' } }); controller.decorate({ data: { changes: [], path: 'path' } });
expect(controller.decorationsController.addDecorations).toHaveBeenCalledWith('path', 'dirtyDiff', jasmine.anything()); expect(controller.decorationsController.addDecorations).toHaveBeenCalledWith(model, 'dirtyDiff', jasmine.anything());
}); });
it('adds decorations into editor', () => { it('adds decorations into editor', () => {
......
...@@ -22,6 +22,10 @@ describe('Multi-file editor library', () => { ...@@ -22,6 +22,10 @@ describe('Multi-file editor library', () => {
expect(editor.editorInstance).not.toBeNull(); expect(editor.editorInstance).not.toBeNull();
}); });
it('creates instance returns cached instance', () => {
expect(editor.create(monaco)).toEqual(instance);
});
describe('createInstance', () => { describe('createInstance', () => {
let el; let el;
...@@ -42,6 +46,12 @@ describe('Multi-file editor library', () => { ...@@ -42,6 +46,12 @@ describe('Multi-file editor library', () => {
expect(instance.dirtyDiffController).not.toBeNull(); expect(instance.dirtyDiffController).not.toBeNull();
}); });
it('creates model manager', () => {
instance.createInstance(el);
expect(instance.modelManager).not.toBeNull();
});
}); });
describe('createModel', () => { describe('createModel', () => {
...@@ -124,5 +134,21 @@ describe('Multi-file editor library', () => { ...@@ -124,5 +134,21 @@ describe('Multi-file editor library', () => {
expect(instance.instance).toBeNull(); expect(instance.instance).toBeNull();
}); });
it('does not dispose modelManager', () => {
spyOn(instance.modelManager, 'dispose');
instance.dispose();
expect(instance.modelManager.dispose).not.toHaveBeenCalled();
});
it('does not dispose decorationsController', () => {
spyOn(instance.decorationsController, 'dispose');
instance.dispose();
expect(instance.decorationsController.dispose).not.toHaveBeenCalled();
});
}); });
}); });
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