-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
debugHover.ts
366 lines (307 loc) · 12.9 KB
/
debugHover.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*---------------------------------------------------------------------------------------------
* 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 lifecycle from 'vs/base/common/lifecycle';
import { KeyCode } from 'vs/base/common/keyCodes';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
import * as dom from 'vs/base/browser/dom';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { ConfigurationChangedEvent, EditorOption } from 'vs/editor/common/config/editorOptions';
import { Position } from 'vs/editor/common/core/position';
import { Range, IRange } from 'vs/editor/common/core/range';
import { IContentWidget, ICodeEditor, IContentWidgetPosition, ContentWidgetPositionPreference } from 'vs/editor/browser/editorBrowser';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IDebugService, IExpression, IExpressionContainer, IStackFrame } from 'vs/workbench/contrib/debug/common/debug';
import { Expression } from 'vs/workbench/contrib/debug/common/debugModel';
import { renderExpressionValue } from 'vs/workbench/contrib/debug/browser/baseDebugView';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { attachStylerCallback } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { editorHoverBackground, editorHoverBorder, editorHoverForeground } from 'vs/platform/theme/common/colorRegistry';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { getExactExpressionStartAndEnd } from 'vs/workbench/contrib/debug/common/debugUtils';
import { AsyncDataTree } from 'vs/base/browser/ui/tree/asyncDataTree';
import { IListAccessibilityProvider } from 'vs/base/browser/ui/list/listWidget';
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { WorkbenchAsyncDataTree } from 'vs/platform/list/browser/listService';
import { coalesce } from 'vs/base/common/arrays';
import { IAsyncDataSource } from 'vs/base/browser/ui/tree/tree';
import { VariablesRenderer } from 'vs/workbench/contrib/debug/browser/variablesView';
import { EvaluatableExpressionProviderRegistry } from 'vs/editor/common/modes';
import { CancellationToken } from 'vs/base/common/cancellation';
const $ = dom.$;
async function doFindExpression(container: IExpressionContainer, namesToFind: string[]): Promise<IExpression | null> {
if (!container) {
return Promise.resolve(null);
}
const children = await container.getChildren();
// look for our variable in the list. First find the parents of the hovered variable if there are any.
const filtered = children.filter(v => namesToFind[0] === v.name);
if (filtered.length !== 1) {
return null;
}
if (namesToFind.length === 1) {
return filtered[0];
} else {
return doFindExpression(filtered[0], namesToFind.slice(1));
}
}
export async function findExpressionInStackFrame(stackFrame: IStackFrame, namesToFind: string[]): Promise<IExpression | undefined> {
const scopes = await stackFrame.getScopes();
const nonExpensive = scopes.filter(s => !s.expensive);
const expressions = coalesce(await Promise.all(nonExpensive.map(scope => doFindExpression(scope, namesToFind))));
// only show if all expressions found have the same value
return expressions.length > 0 && expressions.every(e => e.value === expressions[0].value) ? expressions[0] : undefined;
}
export class DebugHoverWidget implements IContentWidget {
static readonly ID = 'debug.hoverWidget';
// editor.IContentWidget.allowEditorOverflow
allowEditorOverflow = true;
private _isVisible: boolean;
private domNode!: HTMLElement;
private tree!: AsyncDataTree<IExpression, IExpression, any>;
private showAtPosition: Position | null;
private highlightDecorations: string[];
private complexValueContainer!: HTMLElement;
private complexValueTitle!: HTMLElement;
private valueContainer!: HTMLElement;
private treeContainer!: HTMLElement;
private toDispose: lifecycle.IDisposable[];
private scrollbar!: DomScrollableElement;
constructor(
private editor: ICodeEditor,
@IDebugService private readonly debugService: IDebugService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IThemeService private readonly themeService: IThemeService
) {
this.toDispose = [];
this._isVisible = false;
this.showAtPosition = null;
this.highlightDecorations = [];
}
private create(): void {
this.domNode = $('.debug-hover-widget');
this.complexValueContainer = dom.append(this.domNode, $('.complex-value'));
this.complexValueTitle = dom.append(this.complexValueContainer, $('.title'));
this.treeContainer = dom.append(this.complexValueContainer, $('.debug-hover-tree'));
this.treeContainer.setAttribute('role', 'tree');
const dataSource = new DebugHoverDataSource();
this.tree = <WorkbenchAsyncDataTree<IExpression, IExpression, any>>this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'DebugHover', this.treeContainer, new DebugHoverDelegate(), [this.instantiationService.createInstance(VariablesRenderer)],
dataSource, {
accessibilityProvider: new DebugHoverAccessibilityProvider(),
mouseSupport: false,
horizontalScrolling: true,
useShadows: false,
overrideStyles: {
listBackground: editorHoverBackground
}
});
this.valueContainer = $('.value');
this.valueContainer.tabIndex = 0;
this.valueContainer.setAttribute('role', 'tooltip');
this.scrollbar = new DomScrollableElement(this.valueContainer, { horizontal: ScrollbarVisibility.Hidden });
this.domNode.appendChild(this.scrollbar.getDomNode());
this.toDispose.push(this.scrollbar);
this.editor.applyFontInfo(this.domNode);
this.toDispose.push(attachStylerCallback(this.themeService, { editorHoverBackground, editorHoverBorder, editorHoverForeground }, colors => {
if (colors.editorHoverBackground) {
this.domNode.style.backgroundColor = colors.editorHoverBackground.toString();
} else {
this.domNode.style.backgroundColor = '';
}
if (colors.editorHoverBorder) {
this.domNode.style.border = `1px solid ${colors.editorHoverBorder}`;
} else {
this.domNode.style.border = '';
}
if (colors.editorHoverForeground) {
this.domNode.style.color = colors.editorHoverForeground.toString();
} else {
this.domNode.style.color = '';
}
}));
this.toDispose.push(this.tree.onDidChangeContentHeight(() => this.layoutTreeAndContainer(false)));
this.registerListeners();
this.editor.addContentWidget(this);
}
private registerListeners(): void {
this.toDispose.push(dom.addStandardDisposableListener(this.domNode, 'keydown', (e: IKeyboardEvent) => {
if (e.equals(KeyCode.Escape)) {
this.hide();
}
}));
this.toDispose.push(this.editor.onDidChangeConfiguration((e: ConfigurationChangedEvent) => {
if (e.hasChanged(EditorOption.fontInfo)) {
this.editor.applyFontInfo(this.domNode);
}
}));
}
isHovered(): boolean {
return this.domNode.matches(':hover');
}
isVisible(): boolean {
return this._isVisible;
}
getId(): string {
return DebugHoverWidget.ID;
}
getDomNode(): HTMLElement {
return this.domNode;
}
async showAt(range: Range, focus: boolean): Promise<void> {
const session = this.debugService.getViewModel().focusedSession;
if (!session || !this.editor.hasModel()) {
return Promise.resolve(this.hide());
}
const model = this.editor.getModel();
const pos = range.getStartPosition();
let rng: IRange | undefined = undefined;
let matchingExpression: string | undefined;
if (EvaluatableExpressionProviderRegistry.has(model)) {
const supports = EvaluatableExpressionProviderRegistry.ordered(model);
const promises = supports.map(support => {
return Promise.resolve(support.provideEvaluatableExpression(model, pos, CancellationToken.None)).then(expression => {
return expression;
}, err => {
//onUnexpectedExternalError(err);
return undefined;
});
});
const results = await Promise.all(promises).then(coalesce);
if (results.length > 0) {
matchingExpression = results[0].expression;
rng = results[0].range;
if (!matchingExpression) {
const lineContent = model.getLineContent(pos.lineNumber);
matchingExpression = lineContent.substring(rng.startColumn - 1, rng.endColumn - 1);
}
}
} else { // old one-size-fits-all strategy
const lineContent = model.getLineContent(pos.lineNumber);
const { start, end } = getExactExpressionStartAndEnd(lineContent, range.startColumn, range.endColumn);
// use regex to extract the sub-expression #9821
matchingExpression = lineContent.substring(start - 1, end);
rng = new Range(pos.lineNumber, start, pos.lineNumber, start + matchingExpression.length);
}
if (!matchingExpression) {
return Promise.resolve(this.hide());
}
let expression;
if (session.capabilities.supportsEvaluateForHovers) {
expression = new Expression(matchingExpression);
await expression.evaluate(session, this.debugService.getViewModel().focusedStackFrame, 'hover');
} else {
const focusedStackFrame = this.debugService.getViewModel().focusedStackFrame;
if (focusedStackFrame) {
expression = await findExpressionInStackFrame(focusedStackFrame, coalesce(matchingExpression.split('.').map(word => word.trim())));
}
}
if (!expression || (expression instanceof Expression && !expression.available)) {
this.hide();
return;
}
if (rng) {
this.highlightDecorations = this.editor.deltaDecorations(this.highlightDecorations, [{
range: rng,
options: DebugHoverWidget._HOVER_HIGHLIGHT_DECORATION_OPTIONS
}]);
}
return this.doShow(pos, expression, focus);
}
private static readonly _HOVER_HIGHLIGHT_DECORATION_OPTIONS = ModelDecorationOptions.register({
className: 'hoverHighlight'
});
private async doShow(position: Position, expression: IExpression, focus: boolean, forceValueHover = false): Promise<void> {
if (!this.domNode) {
this.create();
}
this.showAtPosition = position;
this._isVisible = true;
if (!expression.hasChildren || forceValueHover) {
this.complexValueContainer.hidden = true;
this.valueContainer.hidden = false;
renderExpressionValue(expression, this.valueContainer, {
showChanged: false,
colorize: true
});
this.valueContainer.title = '';
this.editor.layoutContentWidget(this);
this.scrollbar.scanDomNode();
if (focus) {
this.editor.render();
this.valueContainer.focus();
}
return Promise.resolve(undefined);
}
this.valueContainer.hidden = true;
await this.tree.setInput(expression);
this.complexValueTitle.textContent = expression.value;
this.complexValueTitle.title = expression.value;
this.layoutTreeAndContainer(true);
this.editor.layoutContentWidget(this);
this.scrollbar.scanDomNode();
this.tree.scrollTop = 0;
this.tree.scrollLeft = 0;
this.complexValueContainer.hidden = false;
if (focus) {
this.editor.render();
this.tree.domFocus();
}
}
private layoutTreeAndContainer(initialLayout: boolean): void {
const scrollBarHeight = 10;
const treeHeight = Math.min(this.editor.getLayoutInfo().height * 0.7, this.tree.contentHeight + scrollBarHeight);
this.treeContainer.style.height = `${treeHeight}px`;
this.tree.layout(treeHeight, initialLayout ? 400 : undefined);
}
hide(): void {
if (!this._isVisible) {
return;
}
if (dom.isAncestor(document.activeElement, this.domNode)) {
this.editor.focus();
}
this._isVisible = false;
this.editor.deltaDecorations(this.highlightDecorations, []);
this.highlightDecorations = [];
this.editor.layoutContentWidget(this);
}
getPosition(): IContentWidgetPosition | null {
return this._isVisible ? {
position: this.showAtPosition,
preference: [
ContentWidgetPositionPreference.ABOVE,
ContentWidgetPositionPreference.BELOW
]
} : null;
}
dispose(): void {
this.toDispose = lifecycle.dispose(this.toDispose);
}
}
class DebugHoverAccessibilityProvider implements IListAccessibilityProvider<IExpression> {
getWidgetAriaLabel(): string {
return nls.localize('treeAriaLabel', "Debug Hover");
}
getAriaLabel(element: IExpression): string {
return nls.localize({ key: 'variableAriaLabel', comment: ['Do not translate placholders. Placeholders are name and value of a variable.'] }, "{0}, value {1}, variables, debug", element.name, element.value);
}
}
class DebugHoverDataSource implements IAsyncDataSource<IExpression, IExpression> {
hasChildren(element: IExpression): boolean {
return element.hasChildren;
}
getChildren(element: IExpression): Promise<IExpression[]> {
return element.getChildren();
}
}
class DebugHoverDelegate implements IListVirtualDelegate<IExpression> {
getHeight(element: IExpression): number {
return 18;
}
getTemplateId(element: IExpression): string {
return VariablesRenderer.ID;
}
}