forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeeditor.ts
More file actions
255 lines (213 loc) · 9.37 KB
/
codeeditor.ts
File metadata and controls
255 lines (213 loc) · 9.37 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Widget } from 'vs/base/browser/ui/widget';
import { IOverlayWidget, ICodeEditor, IOverlayWidgetPosition, OverlayWidgetPositionPreference, isCodeEditor, isCompositeEditor } from 'vs/editor/browser/editorBrowser';
import { Emitter } from 'vs/base/common/event';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { $, append, clearNode } from 'vs/base/browser/dom';
import { attachStylerCallback } from 'vs/platform/theme/common/styler';
import { buttonBackground, buttonForeground, editorBackground, editorForeground, contrastBorder } from 'vs/platform/theme/common/colorRegistry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IRange } from 'vs/editor/common/core/range';
import { CursorChangeReason, ICursorPositionChangedEvent } from 'vs/editor/common/cursorEvents';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { TrackedRangeStickiness, IModelDecorationsChangeAccessor } from 'vs/editor/common/model';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IAction } from 'vs/base/common/actions';
import { EmbeddedCodeEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
export interface IRangeHighlightDecoration {
resource: URI;
range: IRange;
isWholeLine?: boolean;
}
export class RangeHighlightDecorations extends Disposable {
private readonly _onHighlightRemoved = this._register(new Emitter<void>());
readonly onHighlightRemoved = this._onHighlightRemoved.event;
private rangeHighlightDecorationId: string | null = null;
private editor: ICodeEditor | null = null;
private readonly editorDisposables = this._register(new DisposableStore());
constructor(@IEditorService private readonly editorService: IEditorService) {
super();
}
removeHighlightRange() {
if (this.editor && this.rangeHighlightDecorationId) {
const decorationId = this.rangeHighlightDecorationId;
this.editor.changeDecorations((accessor) => {
accessor.removeDecoration(decorationId);
});
this._onHighlightRemoved.fire();
}
this.rangeHighlightDecorationId = null;
}
highlightRange(range: IRangeHighlightDecoration, editor?: any) {
editor = editor ?? this.getEditor(range);
if (isCodeEditor(editor)) {
this.doHighlightRange(editor, range);
} else if (isCompositeEditor(editor) && isCodeEditor(editor.activeCodeEditor)) {
this.doHighlightRange(editor.activeCodeEditor, range);
}
}
private doHighlightRange(editor: ICodeEditor, selectionRange: IRangeHighlightDecoration) {
this.removeHighlightRange();
editor.changeDecorations((changeAccessor: IModelDecorationsChangeAccessor) => {
this.rangeHighlightDecorationId = changeAccessor.addDecoration(selectionRange.range, this.createRangeHighlightDecoration(selectionRange.isWholeLine));
});
this.setEditor(editor);
}
private getEditor(resourceRange: IRangeHighlightDecoration): ICodeEditor | undefined {
const resource = this.editorService.activeEditor?.resource;
if (resource && isEqual(resource, resourceRange.resource) && isCodeEditor(this.editorService.activeTextEditorControl)) {
return this.editorService.activeTextEditorControl;
}
return undefined;
}
private setEditor(editor: ICodeEditor) {
if (this.editor !== editor) {
this.editorDisposables.clear();
this.editor = editor;
this.editorDisposables.add(this.editor.onDidChangeCursorPosition((e: ICursorPositionChangedEvent) => {
if (
e.reason === CursorChangeReason.NotSet
|| e.reason === CursorChangeReason.Explicit
|| e.reason === CursorChangeReason.Undo
|| e.reason === CursorChangeReason.Redo
) {
this.removeHighlightRange();
}
}));
this.editorDisposables.add(this.editor.onDidChangeModel(() => { this.removeHighlightRange(); }));
this.editorDisposables.add(this.editor.onDidDispose(() => {
this.removeHighlightRange();
this.editor = null;
}));
}
}
private static readonly _WHOLE_LINE_RANGE_HIGHLIGHT = ModelDecorationOptions.register({
description: 'codeeditor-range-highlight-whole',
stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
className: 'rangeHighlight',
isWholeLine: true
});
private static readonly _RANGE_HIGHLIGHT = ModelDecorationOptions.register({
description: 'codeeditor-range-highlight',
stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
className: 'rangeHighlight'
});
private createRangeHighlightDecoration(isWholeLine: boolean = true): ModelDecorationOptions {
return (isWholeLine ? RangeHighlightDecorations._WHOLE_LINE_RANGE_HIGHLIGHT : RangeHighlightDecorations._RANGE_HIGHLIGHT);
}
override dispose() {
super.dispose();
if (this.editor?.getModel()) {
this.removeHighlightRange();
this.editor = null;
}
}
}
export class FloatingClickWidget extends Widget implements IOverlayWidget {
private readonly _onClick = this._register(new Emitter<void>());
readonly onClick = this._onClick.event;
private _domNode: HTMLElement;
constructor(
private editor: ICodeEditor,
private label: string,
keyBindingAction: string | null,
@IKeybindingService keybindingService: IKeybindingService,
@IThemeService private readonly themeService: IThemeService
) {
super();
this._domNode = $('.floating-click-widget');
this._domNode.style.padding = '6px 11px';
this._domNode.style.borderRadius = '2px';
this._domNode.style.cursor = 'pointer';
if (keyBindingAction) {
const keybinding = keybindingService.lookupKeybinding(keyBindingAction);
if (keybinding) {
this.label += ` (${keybinding.getLabel()})`;
}
}
}
getId(): string {
return 'editor.overlayWidget.floatingClickWidget';
}
getDomNode(): HTMLElement {
return this._domNode;
}
getPosition(): IOverlayWidgetPosition {
return {
preference: OverlayWidgetPositionPreference.BOTTOM_RIGHT_CORNER
};
}
render() {
clearNode(this._domNode);
this._register(attachStylerCallback(this.themeService, { buttonBackground, buttonForeground, editorBackground, editorForeground, contrastBorder }, colors => {
const backgroundColor = colors.buttonBackground ? colors.buttonBackground : colors.editorBackground;
if (backgroundColor) {
this._domNode.style.backgroundColor = backgroundColor.toString();
}
const foregroundColor = colors.buttonForeground ? colors.buttonForeground : colors.editorForeground;
if (foregroundColor) {
this._domNode.style.color = foregroundColor.toString();
}
const borderColor = colors.contrastBorder ? colors.contrastBorder.toString() : '';
this._domNode.style.borderWidth = borderColor ? '1px' : '';
this._domNode.style.borderStyle = borderColor ? 'solid' : '';
this._domNode.style.borderColor = borderColor;
}));
append(this._domNode, $('')).textContent = this.label;
this.onclick(this._domNode, e => this._onClick.fire());
this.editor.addOverlayWidget(this);
}
override dispose(): void {
this.editor.removeOverlayWidget(this);
super.dispose();
}
}
export class FloatingClickMenu extends Disposable implements IEditorContribution {
static readonly ID = 'editor.contrib.floatingClickMenu';
constructor(
editor: ICodeEditor,
@IInstantiationService instantiationService: IInstantiationService,
@IMenuService menuService: IMenuService,
@IContextKeyService contextKeyService: IContextKeyService
) {
super();
// DISABLED for embedded editors. In the future we can use a different MenuId for embedded editors
if (!(editor instanceof EmbeddedCodeEditorWidget)) {
const menu = menuService.createMenu(MenuId.EditorContent, contextKeyService);
const menuDisposables = new DisposableStore();
const renderMenuAsFloatingClickBtn = () => {
menuDisposables.clear();
if (!editor.hasModel() || editor.getOption(EditorOption.inDiffEditor)) {
return;
}
const actions: IAction[] = [];
createAndFillInActionBarActions(menu, { renderShortTitle: true, shouldForwardArgs: true }, actions);
if (actions.length === 0) {
return;
}
// todo@jrieken find a way to handle N actions, like showing a context menu
const [first] = actions;
const widget = instantiationService.createInstance(FloatingClickWidget, editor, first.label, first.id);
menuDisposables.add(widget);
menuDisposables.add(widget.onClick(() => first.run(editor.getModel().uri)));
widget.render();
};
this._store.add(menu);
this._store.add(menuDisposables);
this._store.add(menu.onDidChange(renderMenuAsFloatingClickBtn));
renderMenuAsFloatingClickBtn();
}
}
}