callStackView.ts 25.2 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';
I
isidor 已提交
7
import { RunOnceScheduler, ignoreErrors, sequence } 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
import { IAccessibilityProvider } from 'vs/base/browser/ui/list/listWidget';
24
import { createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
I
isidor 已提交
25
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
import { Event } from 'vs/base/common/event';
32
import { dispose } from 'vs/base/common/lifecycle';
I
isidor 已提交
33

34
const $ = dom.$;
I
isidor 已提交
35

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

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

I
isidor 已提交
40 41
	private pauseMessage!: HTMLSpanElement;
	private pauseMessageLabel!: HTMLSpanElement;
I
isidor 已提交
42
	private onCallStackChangeScheduler: RunOnceScheduler;
I
isidor 已提交
43 44 45
	private needsRefresh = false;
	private ignoreSelectionChangedEvent = false;
	private ignoreFocusStackFrameEvent = false;
46
	private callStackItemType: IContextKey<string>;
I
isidor 已提交
47 48
	private dataSource!: CallStackDataSource;
	private tree!: WorkbenchAsyncDataTree<CallStackItem | IDebugModel, CallStackItem, FuzzyScore>;
I
isidor 已提交
49
	private contributedContextMenu: IMenu;
I
isidor 已提交
50
	private parentSessionToExpand = new Set<IDebugSession>();
I
isidor 已提交
51 52 53 54

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

I
isidor 已提交
66
		this.contributedContextMenu = menuService.createMenu(MenuId.DebugCallStackContext, contextKeyService);
M
Matt Bierner 已提交
67
		this._register(this.contributedContextMenu);
I
isidor 已提交
68

I
isidor 已提交
69 70 71 72
		// 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 已提交
73
			const sessions = this.debugService.getModel().getSessions();
I
isidor 已提交
74 75
			const thread = sessions.length === 1 && sessions[0].getAllThreads().length === 1 ? sessions[0].getAllThreads()[0] : undefined;
			if (thread && thread.stoppedDetails) {
I
isidor 已提交
76 77
				this.pauseMessageLabel.textContent = thread.stoppedDetails.description || nls.localize('debugStopped', "Paused on {0}", thread.stoppedDetails.reason || '');
				this.pauseMessageLabel.title = thread.stoppedDetails.text || '';
I
isidor 已提交
78
				dom.toggleClass(this.pauseMessageLabel, 'exception', thread.stoppedDetails.reason === 'exception');
79
				this.pauseMessage.hidden = false;
I
isidor 已提交
80
			} else {
81
				this.pauseMessage.hidden = true;
I
isidor 已提交
82 83
			}

84
			this.needsRefresh = false;
I
isidor 已提交
85
			this.dataSource.deemphasizedStackFramesToShow = [];
I
isidor 已提交
86 87 88 89 90
			this.tree.updateChildren().then(() => {
				this.parentSessionToExpand.forEach(s => this.tree.expand(s));
				this.parentSessionToExpand.clear();
				this.updateTreeSelection();
			});
I
isidor 已提交
91 92 93
		}, 50);
	}

J
Jackson Kearl 已提交
94
	protected renderHeaderTitle(container: HTMLElement): void {
I
isidor 已提交
95
		const titleContainer = dom.append(container, $('.debug-call-stack-title'));
J
Jackson Kearl 已提交
96 97 98
		super.renderHeaderTitle(titleContainer, this.options.title);

		this.pauseMessage = dom.append(titleContainer, $('span.pause-message'));
99 100
		this.pauseMessage.hidden = true;
		this.pauseMessageLabel = dom.append(this.pauseMessage, $('span.label'));
I
isidor 已提交
101 102
	}

I
isidor 已提交
103
	renderBody(container: HTMLElement): void {
I
isidor 已提交
104
		dom.addClass(container, 'debug-call-stack');
I
isidor 已提交
105 106
		const treeContainer = renderViewTree(container);

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

I
isidor 已提交
127
						return element.getId();
128
					}
I
isidor 已提交
129
				},
J
Joao Moreno 已提交
130
				keyboardNavigationLabelProvider: {
I
isidor 已提交
131
					getKeyboardNavigationLabel: (e: CallStackItem) => {
I
isidor 已提交
132
						if (isDebugSession(e)) {
I
isidor 已提交
133 134 135
							return e.getLabel();
						}
						if (e instanceof Thread) {
136
							return `${e.name} ${e.stateLabel}`;
I
isidor 已提交
137 138 139 140 141 142 143 144 145 146
						}
						if (e instanceof StackFrame || typeof e === 'string') {
							return e;
						}
						if (e instanceof ThreadAndSessionIds) {
							return LoadMoreRenderer.LABEL;
						}

						return nls.localize('showMoreStackFrames2', "Show More Stack Frames");
					}
147
				}
148
			});
I
isidor 已提交
149

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

I
isidor 已提交
152
		const callstackNavigator = new TreeResourceNavigator2(this.tree);
M
Matt Bierner 已提交
153 154
		this._register(callstackNavigator);
		this._register(callstackNavigator.onDidOpenResource(e => {
I
isidor 已提交
155 156 157 158
			if (this.ignoreSelectionChangedEvent) {
				return;
			}

I
isidor 已提交
159
			const focusStackFrame = (stackFrame: IStackFrame | undefined, thread: IThread | undefined, session: IDebugSession) => {
I
isidor 已提交
160 161 162 163 164 165 166 167
				this.ignoreFocusStackFrameEvent = true;
				try {
					this.debugService.focusStackFrame(stackFrame, thread, session, true);
				} finally {
					this.ignoreFocusStackFrameEvent = false;
				}
			};

I
isidor 已提交
168 169
			const element = e.element;
			if (element instanceof StackFrame) {
I
isidor 已提交
170
				focusStackFrame(element, element.thread, element.thread.session);
I
isidor 已提交
171 172 173
				element.openInEditor(this.editorService, e.editorOptions.preserveFocus, e.sideBySide, e.editorOptions.pinned);
			}
			if (element instanceof Thread) {
I
isidor 已提交
174
				focusStackFrame(undefined, element, element.session);
I
isidor 已提交
175
			}
I
isidor 已提交
176
			if (isDebugSession(element)) {
I
isidor 已提交
177
				focusStackFrame(undefined, undefined, element);
I
isidor 已提交
178 179
			}
			if (element instanceof ThreadAndSessionIds) {
180
				const session = this.debugService.getModel().getSession(element.sessionId);
I
isidor 已提交
181 182 183
				const thread = session && session.getThread(element.threadId);
				if (thread) {
					(<Thread>thread).fetchCallStack()
184
						.then(() => this.tree.updateChildren());
I
isidor 已提交
185 186 187 188
				}
			}
			if (element instanceof Array) {
				this.dataSource.deemphasizedStackFramesToShow.push(...element);
189
				this.tree.updateChildren();
I
isidor 已提交
190 191
			}
		}));
I
isidor 已提交
192

M
Matt Bierner 已提交
193
		this._register(this.debugService.getModel().onDidChangeCallStack(() => {
194
			if (!this.isBodyVisible()) {
195 196 197 198
				this.needsRefresh = true;
				return;
			}

I
isidor 已提交
199 200 201 202
			if (!this.onCallStackChangeScheduler.isScheduled()) {
				this.onCallStackChangeScheduler.schedule();
			}
		}));
I
isidor 已提交
203
		const onCallStackChange = Event.any<any>(this.debugService.getViewModel().onDidFocusStackFrame, this.debugService.getViewModel().onDidFocusSession);
M
Matt Bierner 已提交
204
		this._register(onCallStackChange(() => {
I
isidor 已提交
205 206 207
			if (this.ignoreFocusStackFrameEvent) {
				return;
			}
208
			if (!this.isBodyVisible()) {
209 210 211 212
				this.needsRefresh = true;
				return;
			}

213
			this.updateTreeSelection();
214
		}));
M
Matt Bierner 已提交
215
		this._register(this.tree.onContextMenu(e => this.onContextMenu(e)));
I
isidor 已提交
216 217 218

		// 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 已提交
219
			this.onCallStackChangeScheduler.schedule(0);
I
isidor 已提交
220
		}
221

M
Matt Bierner 已提交
222
		this._register(this.onDidChangeBodyVisibility(visible => {
223 224 225 226
			if (visible && this.needsRefresh) {
				this.onCallStackChangeScheduler.schedule();
			}
		}));
I
isidor 已提交
227

M
Matt Bierner 已提交
228
		this._register(this.debugService.onDidNewSession(s => {
I
isidor 已提交
229 230 231 232 233
			if (s.parentSession) {
				// Auto expand sessions that have sub sessions
				this.parentSessionToExpand.add(s.parentSession);
			}
		}));
I
isidor 已提交
234 235
	}

236 237
	layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
S
Sandeep Somavarapu 已提交
238 239
	}

I
isidor 已提交
240 241 242 243
	focus(): void {
		this.tree.domFocus();
	}

I
isidor 已提交
244
	private updateTreeSelection(): void {
I
isidor 已提交
245
		if (!this.tree || !this.tree.getInput()) {
I
isidor 已提交
246
			// Tree not initialized yet
I
isidor 已提交
247
			return;
I
isidor 已提交
248 249
		}

I
isidor 已提交
250
		const updateSelectionAndReveal = (element: IStackFrame | IDebugSession) => {
251 252 253
			this.ignoreSelectionChangedEvent = true;
			try {
				this.tree.setSelection([element]);
I
isidor 已提交
254 255 256
				this.tree.reveal(element);
			} catch (e) { }
			finally {
257 258 259 260
				this.ignoreSelectionChangedEvent = false;
			}
		};

I
isidor 已提交
261 262 263
		const thread = this.debugService.getViewModel().focusedThread;
		const session = this.debugService.getViewModel().focusedSession;
		const stackFrame = this.debugService.getViewModel().focusedStackFrame;
I
isidor 已提交
264
		if (!thread) {
I
isidor 已提交
265
			if (!session) {
I
isidor 已提交
266 267
				this.tree.setSelection([]);
			} else {
I
isidor 已提交
268
				updateSelectionAndReveal(session);
I
isidor 已提交
269
			}
I
isidor 已提交
270
		} else {
I
isidor 已提交
271 272 273 274 275 276
			const expandPromises = [() => ignoreErrors(this.tree.expand(thread))];
			let s: IDebugSession | undefined = thread.session;
			while (s) {
				const sessionToExpand = s;
				expandPromises.push(() => ignoreErrors(this.tree.expand(sessionToExpand)));
				s = s.parentSession;
I
isidor 已提交
277
			}
I
isidor 已提交
278 279 280 281 282 283 284

			sequence(expandPromises.reverse()).then(() => {
				const toReveal = stackFrame || session;
				if (toReveal) {
					updateSelectionAndReveal(toReveal);
				}
			});
I
isidor 已提交
285
		}
I
isidor 已提交
286 287
	}

I
isidor 已提交
288 289
	private onContextMenu(e: ITreeContextMenuEvent<CallStackItem>): void {
		const element = e.element;
I
isidor 已提交
290
		if (isDebugSession(element)) {
I
isidor 已提交
291
			this.callStackItemType.set('session');
292
		} else if (element instanceof Thread) {
I
isidor 已提交
293
			this.callStackItemType.set('thread');
294
		} else if (element instanceof StackFrame) {
I
isidor 已提交
295 296 297
			this.callStackItemType.set('stackFrame');
		} else {
			this.callStackItemType.reset();
298 299
		}

300 301 302
		const actions: IAction[] = [];
		const actionsDisposable = createAndFillInContextMenuActions(this.contributedContextMenu, { arg: this.getContextForContributedActions(element), shouldForwardArgs: true }, actions, this.contextMenuService);

I
isidor 已提交
303 304
		this.contextMenuService.showContextMenu({
			getAnchor: () => e.anchor,
305 306 307
			getActions: () => actions,
			getActionsContext: () => element,
			onHide: () => dispose(actionsDisposable)
I
isidor 已提交
308
		});
309 310
	}

I
isidor 已提交
311
	private getContextForContributedActions(element: CallStackItem | null): string | number | undefined {
I
isidor 已提交
312 313 314 315
		if (element instanceof StackFrame) {
			if (element.source.inMemory) {
				return element.source.raw.path || element.source.reference;
			}
I
isidor 已提交
316

I
isidor 已提交
317
			return element.source.uri.toString();
I
isidor 已提交
318
		}
319
		if (element instanceof Thread) {
I
isidor 已提交
320
			return element.threadId;
321
		}
322 323 324
		if (isDebugSession(element)) {
			return element.getId();
		}
325

I
isidor 已提交
326
		return undefined;
327 328 329 330 331 332 333 334
	}
}

interface IThreadTemplateData {
	thread: HTMLElement;
	name: HTMLElement;
	state: HTMLElement;
	stateLabel: HTMLSpanElement;
I
isidor 已提交
335
	label: HighlightedLabel;
336 337
}

I
isidor 已提交
338 339
interface ISessionTemplateData {
	session: HTMLElement;
340 341 342
	name: HTMLElement;
	state: HTMLElement;
	stateLabel: HTMLSpanElement;
I
isidor 已提交
343
	label: HighlightedLabel;
344 345 346 347 348 349
}

interface IErrorTemplateData {
	label: HTMLElement;
}

I
isidor 已提交
350
interface ILabelTemplateData {
351 352 353 354 355 356 357 358
	label: HTMLElement;
}

interface IStackFrameTemplateData {
	stackFrame: HTMLElement;
	file: HTMLElement;
	fileName: HTMLElement;
	lineNumber: HTMLElement;
I
isidor 已提交
359
	label: HighlightedLabel;
360 361
}

I
isidor 已提交
362
class SessionsRenderer implements ITreeRenderer<IDebugSession, FuzzyScore, ISessionTemplateData> {
I
isidor 已提交
363
	static readonly ID = 'session';
364

I
isidor 已提交
365 366
	get templateId(): string {
		return SessionsRenderer.ID;
367 368
	}

I
isidor 已提交
369
	renderTemplate(container: HTMLElement): ISessionTemplateData {
370 371 372 373 374
		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);
375

376
		return { session, name, state, stateLabel, label };
377 378
	}

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

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

I
isidor 已提交
389 390
	disposeTemplate(templateData: ISessionTemplateData): void {
		// noop
391
	}
I
isidor 已提交
392
}
393

I
isidor 已提交
394
class ThreadsRenderer implements ITreeRenderer<IThread, FuzzyScore, IThreadTemplateData> {
I
isidor 已提交
395 396 397 398
	static readonly ID = 'thread';

	get templateId(): string {
		return ThreadsRenderer.ID;
399 400
	}

I
isidor 已提交
401
	renderTemplate(container: HTMLElement): IThreadTemplateData {
402 403 404 405 406
		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);
407

408
		return { thread, name, state, stateLabel, label };
409 410
	}

I
isidor 已提交
411
	renderElement(element: ITreeNode<IThread, FuzzyScore>, index: number, data: IThreadTemplateData): void {
I
isidor 已提交
412
		const thread = element.element;
413
		data.thread.title = nls.localize('thread', "Thread");
I
isidor 已提交
414
		data.label.set(thread.name, createMatches(element.filterData));
415
		data.stateLabel.textContent = thread.stateLabel;
416 417
	}

I
isidor 已提交
418 419
	disposeTemplate(templateData: IThreadTemplateData): void {
		// noop
420
	}
I
isidor 已提交
421
}
422

I
isidor 已提交
423
class StackFramesRenderer implements ITreeRenderer<IStackFrame, FuzzyScore, IStackFrameTemplateData> {
I
isidor 已提交
424 425
	static readonly ID = 'stackFrame';

426
	constructor(@ILabelService private readonly labelService: ILabelService) { }
I
isidor 已提交
427 428 429 430 431 432

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

	renderTemplate(container: HTMLElement): IStackFrameTemplateData {
433 434 435 436 437 438 439
		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 已提交
440

441
		return { file, fileName, label, lineNumber, stackFrame };
I
isidor 已提交
442 443
	}

I
isidor 已提交
444
	renderElement(element: ITreeNode<IStackFrame, FuzzyScore>, index: number, data: IStackFrameTemplateData): void {
I
isidor 已提交
445
		const stackFrame = element.element;
446
		dom.toggleClass(data.stackFrame, 'disabled', !stackFrame.source || !stackFrame.source.available || isDeemphasized(stackFrame));
447 448 449
		dom.toggleClass(data.stackFrame, 'label', stackFrame.presentationHint === 'label');
		dom.toggleClass(data.stackFrame, 'subtle', stackFrame.presentationHint === 'subtle');

I
isidor 已提交
450
		data.file.title = stackFrame.source.inMemory ? stackFrame.source.uri.path : this.labelService.getUriLabel(stackFrame.source.uri);
451 452 453
		if (stackFrame.source.raw.origin) {
			data.file.title += `\n${stackFrame.source.raw.origin}`;
		}
I
isidor 已提交
454
		data.label.set(stackFrame.name, createMatches(element.filterData), stackFrame.name);
I
isidor 已提交
455
		data.fileName.textContent = stackFrame.getSpecificSourceName();
456 457 458 459 460 461 462 463 464 465 466
		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 已提交
467 468 469 470 471
	disposeTemplate(templateData: IStackFrameTemplateData): void {
		// noop
	}
}

I
isidor 已提交
472
class ErrorsRenderer implements ITreeRenderer<string, FuzzyScore, IErrorTemplateData> {
I
isidor 已提交
473 474 475 476 477 478 479
	static readonly ID = 'error';

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

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

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

I
isidor 已提交
485
	renderElement(element: ITreeNode<string, FuzzyScore>, index: number, data: IErrorTemplateData): void {
I
isidor 已提交
486 487 488 489 490 491 492 493 494 495
		const error = element.element;
		data.label.textContent = error;
		data.label.title = error;
	}

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

I
isidor 已提交
496
class LoadMoreRenderer implements ITreeRenderer<ThreadAndSessionIds, FuzzyScore, ILabelTemplateData> {
I
isidor 已提交
497
	static readonly ID = 'loadMore';
I
isidor 已提交
498
	static readonly LABEL = nls.localize('loadMoreStackFrames', "Load More Stack Frames");
I
isidor 已提交
499 500 501 502 503 504

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

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

507
		return { label };
I
isidor 已提交
508 509
	}

I
isidor 已提交
510
	renderElement(element: ITreeNode<ThreadAndSessionIds, FuzzyScore>, index: number, data: ILabelTemplateData): void {
I
isidor 已提交
511
		data.label.textContent = LoadMoreRenderer.LABEL;
I
isidor 已提交
512 513 514 515 516 517 518
	}

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

I
isidor 已提交
519
class ShowMoreRenderer implements ITreeRenderer<IStackFrame[], FuzzyScore, ILabelTemplateData> {
I
isidor 已提交
520 521 522 523 524 525 526
	static readonly ID = 'showMore';

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

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

529
		return { label };
I
isidor 已提交
530 531
	}

I
isidor 已提交
532
	renderElement(element: ITreeNode<IStackFrame[], FuzzyScore>, index: number, data: ILabelTemplateData): void {
I
isidor 已提交
533
		const stackFrames = element.element;
534
		if (stackFrames.every(sf => !!(sf.source && sf.source.origin && sf.source.origin === stackFrames[0].source.origin))) {
I
isidor 已提交
535 536 537 538 539 540 541
			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 {
542 543 544 545
		// noop
	}
}

I
isidor 已提交
546 547 548 549 550 551 552
class CallStackDelegate implements IListVirtualDelegate<CallStackItem> {

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

	getTemplateId(element: CallStackItem): string {
I
isidor 已提交
553
		if (isDebugSession(element)) {
I
isidor 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
			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 已提交
569 570
		// element instanceof Array
		return ShowMoreRenderer.ID;
I
isidor 已提交
571 572 573
	}
}

J
Joao Moreno 已提交
574 575 576
function isDebugModel(obj: any): obj is IDebugModel {
	return typeof obj.getSessions === 'function';
}
I
isidor 已提交
577

I
isidor 已提交
578 579 580 581
function isDebugSession(obj: any): obj is IDebugSession {
	return typeof obj.getAllThreads === 'function';
}

582 583 584 585
function isDeemphasized(frame: IStackFrame): boolean {
	return frame.source.presentationHint === 'deemphasize' || frame.presentationHint === 'deemphasize';
}

J
Joao Moreno 已提交
586
class CallStackDataSource implements IAsyncDataSource<IDebugModel, CallStackItem> {
I
isidor 已提交
587
	deemphasizedStackFramesToShow: IStackFrame[] = [];
I
isidor 已提交
588

I
isidor 已提交
589 590
	constructor(private debugService: IDebugService) { }

J
Joao Moreno 已提交
591
	hasChildren(element: IDebugModel | CallStackItem): boolean {
I
isidor 已提交
592
		return isDebugModel(element) || isDebugSession(element) || (element instanceof Thread && element.stopped);
I
isidor 已提交
593 594
	}

J
Joao Moreno 已提交
595 596 597
	getChildren(element: IDebugModel | CallStackItem): Promise<CallStackItem[]> {
		if (isDebugModel(element)) {
			const sessions = element.getSessions();
598 599 600
			if (sessions.length === 0) {
				return Promise.resolve([]);
			}
I
isidor 已提交
601
			if (sessions.length > 1 || this.debugService.getViewModel().isMultiSessionView()) {
I
isidor 已提交
602
				return Promise.resolve(sessions.filter(s => !s.parentSession));
I
isidor 已提交
603 604 605 606 607
			}

			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 已提交
608
		} else if (isDebugSession(element)) {
I
isidor 已提交
609
			const childSessions = this.debugService.getModel().getSessions().filter(s => s.parentSession === element);
I
isidor 已提交
610
			const threads: CallStackItem[] = element.getAllThreads();
I
isidor 已提交
611

I
isidor 已提交
612
			return Promise.resolve(threads.concat(childSessions));
J
Joao Moreno 已提交
613 614
		} else {
			return this.getThreadChildren(<Thread>element);
I
isidor 已提交
615 616 617
		}
	}

I
isidor 已提交
618
	private getThreadChildren(thread: Thread): Promise<CallStackItem[]> {
I
isidor 已提交
619 620
		return this.getThreadCallstack(thread).then(children => {
			// Check if some stack frames should be hidden under a parent element since they are deemphasized
I
isidor 已提交
621
			const result: CallStackItem[] = [];
I
isidor 已提交
622
			children.forEach((child, index) => {
623
				if (child instanceof StackFrame && child.source && isDeemphasized(child)) {
I
isidor 已提交
624 625
					// Check if the user clicked to show the deemphasized source
					if (this.deemphasizedStackFramesToShow.indexOf(child) === -1) {
I
isidor 已提交
626 627 628 629 630 631 632
						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 已提交
633 634 635
						}

						const nextChild = index < children.length - 1 ? children[index + 1] : undefined;
636
						if (nextChild instanceof StackFrame && nextChild.source && isDeemphasized(nextChild)) {
I
isidor 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650
							// Start collecting stackframes that will be "collapsed"
							result.push([child]);
							return;
						}
					}
				}

				result.push(child);
			});

			return result;
		});
	}

651
	private getThreadCallstack(thread: Thread): Promise<Array<IStackFrame | string | ThreadAndSessionIds>> {
I
isidor 已提交
652 653 654 655 656 657 658
		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 已提交
659
			if (callStack.length === 1 && thread.session.capabilities.supportsDelayedStackTraceLoading && thread.stoppedDetails && thread.stoppedDetails.totalFrames && thread.stoppedDetails.totalFrames > 1) {
I
isidor 已提交
660 661 662 663 664 665 666 667
				// 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 已提交
668
			if (thread.stoppedDetails && thread.stoppedDetails.totalFrames && thread.stoppedDetails.totalFrames > callStack.length && callStack.length > 1) {
I
isidor 已提交
669 670 671 672 673 674 675 676 677 678
				callStack = callStack.concat([new ThreadAndSessionIds(thread.session.getId(), thread.threadId)]);
			}

			return callStack;
		});
	}
}

class CallStackAccessibilityProvider implements IAccessibilityProvider<CallStackItem> {
	getAriaLabel(element: CallStackItem): string {
679 680 681 682
		if (element instanceof Thread) {
			return nls.localize('threadAriaLabel', "Thread {0}, callstack, debug", (<Thread>element).name);
		}
		if (element instanceof StackFrame) {
I
isidor 已提交
683
			return nls.localize('stackFrameAriaLabel', "Stack Frame {0} line {1} {2}, callstack, debug", element.name, element.range.startLineNumber, element.getSpecificSourceName());
684
		}
I
isidor 已提交
685
		if (isDebugSession(element)) {
I
isidor 已提交
686 687 688 689 690 691 692 693
			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);
		}
694

I
isidor 已提交
695 696
		// element instanceof ThreadAndSessionIds
		return nls.localize('loadMoreStackFrames', "Load More Stack Frames");
697 698
	}
}