-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathconfiguration.ts
More file actions
559 lines (537 loc) · 22.8 KB
/
Copy pathconfiguration.ts
File metadata and controls
559 lines (537 loc) · 22.8 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import * as os from "os";
import * as path from "path";
import { showReloadExtensionNotification } from "./ui/ReloadExtension";
import { WorkspaceContext } from "./WorkspaceContext";
export type DebugAdapters = "auto" | "lldb-dap" | "CodeLLDB";
export type SetupCodeLLDBOptions =
| "prompt"
| "alwaysUpdateGlobal"
| "alwaysUpdateWorkspace"
| "never";
export type CFamilySupportOptions = "enable" | "disable" | "cpptools-inactive";
export type ActionAfterBuildError = "Focus Problems" | "Focus Terminal" | "Do Nothing";
export type OpenAfterCreateNewProjectOptions =
| "always"
| "alwaysNewWindow"
| "whenNoFolderOpen"
| "prompt";
export type ShowBuildStatusOptions = "never" | "swiftStatus" | "progress" | "notification";
export type DiagnosticCollectionOptions =
| "onlySwiftc"
| "onlySourceKit"
| "keepSwiftc"
| "keepSourceKit"
| "keepAll";
export type DiagnosticStyle = "default" | "llvm" | "swift";
/** sourcekit-lsp configuration */
export interface LSPConfiguration {
/** Path to sourcekit-lsp executable */
readonly serverPath: string;
/** Arguments to pass to sourcekit-lsp executable */
readonly serverArguments: string[];
/** Are inlay hints enabled */
readonly inlayHintsEnabled: boolean;
/** Support C Family source files */
readonly supportCFamily: CFamilySupportOptions;
/** Support Languages */
readonly supportedLanguages: string[];
/** Is SourceKit-LSP disabled */
readonly disable: boolean;
}
/** debugger configuration */
export interface DebuggerConfiguration {
/** Get the underlying debug adapter type requested by the user. */
readonly debugAdapter: DebugAdapters;
/** Return path to debug adapter */
readonly customDebugAdapterPath: string;
/** Whether or not to disable setting up the debugger */
readonly disable: boolean;
/** User choices for updating CodeLLDB settings */
readonly setupCodeLLDB: SetupCodeLLDBOptions;
}
/** workspace folder configuration */
export interface FolderConfiguration {
/** Environment variables to set when running tests */
readonly testEnvironmentVariables: { [key: string]: string };
/** Extra arguments to set when building tests */
readonly additionalTestArguments: string[];
/** search sub-folder of workspace folder for Swift Packages */
readonly searchSubfoldersForPackages: boolean;
/** auto-generate launch.json configurations */
readonly autoGenerateLaunchConfigurations: boolean;
/** disable automatic running of swift package resolve */
readonly disableAutoResolve: boolean;
/** location to save swift-testing attachments */
readonly attachmentsPath: string;
/** look up saved permissions for the supplied plugin */
pluginPermissions(pluginId?: string): PluginPermissionConfiguration;
/** look up saved arguments for the supplied plugin, or global plugin arguments if no plugin id is provided */
pluginArguments(pluginId?: string): string[];
}
export interface PluginPermissionConfiguration {
/** Disable using the sandbox when executing plugins */
disableSandbox?: boolean;
/** Allow the plugin to write to the package directory */
allowWritingToPackageDirectory?: boolean;
/** Allow the plugin to write to an additional directory or directories */
allowWritingToDirectory?: string | string[];
/**
* Allow the plugin to make network connections
* For a list of valid options see:
* https://github.com/swiftlang/swift-package-manager/blob/0401a2ae55077cfd1f4c0acd43ae0a1a56ab21ef/Sources/Commands/PackageCommands/PluginCommand.swift#L62
*/
allowNetworkConnections?: string;
}
/**
* Type-safe wrapper around configuration settings.
*/
const configuration = {
/** sourcekit-lsp configuration */
get lsp(): LSPConfiguration {
return {
get serverPath(): string {
return substituteVariablesInString(
vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get<string>("serverPath", "")
);
},
get serverArguments(): string[] {
return vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get<string[]>("serverArguments", [])
.map(substituteVariablesInString);
},
get inlayHintsEnabled(): boolean {
return vscode.workspace
.getConfiguration("sourcekit-lsp")
.get<boolean>("inlayHints.enabled", true);
},
get supportCFamily(): CFamilySupportOptions {
return vscode.workspace
.getConfiguration("sourcekit-lsp")
.get<CFamilySupportOptions>("support-c-cpp", "cpptools-inactive");
},
get supportedLanguages() {
return vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get("supported-languages", [
"swift",
"c",
"cpp",
"objective-c",
"objective-cpp",
]);
},
get disable(): boolean {
return vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get<boolean>("disable", false);
},
};
},
folder(workspaceFolder: vscode.WorkspaceFolder): FolderConfiguration {
function pluginSetting<T>(
setting: string,
pluginId?: string,
resultIsArray: boolean = false
): T | undefined {
if (!pluginId) {
// Check for * as a wildcard plugin ID for configurations that want both
// global arguments as well as specific additional arguments for a plugin.
const wildcardSetting = pluginSetting(setting, "*", resultIsArray) as T | undefined;
if (wildcardSetting) {
return wildcardSetting;
}
// Check if there is a global setting like `"swift.pluginArguments": ["-c", "release"]`
// that should apply to all plugins.
const args = vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<T>(setting);
if (resultIsArray && Array.isArray(args)) {
return args;
} else if (
!resultIsArray &&
args !== null &&
typeof args === "object" &&
Object.keys(args).length !== 0
) {
return args;
}
return undefined;
}
return vscode.workspace.getConfiguration("swift", workspaceFolder).get<{
[key: string]: T;
}>(setting, {})[pluginId];
}
return {
/** Environment variables to set when running tests */
get testEnvironmentVariables(): { [key: string]: string } {
return vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<{ [key: string]: string }>("testEnvironmentVariables", {});
},
/** Extra arguments to pass to swift test and swift build when running and debugging tests. */
get additionalTestArguments(): string[] {
return vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<string[]>("additionalTestArguments", [])
.map(substituteVariablesInString);
},
/** auto-generate launch.json configurations */
get autoGenerateLaunchConfigurations(): boolean {
return vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<boolean>("autoGenerateLaunchConfigurations", true);
},
/** disable automatic running of swift package resolve */
get disableAutoResolve(): boolean {
return vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<boolean>("disableAutoResolve", false);
},
/** search sub-folder of workspace folder for Swift Packages */
get searchSubfoldersForPackages(): boolean {
return vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<boolean>("searchSubfoldersForPackages", false);
},
get attachmentsPath(): string {
return substituteVariablesInString(
vscode.workspace
.getConfiguration("swift", workspaceFolder)
.get<string>("attachmentsPath", "./.build/attachments")
);
},
pluginPermissions(pluginId?: string): PluginPermissionConfiguration {
return pluginSetting("pluginPermissions", pluginId, false) ?? {};
},
pluginArguments(pluginId?: string): string[] {
return pluginSetting("pluginArguments", pluginId, true) ?? [];
},
};
},
/** debugger configuration */
get debugger(): DebuggerConfiguration {
return {
get debugAdapter(): DebugAdapters {
// Use inspect to determine if the user has explicitly set swift.debugger.useDebugAdapterFromToolchain
const inspectUseDebugAdapterFromToolchain = vscode.workspace
.getConfiguration("swift.debugger")
.inspect<boolean>("useDebugAdapterFromToolchain");
let useDebugAdapterFromToolchain =
inspectUseDebugAdapterFromToolchain?.workspaceValue ??
inspectUseDebugAdapterFromToolchain?.globalValue;
// On Windows arm64 we enable swift.debugger.useDebugAdapterFromToolchain by default since CodeLLDB does
// not support this platform and gives an awful error message.
if (process.platform === "win32" && process.arch === "arm64") {
useDebugAdapterFromToolchain = useDebugAdapterFromToolchain ?? true;
}
const selectedAdapter = vscode.workspace
.getConfiguration("swift.debugger")
.get<DebugAdapters>("debugAdapter", "auto");
switch (selectedAdapter) {
case "auto":
if (useDebugAdapterFromToolchain !== undefined) {
return useDebugAdapterFromToolchain ? "lldb-dap" : "CodeLLDB";
}
return "auto";
default:
return selectedAdapter;
}
},
get customDebugAdapterPath(): string {
return substituteVariablesInString(
vscode.workspace.getConfiguration("swift.debugger").get<string>("path", "")
);
},
get disable(): boolean {
return vscode.workspace
.getConfiguration("swift.debugger")
.get<boolean>("disable", false);
},
get setupCodeLLDB(): SetupCodeLLDBOptions {
return vscode.workspace
.getConfiguration("swift.debugger")
.get<SetupCodeLLDBOptions>("setupCodeLLDB", "prompt");
},
};
},
/** Files and directories to exclude from the code coverage. */
get excludeFromCodeCoverage(): string[] {
return vscode.workspace
.getConfiguration("swift")
.get<string[]>("excludeFromCodeCoverage", [])
.map(substituteVariablesInString);
},
/** Files and directories to exclude from the Package Dependencies view. */
get excludePathsFromPackageDependencies(): string[] {
return vscode.workspace
.getConfiguration("swift")
.get<string[]>("excludePathsFromPackageDependencies", []);
},
/** Path to folder that include swift executable */
get path(): string {
return substituteVariablesInString(
vscode.workspace.getConfiguration("swift").get<string>("path", "")
);
},
/** Path to folder that include swift runtime */
get runtimePath(): string {
return substituteVariablesInString(
vscode.workspace.getConfiguration("swift").get<string>("runtimePath", "")
);
},
/** Path to custom --sdk */
get sdk(): string {
return substituteVariablesInString(
vscode.workspace.getConfiguration("swift").get<string>("SDK", "")
);
},
set sdk(value: string | undefined) {
vscode.workspace.getConfiguration("swift").update("SDK", value);
},
/** Path to custom --swift-sdk */
get swiftSDK(): string {
return vscode.workspace.getConfiguration("swift").get<string>("swiftSDK", "");
},
set swiftSDK(value: string | undefined) {
vscode.workspace.getConfiguration("swift").update("swiftSDK", value);
},
/** swift build arguments */
get buildArguments(): string[] {
return vscode.workspace
.getConfiguration("swift")
.get<string[]>("buildArguments", [])
.map(substituteVariablesInString);
},
get scriptSwiftLanguageVersion(): string {
return vscode.workspace
.getConfiguration("swift")
.get<string>("scriptSwiftLanguageVersion", "6");
},
/** swift package arguments */
get packageArguments(): string[] {
return vscode.workspace
.getConfiguration("swift")
.get<string[]>("packageArguments", [])
.map(substituteVariablesInString);
},
/** thread/address sanitizer */
get sanitizer(): string {
return vscode.workspace.getConfiguration("swift").get<string>("sanitizer", "off");
},
get buildPath(): string {
return substituteVariablesInString(
vscode.workspace.getConfiguration("swift").get<string>("buildPath", "")
);
},
get disableSwiftPMIntegration(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("disableSwiftPackageManagerIntegration", false);
},
/** Environment variables to set when building */
get swiftEnvironmentVariables(): { [key: string]: string } {
return vscode.workspace
.getConfiguration("swift")
.get<{ [key: string]: string }>("swiftEnvironmentVariables", {});
},
/** include build errors in problems view */
get diagnosticsCollection(): DiagnosticCollectionOptions {
return vscode.workspace
.getConfiguration("swift")
.get<DiagnosticCollectionOptions>("diagnosticsCollection", "keepSourceKit");
},
/** set the -diagnostic-style option when running `swift` tasks */
get diagnosticsStyle(): DiagnosticStyle {
return vscode.workspace
.getConfiguration("swift")
.get<DiagnosticStyle>("diagnosticsStyle", "llvm");
},
/** where to show the build progress for the running task */
get showBuildStatus(): ShowBuildStatusOptions {
return vscode.workspace
.getConfiguration("swift")
.get<ShowBuildStatusOptions>("showBuildStatus", "swiftStatus");
},
/** background compilation */
get backgroundCompilation(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("backgroundCompilation", false);
},
/** background indexing */
get backgroundIndexing(): "on" | "off" | "auto" {
const value = vscode.workspace
.getConfiguration("swift.sourcekit-lsp")
.get("backgroundIndexing", "auto");
// Legacy versions of this setting were a boolean, convert to the new string version.
if (typeof value === "boolean") {
return value ? "on" : "off";
} else {
return value;
}
},
/** focus on problems view whenever there is a build error */
get actionAfterBuildError(): ActionAfterBuildError {
return vscode.workspace
.getConfiguration("swift")
.get<ActionAfterBuildError>("actionAfterBuildError", "Focus Terminal");
},
/** output additional diagnostics */
get diagnostics(): boolean {
return vscode.workspace.getConfiguration("swift").get<boolean>("diagnostics", false);
},
/**
* Test coverage settings
*/
/** Should test coverage report be displayed after running test coverage */
get displayCoverageReportAfterRun(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("coverage.displayReportAfterRun", true);
},
get alwaysShowCoverageStatusItem(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("coverage.alwaysShowStatusItem", true);
},
get coverageHitColorLightMode(): string {
return vscode.workspace
.getConfiguration("swift")
.get<string>("coverage.colors.lightMode.hit", "#c0ffc0");
},
get coverageMissColorLightMode(): string {
return vscode.workspace
.getConfiguration("swift")
.get<string>("coverage.colors.lightMode.miss", "#ffc0c0");
},
get coverageHitColorDarkMode(): string {
return vscode.workspace
.getConfiguration("swift")
.get<string>("coverage.colors.darkMode.hit", "#003000");
},
get coverageMissColorDarkMode(): string {
return vscode.workspace
.getConfiguration("swift")
.get<string>("coverage.colors.darkMode.miss", "#400000");
},
get openAfterCreateNewProject(): OpenAfterCreateNewProjectOptions {
return vscode.workspace
.getConfiguration("swift")
.get<OpenAfterCreateNewProjectOptions>("openAfterCreateNewProject", "prompt");
},
/** Whether or not the extension should warn about being unable to create symlinks on Windows */
get warnAboutSymlinkCreation(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("warnAboutSymlinkCreation", true);
},
set warnAboutSymlinkCreation(value: boolean) {
vscode.workspace
.getConfiguration("swift")
.update("warnAboutSymlinkCreation", value, vscode.ConfigurationTarget.Global);
},
/** Whether or not the extension will contribute Swift environment variables to the integrated terminal */
get enableTerminalEnvironment(): boolean {
return vscode.workspace
.getConfiguration("swift")
.get<boolean>("enableTerminalEnvironment", true);
},
/** Whether or not to disable SwiftPM sandboxing */
get disableSandbox(): boolean {
return vscode.workspace.getConfiguration("swift").get<boolean>("disableSandbox", false);
},
};
const vsCodeVariableRegex = new RegExp(/\$\{(.+?)\}/g);
export function substituteVariablesInString(val: string): string {
return val.replace(vsCodeVariableRegex, (substring: string, varName: string) =>
typeof varName === "string" ? computeVscodeVar(varName) || substring : substring
);
}
function computeVscodeVar(varName: string): string | null {
const workspaceFolder = () => {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
const documentUri = activeEditor.document.uri;
const folder = vscode.workspace.getWorkspaceFolder(documentUri);
if (folder) {
return folder.uri.fsPath;
}
}
// If there is no active editor then return the first workspace folder
return vscode.workspace.workspaceFolders?.at(0)?.uri.fsPath ?? "";
};
const file = () => vscode.window.activeTextEditor?.document?.uri?.fsPath || "";
const regex = /workspaceFolder:(.*)/gm;
const match = regex.exec(varName);
if (match) {
const name = match[1];
return vscode.workspace.workspaceFolders?.find(f => f.name === name)?.uri.fsPath ?? null;
}
// https://code.visualstudio.com/docs/editor/variables-reference
// Variables to be substituted should be added here.
const supportedVariables: { [k: string]: () => string } = {
workspaceFolder,
fileWorkspaceFolder: workspaceFolder,
workspaceFolderBasename: () => path.basename(workspaceFolder()),
cwd: () => process.cwd(),
userHome: () => os.homedir(),
pathSeparator: () => path.sep,
file,
relativeFile: () => path.relative(workspaceFolder(), file()),
relativeFileDirname: () => path.dirname(path.relative(workspaceFolder(), file())),
fileBasename: () => path.basename(file()),
fileExtname: () => path.extname(file()),
fileDirname: () => path.dirname(file()),
fileDirnameBasename: () => path.basename(path.dirname(file())),
};
return varName in supportedVariables ? supportedVariables[varName]() : null;
}
/**
* Handler for configuration change events that triggers a reload of the extension
* if the setting changed requires one.
* @param ctx The workspace context.
* @returns A disposable that unregisters the provider when disposed.
*/
export function handleConfigurationChangeEvent(
ctx: WorkspaceContext
): (event: vscode.ConfigurationChangeEvent) => void {
return (event: vscode.ConfigurationChangeEvent) => {
// on toolchain config change, reload window
if (
event.affectsConfiguration("swift.path") &&
configuration.path !== ctx.currentFolder?.toolchain.swiftFolderPath
) {
showReloadExtensionNotification(
"Changing the Swift path requires Visual Studio Code be reloaded."
);
} else if (
// on sdk config change, restart sourcekit-lsp
event.affectsConfiguration("swift.SDK") ||
event.affectsConfiguration("swift.swiftSDK")
) {
vscode.commands.executeCommand("swift.restartLSPServer");
} else if (event.affectsConfiguration("swift.swiftEnvironmentVariables")) {
showReloadExtensionNotification(
"Changing environment variables requires the project be reloaded."
);
}
};
}
export default configuration;