Skip to content

Commit 4614c8f

Browse files
committed
Better error handling and only fetch if workspace/playground is experimental capability
1 parent 3e176aa commit 4614c8f

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/playgrounds/LSPPlaygroundsDiscovery.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { checkExperimentalCapability } from "../sourcekit-lsp/LanguageClientManager";
1515
import { LanguageClientManager } from "../sourcekit-lsp/LanguageClientManager";
1616
import { Playground, WorkspacePlaygroundsRequest } from "../sourcekit-lsp/extensions";
17+
import { Version } from "../utilities/version";
1718

1819
export { Playground };
1920

@@ -25,6 +26,11 @@ export { Playground };
2526
*/
2627
export class LSPPlaygroundsDiscovery {
2728
constructor(private languageClient: LanguageClientManager) {}
29+
30+
private get toolchainVersion(): Version {
31+
return this.languageClient.folderContext.toolchain.swiftVersion;
32+
}
33+
2834
/**
2935
* Return list of workspace playgrounds
3036
*/
@@ -39,4 +45,13 @@ export class LSPPlaygroundsDiscovery {
3945
}
4046
});
4147
}
48+
49+
async supportsPlaygrounds(): Promise<boolean> {
50+
if (this.toolchainVersion.isLessThan(new Version(6, 3, 0))) {
51+
return false;
52+
}
53+
return await this.languageClient.useLanguageClient(async client => {
54+
return checkExperimentalCapability(client, WorkspacePlaygroundsRequest.method, 1);
55+
});
56+
}
4257
}

src/playgrounds/PlaygroundProvider.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as vscode from "vscode";
1515

1616
import { FolderContext } from "../FolderContext";
1717
import { FolderOperation, WorkspaceContext } from "../WorkspaceContext";
18+
import { SwiftLogger } from "../logging/SwiftLogger";
1819
import { LSPPlaygroundsDiscovery, Playground } from "./LSPPlaygroundsDiscovery";
1920

2021
export { Playground };
@@ -42,6 +43,10 @@ export class PlaygroundProvider implements vscode.Disposable {
4243
return new LSPPlaygroundsDiscovery(this.folderContext.languageClientManager);
4344
}
4445

46+
private get logger(): SwiftLogger {
47+
return this.folderContext.workspaceContext.logger;
48+
}
49+
4550
/**
4651
* Create folder observer that creates a PlaygroundProvider when a folder is added and
4752
* discovers available playgrounds when the folder is in focus
@@ -107,14 +112,28 @@ export class PlaygroundProvider implements vscode.Disposable {
107112
this.didChangePlaygroundsEmitter.event;
108113

109114
async fetch() {
115+
if (!(await this.lspPlaygroundDiscovery.supportsPlaygrounds())) {
116+
this.logger.debug(
117+
`Fetching playgrounds not supported by the language server`,
118+
this.folderContext.name
119+
);
120+
return;
121+
}
110122
this.fetchPromise = this.lspPlaygroundDiscovery.getWorkspacePlaygrounds();
111-
const playgrounds = await this.fetchPromise;
112-
this.documentPlaygrounds.clear();
113-
for (const playground of playgrounds) {
114-
const uri = playground.location.uri;
115-
this.documentPlaygrounds.set(
116-
uri,
117-
(this.documentPlaygrounds.get(uri) ?? []).concat(playground)
123+
try {
124+
const playgrounds = await this.fetchPromise;
125+
this.documentPlaygrounds.clear();
126+
for (const playground of playgrounds) {
127+
const uri = playground.location.uri;
128+
this.documentPlaygrounds.set(
129+
uri,
130+
(this.documentPlaygrounds.get(uri) ?? []).concat(playground)
131+
);
132+
}
133+
} catch (error) {
134+
this.logger.error(
135+
`Failed to fetch workspace playgrounds: ${error}`,
136+
this.folderContext.name
118137
);
119138
}
120139
this.fetchPromise = undefined;

0 commit comments

Comments
 (0)