|
| 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 assert = require('assert'); |
| 7 | +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; |
| 8 | +import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils'; |
| 9 | +import { Range } from 'vs/editor/common/core/range'; |
| 10 | +import { ITextModel } from 'vs/editor/common/model'; |
| 11 | +import { EditorSimpleWorker } from 'vs/editor/common/services/editorSimpleWorker'; |
| 12 | +import { createModelServices, createTextModel } from 'vs/editor/test/common/testTextModel'; |
| 13 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 14 | +import { transaction } from 'vs/workbench/contrib/audioCues/browser/observable'; |
| 15 | +import { EditorWorkerServiceDiffComputer } from 'vs/workbench/contrib/mergeEditor/browser/model/diffComputer'; |
| 16 | +import { MergeEditorModel } from 'vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel'; |
| 17 | + |
| 18 | +suite('merge editor model', () => { |
| 19 | + ensureNoDisposablesAreLeakedInTestSuite(); |
| 20 | + |
| 21 | + test('prepend line', async () => { |
| 22 | + await testMergeModel( |
| 23 | + { |
| 24 | + "languageId": "plaintext", |
| 25 | + "base": "line1\nline2", |
| 26 | + "input1": "0\nline1\nline2", |
| 27 | + "input2": "0\nline1\nline2", |
| 28 | + "result": "" |
| 29 | + }, |
| 30 | + model => { |
| 31 | + assert.deepStrictEqual(model.getProjections(), { |
| 32 | + base: '⟦⟧₀line1\nline2', |
| 33 | + input1: '⟦0\n⟧₀line1\nline2', |
| 34 | + input2: '⟦0\n⟧₀line1\nline2', |
| 35 | + }); |
| 36 | + |
| 37 | + model.toggleConflict(0, 1); |
| 38 | + assert.deepStrictEqual( |
| 39 | + { result: model.getResult() }, |
| 40 | + { result: '0\nline1\nline2' } |
| 41 | + ); |
| 42 | + |
| 43 | + model.toggleConflict(0, 2); |
| 44 | + assert.deepStrictEqual( |
| 45 | + { result: model.getResult() }, |
| 46 | + ({ result: "0\n0\nline1\nline2" }) |
| 47 | + ); |
| 48 | + } |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test('empty base', async () => { |
| 53 | + await testMergeModel( |
| 54 | + { |
| 55 | + "languageId": "plaintext", |
| 56 | + "base": "", |
| 57 | + "input1": "input1", |
| 58 | + "input2": "input2", |
| 59 | + "result": "" |
| 60 | + }, |
| 61 | + model => { |
| 62 | + assert.deepStrictEqual(model.getProjections(), ({ base: "⟦⟧₀", input1: "⟦input1⟧₀", input2: "⟦input2⟧₀" })); |
| 63 | + |
| 64 | + model.toggleConflict(0, 1); |
| 65 | + assert.deepStrictEqual( |
| 66 | + { result: model.getResult() }, |
| 67 | + ({ result: "input1" }) |
| 68 | + ); |
| 69 | + |
| 70 | + model.toggleConflict(0, 2); |
| 71 | + assert.deepStrictEqual( |
| 72 | + { result: model.getResult() }, |
| 73 | + ({ result: "input2" }) |
| 74 | + ); |
| 75 | + } |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test('can merge word changes', async () => { |
| 80 | + await testMergeModel( |
| 81 | + { |
| 82 | + "languageId": "plaintext", |
| 83 | + "base": "hello", |
| 84 | + "input1": "hallo", |
| 85 | + "input2": "helloworld", |
| 86 | + "result": "" |
| 87 | + }, |
| 88 | + model => { |
| 89 | + assert.deepStrictEqual(model.getProjections(), { |
| 90 | + base: '⟦hello⟧₀', |
| 91 | + input1: '⟦hallo⟧₀', |
| 92 | + input2: '⟦helloworld⟧₀', |
| 93 | + }); |
| 94 | + |
| 95 | + model.toggleConflict(0, 1); |
| 96 | + model.toggleConflict(0, 2); |
| 97 | + |
| 98 | + assert.deepStrictEqual( |
| 99 | + { result: model.getResult() }, |
| 100 | + { result: 'halloworld' } |
| 101 | + ); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + }); |
| 106 | + |
| 107 | + test('can combine insertions at end of document', async () => { |
| 108 | + await testMergeModel( |
| 109 | + { |
| 110 | + "languageId": "plaintext", |
| 111 | + "base": "Zürich\nBern\nBasel\nChur\nGenf\nThun", |
| 112 | + "input1": "Zürich\nBern\nChur\nDavos\nGenf\nThun\nfunction f(b:boolean) {}", |
| 113 | + "input2": "Zürich\nBern\nBasel (FCB)\nChur\nGenf\nThun\nfunction f(a:number) {}", |
| 114 | + "result": "Zürich\nBern\nBasel\nChur\nDavos\nGenf\nThun" |
| 115 | + }, |
| 116 | + model => { |
| 117 | + assert.deepStrictEqual(model.getProjections(), { |
| 118 | + base: 'Zürich\nBern\n⟦Basel\n⟧₀Chur\n⟦⟧₁Genf\nThun⟦⟧₂', |
| 119 | + input1: |
| 120 | + 'Zürich\nBern\n⟦⟧₀Chur\n⟦Davos\n⟧₁Genf\nThun\n⟦function f(b:boolean) {}⟧₂', |
| 121 | + input2: |
| 122 | + 'Zürich\nBern\n⟦Basel (FCB)\n⟧₀Chur\n⟦⟧₁Genf\nThun\n⟦function f(a:number) {}⟧₂', |
| 123 | + }); |
| 124 | + |
| 125 | + model.toggleConflict(2, 1); |
| 126 | + model.toggleConflict(2, 2); |
| 127 | + |
| 128 | + assert.deepStrictEqual( |
| 129 | + { result: model.getResult() }, |
| 130 | + { |
| 131 | + result: |
| 132 | + 'Zürich\nBern\nBasel\nChur\nDavos\nGenf\nThun\nfunction f(b:boolean) {}\nfunction f(a:number) {}', |
| 133 | + } |
| 134 | + ); |
| 135 | + } |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +async function testMergeModel( |
| 141 | + options: MergeModelOptions, |
| 142 | + fn: (model: MergeModelInterface) => void |
| 143 | +): Promise<void> { |
| 144 | + const disposables = new DisposableStore(); |
| 145 | + const modelInterface = disposables.add( |
| 146 | + new MergeModelInterface(options, createModelServices(disposables)) |
| 147 | + ); |
| 148 | + await modelInterface.mergeModel.onInitialized; |
| 149 | + await fn(modelInterface); |
| 150 | + disposables.dispose(); |
| 151 | +} |
| 152 | + |
| 153 | +interface MergeModelOptions { |
| 154 | + languageId: string; |
| 155 | + input1: string; |
| 156 | + input2: string; |
| 157 | + base: string; |
| 158 | + result: string; |
| 159 | +} |
| 160 | + |
| 161 | +function toSmallNumbersDec(value: number): string { |
| 162 | + const smallNumbers = ['₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉']; |
| 163 | + return value.toString().split('').map(c => smallNumbers[parseInt(c)]).join(''); |
| 164 | +} |
| 165 | + |
| 166 | +class MergeModelInterface extends Disposable { |
| 167 | + public readonly mergeModel: MergeEditorModel; |
| 168 | + |
| 169 | + constructor(options: MergeModelOptions, instantiationService: IInstantiationService) { |
| 170 | + super(); |
| 171 | + const input1TextModel = this._register(createTextModel(options.input1, options.languageId)); |
| 172 | + const input2TextModel = this._register(createTextModel(options.input2, options.languageId)); |
| 173 | + const baseTextModel = this._register(createTextModel(options.base, options.languageId)); |
| 174 | + const resultTextModel = this._register(createTextModel(options.result, options.languageId)); |
| 175 | + this.mergeModel = this._register(instantiationService.createInstance(MergeEditorModel, |
| 176 | + baseTextModel, |
| 177 | + input1TextModel, |
| 178 | + '', |
| 179 | + '', |
| 180 | + '', |
| 181 | + input2TextModel, |
| 182 | + '', |
| 183 | + '', |
| 184 | + '', |
| 185 | + resultTextModel, |
| 186 | + { |
| 187 | + async computeDiff(textModel1, textModel2) { |
| 188 | + const result = EditorSimpleWorker.computeDiff(textModel1, textModel2, false, 10000); |
| 189 | + if (!result) { |
| 190 | + return { diffs: null }; |
| 191 | + } |
| 192 | + return { |
| 193 | + diffs: EditorWorkerServiceDiffComputer.fromDiffComputationResult( |
| 194 | + result, |
| 195 | + textModel1, |
| 196 | + textModel2 |
| 197 | + ), |
| 198 | + }; |
| 199 | + }, |
| 200 | + } |
| 201 | + )); |
| 202 | + } |
| 203 | + |
| 204 | + getProjections(): unknown { |
| 205 | + interface LabeledRange { |
| 206 | + range: Range; |
| 207 | + label: string; |
| 208 | + } |
| 209 | + function applyRanges(textModel: ITextModel, ranges: LabeledRange[]): void { |
| 210 | + textModel.applyEdits(ranges.map(({ range, label }) => ({ |
| 211 | + range: range, |
| 212 | + text: `⟦${textModel.getValueInRange(range)}⟧${label}`, |
| 213 | + }))); |
| 214 | + } |
| 215 | + const baseRanges = this.mergeModel.modifiedBaseRanges.get(); |
| 216 | + |
| 217 | + const baseTextModel = createTextModel(this.mergeModel.base.getValue()); |
| 218 | + applyRanges( |
| 219 | + baseTextModel, |
| 220 | + baseRanges.map<LabeledRange>((r, idx) => ({ |
| 221 | + range: r.baseRange.toRange(), |
| 222 | + label: toSmallNumbersDec(idx), |
| 223 | + })) |
| 224 | + ); |
| 225 | + |
| 226 | + const input1TextModel = createTextModel(this.mergeModel.input1.getValue()); |
| 227 | + applyRanges( |
| 228 | + input1TextModel, |
| 229 | + baseRanges.map<LabeledRange>((r, idx) => ({ |
| 230 | + range: r.input1Range.toRange(), |
| 231 | + label: toSmallNumbersDec(idx), |
| 232 | + })) |
| 233 | + ); |
| 234 | + |
| 235 | + const input2TextModel = createTextModel(this.mergeModel.input2.getValue()); |
| 236 | + applyRanges( |
| 237 | + input2TextModel, |
| 238 | + baseRanges.map<LabeledRange>((r, idx) => ({ |
| 239 | + range: r.input2Range.toRange(), |
| 240 | + label: toSmallNumbersDec(idx), |
| 241 | + })) |
| 242 | + ); |
| 243 | + |
| 244 | + const result = { |
| 245 | + base: baseTextModel.getValue(), |
| 246 | + input1: input1TextModel.getValue(), |
| 247 | + input2: input2TextModel.getValue(), |
| 248 | + }; |
| 249 | + baseTextModel.dispose(); |
| 250 | + input1TextModel.dispose(); |
| 251 | + input2TextModel.dispose(); |
| 252 | + return result; |
| 253 | + } |
| 254 | + |
| 255 | + toggleConflict(conflictIdx: number, inputNumber: 1 | 2): void { |
| 256 | + const baseRange = this.mergeModel.modifiedBaseRanges.get()[conflictIdx]; |
| 257 | + if (!baseRange) { |
| 258 | + throw new Error(); |
| 259 | + } |
| 260 | + const state = this.mergeModel.getState(baseRange).get(); |
| 261 | + transaction(tx => { |
| 262 | + this.mergeModel.setState(baseRange, state.toggle(inputNumber), true, tx); |
| 263 | + }); |
| 264 | + } |
| 265 | + |
| 266 | + getResult(): string { |
| 267 | + return this.mergeModel.result.getValue(); |
| 268 | + } |
| 269 | +} |
0 commit comments