Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: secondary quick pick for selecting Swift Version with runSwiftScript command #1476

Merged
merged 13 commits into from
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [],
27 changes: 25 additions & 2 deletions src/commands/runSwiftScript.ts
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 5 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -328,6 +328,11 @@ const configuration = {
.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