Skip to content

Commit 5b2013b

Browse files
committed
Support "swift.play" CodeLens
- Advertise to the LSP we support "swift.play" - Add a "swift.play" command, hiding it from the command palette - Fix order of env variables passed to task so variables like DYLD_LIBRARY_PATH are not overwritten accidentally Issue: #1782
1 parent 320b378 commit 5b2013b

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@
289289
"category": "Swift",
290290
"icon": "$(play)"
291291
},
292+
{
293+
"command": "swift.play",
294+
"title": "Run Swift playground",
295+
"category": "Swift",
296+
"icon": "$(play)"
297+
},
292298
{
293299
"command": "swift.debug",
294300
"title": "Debug Swift executable",
@@ -1372,6 +1378,10 @@
13721378
{
13731379
"command": "swift.openEducationalNote",
13741380
"when": "false"
1381+
},
1382+
{
1383+
"command": "swift.play",
1384+
"when": "false"
13751385
}
13761386
],
13771387
"editor/context": [

src/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { reindexProject } from "./commands/reindexProject";
4242
import { resetPackage } from "./commands/resetPackage";
4343
import restartLSPServer from "./commands/restartLSPServer";
4444
import { runAllTests } from "./commands/runAllTests";
45+
import { runPlayground } from "./commands/runPlayground";
4546
import { runPluginTask } from "./commands/runPluginTask";
4647
import { runSwiftScript } from "./commands/runSwiftScript";
4748
import { runTask } from "./commands/runTask";
@@ -88,6 +89,7 @@ export function registerToolchainCommands(
8889
export enum Commands {
8990
RUN = "swift.run",
9091
DEBUG = "swift.debug",
92+
PLAY = "swift.play",
9193
CLEAN_BUILD = "swift.cleanBuild",
9294
RESOLVE_DEPENDENCIES = "swift.resolveDependencies",
9395
SHOW_FLAT_DEPENDENCIES_LIST = "swift.flatDependenciesList",
@@ -146,6 +148,10 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
146148
Commands.DEBUG,
147149
async target => await debugBuild(ctx, ...unwrapTreeItem(target))
148150
),
151+
vscode.commands.registerCommand(
152+
Commands.PLAY,
153+
async target => await runPlayground(ctx, target)
154+
),
149155
vscode.commands.registerCommand(Commands.CLEAN_BUILD, async () => await cleanBuild(ctx)),
150156
vscode.commands.registerCommand(
151157
Commands.RUN_TESTS_MULTIPLE_TIMES,

src/commands/runPlayground.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as vscode from "vscode";
2+
import { Location, Range } from "vscode-languageclient";
3+
4+
import { WorkspaceContext } from "../WorkspaceContext";
5+
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
6+
import { packageName } from "../utilities/tasks";
7+
8+
export interface PlaygroundItem {
9+
id: string;
10+
label?: string;
11+
}
12+
13+
export interface DocumentPlaygroundItem extends PlaygroundItem {
14+
id: string;
15+
label?: string;
16+
range: Range;
17+
}
18+
19+
export interface WorkspacePlaygroundItem extends PlaygroundItem {
20+
id: string;
21+
label?: string;
22+
location: Location;
23+
}
24+
25+
/**
26+
* Executes a {@link vscode.Task task} to run swift playground.
27+
*/
28+
export async function runPlayground(ctx: WorkspaceContext, item?: PlaygroundItem) {
29+
const folderContext = ctx.currentFolder;
30+
if (!folderContext || !item) {
31+
return false;
32+
}
33+
const id = item.label ?? item.id;
34+
const task = createSwiftTask(
35+
["play", id],
36+
`Play "${id}"`,
37+
{
38+
cwd: folderContext.folder,
39+
scope: folderContext.workspaceFolder,
40+
packageName: packageName(folderContext),
41+
presentationOptions: { reveal: vscode.TaskRevealKind.Always },
42+
},
43+
folderContext.toolchain
44+
);
45+
46+
await vscode.tasks.executeTask(task);
47+
return true;
48+
}

src/sourcekit-lsp/LanguageClientConfiguration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function initializationOptions(swiftVersion: Version): any {
3636
supportedCommands: {
3737
"swift.run": "swift.run",
3838
"swift.debug": "swift.debug",
39+
"swift.play": "swift.play",
3940
},
4041
},
4142
};
@@ -254,6 +255,9 @@ export function lspClientOptions(
254255
case "swift.debug":
255256
codelens.command.title = `$(debug)\u00A0${codelens.command.title}`;
256257
break;
258+
case "swift.play":
259+
codelens.command.title = `$(play)\u00A0${codelens.command.title}`;
260+
break;
257261
}
258262
return codelens;
259263
});

src/tasks/SwiftTaskProvider.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ export function createSwiftTask(
312312
} else {
313313
cwd = config.cwd.fsPath;
314314
}*/
315-
const env = { ...configuration.swiftEnvironmentVariables, ...swiftRuntimeEnv(), ...cmdEnv };
315+
const env = {
316+
...swiftRuntimeEnv(), // From process.env first
317+
...configuration.swiftEnvironmentVariables, // Then swiftEnvironmentVariables settings
318+
...cmdEnv, // Task configuration takes highest precedence
319+
};
316320
const presentation = config?.presentationOptions ?? {};
317321
if (config?.packageName) {
318322
name += ` (${config?.packageName})`;
@@ -469,9 +473,9 @@ export class SwiftTaskProvider implements vscode.TaskProvider {
469473
const env = platform?.env ?? task.definition.env;
470474
const fullCwd = resolveTaskCwd(task, platform?.cwd ?? task.definition.cwd);
471475
const fullEnv = {
472-
...configuration.swiftEnvironmentVariables,
473-
...swiftRuntimeEnv(),
474-
...env,
476+
...swiftRuntimeEnv(), // From process.env first
477+
...configuration.swiftEnvironmentVariables, // Then swiftEnvironmentVariables settings
478+
...env, // Task configuration takes highest precedence
475479
};
476480

477481
const presentation = task.definition.presentation ?? task.presentationOptions ?? {};

0 commit comments

Comments
 (0)