|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import { h } from 'vs/base/browser/dom';
|
7 |
| -import { Disposable } from 'vs/base/common/lifecycle'; |
| 7 | +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; |
| 8 | +import { autorun, IObservable } from 'vs/base/common/observable'; |
8 | 9 | import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
|
9 | 10 | import { ITextModel } from 'vs/editor/common/model';
|
10 | 11 | import { LineRange } from 'vs/workbench/contrib/mergeEditor/browser/model/lineRange';
|
| 12 | +import { MergeEditorViewModel } from 'vs/workbench/contrib/mergeEditor/browser/view/viewModel'; |
| 13 | +import * as nls from 'vs/nls'; |
11 | 14 |
|
12 | 15 | export const conflictMarkers = {
|
13 | 16 | start: '<<<<<<<',
|
14 | 17 | end: '>>>>>>>',
|
15 | 18 | };
|
16 | 19 |
|
17 | 20 | export class MergeMarkersController extends Disposable {
|
18 |
| - public static ID = 'editor.contrib.mergeConflictsController'; |
19 |
| - |
20 | 21 | private readonly viewZoneIds: string[] = [];
|
| 22 | + private readonly disposableStore = new DisposableStore(); |
21 | 23 |
|
22 | 24 | public constructor(
|
23 |
| - public readonly editor: ICodeEditor |
| 25 | + public readonly editor: ICodeEditor, |
| 26 | + public readonly mergeEditorViewModel: IObservable<MergeEditorViewModel | undefined>, |
24 | 27 | ) {
|
25 | 28 | super();
|
26 | 29 |
|
27 |
| - editor.onDidChangeModelContent(e => { |
28 |
| - this.updateDecorations(editor); |
29 |
| - }); |
| 30 | + this._register(editor.onDidChangeModelContent(e => { |
| 31 | + this.updateDecorations(); |
| 32 | + })); |
30 | 33 |
|
31 |
| - editor.onDidChangeModel(e => { |
32 |
| - this.updateDecorations(editor); |
33 |
| - }); |
| 34 | + this._register(editor.onDidChangeModel(e => { |
| 35 | + this.updateDecorations(); |
| 36 | + })); |
34 | 37 |
|
35 |
| - this.updateDecorations(editor); |
| 38 | + this.updateDecorations(); |
36 | 39 | }
|
37 | 40 |
|
38 |
| - private updateDecorations(editor: ICodeEditor) { |
| 41 | + private updateDecorations() { |
39 | 42 | const model = this.editor.getModel();
|
40 | 43 | const blocks = model ? getBlocks(model, { blockToRemoveStartLinePrefix: conflictMarkers.start, blockToRemoveEndLinePrefix: conflictMarkers.end }) : { blocks: [] };
|
41 | 44 |
|
42 |
| - editor.setHiddenAreas(blocks.blocks.map(b => b.lineRange.deltaEnd(-1).toRange()), this); |
43 |
| - editor.changeViewZones(c => { |
| 45 | + this.editor.setHiddenAreas(blocks.blocks.map(b => b.lineRange.deltaEnd(-1).toRange()), this); |
| 46 | + this.editor.changeViewZones(c => { |
| 47 | + this.disposableStore.clear(); |
44 | 48 | for (const id of this.viewZoneIds) {
|
45 | 49 | c.removeZone(id);
|
46 | 50 | }
|
47 | 51 | this.viewZoneIds.length = 0;
|
48 | 52 | for (const b of blocks.blocks) {
|
| 53 | + |
| 54 | + const startLine = model!.getLineContent(b.lineRange.startLineNumber).substring(0, 20); |
| 55 | + const endLine = model!.getLineContent(b.lineRange.endLineNumberExclusive - 1).substring(0, 20); |
| 56 | + |
| 57 | + const domNode = h('div', [ |
| 58 | + h('div.conflict-zone-root', [ |
| 59 | + h('pre', [startLine]), |
| 60 | + h('span.dots', ['...']), |
| 61 | + h('pre', [endLine]), |
| 62 | + h('span.text', [nls.localize('conflictingLines', "Conflicting Lines")]), |
| 63 | + ]), |
| 64 | + ]).root; |
49 | 65 | this.viewZoneIds.push(c.addZone({
|
50 | 66 | afterLineNumber: b.lineRange.endLineNumberExclusive - 1,
|
51 |
| - domNode: h('div.conflict-zone', [ |
52 |
| - h('div.conflict-zone-root', [h('div.conflict-zone-button', ['Conflict Marker'])]), |
53 |
| - ]).root, |
54 |
| - heightInLines: 1, |
| 67 | + domNode, |
| 68 | + heightInLines: 1.5, |
| 69 | + })); |
| 70 | + |
| 71 | + const updateWidth = () => { |
| 72 | + const layoutInfo = this.editor.getLayoutInfo(); |
| 73 | + domNode.style.width = `${layoutInfo.contentWidth - layoutInfo.verticalScrollbarWidth}px`; |
| 74 | + }; |
| 75 | + |
| 76 | + this.disposableStore.add( |
| 77 | + this.editor.onDidLayoutChange(() => { |
| 78 | + updateWidth(); |
| 79 | + }) |
| 80 | + ); |
| 81 | + updateWidth(); |
| 82 | + |
| 83 | + |
| 84 | + this.disposableStore.add(autorun('update classname', reader => { |
| 85 | + const vm = this.mergeEditorViewModel.read(reader); |
| 86 | + if (!vm) { |
| 87 | + return; |
| 88 | + } |
| 89 | + const activeRange = vm.activeModifiedBaseRange.read(reader); |
| 90 | + |
| 91 | + const classNames: string[] = []; |
| 92 | + classNames.push('conflict-zone'); |
| 93 | + |
| 94 | + if (activeRange) { |
| 95 | + const activeRangeInResult = vm.model.getRangeInResult(activeRange.baseRange, reader); |
| 96 | + if (activeRangeInResult.intersects(b.lineRange)) { |
| 97 | + classNames.push('focused'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + domNode.className = classNames.join(' '); |
55 | 102 | }));
|
56 | 103 | }
|
57 | 104 | });
|
|
0 commit comments