-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
replInputHintContentWidget.ts
149 lines (122 loc) · 5.06 KB
/
replInputHintContentWidget.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { status } from 'vs/base/browser/ui/aria/aria';
import { KeybindingLabel } from 'vs/base/browser/ui/keybindingLabel/keybindingLabel';
import { Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { OS } from 'vs/base/common/platform';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { ConfigurationChangedEvent, EditorOption } from 'vs/editor/common/config/editorOptions';
import { localize } from 'vs/nls';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { AccessibilityVerbositySettingId } from 'vs/workbench/contrib/accessibility/browser/accessibilityConfiguration';
import { InteractiveWindowSetting } from 'vs/workbench/contrib/interactive/browser/interactiveCommon';
export class ReplInputHintContentWidget extends Disposable implements IContentWidget {
private static readonly ID = 'replInput.widget.emptyHint';
private domNode: HTMLElement | undefined;
private ariaLabel: string = '';
constructor(
private readonly editor: ICodeEditor,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
) {
super();
this._register(this.editor.onDidChangeConfiguration((e: ConfigurationChangedEvent) => {
if (this.domNode && e.hasChanged(EditorOption.fontInfo)) {
this.editor.applyFontInfo(this.domNode);
}
}));
const onDidFocusEditorText = Event.debounce(this.editor.onDidFocusEditorText, () => undefined, 500);
this._register(onDidFocusEditorText(() => {
if (this.editor.hasTextFocus() && this.ariaLabel && configurationService.getValue(AccessibilityVerbositySettingId.ReplInputHint)) {
status(this.ariaLabel);
}
}));
this._register(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(InteractiveWindowSetting.executeWithShiftEnter)) {
this.setHint();
}
}));
this.editor.addContentWidget(this);
}
getId(): string {
return ReplInputHintContentWidget.ID;
}
getPosition(): IContentWidgetPosition | null {
return {
position: { lineNumber: 1, column: 1 },
preference: [ContentWidgetPositionPreference.EXACT]
};
}
getDomNode(): HTMLElement {
if (!this.domNode) {
this.domNode = dom.$('.empty-editor-hint');
this.domNode.style.width = 'max-content';
this.domNode.style.paddingLeft = '4px';
this.setHint();
this._register(dom.addDisposableListener(this.domNode, 'click', () => {
this.editor.focus();
}));
this.editor.applyFontInfo(this.domNode);
}
return this.domNode;
}
private setHint() {
if (!this.domNode) {
return;
}
while (this.domNode.firstChild) {
this.domNode.removeChild(this.domNode.firstChild);
}
const hintElement = dom.$('div.empty-hint-text');
hintElement.style.cursor = 'text';
hintElement.style.whiteSpace = 'nowrap';
const keybinding = this.getKeybinding();
const keybindingHintLabel = keybinding?.getLabel();
if (keybinding && keybindingHintLabel) {
const actionPart = localize('emptyHintText', 'Press {0} to execute. ', keybindingHintLabel);
const [before, after] = actionPart.split(keybindingHintLabel).map((fragment) => {
const hintPart = dom.$('span', undefined, fragment);
hintPart.style.fontStyle = 'italic';
return hintPart;
});
hintElement.appendChild(before);
const label = new KeybindingLabel(hintElement, OS);
label.set(keybinding);
label.element.style.width = 'min-content';
label.element.style.display = 'inline';
hintElement.appendChild(after);
this.domNode.append(hintElement);
this.ariaLabel = actionPart.concat(localize('disableHint', ' Toggle {0} in settings to disable this hint.', AccessibilityVerbositySettingId.ReplInputHint));
}
}
private getKeybinding() {
const keybindings = this.keybindingService.lookupKeybindings('interactive.execute');
const shiftEnterConfig = this.configurationService.getValue(InteractiveWindowSetting.executeWithShiftEnter);
if (shiftEnterConfig) {
const keybinding = keybindings.find(kb => kb.getLabel() === 'Shift+Enter');
if (keybinding) {
return keybinding;
}
} else {
let keybinding = keybindings.find(kb => kb.getLabel() === 'Enter');
if (keybinding) {
return keybinding;
}
keybinding = this.keybindingService.lookupKeybindings('python.execInREPLEnter')
.find(kb => kb.getLabel() === 'Enter');
if (keybinding) {
return keybinding;
}
}
return undefined;
}
override dispose(): void {
super.dispose();
this.editor.removeContentWidget(this);
}
}