-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
breakpointWidget.ts
168 lines (145 loc) · 7.58 KB
/
breakpointWidget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import nls = require('vs/nls');
import 'vs/css!../browser/media/breakpointWidget';
import async = require('vs/base/common/async');
import errors = require('vs/base/common/errors');
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
import platform = require('vs/base/common/platform');
import {SelectBox} from 'vs/base/browser/ui/selectBox/selectBox';
import lifecycle = require('vs/base/common/lifecycle');
import dom = require('vs/base/browser/dom');
import {InputBox} from 'vs/base/browser/ui/inputbox/inputBox';
import {CommonEditorRegistry, ServicesAccessor, EditorCommand} from 'vs/editor/common/editorCommonExtensions';
import {EditorContextKeys, ICommonCodeEditor} from 'vs/editor/common/editorCommon';
import editorbrowser = require('vs/editor/browser/editorBrowser');
import {ZoneWidget} from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IContextViewService} from 'vs/platform/contextview/browser/contextView';
import {RawContextKey, IContextKeyService, IContextKey} from 'vs/platform/contextkey/common/contextkey';
import debug = require('vs/workbench/parts/debug/common/debug');
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
const $ = dom.$;
const CONTEXT_BREAKPOINT_WIDGET_VISIBLE = new RawContextKey<boolean>('breakpointWidgetVisible', false);
const CLOSE_BREAKPOINT_WIDGET_COMMAND_ID = 'closeBreakpointWidget';
export class BreakpointWidget extends ZoneWidget {
public static INSTANCE: BreakpointWidget;
private inputBox: InputBox;
private toDispose: lifecycle.IDisposable[];
private breakpointWidgetVisible: IContextKey<boolean>;
private expressionPlaceholder: string;
private expressionAriaLabel: string;
private hitCountPlaceholder: string;
private hitCountAriaLabel: string;
private hitCountContext: boolean;
constructor(editor: editorbrowser.ICodeEditor, private lineNumber: number,
@IContextViewService private contextViewService: IContextViewService,
@debug.IDebugService private debugService: debug.IDebugService,
@IContextKeyService contextKeyService: IContextKeyService
) {
super(editor, { showFrame: true, showArrow: false, frameColor: '#007ACC', frameWidth: 1 });
this.toDispose = [];
this.expressionPlaceholder = nls.localize('breakpointWidgetExpressionPlaceholder', "Breakpoint on line {0} will only stop if this expression is true.", this.lineNumber);
this.expressionAriaLabel = nls.localize('breakpointWidgetAriaLabel', "Type the breakpoint condition for line {0}. The program will only stop here if this condition is true. Press Enter to accept or Escape to cancel.", this.lineNumber);
this.hitCountPlaceholder = nls.localize('breakpointWidgetHitCountPlaceholder', "Breakpoint on line {0} will only stop if the hit count condition is true.", this.lineNumber);
this.hitCountAriaLabel = nls.localize('breakpointWidgetHitCountAriaLabel', "Type the breakpoint hit count condition for line {0}. The program will only stop here if the hit count is met. Press Enter to accept or Escape to cancel.", this.lineNumber);
this.create();
this.breakpointWidgetVisible = CONTEXT_BREAKPOINT_WIDGET_VISIBLE.bindTo(contextKeyService);
this.breakpointWidgetVisible.set(true);
BreakpointWidget.INSTANCE = this;
this.toDispose.push(editor.onDidChangeModel(() => this.dispose()));
}
public static createInstance(editor: editorbrowser.ICodeEditor, lineNumber: number, instantiationService: IInstantiationService): void {
if (BreakpointWidget.INSTANCE) {
BreakpointWidget.INSTANCE.dispose();
}
instantiationService.createInstance(BreakpointWidget, editor, lineNumber);
BreakpointWidget.INSTANCE.show({ lineNumber, column: 1 }, 2);
}
protected _fillContainer(container: HTMLElement): void {
dom.addClass(container, 'breakpoint-widget');
const uri = this.editor.getModel().uri;
const breakpoint = this.debugService.getModel().getBreakpoints().filter(bp => bp.lineNumber === this.lineNumber && bp.source.uri.toString() === uri.toString()).pop();
const selectBox = new SelectBox([nls.localize('expression', "Expression"), nls.localize('hitCount', "Hit Count")], 0);
selectBox.render(dom.append(container, $('.breakpoint-select-container')));
selectBox.onDidSelect(e => {
this.hitCountContext = e === 'Hit Count';
this.inputBox.setAriaLabel(this.hitCountContext ? this.hitCountAriaLabel : this.expressionAriaLabel);
this.inputBox.setPlaceHolder(this.hitCountContext ? this.hitCountPlaceholder : this.expressionPlaceholder);
this.inputBox.value = (this.hitCountContext && breakpoint && breakpoint.hitCondition) ? breakpoint.hitCondition : breakpoint && breakpoint.condition ? breakpoint.condition : '';
});
const inputBoxContainer = dom.append(container, $('.inputBoxContainer'));
this.inputBox = new InputBox(inputBoxContainer, this.contextViewService, {
placeholder: this.expressionPlaceholder,
ariaLabel: this.expressionAriaLabel
});
this.toDispose.push(this.inputBox);
dom.addClass(this.inputBox.inputElement, platform.isWindows ? 'windows' : platform.isMacintosh ? 'mac' : 'linux');
this.inputBox.value = (breakpoint && breakpoint.condition) ? breakpoint.condition : '';
// Due to an electron bug we have to do the timeout, otherwise we do not get focus
setTimeout(() => this.inputBox.focus(), 0);
let disposed = false;
const wrapUp = async.once((success: boolean) => {
if (!disposed) {
disposed = true;
if (success) {
const raw: debug.IRawBreakpoint = {
uri,
lineNumber: this.lineNumber,
enabled: true
};
if (this.hitCountContext) {
raw.hitCondition = this.inputBox.value;
} else {
raw.condition = this.inputBox.value;
}
// if there is already a breakpoint on this location - remove it.
const oldBreakpoint = this.debugService.getModel().getBreakpoints()
.filter(bp => bp.lineNumber === this.lineNumber && bp.source.uri.toString() === uri.toString()).pop();
if (oldBreakpoint) {
this.debugService.removeBreakpoints(oldBreakpoint.getId()).done(null, errors.onUnexpectedError);
}
this.debugService.addBreakpoints([raw]).done(null, errors.onUnexpectedError);
}
this.dispose();
}
});
this.toDispose.push(dom.addStandardDisposableListener(this.inputBox.inputElement, 'keydown', (e: IKeyboardEvent) => {
const isEscape = e.equals(KeyCode.Escape);
const isEnter = e.equals(KeyCode.Enter);
if (isEscape || isEnter) {
e.stopPropagation();
wrapUp(isEnter);
}
}));
}
public dispose(): void {
super.dispose();
this.breakpointWidgetVisible.reset();
BreakpointWidget.INSTANCE = undefined;
lifecycle.dispose(this.toDispose);
setTimeout(() => this.editor.focus(), 0);
}
}
class CloseBreakpointWidgetCommand extends EditorCommand {
constructor() {
super({
id: CLOSE_BREAKPOINT_WIDGET_COMMAND_ID,
precondition: CONTEXT_BREAKPOINT_WIDGET_VISIBLE,
kbOpts: {
weight: CommonEditorRegistry.commandWeight(8),
kbExpr: EditorContextKeys.Focus,
primary: KeyCode.Escape,
secondary: [KeyMod.Shift | KeyCode.Escape]
}
});
}
protected runEditorCommand(accessor:ServicesAccessor, editor: ICommonCodeEditor, args: any): void {
if (BreakpointWidget.INSTANCE) {
BreakpointWidget.INSTANCE.dispose();
}
}
}
CommonEditorRegistry.registerEditorCommand(new CloseBreakpointWidgetCommand());