Skip to content

Commit decc231

Browse files
committed
Set swift.swiftSDK for target platforms
SourceKit-LSP now provides code editing support for non-macOS Darwin platforms starting Swift 6.1 using the --swift-sdk flag. Set this flag to the appropriate target triple when using the "Select Target Platform" feature on macOS. Issue: swiftlang#1335
1 parent bd64d77 commit decc231

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
107107
}
108108
}),
109109
// Note: This is only available on macOS (gated in `package.json`) because its the only OS that has the iOS SDK available.
110-
vscode.commands.registerCommand("swift.switchPlatform", () => switchPlatform()),
110+
vscode.commands.registerCommand("swift.switchPlatform", () => switchPlatform(ctx)),
111111
vscode.commands.registerCommand(Commands.RESET_PACKAGE, () => resetPackage(ctx)),
112112
vscode.commands.registerCommand("swift.runScript", () => runSwiftScript(ctx)),
113113
vscode.commands.registerCommand("swift.openPackage", () => {

src/commands/switchPlatform.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import * as vscode from "vscode";
16-
import { DarwinCompatibleTarget, SwiftToolchain } from "../toolchain/toolchain";
16+
import {
17+
DarwinCompatibleTarget,
18+
SwiftToolchain,
19+
getDarwinTargetTriple,
20+
} from "../toolchain/toolchain";
1721
import configuration from "../configuration";
22+
import { Version } from "../utilities/version";
23+
import { WorkspaceContext } from "../WorkspaceContext";
1824

1925
/**
20-
* Switches the target SDK to the platform selected in a QuickPick UI.
26+
* Switches the appropriate SDK setting to the platform selected in a QuickPick UI.
2127
*/
22-
export async function switchPlatform() {
28+
export async function switchPlatform(ctx: WorkspaceContext) {
2329
const picked = await vscode.window.showQuickPick(
2430
[
2531
{ value: undefined, label: "macOS" },
@@ -29,28 +35,39 @@ export async function switchPlatform() {
2935
{ value: DarwinCompatibleTarget.visionOS, label: "visionOS" },
3036
],
3137
{
32-
placeHolder: "Select a new target",
38+
placeHolder: "Select a new target platform",
3339
}
3440
);
3541
if (picked) {
42+
if (ctx.toolchain.swiftVersion.isLessThan(new Version(6, 1, 0))) {
43+
vscode.window.showWarningMessage(
44+
"Code editing support for non-macOS platforms is only available starting Swift 6.1"
45+
);
46+
}
47+
// show a status item as getSDKForTarget can sometimes take a long while to xcrun for the SDK
48+
const statusItemText = `Setting target platform to ${picked.label}`;
49+
ctx.statusItem.start(statusItemText);
3650
try {
37-
const sdkForTarget = picked.value
38-
? await SwiftToolchain.getSDKForTarget(picked.value)
39-
: "";
40-
if (sdkForTarget !== undefined) {
41-
if (sdkForTarget !== "") {
42-
configuration.sdk = sdkForTarget;
43-
vscode.window.showWarningMessage(
44-
`Selecting the ${picked.label} SDK will provide code editing support, but compiling with this SDK will have undefined results.`
45-
);
46-
} else {
47-
configuration.sdk = undefined;
48-
}
51+
if (picked.value) {
52+
// verify that the SDK for the platform actually exists
53+
await SwiftToolchain.getSDKForTarget(picked.value);
54+
}
55+
const swiftSDKTriple = picked.value ? getDarwinTargetTriple(picked.value) : "";
56+
if (swiftSDKTriple !== "") {
57+
// set a swiftSDK for non-macOS Darwin platforms so that SourceKit-LSP can provide syntax highlighting
58+
configuration.swiftSDK = swiftSDKTriple;
59+
vscode.window.showWarningMessage(
60+
`Selecting the ${picked.label} target platform will provide code editing support, but compiling with a ${picked.label} SDK will have undefined results.`
61+
);
4962
} else {
50-
vscode.window.showErrorMessage("Unable to obtain requested SDK path");
63+
// set swiftSDK to undefined for macOS and other platforms
64+
configuration.swiftSDK = undefined;
5165
}
5266
} catch {
53-
vscode.window.showErrorMessage("Unable to obtain requested SDK path");
67+
vscode.window.showErrorMessage(
68+
`Unable set the Swift SDK setting to ${picked.label}, verify that the SDK exists`
69+
);
5470
}
71+
ctx.statusItem.end(statusItemText);
5572
}
5673
}

src/extension.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
103103
event.affectsConfiguration("swift.SDK") ||
104104
event.affectsConfiguration("swift.swiftSDK")
105105
) {
106-
// FIXME: There is a bug stopping us from restarting SourceKit-LSP directly.
107-
// As long as it's fixed we won't need to reload on newer versions.
108-
showReloadExtensionNotification(
109-
"Changing the Swift SDK path requires the project be reloaded."
110-
);
106+
vscode.commands.executeCommand("swift.restartLSPServer");
111107
}
112108
})
113109
);

0 commit comments

Comments
 (0)