Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@
"category": "Swift",
"icon": "$(play)"
},
{
"command": "swift.play",
"title": "Run Swift playground",
"category": "Swift",
"icon": "$(play)"
},
{
"command": "swift.debug",
"title": "Debug Swift executable",
Expand Down Expand Up @@ -1372,6 +1378,10 @@
{
"command": "swift.openEducationalNote",
"when": "false"
},
{
"command": "swift.play",
"when": "false"
}
],
"editor/context": [
Expand Down
9 changes: 9 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { reindexProject } from "./commands/reindexProject";
import { resetPackage } from "./commands/resetPackage";
import restartLSPServer from "./commands/restartLSPServer";
import { runAllTests } from "./commands/runAllTests";
import { runPlayground } from "./commands/runPlayground";
import { runPluginTask } from "./commands/runPluginTask";
import { runSwiftScript } from "./commands/runSwiftScript";
import { runTask } from "./commands/runTask";
Expand Down Expand Up @@ -88,6 +89,7 @@ export function registerToolchainCommands(
export enum Commands {
RUN = "swift.run",
DEBUG = "swift.debug",
PLAY = "swift.play",
CLEAN_BUILD = "swift.cleanBuild",
RESOLVE_DEPENDENCIES = "swift.resolveDependencies",
SHOW_FLAT_DEPENDENCIES_LIST = "swift.flatDependenciesList",
Expand Down Expand Up @@ -146,6 +148,13 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
Commands.DEBUG,
async target => await debugBuild(ctx, ...unwrapTreeItem(target))
),
vscode.commands.registerCommand(Commands.PLAY, async target => {
const folder = ctx.currentFolder;
if (!folder) {
return false;
}
return await runPlayground(folder, ctx.tasks, target);
}),
vscode.commands.registerCommand(Commands.CLEAN_BUILD, async () => await cleanBuild(ctx)),
vscode.commands.registerCommand(
Commands.RUN_TESTS_MULTIPLE_TIMES,
Expand Down
61 changes: 61 additions & 0 deletions src/commands/runPlayground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2025 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import { Location, Range } from "vscode-languageclient";

import { FolderContext } from "../FolderContext";
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
import { TaskManager } from "../tasks/TaskManager";
import { packageName } from "../utilities/tasks";

export interface PlaygroundItem {
id: string;
label?: string;
}

export interface DocumentPlaygroundItem extends PlaygroundItem {
range: Range;
}

export interface WorkspacePlaygroundItem extends PlaygroundItem {
location: Location;
}

/**
* Executes a {@link vscode.Task task} to run swift playground.
*/
export async function runPlayground(
folderContext: FolderContext,
tasks: TaskManager,
item?: PlaygroundItem
) {
if (!item) {
return false;
}
const id = item.label ?? item.id;
const task = createSwiftTask(
["play", id],
`Play "${id}"`,
{
cwd: folderContext.folder,
scope: folderContext.workspaceFolder,
packageName: packageName(folderContext),
presentationOptions: { reveal: vscode.TaskRevealKind.Always },
},
folderContext.toolchain
);

await tasks.executeTaskAndWait(task);
return true;
}
4 changes: 4 additions & 0 deletions src/sourcekit-lsp/LanguageClientConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function initializationOptions(swiftVersion: Version): any {
supportedCommands: {
"swift.run": "swift.run",
"swift.debug": "swift.debug",
"swift.play": "swift.play",
},
},
};
Expand Down Expand Up @@ -254,6 +255,9 @@ export function lspClientOptions(
case "swift.debug":
codelens.command.title = `$(debug)\u00A0${codelens.command.title}`;
break;
case "swift.play":
codelens.command.title = `$(play)\u00A0${codelens.command.title}`;
break;
}
return codelens;
});
Expand Down
12 changes: 8 additions & 4 deletions src/tasks/SwiftTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ export function createSwiftTask(
} else {
cwd = config.cwd.fsPath;
}*/
const env = { ...configuration.swiftEnvironmentVariables, ...swiftRuntimeEnv(), ...cmdEnv };
const env = {
...swiftRuntimeEnv(), // From process.env first
...configuration.swiftEnvironmentVariables, // Then swiftEnvironmentVariables settings
...cmdEnv, // Task configuration takes highest precedence
};
const presentation = config?.presentationOptions ?? {};
if (config?.packageName) {
name += ` (${config?.packageName})`;
Expand Down Expand Up @@ -469,9 +473,9 @@ export class SwiftTaskProvider implements vscode.TaskProvider {
const env = platform?.env ?? task.definition.env;
const fullCwd = resolveTaskCwd(task, platform?.cwd ?? task.definition.cwd);
const fullEnv = {
...configuration.swiftEnvironmentVariables,
...swiftRuntimeEnv(),
...env,
...swiftRuntimeEnv(), // From process.env first
...configuration.swiftEnvironmentVariables, // Then swiftEnvironmentVariables settings
...env, // Task configuration takes highest precedence
};

const presentation = task.definition.presentation ?? task.presentationOptions ?? {};
Expand Down
109 changes: 109 additions & 0 deletions test/integration-tests/commands/runPlayground.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2025 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import { expect } from "chai";
import { stub } from "sinon";
import * as vscode from "vscode";

import { FolderContext } from "@src/FolderContext";
import { WorkspaceContext } from "@src/WorkspaceContext";
import { Commands } from "@src/commands";
import { runPlayground } from "@src/commands/runPlayground";
import { SwiftTask } from "@src/tasks/SwiftTaskProvider";
import { TaskManager } from "@src/tasks/TaskManager";

import { MockedObject, instance, mockObject } from "../../MockUtils";
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";

suite("Run Playground Command", function () {
let folderContext: FolderContext;
let workspaceContext: WorkspaceContext;
let mockTaskManager: MockedObject<TaskManager>;

activateExtensionForSuite({
async setup(ctx) {
workspaceContext = ctx;
folderContext = await folderInRootWorkspace("defaultPackage", workspaceContext);
},
});

setup(async () => {
await workspaceContext.focusFolder(folderContext);
mockTaskManager = mockObject<TaskManager>({ executeTaskAndWait: stub().resolves() });
});

suite("Command", () => {
test("Succeeds", async () => {
expect(
await vscode.commands.executeCommand(Commands.PLAY, {
id: "PackageLib/PackageLib.swift:3",
})
).to.be.true;
});

test("No playground item provided", async () => {
expect(await vscode.commands.executeCommand(Commands.PLAY), undefined).to.be.false;
});

test("No folder focussed", async () => {
await workspaceContext.focusFolder(null);
expect(
await vscode.commands.executeCommand(Commands.PLAY, {
id: "PackageLib/PackageLib.swift:3",
})
).to.be.false;
});
});

suite("Arguments", () => {
test('Runs "swift play" on "id"', async () => {
expect(
await runPlayground(folderContext, instance(mockTaskManager), {
id: "PackageLib/PackageLib.swift:3",
})
).to.be.true;
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;

const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
expect(task.execution.args).to.deep.equal(["play", "PackageLib/PackageLib.swift:3"]);
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
});

test('Runs "swift play" on "id" with space in path', async () => {
expect(
await runPlayground(folderContext, instance(mockTaskManager), {
id: "PackageLib/Package Lib.swift:3",
})
).to.be.true;
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;

const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
expect(task.execution.args).to.deep.equal(["play", "PackageLib/Package Lib.swift:3"]);
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
});

test('Runs "swift play" on "label"', async () => {
expect(
await runPlayground(folderContext, instance(mockTaskManager), {
id: "PackageLib/PackageLib.swift:3",
label: "bar",
})
).to.be.true;
expect(mockTaskManager.executeTaskAndWait).to.have.been.calledOnce;

const task = mockTaskManager.executeTaskAndWait.args[0][0] as SwiftTask;
expect(task.execution.args).to.deep.equal(["play", "bar"]);
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
});
});
});
16 changes: 16 additions & 0 deletions test/unit-tests/sourcekit-lsp/LanguageClientManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,14 @@ suite("LanguageClientManager Suite", () => {
},
isResolved: true,
},
{
range: new vscode.Range(0, 0, 0, 0),
command: {
title: 'Play "bar"',
command: "swift.play",
},
isResolved: true,
},
{
range: new vscode.Range(0, 0, 0, 0),
command: {
Expand Down Expand Up @@ -588,6 +596,14 @@ suite("LanguageClientManager Suite", () => {
},
isResolved: true,
},
{
range: new vscode.Range(0, 0, 0, 0),
command: {
title: '$(play)\u00A0Play "bar"',
command: "swift.play",
},
isResolved: true,
},
{
range: new vscode.Range(0, 0, 0, 0),
command: {
Expand Down
Loading