/
inlineDiffMargin.ts
227 lines (201 loc) · 8.38 KB
/
inlineDiffMargin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
import { Action } from 'vs/base/common/actions';
import { Disposable } from 'vs/base/common/lifecycle';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { Range } from 'vs/editor/common/core/range';
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Codicon } from 'vs/base/common/codicons';
import { ThemeIcon } from 'vs/base/common/themables';
import { EndOfLineSequence, ITextModel } from 'vs/editor/common/model';
export interface IDiffLinesChange {
readonly originalStartLineNumber: number;
readonly originalEndLineNumber: number;
readonly modifiedStartLineNumber: number;
readonly modifiedEndLineNumber: number;
readonly originalModel: ITextModel;
viewLineCounts: number[] | null;
}
export class InlineDiffMargin extends Disposable {
private readonly _diffActions: HTMLElement;
private _visibility: boolean = false;
get visibility(): boolean {
return this._visibility;
}
set visibility(_visibility: boolean) {
if (this._visibility !== _visibility) {
this._visibility = _visibility;
if (_visibility) {
this._diffActions.style.visibility = 'visible';
} else {
this._diffActions.style.visibility = 'hidden';
}
}
}
constructor(
private readonly _viewZoneId: string,
private readonly _marginDomNode: HTMLElement,
public readonly editor: CodeEditorWidget,
public readonly diff: IDiffLinesChange,
private readonly _contextMenuService: IContextMenuService,
private readonly _clipboardService: IClipboardService
) {
super();
// make sure the diff margin shows above overlay.
this._marginDomNode.style.zIndex = '10';
this._diffActions = document.createElement('div');
this._diffActions.className = ThemeIcon.asClassName(Codicon.lightBulb) + ' lightbulb-glyph';
this._diffActions.style.position = 'absolute';
const lineHeight = editor.getOption(EditorOption.lineHeight);
const lineFeed = editor.getModel()!.getEOL();
this._diffActions.style.right = '0px';
this._diffActions.style.visibility = 'hidden';
this._diffActions.style.height = `${lineHeight}px`;
this._diffActions.style.lineHeight = `${lineHeight}px`;
this._marginDomNode.appendChild(this._diffActions);
const actions: Action[] = [];
const isDeletion = diff.modifiedEndLineNumber === 0;
// default action
actions.push(new Action(
'diff.clipboard.copyDeletedContent',
isDeletion
? (diff.originalEndLineNumber > diff.modifiedStartLineNumber
? nls.localize('diff.clipboard.copyDeletedLinesContent.label', "Copy deleted lines")
: nls.localize('diff.clipboard.copyDeletedLinesContent.single.label', "Copy deleted line"))
: (diff.originalEndLineNumber > diff.modifiedStartLineNumber
? nls.localize('diff.clipboard.copyChangedLinesContent.label', "Copy changed lines")
: nls.localize('diff.clipboard.copyChangedLinesContent.single.label', "Copy changed line")),
undefined,
true,
async () => {
const range = new Range(diff.originalStartLineNumber, 1, diff.originalEndLineNumber + 1, 1);
const deletedText = diff.originalModel.getValueInRange(range);
await this._clipboardService.writeText(deletedText);
}
));
let currentLineNumberOffset = 0;
let copyLineAction: Action | undefined = undefined;
if (diff.originalEndLineNumber > diff.modifiedStartLineNumber) {
copyLineAction = new Action(
'diff.clipboard.copyDeletedLineContent',
isDeletion
? nls.localize('diff.clipboard.copyDeletedLineContent.label', "Copy deleted line ({0})", diff.originalStartLineNumber)
: nls.localize('diff.clipboard.copyChangedLineContent.label', "Copy changed line ({0})", diff.originalStartLineNumber),
undefined,
true,
async () => {
const lineContent = diff.originalModel.getLineContent(diff.originalStartLineNumber + currentLineNumberOffset);
if (lineContent === '') {
// empty line
const eof = diff.originalModel.getEndOfLineSequence();
await this._clipboardService.writeText(eof === EndOfLineSequence.LF ? '\n' : '\r\n');
} else {
await this._clipboardService.writeText(lineContent);
}
}
);
actions.push(copyLineAction);
}
const readOnly = editor.getOption(EditorOption.readOnly);
if (!readOnly) {
actions.push(new Action('diff.inline.revertChange', nls.localize('diff.inline.revertChange.label', "Revert this change"), undefined, true, async () => {
const range = new Range(diff.originalStartLineNumber, 1, diff.originalEndLineNumber, diff.originalModel.getLineMaxColumn(diff.originalEndLineNumber));
const deletedText = diff.originalModel.getValueInRange(range);
if (diff.modifiedEndLineNumber === 0) {
// deletion only
const column = editor.getModel()!.getLineMaxColumn(diff.modifiedStartLineNumber);
editor.executeEdits('diffEditor', [
{
range: new Range(diff.modifiedStartLineNumber, column, diff.modifiedStartLineNumber, column),
text: lineFeed + deletedText
}
]);
} else {
const column = editor.getModel()!.getLineMaxColumn(diff.modifiedEndLineNumber);
editor.executeEdits('diffEditor', [
{
range: new Range(diff.modifiedStartLineNumber, 1, diff.modifiedEndLineNumber, column),
text: deletedText
}
]);
}
}));
}
const showContextMenu = (x: number, y: number) => {
this._contextMenuService.showContextMenu({
getAnchor: () => {
return {
x,
y
};
},
getActions: () => {
if (copyLineAction) {
copyLineAction.label =
isDeletion
? nls.localize('diff.clipboard.copyDeletedLineContent.label', "Copy deleted line ({0})", diff.originalStartLineNumber + currentLineNumberOffset)
: nls.localize('diff.clipboard.copyChangedLineContent.label', "Copy changed line ({0})", diff.originalStartLineNumber + currentLineNumberOffset);
}
return actions;
},
autoSelectFirstItem: true
});
};
this._register(dom.addStandardDisposableListener(this._diffActions, 'mousedown', e => {
const { top, height } = dom.getDomNodePagePosition(this._diffActions);
const pad = Math.floor(lineHeight / 3);
e.preventDefault();
showContextMenu(e.posx, top + height + pad);
}));
this._register(editor.onMouseMove((e: IEditorMouseEvent) => {
if (e.target.type === MouseTargetType.CONTENT_VIEW_ZONE || e.target.type === MouseTargetType.GUTTER_VIEW_ZONE) {
const viewZoneId = e.target.detail.viewZoneId;
if (viewZoneId === this._viewZoneId) {
this.visibility = true;
currentLineNumberOffset = this._updateLightBulbPosition(this._marginDomNode, e.event.browserEvent.y, lineHeight);
} else {
this.visibility = false;
}
} else {
this.visibility = false;
}
}));
this._register(editor.onMouseDown((e: IEditorMouseEvent) => {
if (!e.event.rightButton) {
return;
}
if (e.target.type === MouseTargetType.CONTENT_VIEW_ZONE || e.target.type === MouseTargetType.GUTTER_VIEW_ZONE) {
const viewZoneId = e.target.detail.viewZoneId;
if (viewZoneId === this._viewZoneId) {
e.event.preventDefault();
currentLineNumberOffset = this._updateLightBulbPosition(this._marginDomNode, e.event.browserEvent.y, lineHeight);
showContextMenu(e.event.posx, e.event.posy + lineHeight);
}
}
}));
}
private _updateLightBulbPosition(marginDomNode: HTMLElement, y: number, lineHeight: number): number {
const { top } = dom.getDomNodePagePosition(marginDomNode);
const offset = y - top;
const lineNumberOffset = Math.floor(offset / lineHeight);
const newTop = lineNumberOffset * lineHeight;
this._diffActions.style.top = `${newTop}px`;
if (this.diff.viewLineCounts) {
let acc = 0;
for (let i = 0; i < this.diff.viewLineCounts.length; i++) {
acc += this.diff.viewLineCounts[i];
if (lineNumberOffset < acc) {
return i;
}
}
}
return lineNumberOffset;
}
}