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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*---------------------------------------------------------------------------------------------
* 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 { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { localize } from 'vs/nls';
import { registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { inputPlaceholderForeground, textLinkForeground } from 'vs/platform/theme/common/colorRegistry';
import { ChangeLanguageAction } from 'vs/workbench/browser/parts/editor/editorStatus';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { Schemas } from 'vs/base/common/network';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ConfigurationChangedEvent, EditorOption } from 'vs/editor/common/config/editorOptions';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { EventType as GestureEventType, Gesture } from 'vs/base/browser/touch';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
const $ = dom.$;
const untitledTextEditorHintSetting = 'workbench.editor.untitled.hint';
export class UntitledTextEditorHintContribution implements IEditorContribution {
public static readonly ID = 'editor.contrib.untitledTextEditorHint';
private toDispose: IDisposable[];
private untitledTextHintContentWidget: UntitledTextEditorHintContentWidget | undefined;
constructor(
private editor: ICodeEditor,
@IEditorGroupsService private readonly editorGroupsService: IEditorGroupsService,
@ICommandService private readonly commandService: ICommandService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
) {
this.toDispose = [];
this.toDispose.push(this.editor.onDidChangeModel(() => this.update()));
this.toDispose.push(this.editor.onDidChangeModelLanguage(() => this.update()));
this.toDispose.push(this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(untitledTextEditorHintSetting)) {
this.update();
}
}));
}
private update(): void {
this.untitledTextHintContentWidget?.dispose();
const configValue = this.configurationService.getValue(untitledTextEditorHintSetting);
const model = this.editor.getModel();
if (model && model.uri.scheme === Schemas.untitled && model.getLanguageId() === PLAINTEXT_LANGUAGE_ID && configValue === 'text') {
this.untitledTextHintContentWidget = new UntitledTextEditorHintContentWidget(this.editor, this.editorGroupsService, this.commandService, this.configurationService, this.keybindingService);
}
}
dispose(): void {
dispose(this.toDispose);
this.untitledTextHintContentWidget?.dispose();
}
}
class UntitledTextEditorHintContentWidget implements IContentWidget {
private static readonly ID = 'editor.widget.untitledHint';
private domNode: HTMLElement | undefined;
private toDispose: IDisposable[];
constructor(
private readonly editor: ICodeEditor,
private readonly editorGroupsService: IEditorGroupsService,
private readonly commandService: ICommandService,
private readonly configurationService: IConfigurationService,
private readonly keybindingService: IKeybindingService,
) {
this.toDispose = [];
this.toDispose.push(editor.onDidChangeModelContent(() => this.onDidChangeModelContent()));
this.toDispose.push(this.editor.onDidChangeConfiguration((e: ConfigurationChangedEvent) => {
if (this.domNode && e.hasChanged(EditorOption.fontInfo)) {
this.editor.applyFontInfo(this.domNode);
}
}));
this.onDidChangeModelContent();
}
private onDidChangeModelContent(): void {
if (this.editor.getValue() === '') {
this.editor.addContentWidget(this);
} else {
this.editor.removeContentWidget(this);
}
}
getId(): string {
return UntitledTextEditorHintContentWidget.ID;
}
// Select a language to get started. Start typing to dismiss, or don't show this again.
getDomNode(): HTMLElement {
if (!this.domNode) {
this.domNode = $('.untitled-hint');
this.domNode.style.width = 'max-content';
const language = $('a.language-mode');
language.style.cursor = 'pointer';
language.innerText = localize('selectAlanguage2', "Select a language");
const languageKeyBinding = this.keybindingService.lookupKeybinding(ChangeLanguageAction.ID);
const languageKeybindingLabel = languageKeyBinding?.getLabel();
if (languageKeybindingLabel) {
language.title = localize('keyboardBindingTooltip', "{0}", languageKeybindingLabel);
}
this.domNode.appendChild(language);
const or = $('span');
or.innerText = localize('or', " or ",);
this.domNode.appendChild(or);
const editorType = $('a.editor-type');
editorType.style.cursor = 'pointer';
editorType.innerText = localize('openADifferentEditor', "open a different editor");
const selectEditorTypeKeyBinding = this.keybindingService.lookupKeybinding('welcome.showNewFileEntries');
const selectEditorTypeKeybindingLabel = selectEditorTypeKeyBinding?.getLabel();
if (selectEditorTypeKeybindingLabel) {
editorType.title = localize('keyboardBindingTooltip', "{0}", selectEditorTypeKeybindingLabel);
}
this.domNode.appendChild(editorType);
const toGetStarted = $('span');
toGetStarted.innerText = localize('toGetStarted', " to get started.");
this.domNode.appendChild(toGetStarted);
this.domNode.appendChild($('br'));
const startTyping = $('span');
startTyping.innerText = localize('startTyping', "Start typing to dismiss or ");
this.domNode.appendChild(startTyping);
const dontShow = $('a');
dontShow.style.cursor = 'pointer';
dontShow.innerText = localize('dontshow', "don't show");
this.domNode.appendChild(dontShow);
const thisAgain = $('span');
thisAgain.innerText = localize('thisAgain', " this again.");
this.domNode.appendChild(thisAgain);
this.toDispose.push(Gesture.addTarget(this.domNode));
const languageOnClickOrTap = async (e: MouseEvent) => {
e.stopPropagation();
// Need to focus editor before so current editor becomes active and the command is properly executed
this.editor.focus();
await this.commandService.executeCommand(ChangeLanguageAction.ID, { from: 'hint' });
this.editor.focus();
};
this.toDispose.push(dom.addDisposableListener(language, 'click', languageOnClickOrTap));
this.toDispose.push(dom.addDisposableListener(language, GestureEventType.Tap, languageOnClickOrTap));
this.toDispose.push(Gesture.addTarget(language));
const chooseEditorOnClickOrTap = async (e: MouseEvent) => {
e.stopPropagation();
const activeEditorInput = this.editorGroupsService.activeGroup.activeEditor;
const newEditorSelected = await this.commandService.executeCommand('welcome.showNewFileEntries', { from: 'hint' });
// Close the active editor as long as it is untitled (swap the editors out)
if (newEditorSelected && activeEditorInput !== null && activeEditorInput.resource?.scheme === Schemas.untitled) {
this.editorGroupsService.activeGroup.closeEditor(activeEditorInput, { preserveFocus: true });
}
};
this.toDispose.push(dom.addDisposableListener(editorType, 'click', chooseEditorOnClickOrTap));
this.toDispose.push(dom.addDisposableListener(editorType, GestureEventType.Tap, chooseEditorOnClickOrTap));
this.toDispose.push(Gesture.addTarget(editorType));
const dontShowOnClickOrTap = () => {
this.configurationService.updateValue(untitledTextEditorHintSetting, 'hidden');
this.dispose();
this.editor.focus();
};
this.toDispose.push(dom.addDisposableListener(dontShow, 'click', dontShowOnClickOrTap));
this.toDispose.push(dom.addDisposableListener(dontShow, GestureEventType.Tap, dontShowOnClickOrTap));
this.toDispose.push(Gesture.addTarget(dontShow));
this.toDispose.push(dom.addDisposableListener(this.domNode, 'click', () => {
this.editor.focus();
}));
this.domNode.style.fontStyle = 'italic';
this.domNode.style.paddingLeft = '4px';
this.editor.applyFontInfo(this.domNode);
}
return this.domNode;
}
getPosition(): IContentWidgetPosition | null {
return {
position: { lineNumber: 1, column: 1 },
preference: [ContentWidgetPositionPreference.EXACT]
};
}
dispose(): void {
this.editor.removeContentWidget(this);
dispose(this.toDispose);
}
}
registerThemingParticipant((theme, collector) => {
const inputPlaceholderForegroundColor = theme.getColor(inputPlaceholderForeground);
if (inputPlaceholderForegroundColor) {
collector.addRule(`.monaco-editor .contentWidgets .untitled-hint { color: ${inputPlaceholderForegroundColor}; }`);
}
const textLinkForegroundColor = theme.getColor(textLinkForeground);
if (textLinkForegroundColor) {
collector.addRule(`.monaco-editor .contentWidgets .untitled-hint a { color: ${textLinkForegroundColor}; }`);
}
});
registerEditorContribution(UntitledTextEditorHintContribution.ID, UntitledTextEditorHintContribution);
|