Skip to content

Commit 28c4e29

Browse files
introduce a DocumentationManager to the WorkspaceContext
1 parent 3c58c12 commit 28c4e29

File tree

8 files changed

+166
-175
lines changed

8 files changed

+166
-175
lines changed

src/WorkspaceContext.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { DebugAdapter, LaunchConfigType } from "./debugger/debugAdapter";
3333
import { SwiftBuildStatus } from "./ui/SwiftBuildStatus";
3434
import { SwiftToolchain } from "./toolchain/toolchain";
3535
import { DiagnosticsManager } from "./DiagnosticsManager";
36+
import { DocumentationManager } from "./documentation/DocumentationManager";
3637

3738
/**
3839
* Context for whole workspace. Holds array of contexts for each workspace folder
@@ -49,10 +50,12 @@ export class WorkspaceContext implements vscode.Disposable {
4950
public diagnostics: DiagnosticsManager;
5051
public subscriptions: vscode.Disposable[];
5152
public commentCompletionProvider: CommentCompletionProviders;
53+
public documentation: DocumentationManager;
5254
private lastFocusUri: vscode.Uri | undefined;
5355
private initialisationFinished = false;
5456

5557
private constructor(
58+
extensionContext: vscode.ExtensionContext,
5659
public tempFolder: TemporaryFolder,
5760
public outputChannel: SwiftOutputChannel,
5861
public toolchain: SwiftToolchain
@@ -62,6 +65,7 @@ export class WorkspaceContext implements vscode.Disposable {
6265
this.languageClientManager = new LanguageClientManager(this);
6366
this.tasks = new TaskManager(this);
6467
this.diagnostics = new DiagnosticsManager(this);
68+
this.documentation = new DocumentationManager(extensionContext, this);
6569
this.currentDocument = null;
6670
this.commentCompletionProvider = new CommentCompletionProviders();
6771

@@ -182,11 +186,12 @@ export class WorkspaceContext implements vscode.Disposable {
182186

183187
/** Get swift version and create WorkspaceContext */
184188
static async create(
189+
extensionContext: vscode.ExtensionContext,
185190
outputChannel: SwiftOutputChannel,
186191
toolchain: SwiftToolchain
187192
): Promise<WorkspaceContext> {
188193
const tempFolder = await TemporaryFolder.create();
189-
return new WorkspaceContext(tempFolder, outputChannel, toolchain);
194+
return new WorkspaceContext(extensionContext, tempFolder, outputChannel, toolchain);
190195
}
191196

192197
/**

src/commands.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import { runPluginTask } from "./commands/runPluginTask";
3939
import { runTestMultipleTimes } from "./commands/testMultipleTimes";
4040
import { newSwiftFile } from "./commands/newFile";
4141
import { runAllTestsParallel } from "./commands/runParallelTests";
42-
import { previewDocumentation } from "./commands/previewDocumentation";
4342

4443
/**
4544
* References:
@@ -68,10 +67,7 @@ export function registerToolchainCommands(
6867
/**
6968
* Registers this extension's commands in the given {@link vscode.ExtensionContext context}.
7069
*/
71-
export function register(
72-
ctx: WorkspaceContext,
73-
extension: vscode.ExtensionContext
74-
): vscode.Disposable[] {
70+
export function register(ctx: WorkspaceContext): vscode.Disposable[] {
7571
return [
7672
vscode.commands.registerCommand("swift.newFile", uri => newSwiftFile(uri)),
7773
vscode.commands.registerCommand("swift.resolveDependencies", () =>
@@ -146,7 +142,7 @@ export function register(
146142
),
147143
vscode.commands.registerCommand(
148144
"swift.previewDocumentation",
149-
async () => await previewDocumentation(extension, ctx)
145+
async () => await ctx.documentation.launchDocumentationPreview()
150146
),
151147
];
152148
}

src/commands/previewDocumentation.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import * as vscode from "vscode";
16+
import * as fs from "fs/promises";
17+
import * as path from "path";
18+
import { DocumentationPreviewEditor } from "./DocumentationPreviewEditor";
19+
import { WorkspaceContext } from "../WorkspaceContext";
20+
import { FolderContext } from "../FolderContext";
21+
import { createSwiftTask } from "../tasks/SwiftTaskProvider";
22+
import { executeTaskWithUI, updateAfterError } from "../commands/utilities";
23+
24+
export class DocumentationManager {
25+
private previewEditor?: DocumentationPreviewEditor;
26+
27+
constructor(
28+
private readonly extension: vscode.ExtensionContext,
29+
private readonly context: WorkspaceContext
30+
) {}
31+
32+
async launchDocumentationPreview(): Promise<void> {
33+
if (!this.previewEditor) {
34+
const folderContext = this.context.folders.at(0);
35+
if (!folderContext) {
36+
return;
37+
}
38+
39+
const archive = await this.buildDocumentation(folderContext);
40+
if (!archive) {
41+
return;
42+
}
43+
44+
this.previewEditor = new DocumentationPreviewEditor(
45+
archive,
46+
this.extension,
47+
this.context
48+
);
49+
const subscriptions: vscode.Disposable[] = [
50+
this.previewEditor.onDidDispose(() => {
51+
subscriptions.forEach(d => d.dispose());
52+
this.previewEditor = undefined;
53+
}),
54+
];
55+
}
56+
}
57+
58+
async buildDocumentation(folderContext: FolderContext): Promise<string | undefined> {
59+
const buildPath = path.join(folderContext.folder.fsPath, ".build", "vscode-swift");
60+
const outputPath = path.join(buildPath, "documentation-preview");
61+
await fs.rm(outputPath, { recursive: true, force: true });
62+
await fs.mkdir(outputPath, { recursive: true });
63+
const task = createSwiftTask(
64+
[
65+
"package",
66+
"--disable-sandbox",
67+
"generate-documentation",
68+
"--enable-experimental-combined-documentation",
69+
"--output-path",
70+
outputPath,
71+
],
72+
"Build Documentation",
73+
{
74+
cwd: folderContext.folder,
75+
env: {
76+
DOCC_HTML_DIR: this.extension.asAbsolutePath("assets/swift-docc-render"),
77+
},
78+
scope: folderContext.workspaceFolder,
79+
prefix: folderContext.name,
80+
presentationOptions: { reveal: vscode.TaskRevealKind.Silent },
81+
},
82+
folderContext.workspaceContext.toolchain
83+
);
84+
const succeeded = await executeTaskWithUI(task, "Building Documentation", folderContext);
85+
updateAfterError(succeeded, folderContext);
86+
if (!succeeded) {
87+
vscode.window.showErrorMessage("Failed to build documentation via SwiftDocC");
88+
return;
89+
}
90+
return outputPath;
91+
}
92+
}

0 commit comments

Comments
 (0)