Skip to content

Commit 962681f

Browse files
feat: secondary quick pick for selecting Swift Version with runSwiftScript command (#1476)
Implement a secondary quick pick for selecting desired Swift Version for Run Swift Script command, and also enabling global settings for it in vscode workspace.
1 parent 91a5b94 commit 962681f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Diff for: package.json

+16
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,22 @@
324324
},
325325
"markdownDescription": "Additional arguments to pass to `swift build` and `swift test`. Keys and values should be provided as individual entries in the list. If you have created a copy of the build task in `tasks.json` then these build arguments will not be propagated to that task."
326326
},
327+
"swift.scriptSwiftLanguageVersion": {
328+
"type": "string",
329+
"enum": [
330+
"6",
331+
"5",
332+
"Ask Every Run"
333+
],
334+
"enumDescriptions": [
335+
"Use Swift 6 when running Swift scripts.",
336+
"Use Swift 5 when running Swift scripts.",
337+
"Prompt to select the Swift version each time a script is run."
338+
],
339+
"default": "6",
340+
"markdownDescription": "The default Swift version to use when running Swift scripts.",
341+
"scope": "machine-overridable"
342+
},
327343
"swift.packageArguments": {
328344
"type": "array",
329345
"default": [],

Diff for: src/commands/runSwiftScript.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as fs from "fs/promises";
1818
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
1919
import { WorkspaceContext } from "../WorkspaceContext";
2020
import { Version } from "../utilities/version";
21+
import configuration from "../configuration";
2122

2223
/**
2324
* Run the active document through the Swift REPL
@@ -40,6 +41,29 @@ export async function runSwiftScript(ctx: WorkspaceContext) {
4041
return;
4142
}
4243

44+
let target: string;
45+
46+
const defaultVersion = configuration.scriptSwiftLanguageVersion;
47+
if (defaultVersion === "Ask Every Run") {
48+
const picked = await vscode.window.showQuickPick(
49+
[
50+
// Potentially add more versions here
51+
{ value: "5", label: "Swift 5" },
52+
{ value: "6", label: "Swift 6" },
53+
],
54+
{
55+
placeHolder: "Select a target Swift version",
56+
}
57+
);
58+
59+
if (!picked) {
60+
return;
61+
}
62+
target = picked.value;
63+
} else {
64+
target = defaultVersion;
65+
}
66+
4367
let filename = document.fileName;
4468
let isTempFile = false;
4569
if (document.isUntitled) {
@@ -52,9 +76,8 @@ export async function runSwiftScript(ctx: WorkspaceContext) {
5276
// otherwise save document
5377
await document.save();
5478
}
55-
5679
const runTask = createSwiftTask(
57-
[filename],
80+
["-swift-version", target, filename],
5881
`Run ${filename}`,
5982
{
6083
scope: vscode.TaskScope.Global,

Diff for: src/configuration.ts

+5
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ const configuration = {
328328
.get<string[]>("buildArguments", [])
329329
.map(substituteVariablesInString);
330330
},
331+
get scriptSwiftLanguageVersion(): string {
332+
return vscode.workspace
333+
.getConfiguration("swift")
334+
.get<string>("scriptSwiftLanguageVersion", "6");
335+
},
331336
/** swift package arguments */
332337
get packageArguments(): string[] {
333338
return vscode.workspace

0 commit comments

Comments
 (0)