callStackView.ts 24.1 KB
Newer Older
I
isidor 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  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';
7
import { RunOnceScheduler, ignoreErrors } from 'vs/base/common/async';
I
isidor 已提交
8
import * as dom from 'vs/base/browser/dom';
I
isidor 已提交
9
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
10 11
import { IDebugService, State, IStackFrame, IDebugSession, IThread, CONTEXT_CALLSTACK_ITEM_TYPE, IDebugModel } from 'vs/workbench/contrib/debug/common/debug';
import { Thread, StackFrame, ThreadAndSessionIds } from 'vs/workbench/contrib/debug/common/debugModel';
I
isidor 已提交
12 13
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
14
import { MenuId, IMenu, IMenuService } from 'vs/platform/actions/common/actions';
I
isidor 已提交
15
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
16
import { renderViewTree } from 'vs/workbench/contrib/debug/browser/baseDebugView';
I
isidor 已提交
17
import { IAction } from 'vs/base/common/actions';
18
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
19
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
20
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
I
isidor 已提交
21
import { IViewletPanelOptions, ViewletPanel } from 'vs/workbench/browser/parts/views/panelViewlet';
I
isidor 已提交
22
import { ILabelService } from 'vs/platform/label/common/label';
I
isidor 已提交
23 24 25
import { IAccessibilityProvider } from 'vs/base/browser/ui/list/listWidget';
import { fillInContextMenuActions } from 'vs/platform/actions/browser/menuItemActionItem';
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
J
Joao Moreno 已提交
26
import { ITreeRenderer, ITreeNode, ITreeContextMenuEvent, IAsyncDataSource } from 'vs/base/browser/ui/tree/tree';
27
import { TreeResourceNavigator2, WorkbenchAsyncDataTree } from 'vs/platform/list/browser/listService';
I
isidor 已提交
28
import { onUnexpectedError } from 'vs/base/common/errors';
I
isidor 已提交
29 30
import { HighlightedLabel } from 'vs/base/browser/ui/highlightedlabel/highlightedLabel';
import { createMatches, FuzzyScore } from 'vs/base/common/filters';
I
isidor 已提交
31

32
const $ = dom.$;
I
isidor 已提交
33

I
isidor 已提交
34 35 36
type CallStackItem = IStackFrame | IThread | IDebugSession | string | ThreadAndSessionIds | IStackFrame[];

export class CallStackView extends ViewletPanel {
I
isidor 已提交
37

38 39
	private pauseMessage: HTMLSpanElement;
	private pauseMessageLabel: HTMLSpanElement;
I
isidor 已提交
40
	private onCallStackChangeScheduler: RunOnceScheduler;
41
	private needsRefresh: boolean;
42
	private ignoreSelectionChangedEvent: boolean;
I
isidor 已提交
43
	private ignoreFocusStackFrameEvent: boolean;
44
	private callStackItemType: IContextKey<string>;
I
isidor 已提交
45
	private dataSource: CallStackDataSource;
46
	private tree: WorkbenchAsyncDataTree<CallStackItem | IDebugModel, CallStackItem, FuzzyScore>;
I
isidor 已提交
47
	private contributedContextMenu: IMenu;
I
isidor 已提交
48 49 50 51

	constructor(
		private options: IViewletViewOptions,
		@IContextMenuService contextMenuService: IContextMenuService,
52
		@IDebugService private readonly debugService: IDebugService,
I
isidor 已提交
53
		@IKeybindingService keybindingService: IKeybindingService,
54 55
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IEditorService private readonly editorService: IEditorService,
56
		@IConfigurationService configurationService: IConfigurationService,
I
isidor 已提交
57
		@IMenuService menuService: IMenuService,
58
		@IContextKeyService readonly contextKeyService: IContextKeyService,
I
isidor 已提交
59
	) {
60
		super({ ...(options as IViewletPanelOptions), ariaHeaderLabel: nls.localize('callstackSection', "Call Stack Section") }, keybindingService, contextMenuService, configurationService);
61
		this.callStackItemType = CONTEXT_CALLSTACK_ITEM_TYPE.bindTo(contextKeyService);
I
isidor 已提交
62

I
isidor 已提交
63 64 65
		this.contributedContextMenu = menuService.createMenu(MenuId.DebugCallStackContext, contextKeyService);
		this.disposables.push(this.contributedContextMenu);

I
isidor 已提交
66 67 68 69
		// Create scheduler to prevent unnecessary flashing of tree when reacting to changes
		this.onCallStackChangeScheduler = new RunOnceScheduler(() => {
			// Only show the global pause message if we do not display threads.
			// Otherwise there will be a pause message per thread and there is no need for a global one.
I
isidor 已提交
70
			const sessions = this.debugService.getModel().getSessions();
I
isidor 已提交
71 72
			const thread = sessions.length === 1 && sessions[0].getAllThreads().length === 1 ? sessions[0].getAllThreads()[0] : undefined;
			if (thread && thread.stoppedDetails) {
I
isidor 已提交
73 74
				this.pauseMessageLabel.textContent = thread.stoppedDetails.description || nls.localize('debugStopped', "Paused on {0}", thread.stoppedDetails.reason || '');
				this.pauseMessageLabel.title = thread.stoppedDetails.text || '';
I
isidor 已提交
75
				dom.toggleClass(this.pauseMessageLabel, 'exception', thread.stoppedDetails.reason === 'exception');
76
				this.pauseMessage.hidden = false;
I
isidor 已提交
77
			} else {
78
				this.pauseMessage.hidden = true;
I
isidor 已提交
79 80
			}

81
			this.needsRefresh = false;
I
isidor 已提交
82
			this.dataSource.deemphasizedStackFramesToShow = [];
83
			this.tree.updateChildren().then(() => this.updateTreeSelection());
I
isidor 已提交
84 85 86
		}, 50);
	}

J
Jackson Kearl 已提交
87
	protected renderHeaderTitle(container: HTMLElement): void {
I
isidor 已提交
88
		const titleContainer = dom.append(container, $('.debug-call-stack-title'));
J
Jackson Kearl 已提交
89 90 91
		super.renderHeaderTitle(titleContainer, this.options.title);

		this.pauseMessage = dom.append(titleContainer, $('span.pause-message'));
92 93
		this.pauseMessage.hidden = true;
		this.pauseMessageLabel = dom.append(this.pauseMessage, $('span.label'));
I
isidor 已提交
94 95
	}

I
isidor 已提交
96
	renderBody(container: HTMLElement): void {
I
isidor 已提交
97
		dom.addClass(container, 'debug-call-stack');
I
isidor 已提交
98 99
		const treeContainer = renderViewTree(container);

J
Joao Moreno 已提交
100
		this.dataSource = new CallStackDataSource();
101
		this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, treeContainer, new CallStackDelegate(), [
I
isidor 已提交
102 103 104 105 106 107 108 109
			new SessionsRenderer(),
			new ThreadsRenderer(),
			this.instantiationService.createInstance(StackFramesRenderer),
			new ErrorsRenderer(),
			new LoadMoreRenderer(),
			new ShowMoreRenderer()
		], this.dataSource, {
				accessibilityProvider: new CallStackAccessibilityProvider(),
I
isidor 已提交
110
				ariaLabel: nls.localize({ comment: ['Debug is a noun in this context, not a verb.'], key: 'callStackAriaLabel' }, "Debug Call Stack"),
111 112 113 114 115 116 117 118 119
				identityProvider: {
					getId: element => {
						if (typeof element === 'string') {
							return element;
						}
						if (element instanceof Array) {
							return `showMore ${element[0].getId()}`;
						}

120
						return (<IStackFrame | IThread | IDebugSession | ThreadAndSessionIds>element).getId();
121
					}
I
isidor 已提交
122
				},
J
Joao Moreno 已提交
123 124
				keyboardNavigationLabelProvider: {
					getKeyboardNavigationLabel: e => {
I
isidor 已提交
125
						if (isDebugSession(e)) {
I
isidor 已提交
126 127 128
							return e.getLabel();
						}
						if (e instanceof Thread) {
129
							return `${e.name} ${e.stateLabel}`;
I
isidor 已提交
130 131 132 133 134 135 136 137 138 139
						}
						if (e instanceof StackFrame || typeof e === 'string') {
							return e;
						}
						if (e instanceof ThreadAndSessionIds) {
							return LoadMoreRenderer.LABEL;
						}

						return nls.localize('showMoreStackFrames2', "Show More Stack Frames");
					}
140
				}
141
			}) as WorkbenchAsyncDataTree<CallStackItem | IDebugModel, CallStackItem, FuzzyScore>;
I
isidor 已提交
142

I
isidor 已提交
143
		this.tree.setInput(this.debugService.getModel()).then(undefined, onUnexpectedError);
J
Joao Moreno 已提交
144

I
isidor 已提交
145
		const callstackNavigator = new TreeResourceNavigator2(this.tree);
I
isidor 已提交
146
		this.disposables.push(callstackNavigator);
J
Joao Moreno 已提交
147
		this.disposables.push(callstackNavigator.onDidOpenResource(e => {
I
isidor 已提交
148 149 150 151
			if (this.ignoreSelectionChangedEvent) {
				return;
			}

I
isidor 已提交
152
			const focusStackFrame = (stackFrame: IStackFrame | undefined, thread: IThread | undefined, session: IDebugSession) => {
I
isidor 已提交
153 154 155 156 157 158 159 160
				this.ignoreFocusStackFrameEvent = true;
				try {
					this.debugService.focusStackFrame(stackFrame, thread, session, true);
				} finally {
					this.ignoreFocusStackFrameEvent = false;
				}
			};

I
isidor 已提交
161 162
			const element = e.element;
			if (element instanceof StackFrame) {
I
isidor 已提交
163
				focusStackFrame(element, element.thread, element.thread.session);
I
isidor 已提交
164 165 166
				element.openInEditor(this.editorService, e.editorOptions.preserveFocus, e.sideBySide, e.editorOptions.pinned);
			}
			if (element instanceof Thread) {
I
isidor 已提交
167
				focusStackFrame(undefined, element, element.session);
I
isidor 已提交
168
			}
I
isidor 已提交
169
			if (isDebugSession(element)) {
I
isidor 已提交
170
				focusStackFrame(undefined, undefined, element);
I
isidor 已提交
171 172 173 174 175 176
			}
			if (element instanceof ThreadAndSessionIds) {
				const session = this.debugService.getModel().getSessions().filter(p => p.getId() === element.sessionId).pop();
				const thread = session && session.getThread(element.threadId);
				if (thread) {
					(<Thread>thread).fetchCallStack()
177
						.then(() => this.tree.updateChildren());
I
isidor 已提交
178 179 180 181
				}
			}
			if (element instanceof Array) {
				this.dataSource.deemphasizedStackFramesToShow.push(...element);
182
				this.tree.updateChildren();
I
isidor 已提交
183 184
			}
		}));
I
isidor 已提交
185 186

		this.disposables.push(this.debugService.getModel().onDidChangeCallStack(() => {
187
			if (!this.isBodyVisible()) {
188 189 190 191
				this.needsRefresh = true;
				return;
			}

I
isidor 已提交
192 193 194 195
			if (!this.onCallStackChangeScheduler.isScheduled()) {
				this.onCallStackChangeScheduler.schedule();
			}
		}));
196
		this.disposables.push(this.debugService.getViewModel().onDidFocusStackFrame(() => {
I
isidor 已提交
197 198 199
			if (this.ignoreFocusStackFrameEvent) {
				return;
			}
200
			if (!this.isBodyVisible()) {
201 202 203 204
				this.needsRefresh = true;
				return;
			}

205
			this.updateTreeSelection();
206
		}));
I
isidor 已提交
207
		this.disposables.push(this.tree.onContextMenu(e => this.onContextMenu(e)));
I
isidor 已提交
208 209 210

		// Schedule the update of the call stack tree if the viewlet is opened after a session started #14684
		if (this.debugService.state === State.Stopped) {
I
isidor 已提交
211
			this.onCallStackChangeScheduler.schedule(0);
I
isidor 已提交
212
		}
213 214 215 216 217 218

		this.disposables.push(this.onDidChangeBodyVisibility(visible => {
			if (visible && this.needsRefresh) {
				this.onCallStackChangeScheduler.schedule();
			}
		}));
I
isidor 已提交
219 220
	}

221 222
	layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
S
Sandeep Somavarapu 已提交
223 224
	}

I
isidor 已提交
225 226 227 228
	focus(): void {
		this.tree.domFocus();
	}

I
isidor 已提交
229
	private updateTreeSelection(): void {
I
isidor 已提交
230
		if (!this.tree || !this.tree.getInput()) {
I
isidor 已提交
231
			// Tree not initialized yet
I
isidor 已提交
232
			return;
I
isidor 已提交
233 234
		}

I
isidor 已提交
235
		const updateSelectionAndReveal = (element: IStackFrame | IDebugSession) => {
236 237 238
			this.ignoreSelectionChangedEvent = true;
			try {
				this.tree.setSelection([element]);
I
isidor 已提交
239 240 241
				this.tree.reveal(element);
			} catch (e) { }
			finally {
242 243 244 245
				this.ignoreSelectionChangedEvent = false;
			}
		};

I
isidor 已提交
246 247 248
		const thread = this.debugService.getViewModel().focusedThread;
		const session = this.debugService.getViewModel().focusedSession;
		const stackFrame = this.debugService.getViewModel().focusedStackFrame;
I
isidor 已提交
249
		if (!thread) {
I
isidor 已提交
250
			if (!session) {
I
isidor 已提交
251 252
				this.tree.setSelection([]);
			} else {
I
isidor 已提交
253
				updateSelectionAndReveal(session);
I
isidor 已提交
254
			}
I
isidor 已提交
255
		} else {
256 257
			const expansionsPromise = ignoreErrors(this.tree.expand(thread.session))
				.then(() => ignoreErrors(this.tree.expand(thread)));
I
isidor 已提交
258
			if (stackFrame) {
I
isidor 已提交
259
				expansionsPromise.then(() => updateSelectionAndReveal(stackFrame));
I
isidor 已提交
260
			}
I
isidor 已提交
261
		}
I
isidor 已提交
262 263
	}

I
isidor 已提交
264 265
	private onContextMenu(e: ITreeContextMenuEvent<CallStackItem>): void {
		const element = e.element;
I
isidor 已提交
266
		if (isDebugSession(element)) {
I
isidor 已提交
267
			this.callStackItemType.set('session');
268
		} else if (element instanceof Thread) {
I
isidor 已提交
269
			this.callStackItemType.set('thread');
270
		} else if (element instanceof StackFrame) {
I
isidor 已提交
271 272 273
			this.callStackItemType.set('stackFrame');
		} else {
			this.callStackItemType.reset();
274 275
		}

I
isidor 已提交
276 277 278
		this.contextMenuService.showContextMenu({
			getAnchor: () => e.anchor,
			getActions: () => {
279 280
				const actions: IAction[] = [];
				fillInContextMenuActions(this.contributedContextMenu, { arg: this.getContextForContributedActions(element), shouldForwardArgs: true }, actions, this.contextMenuService);
I
isidor 已提交
281 282 283 284
				return actions;
			},
			getActionsContext: () => element
		});
285 286
	}

I
isidor 已提交
287
	private getContextForContributedActions(element: CallStackItem | null): string | number | undefined {
I
isidor 已提交
288 289 290 291
		if (element instanceof StackFrame) {
			if (element.source.inMemory) {
				return element.source.raw.path || element.source.reference;
			}
I
isidor 已提交
292

I
isidor 已提交
293
			return element.source.uri.toString();
I
isidor 已提交
294
		}
295
		if (element instanceof Thread) {
I
isidor 已提交
296
			return element.threadId;
297
		}
298 299 300
		if (isDebugSession(element)) {
			return element.getId();
		}
301

I
isidor 已提交
302
		return undefined;
303 304 305 306 307 308 309 310
	}
}

interface IThreadTemplateData {
	thread: HTMLElement;
	name: HTMLElement;
	state: HTMLElement;
	stateLabel: HTMLSpanElement;
I
isidor 已提交
311
	label: HighlightedLabel;
312 313
}

I
isidor 已提交
314 315
interface ISessionTemplateData {
	session: HTMLElement;
316 317 318
	name: HTMLElement;
	state: HTMLElement;
	stateLabel: HTMLSpanElement;
I
isidor 已提交
319
	label: HighlightedLabel;
320 321 322 323 324 325
}

interface IErrorTemplateData {
	label: HTMLElement;
}

I
isidor 已提交
326
interface ILabelTemplateData {
327 328 329 330 331 332 333 334
	label: HTMLElement;
}

interface IStackFrameTemplateData {
	stackFrame: HTMLElement;
	file: HTMLElement;
	fileName: HTMLElement;
	lineNumber: HTMLElement;
I
isidor 已提交
335
	label: HighlightedLabel;
336 337
}

I
isidor 已提交
338
class SessionsRenderer implements ITreeRenderer<IDebugSession, FuzzyScore, ISessionTemplateData> {
I
isidor 已提交
339
	static readonly ID = 'session';
340

I
isidor 已提交
341 342
	get templateId(): string {
		return SessionsRenderer.ID;
343 344
	}

I
isidor 已提交
345
	renderTemplate(container: HTMLElement): ISessionTemplateData {
346 347 348 349 350
		const session = dom.append(container, $('.session'));
		const name = dom.append(session, $('.name'));
		const state = dom.append(session, $('.state'));
		const stateLabel = dom.append(state, $('span.label'));
		const label = new HighlightedLabel(name, false);
351

352
		return { session, name, state, stateLabel, label };
353 354
	}

I
isidor 已提交
355
	renderElement(element: ITreeNode<IDebugSession, FuzzyScore>, index: number, data: ISessionTemplateData): void {
I
isidor 已提交
356 357
		const session = element.element;
		data.session.title = nls.localize({ key: 'session', comment: ['Session is a noun'] }, "Session");
I
isidor 已提交
358
		data.label.set(session.getLabel(), createMatches(element.filterData));
I
isidor 已提交
359
		const stoppedThread = session.getAllThreads().filter(t => t.stopped).pop();
360

I
isidor 已提交
361 362 363
		data.stateLabel.textContent = stoppedThread ? nls.localize('paused', "Paused")
			: nls.localize({ key: 'running', comment: ['indicates state'] }, "Running");
	}
364

I
isidor 已提交
365 366
	disposeTemplate(templateData: ISessionTemplateData): void {
		// noop
367
	}
I
isidor 已提交
368
}
369

I
isidor 已提交
370
class ThreadsRenderer implements ITreeRenderer<IThread, FuzzyScore, IThreadTemplateData> {
I
isidor 已提交
371 372 373 374
	static readonly ID = 'thread';

	get templateId(): string {
		return ThreadsRenderer.ID;
375 376
	}

I
isidor 已提交
377
	renderTemplate(container: HTMLElement): IThreadTemplateData {
378 379 380 381 382
		const thread = dom.append(container, $('.thread'));
		const name = dom.append(thread, $('.name'));
		const state = dom.append(thread, $('.state'));
		const stateLabel = dom.append(state, $('span.label'));
		const label = new HighlightedLabel(name, false);
383

384
		return { thread, name, state, stateLabel, label };
385 386
	}

I
isidor 已提交
387
	renderElement(element: ITreeNode<IThread, FuzzyScore>, index: number, data: IThreadTemplateData): void {
I
isidor 已提交
388
		const thread = element.element;
389
		data.thread.title = nls.localize('thread', "Thread");
I
isidor 已提交
390
		data.label.set(thread.name, createMatches(element.filterData));
391
		data.stateLabel.textContent = thread.stateLabel;
392 393
	}

I
isidor 已提交
394 395
	disposeTemplate(templateData: IThreadTemplateData): void {
		// noop
396
	}
I
isidor 已提交
397
}
398

I
isidor 已提交
399
class StackFramesRenderer implements ITreeRenderer<IStackFrame, FuzzyScore, IStackFrameTemplateData> {
I
isidor 已提交
400 401
	static readonly ID = 'stackFrame';

402
	constructor(@ILabelService private readonly labelService: ILabelService) { }
I
isidor 已提交
403 404 405 406 407 408

	get templateId(): string {
		return StackFramesRenderer.ID;
	}

	renderTemplate(container: HTMLElement): IStackFrameTemplateData {
409 410 411 412 413 414 415
		const stackFrame = dom.append(container, $('.stack-frame'));
		const labelDiv = dom.append(stackFrame, $('span.label.expression'));
		const file = dom.append(stackFrame, $('.file'));
		const fileName = dom.append(file, $('span.file-name'));
		const wrapper = dom.append(file, $('span.line-number-wrapper'));
		const lineNumber = dom.append(wrapper, $('span.line-number'));
		const label = new HighlightedLabel(labelDiv, false);
I
isidor 已提交
416

417
		return { file, fileName, label, lineNumber, stackFrame };
I
isidor 已提交
418 419
	}

I
isidor 已提交
420
	renderElement(element: ITreeNode<IStackFrame, FuzzyScore>, index: number, data: IStackFrameTemplateData): void {
I
isidor 已提交
421
		const stackFrame = element.element;
422
		dom.toggleClass(data.stackFrame, 'disabled', !stackFrame.source || !stackFrame.source.available || isDeemphasized(stackFrame));
423 424 425
		dom.toggleClass(data.stackFrame, 'label', stackFrame.presentationHint === 'label');
		dom.toggleClass(data.stackFrame, 'subtle', stackFrame.presentationHint === 'subtle');

I
isidor 已提交
426
		data.file.title = stackFrame.source.inMemory ? stackFrame.source.uri.path : this.labelService.getUriLabel(stackFrame.source.uri);
427 428 429
		if (stackFrame.source.raw.origin) {
			data.file.title += `\n${stackFrame.source.raw.origin}`;
		}
I
isidor 已提交
430
		data.label.set(stackFrame.name, createMatches(element.filterData), stackFrame.name);
I
isidor 已提交
431
		data.fileName.textContent = stackFrame.getSpecificSourceName();
432 433 434 435 436 437 438 439 440 441 442
		if (stackFrame.range.startLineNumber !== undefined) {
			data.lineNumber.textContent = `${stackFrame.range.startLineNumber}`;
			if (stackFrame.range.startColumn) {
				data.lineNumber.textContent += `:${stackFrame.range.startColumn}`;
			}
			dom.removeClass(data.lineNumber, 'unavailable');
		} else {
			dom.addClass(data.lineNumber, 'unavailable');
		}
	}

I
isidor 已提交
443 444 445 446 447
	disposeTemplate(templateData: IStackFrameTemplateData): void {
		// noop
	}
}

I
isidor 已提交
448
class ErrorsRenderer implements ITreeRenderer<string, FuzzyScore, IErrorTemplateData> {
I
isidor 已提交
449 450 451 452 453 454 455
	static readonly ID = 'error';

	get templateId(): string {
		return ErrorsRenderer.ID;
	}

	renderTemplate(container: HTMLElement): IErrorTemplateData {
456
		const label = dom.append(container, $('.error'));
I
isidor 已提交
457

458
		return { label };
I
isidor 已提交
459 460
	}

I
isidor 已提交
461
	renderElement(element: ITreeNode<string, FuzzyScore>, index: number, data: IErrorTemplateData): void {
I
isidor 已提交
462 463 464 465 466 467 468 469 470 471
		const error = element.element;
		data.label.textContent = error;
		data.label.title = error;
	}

	disposeTemplate(templateData: IErrorTemplateData): void {
		// noop
	}
}

I
isidor 已提交
472
class LoadMoreRenderer implements ITreeRenderer<ThreadAndSessionIds, FuzzyScore, ILabelTemplateData> {
I
isidor 已提交
473
	static readonly ID = 'loadMore';
I
isidor 已提交
474
	static readonly LABEL = nls.localize('loadMoreStackFrames', "Load More Stack Frames");
I
isidor 已提交
475 476 477 478 479 480

	get templateId(): string {
		return LoadMoreRenderer.ID;
	}

	renderTemplate(container: HTMLElement): IErrorTemplateData {
481
		const label = dom.append(container, $('.load-more'));
I
isidor 已提交
482

483
		return { label };
I
isidor 已提交
484 485
	}

I
isidor 已提交
486
	renderElement(element: ITreeNode<ThreadAndSessionIds, FuzzyScore>, index: number, data: ILabelTemplateData): void {
I
isidor 已提交
487
		data.label.textContent = LoadMoreRenderer.LABEL;
I
isidor 已提交
488 489 490 491 492 493 494
	}

	disposeTemplate(templateData: ILabelTemplateData): void {
		// noop
	}
}

I
isidor 已提交
495
class ShowMoreRenderer implements ITreeRenderer<IStackFrame[], FuzzyScore, ILabelTemplateData> {
I
isidor 已提交
496 497 498 499 500 501 502
	static readonly ID = 'showMore';

	get templateId(): string {
		return ShowMoreRenderer.ID;
	}

	renderTemplate(container: HTMLElement): IErrorTemplateData {
503
		const label = dom.append(container, $('.show-more'));
I
isidor 已提交
504

505
		return { label };
I
isidor 已提交
506 507
	}

I
isidor 已提交
508
	renderElement(element: ITreeNode<IStackFrame[], FuzzyScore>, index: number, data: ILabelTemplateData): void {
I
isidor 已提交
509
		const stackFrames = element.element;
510
		if (stackFrames.every(sf => !!(sf.source && sf.source.origin && sf.source.origin === stackFrames[0].source.origin))) {
I
isidor 已提交
511 512 513 514 515 516 517
			data.label.textContent = nls.localize('showMoreAndOrigin', "Show {0} More: {1}", stackFrames.length, stackFrames[0].source.origin);
		} else {
			data.label.textContent = nls.localize('showMoreStackFrames', "Show {0} More Stack Frames", stackFrames.length);
		}
	}

	disposeTemplate(templateData: ILabelTemplateData): void {
518 519 520 521
		// noop
	}
}

I
isidor 已提交
522 523 524 525 526 527 528
class CallStackDelegate implements IListVirtualDelegate<CallStackItem> {

	getHeight(element: CallStackItem): number {
		return 22;
	}

	getTemplateId(element: CallStackItem): string {
I
isidor 已提交
529
		if (isDebugSession(element)) {
I
isidor 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
			return SessionsRenderer.ID;
		}
		if (element instanceof Thread) {
			return ThreadsRenderer.ID;
		}
		if (element instanceof StackFrame) {
			return StackFramesRenderer.ID;
		}
		if (typeof element === 'string') {
			return ErrorsRenderer.ID;
		}
		if (element instanceof ThreadAndSessionIds) {
			return LoadMoreRenderer.ID;
		}

I
isidor 已提交
545 546
		// element instanceof Array
		return ShowMoreRenderer.ID;
I
isidor 已提交
547 548 549
	}
}

J
Joao Moreno 已提交
550 551 552
function isDebugModel(obj: any): obj is IDebugModel {
	return typeof obj.getSessions === 'function';
}
I
isidor 已提交
553

I
isidor 已提交
554 555 556 557
function isDebugSession(obj: any): obj is IDebugSession {
	return typeof obj.getAllThreads === 'function';
}

558 559 560 561
function isDeemphasized(frame: IStackFrame): boolean {
	return frame.source.presentationHint === 'deemphasize' || frame.presentationHint === 'deemphasize';
}

J
Joao Moreno 已提交
562 563
class CallStackDataSource implements IAsyncDataSource<IDebugModel, CallStackItem> {
	deemphasizedStackFramesToShow: IStackFrame[];
I
isidor 已提交
564

J
Joao Moreno 已提交
565
	hasChildren(element: IDebugModel | CallStackItem): boolean {
I
isidor 已提交
566
		return isDebugModel(element) || isDebugSession(element) || (element instanceof Thread && element.stopped);
I
isidor 已提交
567 568
	}

J
Joao Moreno 已提交
569 570 571
	getChildren(element: IDebugModel | CallStackItem): Promise<CallStackItem[]> {
		if (isDebugModel(element)) {
			const sessions = element.getSessions();
572 573 574 575
			if (sessions.length === 0) {
				return Promise.resolve([]);
			}
			if (sessions.length > 1) {
I
isidor 已提交
576 577 578 579 580 581
				return Promise.resolve(sessions);
			}

			const threads = sessions[0].getAllThreads();
			// Only show the threads in the call stack if there is more than 1 thread.
			return threads.length === 1 ? this.getThreadChildren(<Thread>threads[0]) : Promise.resolve(threads);
I
isidor 已提交
582
		} else if (isDebugSession(element)) {
I
isidor 已提交
583
			return Promise.resolve(element.getAllThreads());
J
Joao Moreno 已提交
584 585
		} else {
			return this.getThreadChildren(<Thread>element);
I
isidor 已提交
586 587 588
		}
	}

I
isidor 已提交
589
	private getThreadChildren(thread: Thread): Promise<CallStackItem[]> {
I
isidor 已提交
590 591
		return this.getThreadCallstack(thread).then(children => {
			// Check if some stack frames should be hidden under a parent element since they are deemphasized
I
isidor 已提交
592
			const result: CallStackItem[] = [];
I
isidor 已提交
593
			children.forEach((child, index) => {
594
				if (child instanceof StackFrame && child.source && isDeemphasized(child)) {
I
isidor 已提交
595 596
					// Check if the user clicked to show the deemphasized source
					if (this.deemphasizedStackFramesToShow.indexOf(child) === -1) {
I
isidor 已提交
597 598 599 600 601 602 603
						if (result.length) {
							const last = result[result.length - 1];
							if (last instanceof Array) {
								// Collect all the stackframes that will be "collapsed"
								last.push(child);
								return;
							}
I
isidor 已提交
604 605 606
						}

						const nextChild = index < children.length - 1 ? children[index + 1] : undefined;
607
						if (nextChild instanceof StackFrame && nextChild.source && isDeemphasized(nextChild)) {
I
isidor 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621
							// Start collecting stackframes that will be "collapsed"
							result.push([child]);
							return;
						}
					}
				}

				result.push(child);
			});

			return result;
		});
	}

622
	private getThreadCallstack(thread: Thread): Promise<Array<IStackFrame | string | ThreadAndSessionIds>> {
I
isidor 已提交
623 624 625 626 627 628 629
		let callStack: any[] = thread.getCallStack();
		let callStackPromise: Promise<any> = Promise.resolve(null);
		if (!callStack || !callStack.length) {
			callStackPromise = thread.fetchCallStack().then(() => callStack = thread.getCallStack());
		}

		return callStackPromise.then(() => {
I
isidor 已提交
630
			if (callStack.length === 1 && thread.session.capabilities.supportsDelayedStackTraceLoading && thread.stoppedDetails && thread.stoppedDetails.totalFrames && thread.stoppedDetails.totalFrames > 1) {
I
isidor 已提交
631 632 633 634 635 636 637 638
				// To reduce flashing of the call stack view simply append the stale call stack
				// once we have the correct data the tree will refresh and we will no longer display it.
				callStack = callStack.concat(thread.getStaleCallStack().slice(1));
			}

			if (thread.stoppedDetails && thread.stoppedDetails.framesErrorMessage) {
				callStack = callStack.concat([thread.stoppedDetails.framesErrorMessage]);
			}
I
isidor 已提交
639
			if (thread.stoppedDetails && thread.stoppedDetails.totalFrames && thread.stoppedDetails.totalFrames > callStack.length && callStack.length > 1) {
I
isidor 已提交
640 641 642 643 644 645 646 647 648 649
				callStack = callStack.concat([new ThreadAndSessionIds(thread.session.getId(), thread.threadId)]);
			}

			return callStack;
		});
	}
}

class CallStackAccessibilityProvider implements IAccessibilityProvider<CallStackItem> {
	getAriaLabel(element: CallStackItem): string {
650 651 652 653
		if (element instanceof Thread) {
			return nls.localize('threadAriaLabel', "Thread {0}, callstack, debug", (<Thread>element).name);
		}
		if (element instanceof StackFrame) {
I
isidor 已提交
654
			return nls.localize('stackFrameAriaLabel', "Stack Frame {0} line {1} {2}, callstack, debug", element.name, element.range.startLineNumber, element.getSpecificSourceName());
655
		}
I
isidor 已提交
656
		if (isDebugSession(element)) {
I
isidor 已提交
657 658 659 660 661 662 663 664
			return nls.localize('sessionLabel', "Debug Session {0}", element.getLabel());
		}
		if (typeof element === 'string') {
			return element;
		}
		if (element instanceof Array) {
			return nls.localize('showMoreStackFrames', "Show {0} More Stack Frames", element.length);
		}
665

I
isidor 已提交
666 667
		// element instanceof ThreadAndSessionIds
		return nls.localize('loadMoreStackFrames', "Load More Stack Frames");
668 669
	}
}