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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction, registerEditorAction, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider } from 'vs/editor/common/languages';
import * as nls from 'vs/nls';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { formatDocumentRangesWithProvider, formatDocumentWithProvider, getRealAndSyntheticDocumentFormattersOrdered, FormattingConflicts, FormattingMode } from 'vs/editor/contrib/format/browser/format';
import { Range } from 'vs/editor/common/core/range';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { Registry } from 'vs/platform/registry/common/platform';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { IExtensionService, toExtension } from 'vs/workbench/services/extensions/common/extensions';
import { Disposable } from 'vs/base/common/lifecycle';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ITextModel } from 'vs/editor/common/model';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { IWorkbenchExtensionEnablementService } from 'vs/workbench/services/extensionManagement/common/extensionManagement';
import { editorConfigurationBaseNode } from 'vs/editor/common/config/editorConfigurationSchema';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
type FormattingEditProvider = DocumentFormattingEditProvider | DocumentRangeFormattingEditProvider;
class DefaultFormatter extends Disposable implements IWorkbenchContribution {
static readonly configName = 'editor.defaultFormatter';
static extensionIds: (string | null)[] = [];
static extensionItemLabels: string[] = [];
static extensionDescriptions: string[] = [];
constructor(
@IExtensionService private readonly _extensionService: IExtensionService,
@IWorkbenchExtensionEnablementService private readonly _extensionEnablementService: IWorkbenchExtensionEnablementService,
@IConfigurationService private readonly _configService: IConfigurationService,
@INotificationService private readonly _notificationService: INotificationService,
@IDialogService private readonly _dialogService: IDialogService,
@IQuickInputService private readonly _quickInputService: IQuickInputService,
@ILanguageService private readonly _languageService: ILanguageService,
) {
super();
this._register(this._extensionService.onDidChangeExtensions(this._updateConfigValues, this));
this._register(FormattingConflicts.setFormatterSelector((formatter, document, mode) => this._selectFormatter(formatter, document, mode)));
this._updateConfigValues();
}
private async _updateConfigValues(): Promise<void> {
let extensions = await this._extensionService.getExtensions();
extensions = extensions.sort((a, b) => {
let boostA = a.categories?.find(cat => cat === 'Formatters' || cat === 'Programming Languages');
let boostB = b.categories?.find(cat => cat === 'Formatters' || cat === 'Programming Languages');
if (boostA && !boostB) {
return -1;
} else if (!boostA && boostB) {
return 1;
} else {
return a.name.localeCompare(b.name);
}
});
DefaultFormatter.extensionIds.length = 0;
DefaultFormatter.extensionItemLabels.length = 0;
DefaultFormatter.extensionDescriptions.length = 0;
DefaultFormatter.extensionIds.push(null);
DefaultFormatter.extensionItemLabels.push(nls.localize('null', 'None'));
DefaultFormatter.extensionDescriptions.push(nls.localize('nullFormatterDescription', "None"));
for (const extension of extensions) {
if (extension.main || extension.browser) {
DefaultFormatter.extensionIds.push(extension.identifier.value);
DefaultFormatter.extensionItemLabels.push(extension.displayName ?? '');
DefaultFormatter.extensionDescriptions.push(extension.description ?? '');
}
}
}
static _maybeQuotes(s: string): string {
return s.match(/\s/) ? `'${s}'` : s;
}
private async _selectFormatter<T extends FormattingEditProvider>(formatter: T[], document: ITextModel, mode: FormattingMode): Promise<T | undefined> {
const defaultFormatterId = this._configService.getValue<string>(DefaultFormatter.configName, {
resource: document.uri,
overrideIdentifier: document.getLanguageId()
});
if (defaultFormatterId) {
// good -> formatter configured
const defaultFormatter = formatter.find(formatter => ExtensionIdentifier.equals(formatter.extensionId, defaultFormatterId));
if (defaultFormatter) {
// formatter available
return defaultFormatter;
}
// bad -> formatter gone
const extension = await this._extensionService.getExtension(defaultFormatterId);
if (extension && this._extensionEnablementService.isEnabled(toExtension(extension))) {
// formatter does not target this file
const langName = this._languageService.getLanguageName(document.getLanguageId()) || document.getLanguageId();
const detail = nls.localize('miss', "Extension '{0}' is configured as formatter but it cannot format '{1}'-files", extension.displayName || extension.name, langName);
if (mode === FormattingMode.Silent) {
this._notificationService.status(detail, { hideAfter: 4000 });
return undefined;
} else {
const result = await this._dialogService.confirm({
message: nls.localize('miss.1', "Change Default Formatter"),
detail,
primaryButton: nls.localize('do.config', "Configure..."),
secondaryButton: nls.localize('cancel', "Cancel")
});
if (result.confirmed) {
return this._pickAndPersistDefaultFormatter(formatter, document);
} else {
return undefined;
}
}
}
} else if (formatter.length === 1) {
// ok -> nothing configured but only one formatter available
return formatter[0];
}
const langName = this._languageService.getLanguageName(document.getLanguageId()) || document.getLanguageId();
const message = !defaultFormatterId
? nls.localize('config.needed', "There are multiple formatters for '{0}' files. Select a default formatter to continue.", DefaultFormatter._maybeQuotes(langName))
: nls.localize('config.bad', "Extension '{0}' is configured as formatter but not available. Select a different default formatter to continue.", defaultFormatterId);
if (mode !== FormattingMode.Silent) {
// running from a user action -> show modal dialog so that users configure
// a default formatter
const result = await this._dialogService.confirm({
message,
primaryButton: nls.localize('do.config', "Configure..."),
secondaryButton: nls.localize('cancel', "Cancel")
});
if (result.confirmed) {
return this._pickAndPersistDefaultFormatter(formatter, document);
}
} else {
// no user action -> show a silent notification and proceed
this._notificationService.prompt(
Severity.Info,
message,
[{ label: nls.localize('do.config', "Configure..."), run: () => this._pickAndPersistDefaultFormatter(formatter, document) }],
{ silent: true }
);
}
return undefined;
}
private async _pickAndPersistDefaultFormatter<T extends FormattingEditProvider>(formatter: T[], document: ITextModel): Promise<T | undefined> {
const picks = formatter.map((formatter, index): IIndexedPick => {
return {
index,
label: formatter.displayName || (formatter.extensionId ? formatter.extensionId.value : '?'),
description: formatter.extensionId && formatter.extensionId.value
};
});
const langName = this._languageService.getLanguageName(document.getLanguageId()) || document.getLanguageId();
const pick = await this._quickInputService.pick(picks, { placeHolder: nls.localize('select', "Select a default formatter for '{0}' files", DefaultFormatter._maybeQuotes(langName)) });
if (!pick || !formatter[pick.index].extensionId) {
return undefined;
}
this._configService.updateValue(DefaultFormatter.configName, formatter[pick.index].extensionId!.value, {
resource: document.uri,
overrideIdentifier: document.getLanguageId()
});
return formatter[pick.index];
}
}
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
DefaultFormatter,
LifecyclePhase.Restored
);
Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).registerConfiguration({
...editorConfigurationBaseNode,
properties: {
[DefaultFormatter.configName]: {
description: nls.localize('formatter.default', "Defines a default formatter which takes precedence over all other formatter settings. Must be the identifier of an extension contributing a formatter."),
type: ['string', 'null'],
default: null,
enum: DefaultFormatter.extensionIds,
enumItemLabels: DefaultFormatter.extensionItemLabels,
markdownEnumDescriptions: DefaultFormatter.extensionDescriptions
}
}
});
interface IIndexedPick extends IQuickPickItem {
index: number;
}
function logFormatterTelemetry<T extends { extensionId?: ExtensionIdentifier }>(telemetryService: ITelemetryService, mode: 'document' | 'range', options: T[], pick?: T) {
function extKey(obj: T): string {
return obj.extensionId ? ExtensionIdentifier.toKey(obj.extensionId) : 'unknown';
}
/*
* __GDPR__
"formatterpick" : {
"mode" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"extensions" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"pick" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
telemetryService.publicLog('formatterpick', {
mode,
extensions: options.map(extKey),
pick: pick ? extKey(pick) : 'none'
});
}
async function showFormatterPick(accessor: ServicesAccessor, model: ITextModel, formatters: FormattingEditProvider[]): Promise<number | undefined> {
const quickPickService = accessor.get(IQuickInputService);
const configService = accessor.get(IConfigurationService);
const languageService = accessor.get(ILanguageService);
const overrides = { resource: model.uri, overrideIdentifier: model.getLanguageId() };
const defaultFormatter = configService.getValue<string>(DefaultFormatter.configName, overrides);
let defaultFormatterPick: IIndexedPick | undefined;
const picks = formatters.map((provider, index) => {
const isDefault = ExtensionIdentifier.equals(provider.extensionId, defaultFormatter);
const pick: IIndexedPick = {
index,
label: provider.displayName || '',
description: isDefault ? nls.localize('def', "(default)") : undefined,
};
if (isDefault) {
// autofocus default pick
defaultFormatterPick = pick;
}
return pick;
});
const configurePick: IQuickPickItem = {
label: nls.localize('config', "Configure Default Formatter...")
};
const pick = await quickPickService.pick([...picks, { type: 'separator' }, configurePick],
{
placeHolder: nls.localize('format.placeHolder', "Select a formatter"),
activeItem: defaultFormatterPick
}
);
if (!pick) {
// dismissed
return undefined;
} else if (pick === configurePick) {
// config default
const langName = languageService.getLanguageName(model.getLanguageId()) || model.getLanguageId();
const pick = await quickPickService.pick(picks, { placeHolder: nls.localize('select', "Select a default formatter for '{0}' files", DefaultFormatter._maybeQuotes(langName)) });
if (pick && formatters[pick.index].extensionId) {
configService.updateValue(DefaultFormatter.configName, formatters[pick.index].extensionId!.value, overrides);
}
return undefined;
} else {
// picked one
return (<IIndexedPick>pick).index;
}
}
registerEditorAction(class FormatDocumentMultipleAction extends EditorAction {
constructor() {
super({
id: 'editor.action.formatDocument.multiple',
label: nls.localize('formatDocument.label.multiple', "Format Document With..."),
alias: 'Format Document...',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasMultipleDocumentFormattingProvider),
contextMenuOpts: {
group: '1_modification',
order: 1.3
}
});
}
async run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): Promise<void> {
if (!editor.hasModel()) {
return;
}
const instaService = accessor.get(IInstantiationService);
const telemetryService = accessor.get(ITelemetryService);
const languageFeaturesService = accessor.get(ILanguageFeaturesService);
const model = editor.getModel();
const provider = getRealAndSyntheticDocumentFormattersOrdered(languageFeaturesService.documentFormattingEditProvider, languageFeaturesService.documentRangeFormattingEditProvider, model);
const pick = await instaService.invokeFunction(showFormatterPick, model, provider);
if (typeof pick === 'number') {
await instaService.invokeFunction(formatDocumentWithProvider, provider[pick], editor, FormattingMode.Explicit, CancellationToken.None);
}
logFormatterTelemetry(telemetryService, 'document', provider, typeof pick === 'number' && provider[pick] || undefined);
}
});
registerEditorAction(class FormatSelectionMultipleAction extends EditorAction {
constructor() {
super({
id: 'editor.action.formatSelection.multiple',
label: nls.localize('formatSelection.label.multiple', "Format Selection With..."),
alias: 'Format Code...',
precondition: ContextKeyExpr.and(ContextKeyExpr.and(EditorContextKeys.writable), EditorContextKeys.hasMultipleDocumentSelectionFormattingProvider),
contextMenuOpts: {
when: ContextKeyExpr.and(EditorContextKeys.hasNonEmptySelection),
group: '1_modification',
order: 1.31
}
});
}
async run(accessor: ServicesAccessor, editor: ICodeEditor): Promise<void> {
if (!editor.hasModel()) {
return;
}
const instaService = accessor.get(IInstantiationService);
const languageFeaturesService = accessor.get(ILanguageFeaturesService);
const telemetryService = accessor.get(ITelemetryService);
const model = editor.getModel();
let range: Range = editor.getSelection();
if (range.isEmpty()) {
range = new Range(range.startLineNumber, 1, range.startLineNumber, model.getLineMaxColumn(range.startLineNumber));
}
const provider = languageFeaturesService.documentRangeFormattingEditProvider.ordered(model);
const pick = await instaService.invokeFunction(showFormatterPick, model, provider);
if (typeof pick === 'number') {
await instaService.invokeFunction(formatDocumentRangesWithProvider, provider[pick], editor, range, CancellationToken.None);
}
logFormatterTelemetry(telemetryService, 'range', provider, typeof pick === 'number' && provider[pick] || undefined);
}
});
|