|
| 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 { Disposable } from 'vs/base/common/lifecycle'; |
| 7 | +import { IObservable, derived } from 'vs/base/common/observable'; |
| 8 | +import { isDefined } from 'vs/base/common/types'; |
| 9 | +import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget'; |
| 10 | +import { arrowRevertChange, diffAddDecoration, diffAddDecorationEmpty, diffDeleteDecoration, diffDeleteDecorationEmpty, diffLineAddDecorationBackground, diffLineDeleteDecorationBackground } from 'vs/editor/browser/widget/diffEditorWidget2/decorations'; |
| 11 | +import { DiffModel } from 'vs/editor/browser/widget/diffEditorWidget2/diffModel'; |
| 12 | +import { MovedBlocksLinesPart } from 'vs/editor/browser/widget/diffEditorWidget2/movedBlocksLines'; |
| 13 | +import { applyObservableDecorations } from 'vs/editor/browser/widget/diffEditorWidget2/utils'; |
| 14 | +import { LineRange } from 'vs/editor/common/core/lineRange'; |
| 15 | +import { Position } from 'vs/editor/common/core/position'; |
| 16 | +import { Range } from 'vs/editor/common/core/range'; |
| 17 | +import { IModelDeltaDecoration } from 'vs/editor/common/model'; |
| 18 | + |
| 19 | +export class DiffEditorDecorations extends Disposable { |
| 20 | + constructor( |
| 21 | + private readonly _originalEditor: CodeEditorWidget, |
| 22 | + private readonly _modifiedEditor: CodeEditorWidget, |
| 23 | + private readonly _diffModel: IObservable<DiffModel | undefined>, |
| 24 | + private readonly _renderSideBySide: IObservable<boolean>, |
| 25 | + ) { |
| 26 | + super(); |
| 27 | + |
| 28 | + this._register(applyObservableDecorations(this._originalEditor, this._decorations.map(d => d?.originalDecorations || []))); |
| 29 | + this._register(applyObservableDecorations(this._modifiedEditor, this._decorations.map(d => d?.modifiedDecorations || []))); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + private readonly _decorations = derived('decorations', (reader) => { |
| 34 | + const diff = this._diffModel.read(reader)?.diff.read(reader); |
| 35 | + if (!diff) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + const currentMove = this._diffModel.read(reader)!.syncedMovedTexts.read(reader); |
| 40 | + |
| 41 | + const originalDecorations: IModelDeltaDecoration[] = []; |
| 42 | + const modifiedDecorations: IModelDeltaDecoration[] = []; |
| 43 | + for (const m of diff.mappings) { |
| 44 | + const fullRangeOriginal = LineRange.subtract(m.lineRangeMapping.originalRange, currentMove?.lineRangeMapping.originalRange) |
| 45 | + .map(i => i.toInclusiveRange()).filter(isDefined); |
| 46 | + for (const range of fullRangeOriginal) { |
| 47 | + originalDecorations.push({ range, options: diffLineDeleteDecorationBackground }); |
| 48 | + } |
| 49 | + |
| 50 | + const fullRangeModified = LineRange.subtract(m.lineRangeMapping.modifiedRange, currentMove?.lineRangeMapping.modifiedRange) |
| 51 | + .map(i => i.toInclusiveRange()).filter(isDefined); |
| 52 | + for (const range of fullRangeModified) { |
| 53 | + modifiedDecorations.push({ range, options: diffLineAddDecorationBackground }); |
| 54 | + } |
| 55 | + |
| 56 | + for (const i of m.lineRangeMapping.innerChanges || []) { |
| 57 | + if (currentMove |
| 58 | + && (currentMove.lineRangeMapping.originalRange.intersect(new LineRange(i.originalRange.startLineNumber, i.originalRange.endLineNumber)) |
| 59 | + || currentMove.lineRangeMapping.modifiedRange.intersect(new LineRange(i.modifiedRange.startLineNumber, i.modifiedRange.endLineNumber)))) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + originalDecorations.push({ range: i.originalRange, options: i.originalRange.isEmpty() ? diffDeleteDecorationEmpty : diffDeleteDecoration }); |
| 64 | + modifiedDecorations.push({ range: i.modifiedRange, options: i.modifiedRange.isEmpty() ? diffAddDecorationEmpty : diffAddDecoration }); |
| 65 | + } |
| 66 | + |
| 67 | + if (!m.lineRangeMapping.modifiedRange.isEmpty && this._renderSideBySide.read(reader) && !currentMove) { |
| 68 | + modifiedDecorations.push({ range: Range.fromPositions(new Position(m.lineRangeMapping.modifiedRange.startLineNumber, 1)), options: arrowRevertChange }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (currentMove) { |
| 73 | + for (const m of currentMove.changes) { |
| 74 | + const fullRangeOriginal = m.originalRange.toInclusiveRange(); |
| 75 | + if (fullRangeOriginal) { |
| 76 | + originalDecorations.push({ range: fullRangeOriginal, options: diffLineDeleteDecorationBackground }); |
| 77 | + } |
| 78 | + const fullRangeModified = m.modifiedRange.toInclusiveRange(); |
| 79 | + if (fullRangeModified) { |
| 80 | + modifiedDecorations.push({ range: fullRangeModified, options: diffLineAddDecorationBackground }); |
| 81 | + } |
| 82 | + |
| 83 | + for (const i of m.innerChanges || []) { |
| 84 | + originalDecorations.push({ range: i.originalRange, options: diffDeleteDecoration }); |
| 85 | + modifiedDecorations.push({ range: i.modifiedRange, options: diffAddDecoration }); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for (const m of diff.movedTexts) { |
| 91 | + originalDecorations.push({ |
| 92 | + range: m.lineRangeMapping.originalRange.toInclusiveRange()!, options: { |
| 93 | + description: 'moved', |
| 94 | + blockClassName: 'movedOriginal', |
| 95 | + blockPadding: [MovedBlocksLinesPart.movedCodeBlockPadding, 0, MovedBlocksLinesPart.movedCodeBlockPadding, MovedBlocksLinesPart.movedCodeBlockPadding], |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + modifiedDecorations.push({ |
| 100 | + range: m.lineRangeMapping.modifiedRange.toInclusiveRange()!, options: { |
| 101 | + description: 'moved', |
| 102 | + blockClassName: 'movedModified', |
| 103 | + blockPadding: [4, 0, 4, 4], |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + return { originalDecorations, modifiedDecorations }; |
| 109 | + }); |
| 110 | +} |
0 commit comments