Skip to content
Draft
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
6 changes: 6 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,10 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
Commands.DEBUG,
async target => await debugBuild(ctx, ...unwrapTreeItem(target))
),
vscode.commands.registerCommand(
Commands.PLAY,
async target => await runPlayground(ctx, 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 { WorkspaceContext } from "../WorkspaceContext";
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
import { packageName } from "../utilities/tasks";

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

export interface DocumentPlaygroundItem extends PlaygroundItem {
id: string;
label?: string;
range: Range;
}

export interface WorkspacePlaygroundItem extends PlaygroundItem {
id: string;
label?: string;
location: Location;
}

/**
* Executes a {@link vscode.Task task} to run swift playground.
*/
export async function runPlayground(ctx: WorkspaceContext, item?: PlaygroundItem) {
const folderContext = ctx.currentFolder;
if (!folderContext || !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 vscode.tasks.executeTask(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