From 7fb0b883c5b3f61cb30211b0883d5e0f3d154418 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Tue, 1 Apr 2025 21:43:26 -0600 Subject: [PATCH 01/12] feat: secondary quick pick for selecting Swift Version with `runSwiftScript` command --- src/commands/runSwiftScript.ts | 67 +++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 5eb77bfc9..4aa6abba6 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -40,33 +40,50 @@ export async function runSwiftScript(ctx: WorkspaceContext) { return; } - let filename = document.fileName; - let isTempFile = false; - if (document.isUntitled) { - // if document hasn't been saved, save it to a temporary file - isTempFile = true; - filename = ctx.tempFolder.filename(document.fileName, "swift"); - const text = document.getText(); - await fs.writeFile(filename, text); - } else { - // otherwise save document - await document.save(); - } - - const runTask = createSwiftTask( - [filename], - `Run ${filename}`, + const picked = await vscode.window.showQuickPick( + [ + // Potnetially add more versions here + { value: 5, label: "Swift 5" }, + { value: 6, label: "Swift 6" }, + ], { - scope: vscode.TaskScope.Global, - cwd: vscode.Uri.file(path.dirname(filename)), - presentationOptions: { reveal: vscode.TaskRevealKind.Always, clear: true }, - }, - ctx.toolchain + placeHolder: "Select a target Swift version", + } ); - await ctx.tasks.executeTaskAndWait(runTask); - // delete file after running swift - if (isTempFile) { - await fs.rm(filename); + if (!picked) { + return; + } + + if (picked) { + const target = picked.value; + let filename = document.fileName; + let isTempFile = false; + if (document.isUntitled) { + // if document hasn't been saved, save it to a temporary file + isTempFile = true; + filename = ctx.tempFolder.filename(document.fileName, "swift"); + const text = document.getText(); + await fs.writeFile(filename, text); + } else { + // otherwise save document + await document.save(); + } + const runTask = createSwiftTask( + ["-swift-version", target.toString(), filename], + `Run ${filename}`, + { + scope: vscode.TaskScope.Global, + cwd: vscode.Uri.file(path.dirname(filename)), + presentationOptions: { reveal: vscode.TaskRevealKind.Always, clear: true }, + }, + ctx.toolchain + ); + await ctx.tasks.executeTaskAndWait(runTask); + + // delete file after running swift + if (isTempFile) { + await fs.rm(filename); + } } } From 698039d75ced26c60e0ab4576418c5fa885f4470 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 18:03:44 -0600 Subject: [PATCH 02/12] chore: fix spelling --- src/commands/runSwiftScript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 4aa6abba6..b42991817 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -42,7 +42,7 @@ export async function runSwiftScript(ctx: WorkspaceContext) { const picked = await vscode.window.showQuickPick( [ - // Potnetially add more versions here + // Potentially add more versions here { value: 5, label: "Swift 5" }, { value: 6, label: "Swift 6" }, ], From 0cdc25fc06600b1a195d198871586868f1c31300 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 18:36:41 -0600 Subject: [PATCH 03/12] feat: add Swift version configuration to package.json --- package.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/package.json b/package.json index 8ffc02a25..7698456da 100644 --- a/package.json +++ b/package.json @@ -324,6 +324,19 @@ }, "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.script.defaultSwiftVersion": { + "type": "number", + "default": null, + "description": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", + "enum": [ + 5, + 6 + ], + "enumDescriptions": [ + "Swift 5", + "Swift 6" + ] + }, "swift.packageArguments": { "type": "array", "default": [], From d779f90ed36a93e77251c1eae88c31aec896645c Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 18:46:16 -0600 Subject: [PATCH 04/12] feat: add `defaultSwiftVersion` to configuration & reads from `runSwiftScript` --- src/commands/runSwiftScript.ts | 86 ++++++++++++++++++---------------- src/configuration.ts | 3 ++ 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index b42991817..62733f437 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,50 +41,55 @@ export async function runSwiftScript(ctx: WorkspaceContext) { return; } - 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", - } - ); + let target: number; - if (!picked) { - return; - } - - if (picked) { - const target = picked.value; - let filename = document.fileName; - let isTempFile = false; - if (document.isUntitled) { - // if document hasn't been saved, save it to a temporary file - isTempFile = true; - filename = ctx.tempFolder.filename(document.fileName, "swift"); - const text = document.getText(); - await fs.writeFile(filename, text); - } else { - // otherwise save document - await document.save(); - } - const runTask = createSwiftTask( - ["-swift-version", target.toString(), filename], - `Run ${filename}`, + const defaultVersion = configuration.defaultSwiftVersion; + if (defaultVersion !== undefined) { + target = defaultVersion; + } else { + const picked = await vscode.window.showQuickPick( + [ + // Potentially add more versions here + { value: 5, label: "Swift 5" }, + { value: 6, label: "Swift 6" }, + ], { - scope: vscode.TaskScope.Global, - cwd: vscode.Uri.file(path.dirname(filename)), - presentationOptions: { reveal: vscode.TaskRevealKind.Always, clear: true }, - }, - ctx.toolchain + placeHolder: "Select a target Swift version", + } ); - await ctx.tasks.executeTaskAndWait(runTask); - // delete file after running swift - if (isTempFile) { - await fs.rm(filename); + if (!picked) { + return; } + target = picked.value; + } + + let filename = document.fileName; + let isTempFile = false; + if (document.isUntitled) { + // if document hasn't been saved, save it to a temporary file + isTempFile = true; + filename = ctx.tempFolder.filename(document.fileName, "swift"); + const text = document.getText(); + await fs.writeFile(filename, text); + } else { + // otherwise save document + await document.save(); + } + const runTask = createSwiftTask( + ["-swift-version", target.toString(), filename], + `Run ${filename}`, + { + scope: vscode.TaskScope.Global, + cwd: vscode.Uri.file(path.dirname(filename)), + presentationOptions: { reveal: vscode.TaskRevealKind.Always, clear: true }, + }, + ctx.toolchain + ); + await ctx.tasks.executeTaskAndWait(runTask); + + // delete file after running swift + if (isTempFile) { + await fs.rm(filename); } } diff --git a/src/configuration.ts b/src/configuration.ts index e4bf5eb30..c7a33881c 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -328,6 +328,9 @@ const configuration = { .get<string[]>("buildArguments", []) .map(substituteVariablesInString); }, + get defaultSwiftVersion(): number | undefined { + return vscode.workspace.getConfiguration("swift").get<number>("script.defaultSwiftVersion"); + }, /** swift package arguments */ get packageArguments(): string[] { return vscode.workspace From fec0b941b85fa33f314abf684fd7676de3c1a905 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 18:48:30 -0600 Subject: [PATCH 05/12] chore: remove extra `script` args in configuration --- package.json | 2 +- src/configuration.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7698456da..a5ff24921 100644 --- a/package.json +++ b/package.json @@ -324,7 +324,7 @@ }, "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.script.defaultSwiftVersion": { + "swift.defaultSwiftVersion": { "type": "number", "default": null, "description": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", diff --git a/src/configuration.ts b/src/configuration.ts index c7a33881c..ff1ff2ef6 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -329,7 +329,7 @@ const configuration = { .map(substituteVariablesInString); }, get defaultSwiftVersion(): number | undefined { - return vscode.workspace.getConfiguration("swift").get<number>("script.defaultSwiftVersion"); + return vscode.workspace.getConfiguration("swift").get<number>("defaultSwiftVersion"); }, /** swift package arguments */ get packageArguments(): string[] { From 49b31311bd329c89302391f4d185a236a114ceab Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 18:51:20 -0600 Subject: [PATCH 06/12] feat: use markdownDescription in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a5ff24921..4e11df00e 100644 --- a/package.json +++ b/package.json @@ -327,7 +327,7 @@ "swift.defaultSwiftVersion": { "type": "number", "default": null, - "description": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", + "markdownDescription": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", "enum": [ 5, 6 From 00181ccc2ab867211835f31df65c835de7aa7c9c Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Wed, 2 Apr 2025 19:48:40 -0600 Subject: [PATCH 07/12] chore: fix package.json default swift version type & null check --- package.json | 14 +++++--------- src/commands/runSwiftScript.ts | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 4e11df00e..a96edc03a 100644 --- a/package.json +++ b/package.json @@ -325,17 +325,13 @@ "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": { - "type": "number", + "type": [ + "number", + null + ], "default": null, "markdownDescription": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", - "enum": [ - 5, - 6 - ], - "enumDescriptions": [ - "Swift 5", - "Swift 6" - ] + "scope": "machine-overridable" }, "swift.packageArguments": { "type": "array", diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 62733f437..4a877167c 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -44,7 +44,7 @@ export async function runSwiftScript(ctx: WorkspaceContext) { let target: number; const defaultVersion = configuration.defaultSwiftVersion; - if (defaultVersion !== undefined) { + if (defaultVersion !== undefined && defaultVersion !== null) { target = defaultVersion; } else { const picked = await vscode.window.showQuickPick( From 759e17b35b645819187800fe953ec406ed614e76 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Thu, 3 Apr 2025 23:17:04 -0600 Subject: [PATCH 08/12] fix: rename `defaultSwiftVersion` to `scriptSwiftLanguageVersion` --- package.json | 2 +- src/commands/runSwiftScript.ts | 2 +- src/configuration.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a96edc03a..0566817f1 100644 --- a/package.json +++ b/package.json @@ -324,7 +324,7 @@ }, "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": { + "swift.scriptSwiftLanguageVersion": { "type": [ "number", null diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 4a877167c..4cd8b8081 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -43,7 +43,7 @@ export async function runSwiftScript(ctx: WorkspaceContext) { let target: number; - const defaultVersion = configuration.defaultSwiftVersion; + const defaultVersion = configuration.scriptSwiftLanguageVersion; if (defaultVersion !== undefined && defaultVersion !== null) { target = defaultVersion; } else { diff --git a/src/configuration.ts b/src/configuration.ts index ff1ff2ef6..f95908932 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -328,8 +328,8 @@ const configuration = { .get<string[]>("buildArguments", []) .map(substituteVariablesInString); }, - get defaultSwiftVersion(): number | undefined { - return vscode.workspace.getConfiguration("swift").get<number>("defaultSwiftVersion"); + get scriptSwiftLanguageVersion(): number | undefined { + return vscode.workspace.getConfiguration("swift").get<number>("scriptSwiftLanguageVersion"); }, /** swift package arguments */ get packageArguments(): string[] { From a3a5dbf254d89e4b0f77ffe6efa2a3ad9108d475 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Thu, 3 Apr 2025 23:19:56 -0600 Subject: [PATCH 09/12] fix: make picker type `enum` --- package.json | 15 +++++++++++---- src/commands/runSwiftScript.ts | 10 +++++----- src/configuration.ts | 6 ++++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 0566817f1..30c3643d3 100644 --- a/package.json +++ b/package.json @@ -325,11 +325,18 @@ "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": [ - "number", - null + "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": null, + "default": "6", "markdownDescription": "The default Swift version to use when running Swift scripts. If set, skips the version selection dialog.", "scope": "machine-overridable" }, diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index 4cd8b8081..d5c50aa29 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -41,17 +41,17 @@ export async function runSwiftScript(ctx: WorkspaceContext) { return; } - let target: number; + let target: string; const defaultVersion = configuration.scriptSwiftLanguageVersion; - if (defaultVersion !== undefined && defaultVersion !== null) { + if (defaultVersion !== "") { target = defaultVersion; } else { const picked = await vscode.window.showQuickPick( [ // Potentially add more versions here - { value: 5, label: "Swift 5" }, - { value: 6, label: "Swift 6" }, + { value: "5", label: "Swift 5" }, + { value: "6", label: "Swift 6" }, ], { placeHolder: "Select a target Swift version", @@ -77,7 +77,7 @@ export async function runSwiftScript(ctx: WorkspaceContext) { await document.save(); } const runTask = createSwiftTask( - ["-swift-version", target.toString(), filename], + ["-swift-version", target, filename], `Run ${filename}`, { scope: vscode.TaskScope.Global, diff --git a/src/configuration.ts b/src/configuration.ts index f95908932..7bf55a38a 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -328,8 +328,10 @@ const configuration = { .get<string[]>("buildArguments", []) .map(substituteVariablesInString); }, - get scriptSwiftLanguageVersion(): number | undefined { - return vscode.workspace.getConfiguration("swift").get<number>("scriptSwiftLanguageVersion"); + get scriptSwiftLanguageVersion(): string { + return vscode.workspace + .getConfiguration("swift") + .get<string>("scriptSwiftLanguageVersion", ""); }, /** swift package arguments */ get packageArguments(): string[] { From a4774054e3fd5daa7b13d74d21db272cbe571aa8 Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Thu, 3 Apr 2025 23:25:18 -0600 Subject: [PATCH 10/12] chore: make getter default to 6 --- src/configuration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/configuration.ts b/src/configuration.ts index 7bf55a38a..1cdfceca4 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -331,7 +331,7 @@ const configuration = { get scriptSwiftLanguageVersion(): string { return vscode.workspace .getConfiguration("swift") - .get<string>("scriptSwiftLanguageVersion", ""); + .get<string>("scriptSwiftLanguageVersion", "6"); }, /** swift package arguments */ get packageArguments(): string[] { From 9553944fa935122a56d7c977c68f2fcdaf0bb48e Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Thu, 3 Apr 2025 23:25:36 -0600 Subject: [PATCH 11/12] feat: show picker when selected `Ask Every Run` --- src/commands/runSwiftScript.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/runSwiftScript.ts b/src/commands/runSwiftScript.ts index d5c50aa29..713534160 100644 --- a/src/commands/runSwiftScript.ts +++ b/src/commands/runSwiftScript.ts @@ -44,9 +44,7 @@ export async function runSwiftScript(ctx: WorkspaceContext) { let target: string; const defaultVersion = configuration.scriptSwiftLanguageVersion; - if (defaultVersion !== "") { - target = defaultVersion; - } else { + if (defaultVersion === "Ask Every Run") { const picked = await vscode.window.showQuickPick( [ // Potentially add more versions here @@ -62,6 +60,8 @@ export async function runSwiftScript(ctx: WorkspaceContext) { return; } target = picked.value; + } else { + target = defaultVersion; } let filename = document.fileName; From de87bcfbb11eb9e2335b12f4a891e9dcdfe6656f Mon Sep 17 00:00:00 2001 From: Louis Qian <yuq4@illinois.edu> Date: Fri, 4 Apr 2025 17:48:33 -0500 Subject: [PATCH 12/12] chore: remove unnecessary markdown description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 30c3643d3..30ed73df8 100644 --- a/package.json +++ b/package.json @@ -337,7 +337,7 @@ "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. If set, skips the version selection dialog.", + "markdownDescription": "The default Swift version to use when running Swift scripts.", "scope": "machine-overridable" }, "swift.packageArguments": {