Skip to content
Open
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@
"command": "swift.generateSourcekitConfiguration",
"title": "Generate SourceKit-LSP Configuration",
"category": "Swift"
},
{
"command": "swift.createDocumentationCatalog",
"title": "Create Documentation Catalog",
"category": "Swift"
}
],
"configuration": [
Expand Down
5 changes: 5 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { WorkspaceContext } from "./WorkspaceContext";
import { attachDebugger } from "./commands/attachDebugger";
import { cleanBuild, debugBuild, runBuild } from "./commands/build";
import { captureDiagnostics } from "./commands/captureDiagnostics";
import { createDocumentationCatalog } from "./commands/createDocumentationCatalog";
import { createNewProject } from "./commands/createNewProject";
import { editDependency } from "./commands/dependencies/edit";
import { resolveDependencies } from "./commands/dependencies/resolve";
Expand Down Expand Up @@ -350,6 +351,10 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(packagePath));
}),
vscode.commands.registerCommand("swift.openDocumentation", () => openDocumentation()),
vscode.commands.registerCommand(
"swift.createDocumentationCatalog",
async () => await createDocumentationCatalog()
),
vscode.commands.registerCommand(
Commands.GENERATE_SOURCEKIT_CONFIG,
async () => await generateSourcekitConfiguration(ctx)
Expand Down
74 changes: 74 additions & 0 deletions src/commands/createDocumentationCatalog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021-2024 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 fsSync from "fs";
import * as fs from "fs/promises";
import * as path from "path";
import * as vscode from "vscode";

export async function createDocumentationCatalog(): Promise<void> {
const folders = vscode.workspace.workspaceFolders;
if (!folders || folders.length === 0) {
void vscode.window.showErrorMessage(
"Creating a documentation catalog requires that a folder or workspace be opened"
);
return;
}

let folder: vscode.WorkspaceFolder | undefined;

if (folders.length === 1) {
folder = folders[0];
} else {
folder = await vscode.window.showWorkspaceFolderPick({
placeHolder: "Select a workspace folder to create the DocC catalog in",
});
}

if (!folder) {
return;
}

const rootPath = folder.uri.fsPath;

const moduleName = await vscode.window.showInputBox({
prompt: "Enter Swift module name",
placeHolder: "MyModule",
validateInput: value => {
if (value.trim().length === 0) {
return "Module name cannot be empty";
}

const doccDir = path.join(rootPath, `${value}.docc`);
if (fsSync.existsSync(doccDir)) {
return `Documentation catalog "${value}.docc" already exists`;
}

return undefined;
},
});

if (!moduleName) {
return; // user cancelled
}

const doccDir = path.join(rootPath, `${moduleName}.docc`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: We can't assume that the user will only place a catalog at the root of the workspace. Documentation catalogs can be located anywhere in the project structure. However, they are most commonly placed within targets from the Package.swift to provide documentation for them. I would show a quick pick with the available targets and an option at the end for creating a standalone documentation catalog.

const markdownFile = path.join(doccDir, `${moduleName}.md`);

await fs.mkdir(doccDir, { recursive: true });
await fs.writeFile(markdownFile, `# ${moduleName}\n`, "utf8");

void vscode.window.showInformationMessage(
`Created DocC documentation catalog: ${moduleName}.docc`
);
}