debugCommands.ts 17.4 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
7 8
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { List } from 'vs/base/browser/ui/list/listWidget';
9
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
10
import { IListService } from 'vs/platform/list/browser/listService';
11
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
I
isidor 已提交
12
import { IDebugService, IEnablement, CONTEXT_BREAKPOINTS_FOCUSED, CONTEXT_WATCH_EXPRESSIONS_FOCUSED, CONTEXT_VARIABLES_FOCUSED, EDITOR_CONTRIBUTION_ID, IDebugEditorContribution, CONTEXT_IN_DEBUG_MODE, CONTEXT_EXPRESSION_SELECTED, CONTEXT_BREAKPOINT_SELECTED, IConfig, IStackFrame, IThread, IDebugSession, CONTEXT_DEBUG_STATE, REPL_ID } from 'vs/workbench/contrib/debug/common/debug';
13
import { Expression, Variable, Breakpoint, FunctionBreakpoint, Thread } from 'vs/workbench/contrib/debug/common/debugModel';
14
import { IExtensionsViewlet, VIEWLET_ID as EXTENSIONS_VIEWLET_ID } from 'vs/workbench/contrib/extensions/common/extensions';
I
isidor 已提交
15
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
B
Benjamin Pasero 已提交
16
import { ICodeEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser';
I
isidor 已提交
17
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
18
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
I
isidor 已提交
19 20
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
21
import { openBreakpointSource } from 'vs/workbench/contrib/debug/browser/breakpointsView';
22
import { INotificationService } from 'vs/platform/notification/common/notification';
23
import { InputFocusedContext } from 'vs/platform/contextkey/common/contextkeys';
I
isidor 已提交
24
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
25
import { PanelFocusContext } from 'vs/workbench/common/panel';
26 27
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { onUnexpectedError } from 'vs/base/common/errors';
28 29 30 31
import { ITextResourcePropertiesService } from 'vs/editor/common/services/resourceConfiguration';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { startDebugging } from 'vs/workbench/contrib/debug/common/debugUtils';
I
isidor 已提交
32
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
33

34 35
export const ADD_CONFIGURATION_ID = 'debug.addConfiguration';
export const TOGGLE_INLINE_BREAKPOINT_ID = 'editor.debug.action.toggleInlineBreakpoint';
36 37 38 39 40 41 42 43 44 45 46 47 48
export const COPY_STACK_TRACE_ID = 'debug.copyStackTrace';
export const REVERSE_CONTINUE_ID = 'workbench.action.debug.reverseContinue';
export const STEP_BACK_ID = 'workbench.action.debug.stepBack';
export const RESTART_SESSION_ID = 'workbench.action.debug.restart';
export const TERMINATE_THREAD_ID = 'workbench.action.debug.terminateThread';
export const STEP_OVER_ID = 'workbench.action.debug.stepOver';
export const STEP_INTO_ID = 'workbench.action.debug.stepInto';
export const STEP_OUT_ID = 'workbench.action.debug.stepOut';
export const PAUSE_ID = 'workbench.action.debug.pause';
export const DISCONNECT_ID = 'workbench.action.debug.disconnect';
export const STOP_ID = 'workbench.action.debug.stop';
export const RESTART_FRAME_ID = 'workbench.action.debug.restartFrame';
export const CONTINUE_ID = 'workbench.action.debug.continue';
I
isidor 已提交
49
export const FOCUS_REPL_ID = 'workbench.debug.action.focusRepl';
50 51 52 53 54 55 56 57 58 59 60

function getThreadAndRun(accessor: ServicesAccessor, thread: IThread | undefined, run: (thread: IThread) => Promise<void>, ): void {
	const debugService = accessor.get(IDebugService);
	if (!(thread instanceof Thread)) {
		thread = debugService.getViewModel().focusedThread;
	}

	if (thread) {
		run(thread).then(undefined, onUnexpectedError);
	}
}
61

62 63
export function registerCommands(): void {

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
	CommandsRegistry.registerCommand({
		id: COPY_STACK_TRACE_ID,
		handler: (accessor: ServicesAccessor, _: string, frame: IStackFrame) => {
			const textResourcePropertiesService = accessor.get(ITextResourcePropertiesService);
			const clipboardService = accessor.get(IClipboardService);
			const eol = textResourcePropertiesService.getEOL(frame.source.uri);
			clipboardService.writeText(frame.thread.getCallStack().map(sf => sf.toString()).join(eol));
		}
	});

	CommandsRegistry.registerCommand({
		id: REVERSE_CONTINUE_ID,
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.reverseContinue());
		}
	});

	CommandsRegistry.registerCommand({
		id: STEP_BACK_ID,
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.stepBack());
		}
	});

	CommandsRegistry.registerCommand({
		id: TERMINATE_THREAD_ID,
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.terminate());
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: RESTART_SESSION_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyMod.Shift | KeyMod.CtrlCmd | KeyCode.F5,
		when: CONTEXT_IN_DEBUG_MODE,
		handler: (accessor: ServicesAccessor, _: string, session: IDebugSession | undefined) => {
			const debugService = accessor.get(IDebugService);
			if (!session || !session.getId) {
				session = debugService.getViewModel().focusedSession;
			}

			if (!session) {
				const historyService = accessor.get(IHistoryService);
				startDebugging(debugService, historyService, false);
			} else {
				session.removeReplExpressions();
				debugService.restartSession(session).then(undefined, onUnexpectedError);
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: STEP_OVER_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyCode.F10,
		when: CONTEXT_DEBUG_STATE.isEqualTo('stopped'),
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.next());
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: STEP_INTO_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyCode.F11,
		when: CONTEXT_DEBUG_STATE.isEqualTo('stopped'),
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.stepIn());
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: STEP_OUT_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyMod.Shift | KeyCode.F11,
		when: CONTEXT_DEBUG_STATE.isEqualTo('stopped'),
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.stepOut());
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: PAUSE_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyCode.F6,
		when: CONTEXT_DEBUG_STATE.isEqualTo('running'),
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.pause());
		}
	});

	CommandsRegistry.registerCommand({
		id: DISCONNECT_ID,
		handler: (accessor: ServicesAccessor) => {
			const debugService = accessor.get(IDebugService);
			const session = debugService.getViewModel().focusedSession;
			debugService.stopSession(session).then(undefined, onUnexpectedError);
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: STOP_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyMod.Shift | KeyCode.F5,
		when: CONTEXT_IN_DEBUG_MODE,
		handler: (accessor: ServicesAccessor, _: string, session: IDebugSession | undefined) => {
			const debugService = accessor.get(IDebugService);
			if (!session || !session.getId) {
				session = debugService.getViewModel().focusedSession;
			}

			debugService.stopSession(session).then(undefined, onUnexpectedError);
		}
	});

	CommandsRegistry.registerCommand({
		id: RESTART_FRAME_ID,
		handler: (accessor: ServicesAccessor, _: string, frame: IStackFrame | undefined) => {
			const debugService = accessor.get(IDebugService);
			if (!frame) {
				frame = debugService.getViewModel().focusedStackFrame;
			}

			return frame!.restart();
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: CONTINUE_ID,
		weight: KeybindingWeight.WorkbenchContrib,
		primary: KeyCode.F5,
		when: CONTEXT_IN_DEBUG_MODE,
		handler: (accessor: ServicesAccessor, _: string, thread: IThread | undefined) => {
			getThreadAndRun(accessor, thread, thread => thread.continue());
		}
	});

I
isidor 已提交
202 203 204 205 206 207 208 209
	CommandsRegistry.registerCommand({
		id: FOCUS_REPL_ID,
		handler: (accessor) => {
			const panelService = accessor.get(IPanelService);
			panelService.openPanel(REPL_ID, true);
		}
	});

210 211 212
	CommandsRegistry.registerCommand({
		id: 'debug.startFromConfig',
		handler: (accessor, config: IConfig) => {
213
			const debugService = accessor.get(IDebugService);
214
			debugService.startDebugging(undefined, config).then(undefined, onUnexpectedError);
215 216 217
		}
	});

218 219
	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.toggleBreakpoint',
220
		weight: KeybindingWeight.WorkbenchContrib + 5,
I
isidor 已提交
221
		when: ContextKeyExpr.and(CONTEXT_BREAKPOINTS_FOCUSED, InputFocusedContext.toNegated()),
222 223 224 225
		primary: KeyCode.Space,
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
226 227 228 229
			const list = listService.lastFocusedList;
			if (list instanceof List) {
				const focused = <IEnablement[]>list.getFocusedElements();
				if (focused && focused.length) {
230
					debugService.enableOrDisableBreakpoints(!focused[0].enabled, focused[0]);
231
				}
232 233 234 235
			}
		}
	});

236
	KeybindingsRegistry.registerCommandAndKeybindingRule({
237
		id: 'debug.enableOrDisableBreakpoint',
238
		weight: KeybindingWeight.WorkbenchContrib,
239
		primary: undefined,
240 241 242
		when: EditorContextKeys.editorTextFocus,
		handler: (accessor) => {
			const debugService = accessor.get(IDebugService);
243
			const editorService = accessor.get(IEditorService);
244 245 246
			const widget = editorService.activeTextEditorWidget;
			if (isCodeEditor(widget)) {
				const model = widget.getModel();
I
isidor 已提交
247
				if (model) {
248
					const position = widget.getPosition();
I
isidor 已提交
249 250 251 252 253
					if (position) {
						const bps = debugService.getModel().getBreakpoints({ uri: model.uri, lineNumber: position.lineNumber });
						if (bps.length) {
							debugService.enableOrDisableBreakpoints(!bps[0].enabled, bps[0]);
						}
I
isidor 已提交
254
					}
255 256 257 258 259
				}
			}
		}
	});

260 261
	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.renameWatchExpression',
262
		weight: KeybindingWeight.WorkbenchContrib + 5,
263 264 265 266 267 268
		when: CONTEXT_WATCH_EXPRESSIONS_FOCUSED,
		primary: KeyCode.F2,
		mac: { primary: KeyCode.Enter },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
269
			const focused = listService.lastFocusedList;
270

I
isidor 已提交
271 272 273 274
			if (focused) {
				const elements = focused.getFocus();
				if (Array.isArray(elements) && elements[0] instanceof Expression) {
					debugService.getViewModel().setSelectedExpression(elements[0]);
275 276 277 278 279 280 281
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.setVariable',
282
		weight: KeybindingWeight.WorkbenchContrib + 5,
283 284 285 286 287 288
		when: CONTEXT_VARIABLES_FOCUSED,
		primary: KeyCode.F2,
		mac: { primary: KeyCode.Enter },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
289
			const focused = listService.lastFocusedList;
290

I
isidor 已提交
291 292 293 294
			if (focused) {
				const elements = focused.getFocus();
				if (Array.isArray(elements) && elements[0] instanceof Variable) {
					debugService.getViewModel().setSelectedExpression(elements[0]);
295 296 297 298 299 300 301
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.removeWatchExpression',
302
		weight: KeybindingWeight.WorkbenchContrib,
303
		when: ContextKeyExpr.and(CONTEXT_WATCH_EXPRESSIONS_FOCUSED, CONTEXT_EXPRESSION_SELECTED.toNegated()),
304 305 306 307 308
		primary: KeyCode.Delete,
		mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
309
			const focused = listService.lastFocusedList;
310

I
isidor 已提交
311 312 313 314
			if (focused) {
				const elements = focused.getFocus();
				if (Array.isArray(elements) && elements[0] instanceof Expression) {
					debugService.removeWatchExpressions(elements[0].getId());
315 316 317 318 319 320 321
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.removeBreakpoint',
322
		weight: KeybindingWeight.WorkbenchContrib,
323
		when: ContextKeyExpr.and(CONTEXT_BREAKPOINTS_FOCUSED, CONTEXT_BREAKPOINT_SELECTED.toNegated()),
324 325 326 327 328
		primary: KeyCode.Delete,
		mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
329
			const list = listService.lastFocusedList;
330

331 332 333
			if (list instanceof List) {
				const focused = list.getFocusedElements();
				const element = focused.length ? focused[0] : undefined;
334
				if (element instanceof Breakpoint) {
335
					debugService.removeBreakpoints(element.getId());
336
				} else if (element instanceof FunctionBreakpoint) {
337
					debugService.removeFunctionBreakpoints(element.getId());
338 339 340 341
				}
			}
		}
	});
I
isidor 已提交
342 343

	KeybindingsRegistry.registerCommandAndKeybindingRule({
I
isidor 已提交
344
		id: 'debug.installAdditionalDebuggers',
345
		weight: KeybindingWeight.WorkbenchContrib,
I
isidor 已提交
346
		when: undefined,
I
isidor 已提交
347 348 349 350 351 352
		primary: undefined,
		handler: (accessor) => {
			const viewletService = accessor.get(IViewletService);
			return viewletService.openViewlet(EXTENSIONS_VIEWLET_ID, true)
				.then(viewlet => viewlet as IExtensionsViewlet)
				.then(viewlet => {
353
					viewlet.search('tag:debuggers @sort:installs');
I
isidor 已提交
354 355 356 357
					viewlet.focus();
				});
		}
	});
I
isidor 已提交
358 359

	KeybindingsRegistry.registerCommandAndKeybindingRule({
360
		id: ADD_CONFIGURATION_ID,
361
		weight: KeybindingWeight.WorkbenchContrib,
I
isidor 已提交
362 363
		when: undefined,
		primary: undefined,
I
isidor 已提交
364
		handler: (accessor, launchUri: string) => {
I
isidor 已提交
365
			const manager = accessor.get(IDebugService).getConfigurationManager();
366
			if (accessor.get(IWorkspaceContextService).getWorkbenchState() === WorkbenchState.EMPTY) {
367
				accessor.get(INotificationService).info(nls.localize('noFolderDebugConfig', "Please first open a folder in order to do advanced debug configuration."));
368
				return undefined;
369
			}
370
			const launch = manager.getLaunches().filter(l => l.uri.toString() === launchUri).pop() || manager.selectedConfiguration.launch;
371

I
isidor 已提交
372
			return launch!.openConfigFile(false, false).then(({ editor, created }) => {
373
				if (editor && !created) {
I
isidor 已提交
374
					const codeEditor = <ICodeEditor>editor.getControl();
I
isidor 已提交
375 376 377 378 379 380 381 382 383
					if (codeEditor) {
						return codeEditor.getContribution<IDebugEditorContribution>(EDITOR_CONTRIBUTION_ID).addLaunchConfiguration();
					}
				}

				return undefined;
			});
		}
	});
I
isidor 已提交
384

I
isidor 已提交
385
	const inlineBreakpointHandler = (accessor: ServicesAccessor) => {
386
		const debugService = accessor.get(IDebugService);
387
		const editorService = accessor.get(IEditorService);
388 389 390
		const widget = editorService.activeTextEditorWidget;
		if (isCodeEditor(widget)) {
			const position = widget.getPosition();
I
isidor 已提交
391 392 393 394
			if (!position || !widget.hasModel()) {
				return undefined;
			}

395
			const modelUri = widget.getModel().uri;
396 397 398 399
			const bp = debugService.getModel().getBreakpoints({ lineNumber: position.lineNumber, uri: modelUri })
				.filter(bp => (bp.column === position.column || !bp.column && position.column <= 1)).pop();

			if (bp) {
400
				return undefined;
401
			}
402
			if (debugService.getConfigurationManager().canSetBreakpointsIn(widget.getModel())) {
403
				return debugService.addBreakpoints(modelUri, [{ lineNumber: position.lineNumber, column: position.column > 1 ? position.column : undefined }], 'debugCommands.inlineBreakpointCommand');
404 405 406
			}
		}

407
		return undefined;
408
	};
409
	KeybindingsRegistry.registerCommandAndKeybindingRule({
410
		weight: KeybindingWeight.WorkbenchContrib,
411
		primary: KeyMod.Shift | KeyCode.F9,
412
		when: EditorContextKeys.editorTextFocus,
413
		id: TOGGLE_INLINE_BREAKPOINT_ID,
414 415
		handler: inlineBreakpointHandler
	});
I
isidor 已提交
416 417 418

	MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
		command: {
419
			id: TOGGLE_INLINE_BREAKPOINT_ID,
B
Benjamin Pasero 已提交
420
			title: { value: nls.localize('inlineBreakpoint', "Inline Breakpoint"), original: 'Debug: Inline Breakpoint' },
I
isidor 已提交
421 422 423 424 425
			category: nls.localize('debug', "Debug")
		}
	});
	MenuRegistry.appendMenuItem(MenuId.EditorContext, {
		command: {
426
			id: TOGGLE_INLINE_BREAKPOINT_ID,
427
			title: nls.localize('addInlineBreakpoint', "Add Inline Breakpoint")
I
isidor 已提交
428
		},
I
isidor 已提交
429
		when: ContextKeyExpr.and(CONTEXT_IN_DEBUG_MODE, PanelFocusContext.toNegated(), EditorContextKeys.editorTextFocus),
I
isidor 已提交
430 431 432
		group: 'debug',
		order: 1
	});
433 434 435

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.openBreakpointToSide',
436
		weight: KeybindingWeight.WorkbenchContrib,
437 438 439 440 441 442 443 444 445
		when: CONTEXT_BREAKPOINTS_FOCUSED,
		primary: KeyMod.CtrlCmd | KeyCode.Enter,
		secondary: [KeyMod.Alt | KeyCode.Enter],
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const list = listService.lastFocusedList;
			if (list instanceof List) {
				const focus = list.getFocusedElements();
				if (focus.length && focus[0] instanceof Breakpoint) {
446
					return openBreakpointSource(focus[0], true, false, accessor.get(IDebugService), accessor.get(IEditorService));
447 448 449
				}
			}

450
			return undefined;
451 452
		}
	});
453
}