debugHover.ts 12.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

I
isidor 已提交
6 7
import * as nls from 'vs/nls';
import * as lifecycle from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
8 9
import { TPromise } from 'vs/base/common/winjs.base';
import { KeyCode } from 'vs/base/common/keyCodes';
I
isidor 已提交
10
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
11 12
import { ITree } from 'vs/base/parts/tree/browser/tree';
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
I
isidor 已提交
13
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
14
import { DefaultController, ICancelableEvent, ClickBehavior } from 'vs/base/parts/tree/browser/treeDefaults';
15
import { IConfigurationChangedEvent } from 'vs/editor/common/config/editorOptions';
J
Johannes Rieken 已提交
16 17
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
I
isidor 已提交
18 19 20 21 22
import { IContentWidget, ICodeEditor, IContentWidgetPosition, ContentWidgetPositionPreference } from 'vs/editor/browser/editorBrowser';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IDebugService, IExpression, IExpressionContainer } from 'vs/workbench/parts/debug/common/debug';
import { Expression } from 'vs/workbench/parts/debug/common/debugModel';
import { VariablesRenderer, renderExpressionValue, VariablesDataSource } from 'vs/workbench/parts/debug/electron-browser/debugViewer';
23
import { IListService } from 'vs/platform/list/browser/listService';
24
import { attachListStyler, attachStylerCallback } from 'vs/platform/theme/common/styler';
25
import { IThemeService } from 'vs/platform/theme/common/themeService';
26
import { editorHoverBackground, editorHoverBorder } from "vs/platform/theme/common/colorRegistry";
E
Erich Gamma 已提交
27

J
Joao Moreno 已提交
28
const $ = dom.$;
29
const MAX_ELEMENTS_SHOWN = 18;
30
const MAX_VALUE_RENDER_LENGTH_IN_HOVER = 4096;
E
Erich Gamma 已提交
31

I
isidor 已提交
32
export class DebugHoverWidget implements IContentWidget {
E
Erich Gamma 已提交
33 34

	public static ID = 'debug.hoverWidget';
I
isidor 已提交
35
	// editor.IContentWidget.allowEditorOverflow
E
Erich Gamma 已提交
36 37
	public allowEditorOverflow = true;

I
isidor 已提交
38
	private _isVisible: boolean;
E
Erich Gamma 已提交
39
	private domNode: HTMLElement;
I
isidor 已提交
40
	private tree: ITree;
A
Alex Dima 已提交
41
	private showAtPosition: Position;
I
isidor 已提交
42
	private highlightDecorations: string[];
43
	private complexValueContainer: HTMLElement;
44
	private treeContainer: HTMLElement;
45
	private complexValueTitle: HTMLElement;
46
	private valueContainer: HTMLElement;
I
isidor 已提交
47 48
	private stoleFocus: boolean;
	private toDispose: lifecycle.IDisposable[];
E
Erich Gamma 已提交
49

50 51 52 53
	constructor(
		private editor: ICodeEditor,
		private debugService: IDebugService,
		private listService: IListService,
B
Benjamin Pasero 已提交
54 55
		instantiationService: IInstantiationService,
		private themeService: IThemeService
56
	) {
I
isidor 已提交
57
		this.toDispose = [];
I
isidor 已提交
58
		this.create(instantiationService);
I
isidor 已提交
59 60 61 62 63 64
		this.registerListeners();

		this.valueContainer = dom.append(this.domNode, $('.value'));
		this.valueContainer.tabIndex = 0;
		this.valueContainer.setAttribute('role', 'tooltip');

I
isidor 已提交
65
		this._isVisible = false;
I
isidor 已提交
66 67 68 69 70 71 72
		this.showAtPosition = null;
		this.highlightDecorations = [];

		this.editor.addContentWidget(this);
		this.editor.applyFontInfo(this.domNode);
	}

I
isidor 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
	private create(instantiationService: IInstantiationService): 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');
		this.tree = new Tree(this.treeContainer, {
			dataSource: new VariablesDataSource(),
			renderer: instantiationService.createInstance(VariablesHoverRenderer),
			controller: new DebugHoverController(this.editor)
		}, {
				indentPixels: 6,
				twistiePixels: 15,
86 87
				ariaLabel: nls.localize('treeAriaLabel', "Debug Hover"),
				keyboardSupport: false
I
isidor 已提交
88
			});
89

B
Benjamin Pasero 已提交
90
		this.toDispose.push(attachListStyler(this.tree, this.themeService));
91
		this.toDispose.push(this.listService.register(this.tree));
92 93 94 95 96 97 98 99
		this.toDispose.push(attachStylerCallback(this.themeService, { editorHoverBackground, editorHoverBorder }, colors => {
			this.domNode.style.backgroundColor = colors.editorHoverBackground;
			if (colors.editorHoverBorder) {
				this.domNode.style.border = `1px solid ${colors.editorHoverBorder}`;
			} else {
				this.domNode.style.border = null;
			}
		}));
I
isidor 已提交
100 101
	}

I
isidor 已提交
102
	private registerListeners(): void {
A
Alex Dima 已提交
103
		this.toDispose.push(this.tree.addListener('item:expanded', () => {
104
			this.layoutTree();
I
isidor 已提交
105
		}));
A
Alex Dima 已提交
106
		this.toDispose.push(this.tree.addListener('item:collapsed', () => {
107
			this.layoutTree();
I
isidor 已提交
108
		}));
109

A
Cleanup  
Alex Dima 已提交
110
		this.toDispose.push(dom.addStandardDisposableListener(this.domNode, 'keydown', (e: IKeyboardEvent) => {
A
Alexandru Dima 已提交
111
			if (e.equals(KeyCode.Escape)) {
I
isidor 已提交
112 113 114
				this.hide();
			}
		}));
A
Alex Dima 已提交
115
		this.toDispose.push(this.editor.onDidChangeConfiguration((e: IConfigurationChangedEvent) => {
I
isidor 已提交
116 117 118 119
			if (e.fontInfo) {
				this.editor.applyFontInfo(this.domNode);
			}
		}));
E
Erich Gamma 已提交
120 121
	}

I
isidor 已提交
122 123 124 125
	public isVisible(): boolean {
		return this._isVisible;
	}

E
Erich Gamma 已提交
126 127 128 129 130 131 132 133
	public getId(): string {
		return DebugHoverWidget.ID;
	}

	public getDomNode(): HTMLElement {
		return this.domNode;
	}

J
Johannes Rieken 已提交
134
	private getExactExpressionRange(lineContent: string, range: Range): Range {
B
Benjamin Pasero 已提交
135
		let matchingExpression: string = undefined;
R
rajkumar42 已提交
136 137 138 139 140
		let startOffset = 0;

		// Some example supported expressions: myVar.prop, a.b.c.d, myVar?.prop, myVar->prop, MyClass::StaticProp, *myVar
		// Match any character except a set of characters which often break interesting sub-expressions
		let expression: RegExp = /([^()\[\]{}<>\s+\-/%~#^;=|,`!]|\->)+/g;
B
Benjamin Pasero 已提交
141
		let result: RegExpExecArray = undefined;
R
rajkumar42 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

		// First find the full expression under the cursor
		while (result = expression.exec(lineContent)) {
			let start = result.index + 1;
			let end = start + result[0].length;

			if (start <= range.startColumn && end >= range.endColumn) {
				matchingExpression = result[0];
				startOffset = start;
				break;
			}
		}

		// If there are non-word characters after the cursor, we want to truncate the expression then.
		// For example in expression 'a.b.c.d', if the focus was under 'b', 'a.b' would be evaluated.
		if (matchingExpression) {
			let subExpression: RegExp = /\w+/g;
B
Benjamin Pasero 已提交
159
			let subExpressionResult: RegExpExecArray = undefined;
R
rajkumar42 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
			while (subExpressionResult = subExpression.exec(matchingExpression)) {
				let subEnd = subExpressionResult.index + 1 + startOffset + subExpressionResult[0].length;
				if (subEnd >= range.endColumn) {
					break;
				}
			}

			if (subExpressionResult) {
				matchingExpression = matchingExpression.substring(0, subExpression.lastIndex);
			}
		}

		return matchingExpression ?
			new Range(range.startLineNumber, startOffset, range.endLineNumber, startOffset + matchingExpression.length - 1) :
			new Range(range.startLineNumber, 0, range.endLineNumber, 0);
	}

I
isidor 已提交
177
	public showAt(range: Range, focus: boolean): TPromise<void> {
E
Erich Gamma 已提交
178 179
		const pos = range.getStartPosition();

180
		const process = this.debugService.getViewModel().focusedProcess;
181 182 183 184
		const lineContent = this.editor.getModel().getLineContent(pos.lineNumber);
		const expressionRange = this.getExactExpressionRange(lineContent, range);
		// use regex to extract the sub-expression #9821
		const matchingExpression = lineContent.substring(expressionRange.startColumn - 1, expressionRange.endColumn);
185 186 187 188
		if (!matchingExpression) {
			return TPromise.as(this.hide());
		}

I
isidor 已提交
189
		let promise: TPromise<IExpression>;
190
		if (process.session.capabilities.supportsEvaluateForHovers) {
191
			const result = new Expression(matchingExpression);
192
			promise = result.evaluate(process, this.debugService.getViewModel().focusedStackFrame, 'hover').then(() => result);
193
		} else {
I
isidor 已提交
194
			promise = this.findExpressionInStackFrame(matchingExpression.split('.').map(word => word.trim()).filter(word => !!word), expressionRange);
195
		}
R
rajkumar42 已提交
196

197 198
		return promise.then(expression => {
			if (!expression || (expression instanceof Expression && !expression.available)) {
199
				this.hide();
200
				return undefined;
201 202
			}

I
isidor 已提交
203
			this.highlightDecorations = this.editor.deltaDecorations(this.highlightDecorations, [{
204
				range: new Range(pos.lineNumber, expressionRange.startColumn, pos.lineNumber, expressionRange.startColumn + matchingExpression.length),
I
isidor 已提交
205 206 207 208
				options: {
					className: 'hoverHighlight'
				}
			}]);
I
isidor 已提交
209 210 211

			return this.doShow(pos, expression, focus);
		});
212 213
	}

I
isidor 已提交
214
	private doFindExpression(container: IExpressionContainer, namesToFind: string[]): TPromise<IExpression> {
I
isidor 已提交
215 216 217 218
		if (!container) {
			return TPromise.as(null);
		}

I
isidor 已提交
219
		return container.getChildren().then(children => {
220
			// look for our variable in the list. First find the parents of the hovered variable if there are any.
221
			const filtered = children.filter(v => namesToFind[0] === v.name);
222 223 224
			if (filtered.length !== 1) {
				return null;
			}
E
Erich Gamma 已提交
225

226 227 228 229 230 231 232
			if (namesToFind.length === 1) {
				return filtered[0];
			} else {
				return this.doFindExpression(filtered[0], namesToFind.slice(1));
			}
		});
	}
E
Erich Gamma 已提交
233

I
isidor 已提交
234
	private findExpressionInStackFrame(namesToFind: string[], expressionRange: Range): TPromise<IExpression> {
I
isidor 已提交
235
		return this.debugService.getViewModel().focusedStackFrame.getMostSpecificScopes(expressionRange)
236 237
			.then(scopes => TPromise.join(scopes.map(scope => this.doFindExpression(scope, namesToFind))))
			.then(expressions => expressions.filter(exp => !!exp))
238 239
			// only show if all expressions found have the same value
			.then(expressions => (expressions.length > 0 && expressions.every(e => e.value === expressions[0].value)) ? expressions[0] : null);
240
	}
E
Erich Gamma 已提交
241

I
isidor 已提交
242
	private doShow(position: Position, expression: IExpression, focus: boolean, forceValueHover = false): TPromise<void> {
243
		this.showAtPosition = position;
I
isidor 已提交
244
		this._isVisible = true;
I
isidor 已提交
245
		this.stoleFocus = focus;
246

247
		if (!expression.hasChildren || forceValueHover) {
248
			this.complexValueContainer.hidden = true;
249
			this.valueContainer.hidden = false;
250 251 252 253 254
			renderExpressionValue(expression, this.valueContainer, {
				showChanged: false,
				maxValueLength: MAX_VALUE_RENDER_LENGTH_IN_HOVER,
				preserveWhitespace: true
			});
I
isidor 已提交
255
			this.valueContainer.title = '';
256
			this.editor.layoutContentWidget(this);
I
isidor 已提交
257 258 259 260
			if (focus) {
				this.editor.render();
				this.valueContainer.focus();
			}
261

I
isidor 已提交
262
			return TPromise.as(null);
263
		}
I
isidor 已提交
264 265

		this.valueContainer.hidden = true;
266
		this.complexValueContainer.hidden = false;
I
isidor 已提交
267 268

		return this.tree.setInput(expression).then(() => {
269
			this.complexValueTitle.textContent = expression.value;
I
isidor 已提交
270
			this.complexValueTitle.title = expression.value;
I
isidor 已提交
271 272 273 274 275 276 277
			this.layoutTree();
			this.editor.layoutContentWidget(this);
			if (focus) {
				this.editor.render();
				this.tree.DOMFocus();
			}
		});
E
Erich Gamma 已提交
278 279
	}

280 281 282 283 284 285 286
	private layoutTree(): void {
		const navigator = this.tree.getNavigator();
		let visibleElementsCount = 0;
		while (navigator.next()) {
			visibleElementsCount++;
		}

287
		if (visibleElementsCount === 0) {
I
isidor 已提交
288
			this.doShow(this.showAtPosition, this.tree.getInput(), false, true);
289 290 291 292
		} else {
			const height = Math.min(visibleElementsCount, MAX_ELEMENTS_SHOWN) * 18;

			if (this.treeContainer.clientHeight !== height) {
J
Johannes Rieken 已提交
293
				this.treeContainer.style.height = `${height}px`;
294 295
				this.tree.layout();
			}
296
		}
297 298
	}

E
Erich Gamma 已提交
299
	public hide(): void {
I
isidor 已提交
300
		if (!this._isVisible) {
E
Erich Gamma 已提交
301 302
			return;
		}
303

I
isidor 已提交
304
		this._isVisible = false;
I
isidor 已提交
305 306
		this.editor.deltaDecorations(this.highlightDecorations, []);
		this.highlightDecorations = [];
E
Erich Gamma 已提交
307
		this.editor.layoutContentWidget(this);
I
isidor 已提交
308 309 310
		if (this.stoleFocus) {
			this.editor.focus();
		}
E
Erich Gamma 已提交
311 312
	}

I
isidor 已提交
313
	public getPosition(): IContentWidgetPosition {
I
isidor 已提交
314
		return this._isVisible ? {
E
Erich Gamma 已提交
315 316
			position: this.showAtPosition,
			preference: [
I
isidor 已提交
317 318
				ContentWidgetPositionPreference.ABOVE,
				ContentWidgetPositionPreference.BELOW
E
Erich Gamma 已提交
319 320 321
			]
		} : null;
	}
I
isidor 已提交
322 323

	public dispose(): void {
J
Joao Moreno 已提交
324
		this.toDispose = lifecycle.dispose(this.toDispose);
I
isidor 已提交
325
	}
E
Erich Gamma 已提交
326
}
I
isidor 已提交
327 328 329

class DebugHoverController extends DefaultController {

I
isidor 已提交
330
	constructor(private editor: ICodeEditor) {
331
		super({ clickBehavior: ClickBehavior.ON_MOUSE_UP, keyboardSupport: false });
B
Benjamin Pasero 已提交
332 333
	}

I
isidor 已提交
334
	protected onLeftClick(tree: ITree, element: any, eventish: ICancelableEvent, origin = 'mouse'): boolean {
I
isidor 已提交
335 336 337 338
		if (element.reference > 0) {
			super.onLeftClick(tree, element, eventish, origin);
			tree.clearFocus();
			tree.deselect(element);
B
Benjamin Pasero 已提交
339
			this.editor.focus();
I
isidor 已提交
340 341 342 343 344
		}

		return true;
	}
}
345

I
isidor 已提交
346
class VariablesHoverRenderer extends VariablesRenderer {
347 348 349 350 351

	public getHeight(tree: ITree, element: any): number {
		return 18;
	}
}