diff --git a/package.json b/package.json index 8ffc02a25..30ed73df8 100644 --- a/package.json +++ b/package.json @@ -324,6 +324,22 @@ }, "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." }, + "swift.scriptSwiftLanguageVersion": { + "type": "string", + "enum": [ + "6", + "5", + "Ask Every Run" + ], + "enumDescriptions": [ + "Use Swift 6 when running Swift scripts.", + "Use Swift 5 when running Swift scripts.", + "Prompt to select the Swift version each time a script is run." + ], + "default": "6", + "markdownDescription": "The default Swift version to use when running Swift scripts.", + "scope": "machine-overridable" + }, "swift.packageArguments": { "type": "array", "default": [], diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 5eb77bfc9..713534160 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -18,6 +18,7 @@ import * as fs from "fs/promises"; import { createSwiftTask } from "../tasks/SwiftTaskProvider"; import { WorkspaceContext } from "../WorkspaceContext"; import { Version } from "../utilities/version"; +import configuration from "../configuration"; /** * Run the active document through the Swift REPL @@ -40,6 +41,29 @@ export async function runSwiftScript(ctx: WorkspaceContext) { return; } + let target: string; + + const defaultVersion = configuration.scriptSwiftLanguageVersion; + if (defaultVersion === "Ask Every Run") { + const picked = await vscode.window.showQuickPick( + [ + // Potentially add more versions here + { value: "5", label: "Swift 5" }, + { value: "6", label: "Swift 6" }, + ], + { + placeHolder: "Select a target Swift version", + } + ); + + if (!picked) { + return; + } + target = picked.value; + } else { + target = defaultVersion; + } + let filename = document.fileName; let isTempFile = false; if (document.isUntitled) { @@ -52,9 +76,8 @@ export async function runSwiftScript(ctx: WorkspaceContext) { // otherwise save document await document.save(); } - const runTask = createSwiftTask( - [filename], + ["-swift-version", target, filename], `Run ${filename}`, { scope: vscode.TaskScope.Global, diff --git a/src/configuration.ts b/src/configuration.ts index e4bf5eb30..1cdfceca4 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -328,6 +328,11 @@ const configuration = { .get("buildArguments", []) .map(substituteVariablesInString); }, + get scriptSwiftLanguageVersion(): string { + return vscode.workspace + .getConfiguration("swift") + .get("scriptSwiftLanguageVersion", "6"); + }, /** swift package arguments */ get packageArguments(): string[] { return vscode.workspace