debugCommands.ts 9.2 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
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
8
import { TPromise } from 'vs/base/common/winjs.base';
9
import severity from 'vs/base/common/severity';
10 11 12 13
import { List } from 'vs/base/browser/ui/list/listWidget';
import * as errors from 'vs/base/common/errors';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IListService } from 'vs/platform/list/browser/listService';
14
import { IMessageService } from 'vs/platform/message/common/message';
15
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
I
isidor 已提交
16
import { IDebugService, IEnablement, CONTEXT_BREAKPOINTS_FOCUSED, CONTEXT_WATCH_EXPRESSIONS_FOCUSED, CONTEXT_VARIABLES_FOCUSED, EDITOR_CONTRIBUTION_ID, IDebugEditorContribution, CONTEXT_IN_DEBUG_MODE, CONTEXT_NOT_IN_DEBUG_REPL } from 'vs/workbench/parts/debug/common/debug';
17
import { Expression, Variable, Breakpoint, FunctionBreakpoint } from 'vs/workbench/parts/debug/common/debugModel';
I
isidor 已提交
18 19
import { IExtensionsViewlet, VIEWLET_ID as EXTENSIONS_VIEWLET_ID } from 'vs/workbench/parts/extensions/common/extensions';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
20
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
I
isidor 已提交
21 22 23 24 25
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
26
import { openBreakpointSource } from 'vs/workbench/parts/debug/electron-browser/breakpointsView';
27 28 29 30 31 32 33 34 35 36 37

export function registerCommands(): void {

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.toggleBreakpoint',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(5),
		when: CONTEXT_BREAKPOINTS_FOCUSED,
		primary: KeyCode.Space,
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
38
			const focused = listService.lastFocusedList;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

			// Tree only
			if (!(focused instanceof List)) {
				const tree = focused;
				const element = <IEnablement>tree.getFocus();
				debugService.enableOrDisableBreakpoints(!element.enabled, element).done(null, errors.onUnexpectedError);
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.renameWatchExpression',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(5),
		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 已提交
58
			const focused = listService.lastFocusedList;
59 60 61 62 63

			// Tree only
			if (!(focused instanceof List)) {
				const element = focused.getFocus();
				if (element instanceof Expression) {
64
					debugService.getViewModel().setSelectedExpression(element);
65 66 67 68 69 70 71 72 73 74 75 76 77 78
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.setVariable',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(5),
		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 已提交
79
			const focused = listService.lastFocusedList;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

			// Tree only
			if (!(focused instanceof List)) {
				const element = focused.getFocus();
				if (element instanceof Variable) {
					debugService.getViewModel().setSelectedExpression(element);
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.removeWatchExpression',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: CONTEXT_WATCH_EXPRESSIONS_FOCUSED,
		primary: KeyCode.Delete,
		mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
100
			const focused = listService.lastFocusedList;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

			// Tree only
			if (!(focused instanceof List)) {
				const element = focused.getFocus();
				if (element instanceof Expression) {
					debugService.removeWatchExpressions(element.getId());
				}
			}
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.removeBreakpoint',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: CONTEXT_BREAKPOINTS_FOCUSED,
		primary: KeyCode.Delete,
		mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace },
		handler: (accessor) => {
			const listService = accessor.get(IListService);
			const debugService = accessor.get(IDebugService);
J
Joao Moreno 已提交
121
			const focused = listService.lastFocusedList;
122 123 124 125 126 127 128 129 130 131 132 133

			// Tree only
			if (!(focused instanceof List)) {
				const element = focused.getFocus();
				if (element instanceof Breakpoint) {
					debugService.removeBreakpoints(element.getId()).done(null, errors.onUnexpectedError);
				} else if (element instanceof FunctionBreakpoint) {
					debugService.removeFunctionBreakpoints(element.getId()).done(null, errors.onUnexpectedError);
				}
			}
		}
	});
I
isidor 已提交
134 135

	KeybindingsRegistry.registerCommandAndKeybindingRule({
I
isidor 已提交
136
		id: 'debug.installAdditionalDebuggers',
I
isidor 已提交
137
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
I
isidor 已提交
138
		when: undefined,
I
isidor 已提交
139 140 141 142 143 144
		primary: undefined,
		handler: (accessor) => {
			const viewletService = accessor.get(IViewletService);
			return viewletService.openViewlet(EXTENSIONS_VIEWLET_ID, true)
				.then(viewlet => viewlet as IExtensionsViewlet)
				.then(viewlet => {
145
					viewlet.search('tag:debuggers @sort:installs');
I
isidor 已提交
146 147 148 149
					viewlet.focus();
				});
		}
	});
I
isidor 已提交
150 151 152 153 154 155

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.addConfiguration',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: undefined,
		primary: undefined,
I
isidor 已提交
156
		handler: (accessor, launchUri: string) => {
I
isidor 已提交
157
			const manager = accessor.get(IDebugService).getConfigurationManager();
158
			if (accessor.get(IWorkspaceContextService).getWorkbenchState() === WorkbenchState.EMPTY) {
159 160 161
				accessor.get(IMessageService).show(severity.Info, nls.localize('noFolderDebugConfig', "Please first open a folder in order to do advanced debug configuration."));
				return TPromise.as(null);
			}
162
			const launch = manager.getLaunches().filter(l => l.uri.toString() === launchUri).pop() || manager.selectedConfiguration.launch;
163

I
isidor 已提交
164 165 166
			return launch.openConfigFile(false).done(editor => {
				if (editor) {
					const codeEditor = <ICodeEditor>editor.getControl();
I
isidor 已提交
167 168 169 170 171 172 173 174 175
					if (codeEditor) {
						return codeEditor.getContribution<IDebugEditorContribution>(EDITOR_CONTRIBUTION_ID).addLaunchConfiguration();
					}
				}

				return undefined;
			});
		}
	});
I
isidor 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	const COLUMN_BREAKPOINT_COMMAND_ID = 'editor.debug.action.toggleColumnBreakpoint';
	CommandsRegistry.registerCommand({
		id: COLUMN_BREAKPOINT_COMMAND_ID,
		handler: (accessor) => {
			const debugService = accessor.get(IDebugService);
			const editorService = accessor.get(IWorkbenchEditorService);
			const editor = editorService.getActiveEditor();
			const control = editor && <ICodeEditor>editor.getControl();
			if (control) {
				const position = control.getPosition();
				const modelUri = control.getModel().uri;
				const bp = debugService.getModel().getBreakpoints()
					.filter(bp => bp.lineNumber === position.lineNumber && bp.column === position.column && bp.uri.toString() === modelUri.toString()).pop();

				if (bp) {
					return TPromise.as(null);
				}
				if (debugService.getConfigurationManager().canSetBreakpointsIn(control.getModel())) {
					return debugService.addBreakpoints(modelUri, [{ lineNumber: position.lineNumber, column: position.column }]);
				}
			}

			return TPromise.as(null);
		}
	});

	MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
		command: {
			id: COLUMN_BREAKPOINT_COMMAND_ID,
			title: nls.localize('columnBreakpoint', "Column Breakpoint"),
			category: nls.localize('debug', "Debug")
		}
	});
	MenuRegistry.appendMenuItem(MenuId.EditorContext, {
		command: {
			id: COLUMN_BREAKPOINT_COMMAND_ID,
			title: nls.localize('addColumnBreakpoint', "Add Column Breakpoint")
		},
		when: ContextKeyExpr.and(CONTEXT_IN_DEBUG_MODE, CONTEXT_NOT_IN_DEBUG_REPL, EditorContextKeys.writable),
		group: 'debug',
		order: 1
	});
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'debug.openBreakpointToSide',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		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) {
					return openBreakpointSource(focus[0], true, false, accessor.get(IDebugService), accessor.get(IWorkbenchEditorService));
				}
			}

			return TPromise.as(undefined);
		}
	});
239
}