Skip to content

Commit

Permalink
clean up live preview VS Code event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbastien committed Dec 18, 2024
1 parent 5044a3d commit 253cc1c
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions src/documentation/DocumentationPreviewEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { ConvertDocumentationRequest } from "../sourcekit-lsp/extensions/Convert
export enum PreviewEditorConstant {
VIEW_TYPE = "swift.previewDocumentationEditor",
TITLE = "Preview Swift Documentation",
UNSUPPORTED_EDITOR_ERROR_MESSAGE = "The active text editor does not support Swift Documentation Live Preview",
}

export class DocumentationPreviewEditor implements vscode.Disposable {
private readonly webviewPanel: vscode.WebviewPanel;
private activeTextEditor?: vscode.TextEditor;
private subscriptions: vscode.Disposable[] = [];

private disposeEmitter = new vscode.EventEmitter<void>();
Expand All @@ -36,6 +38,7 @@ export class DocumentationPreviewEditor implements vscode.Disposable {
private readonly extension: vscode.ExtensionContext,
private readonly context: WorkspaceContext
) {
this.activeTextEditor = vscode.window.activeTextEditor;
const swiftDoccRenderPath = this.extension.asAbsolutePath(
path.join("assets", "swift-docc-render")
);
Expand Down Expand Up @@ -65,26 +68,20 @@ export class DocumentationPreviewEditor implements vscode.Disposable {
)
)
);
fs.readFile(path.join(swiftDoccRenderPath, "index.html"), "utf-8").then(
documentationHTML => {
documentationHTML = documentationHTML
.replaceAll("{{BASE_PATH}}", webviewBaseURI.toString())
.replace("</body>", `<script src="${scriptURI.toString()}"></script></body>`);
this.webviewPanel.webview.html = documentationHTML;
this.subscriptions.push(
this.webviewPanel.webview.onDidReceiveMessage(this.receiveMessage.bind(this)),
vscode.window.onDidChangeActiveTextEditor(editor => {
this.convertDocumentation(editor);
}),
vscode.window.onDidChangeTextEditorSelection(event => {
this.convertDocumentation(event.textEditor);
}),
this.webviewPanel.onDidDispose(this.dispose.bind(this))
);
// Reveal the editor, but don't change the focus of the active text editor
this.webviewPanel.reveal(undefined, true);
}
);
fs.readFile(path.join(swiftDoccRenderPath, "index.html"), "utf-8").then(doccRenderHTML => {
doccRenderHTML = doccRenderHTML
.replaceAll("{{BASE_PATH}}", webviewBaseURI.toString())
.replace("</body>", `<script src="${scriptURI.toString()}"></script></body>`);
this.webviewPanel.webview.html = doccRenderHTML;
this.subscriptions.push(
this.webviewPanel.webview.onDidReceiveMessage(this.receiveMessage, this),
vscode.window.onDidChangeActiveTextEditor(this.handleActiveTextEditorChange, this),
vscode.workspace.onDidChangeTextDocument(this.handleDocumentChange, this),
this.webviewPanel.onDidDispose(this.dispose, this)
);
// Reveal the editor, but don't change the focus of the active text editor
this.webviewPanel.reveal(undefined, true);
});
}

/** An event that is fired when the Documentation Preview Editor is disposed */
Expand Down Expand Up @@ -117,18 +114,45 @@ export class DocumentationPreviewEditor implements vscode.Disposable {
private receiveMessage(message: WebviewMessage) {
switch (message.type) {
case "loaded":
this.convertDocumentation(vscode.window.activeTextEditor);
if (!this.activeTextEditor) {
break;
}
this.convertDocumentation(this.activeTextEditor);
break;
case "rendered":
this.renderEmitter.fire();
break;
}
}

private async convertDocumentation(editor: vscode.TextEditor | undefined): Promise<void> {
const document = editor?.document;
if (!document || document.uri.scheme !== "file") {
return undefined;
private handleActiveTextEditorChange(activeTextEditor: vscode.TextEditor | undefined) {
if (this.activeTextEditor === activeTextEditor || activeTextEditor === undefined) {
return;
}
this.activeTextEditor = activeTextEditor;
this.convertDocumentation(activeTextEditor);
}

private handleDocumentChange(event: vscode.TextDocumentChangeEvent) {
if (this.activeTextEditor?.document === event.document) {
this.convertDocumentation(this.activeTextEditor);
}
}

private async convertDocumentation(textEditor: vscode.TextEditor): Promise<void> {
const document = textEditor.document;
if (
document.uri.scheme !== "file" ||
!["markdown", "tutorial", "swift"].includes(document.languageId)
) {
this.postMessage({
type: "update-content",
content: {
type: "error",
errorMessage: PreviewEditorConstant.UNSUPPORTED_EDITOR_ERROR_MESSAGE,
},
});
return;
}

const response = await this.context.languageClientManager.useLanguageClient(
Expand All @@ -137,7 +161,7 @@ export class DocumentationPreviewEditor implements vscode.Disposable {
textDocument: {
uri: document.uri.toString(),
},
position: editor.selection.start,
position: textEditor.selection.start,
});
}
);
Expand Down

0 comments on commit 253cc1c

Please sign in to comment.