Skip to content

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

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

Merged
merged 13 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@
},
"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.defaultSwiftVersion": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setting should be an enum type, with the options "6", "5", and "Ask Every Run". enumDescriptions should be provided for each option as well. I think the default value for the setting should be 6.

You can see an example of an enum style setting here: https://github.com/swiftlang/vscode-swift/blob/main/package.json#L557. This way the user can't pick an unsupported value, they'll get a warning in the JSON editor and they'll get a nice picker UI in the settings UI page.

Also I think the name defaultSwiftVersion is too generic and may be confusing. This setting only applies when running Swift scripts but could be interpreted as some kind of global. I'd rename it to scriptSwiftLanguageVersion.

"type": [
"number",
null
],
"default": null,
"markdownDescription": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.",
"scope": "machine-overridable"
},
"swift.packageArguments": {
"type": "array",
"default": [],
Expand Down
27 changes: 25 additions & 2 deletions src/commands/runSwiftScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +41,29 @@ export async function runSwiftScript(ctx: WorkspaceContext) {
return;
}

let target: number;

const defaultVersion = configuration.defaultSwiftVersion;
if (defaultVersion !== undefined && defaultVersion !== null) {
target = defaultVersion;
} else {
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;
}

let filename = document.fileName;
let isTempFile = false;
if (document.isUntitled) {
Expand All @@ -52,9 +76,8 @@ export async function runSwiftScript(ctx: WorkspaceContext) {
// otherwise save document
await document.save();
}

const runTask = createSwiftTask(
[filename],
["-swift-version", target.toString(), filename],
`Run ${filename}`,
{
scope: vscode.TaskScope.Global,
Expand Down
3 changes: 3 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ const configuration = {
.get<string[]>("buildArguments", [])
.map(substituteVariablesInString);
},
get defaultSwiftVersion(): number | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of this setting would also have to change to string since the valid options are "6", "5" and `"Ask Every Run"

return vscode.workspace.getConfiguration("swift").get<number>("defaultSwiftVersion");
},
/** swift package arguments */
get packageArguments(): string[] {
return vscode.workspace
Expand Down