|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { IFileService } from 'vs/platform/files/common/files'; |
| 7 | +import { URI } from 'vs/base/common/uri'; |
| 8 | +import { WorkspaceEdit, isResourceTextEdit } from 'vs/editor/common/modes'; |
| 9 | +import { IModelService } from 'vs/editor/common/services/modelService'; |
| 10 | +import { ResourceMap } from 'vs/base/common/map'; |
| 11 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 12 | +import { Emitter, Event } from 'vs/base/common/event'; |
| 13 | +import type { ITextModel } from 'vs/editor/common/model'; |
| 14 | + |
| 15 | +export abstract class Recording { |
| 16 | + |
| 17 | + static start(fileService: IFileService): Recording { |
| 18 | + |
| 19 | + let _changes = new Set<string>(); |
| 20 | + let subscription = fileService.onAfterOperation(e => { |
| 21 | + _changes.add(e.resource.toString()); |
| 22 | + }); |
| 23 | + |
| 24 | + return { |
| 25 | + stop() { return subscription.dispose(); }, |
| 26 | + hasChanged(resource) { return _changes.has(resource.toString()); } |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + abstract stop(): void; |
| 31 | + abstract hasChanged(resource: URI): boolean; |
| 32 | +} |
| 33 | + |
| 34 | +export class ConflictDetector { |
| 35 | + |
| 36 | + private readonly _conflicts = new ResourceMap<boolean>(); |
| 37 | + private readonly _changes = new ResourceMap<boolean>(); |
| 38 | + private readonly _disposables = new DisposableStore(); |
| 39 | + |
| 40 | + private readonly _onDidConflict = new Emitter<this>(); |
| 41 | + readonly onDidConflict: Event<this> = this._onDidConflict.event; |
| 42 | + |
| 43 | + constructor( |
| 44 | + workspaceEdit: WorkspaceEdit, |
| 45 | + @IFileService fileService: IFileService, |
| 46 | + @IModelService modelService: IModelService, |
| 47 | + ) { |
| 48 | + |
| 49 | + const _workspaceEditResources = new ResourceMap<boolean>(); |
| 50 | + |
| 51 | + for (let edit of workspaceEdit.edits) { |
| 52 | + if (isResourceTextEdit(edit)) { |
| 53 | + |
| 54 | + _workspaceEditResources.set(edit.resource, true); |
| 55 | + |
| 56 | + if (typeof edit.modelVersionId === 'number') { |
| 57 | + const model = modelService.getModel(edit.resource); |
| 58 | + if (model && model.getVersionId() !== edit.modelVersionId) { |
| 59 | + this._conflicts.set(edit.resource, true); |
| 60 | + this._onDidConflict.fire(this); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + } else if (edit.newUri) { |
| 65 | + _workspaceEditResources.set(edit.newUri, true); |
| 66 | + |
| 67 | + } else if (edit.oldUri) { |
| 68 | + _workspaceEditResources.set(edit.oldUri, true); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // listen to file changes |
| 73 | + this._disposables.add(fileService.onFileChanges(e => { |
| 74 | + for (let change of e.changes) { |
| 75 | + |
| 76 | + // change |
| 77 | + this._changes.set(change.resource, true); |
| 78 | + |
| 79 | + // conflict |
| 80 | + if (_workspaceEditResources.has(change.resource)) { |
| 81 | + this._conflicts.set(change.resource, true); |
| 82 | + this._onDidConflict.fire(this); |
| 83 | + } |
| 84 | + } |
| 85 | + })); |
| 86 | + |
| 87 | + |
| 88 | + // listen to model changes...? |
| 89 | + const onDidChangeModel = (model: ITextModel) => { |
| 90 | + // change |
| 91 | + this._changes.set(model.uri, true); |
| 92 | + |
| 93 | + // conflict |
| 94 | + if (_workspaceEditResources.has(model.uri)) { |
| 95 | + this._conflicts.set(model.uri, true); |
| 96 | + this._onDidConflict.fire(this); |
| 97 | + } |
| 98 | + }; |
| 99 | + for (let model of modelService.getModels()) { |
| 100 | + this._disposables.add(model.onDidChangeContent(() => onDidChangeModel(model))); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + dispose(): void { |
| 105 | + this._disposables.dispose(); |
| 106 | + this._onDidConflict.dispose(); |
| 107 | + } |
| 108 | + |
| 109 | + list(): URI[] { |
| 110 | + const result: URI[] = this._conflicts.keys(); |
| 111 | + this._changes.forEach((_value, key) => { |
| 112 | + if (!this._conflicts.has(key)) { |
| 113 | + result.push(key); |
| 114 | + } |
| 115 | + }); |
| 116 | + return result; |
| 117 | + } |
| 118 | +} |
0 commit comments