From 9e717a1e7e0051951e6a324383ad51e5964a0b14 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 23 Oct 2020 06:40:41 +0800 Subject: [PATCH] Refactor & bugfix. --- README.md | 6 +++--- src/converter.ts | 4 ++-- src/extension.ts | 1 + src/options.ts | 12 ++++++------ src/preview.ts | 19 ++++++++++--------- src/utils.ts | 8 ++++++-- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 27b01f3..ee11496 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ Texinfo language support for Visual Studio Code. * Provided by the same [TextMate grammar](https://github.com/Alhadis/language-texinfo/blob/v1.0.0/grammars/texinfo.cson) as [used in GitHub](https://github.com/github/linguist/pull/4589). * **Code Completion** * Completion for most @\-commands. - * Code snippets for blocks and brace commands. + * Code snippets for block and brace commands. * **Folding** * Fold on blocks, headers and multiline comments. * **Preview** * Display HTML preview in a webview. - * Texinfo to HTML conversion is provided by [GNU Texinfo](https://www.gnu.org/software/texinfo/). + * Texinfo to HTML conversion is provided by [GNU Texinfo](https://www.gnu.org/software/texinfo).
Screenshots: @@ -40,4 +40,4 @@ See `File -> Preferences -> Settings -> Extensions -> Texinfo` for details. The ## Future Plans -* Implement [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) for the Texinfo language, preferably by extending GNU Texinfo, to alleviate the limitations of the current implementaion. +* Implement [Language Server Protocol](https://microsoft.github.io/language-server-protocol) for the Texinfo language, preferably by extending GNU Texinfo, to alleviate the limitations of the current implementaion. diff --git a/src/converter.ts b/src/converter.ts index 53745e7..a7f74e4 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -6,7 +6,7 @@ */ import Options from './options'; -import * as utils from './utils'; +import { exec } from './utils'; /** * Texinfo to HTML converter. @@ -38,6 +38,6 @@ export default class Converter { private async convert(path: string) { const maxBuffer = Options.maxSize * 1024 * 1024; - return await utils.exec(Options.makeinfo, this.options.concat(path), maxBuffer); + return await exec(Options.makeinfo, this.options.concat(path), maxBuffer); } } diff --git a/src/extension.ts b/src/extension.ts index f76451b..e390343 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,7 @@ export function activate(context: vscode.ExtensionContext) { vscode.workspace.onDidChangeTextDocument(Document.update), vscode.workspace.onDidSaveTextDocument(Document.save), vscode.workspace.onDidCloseTextDocument(Document.close), + vscode.workspace.onDidChangeConfiguration(Options.clear), vscode.commands.registerTextEditorCommand('texinfo.showPreview', Preview.show), vscode.languages.registerCompletionItemProvider('texinfo', new CompletionItemProvider(), '@'), vscode.languages.registerFoldingRangeProvider('texinfo', new FoldingRangeProvider()), diff --git a/src/options.ts b/src/options.ts index b59aa96..8c7e243 100644 --- a/src/options.ts +++ b/src/options.ts @@ -62,15 +62,15 @@ export default class Options { this.configuration = vscode.workspace.getConfiguration(section); } - private getString(section: string): string { - return this.configuration.get(section) ?? ''; + private getString(section: string) { + return this.configuration.get(section, ''); } - private getBoolean(section: string): boolean { - return this.configuration.get(section) ?? false; + private getBoolean(section: string) { + return this.configuration.get(section, false); } - private getNumber(section: string): number { - return this.configuration.get(section) ?? 0; + private getNumber(section: string) { + return this.configuration.get(section, 0); } } diff --git a/src/preview.ts b/src/preview.ts index 3df7c61..f325b23 100644 --- a/src/preview.ts +++ b/src/preview.ts @@ -9,8 +9,9 @@ import * as path from 'path'; import * as vscode from 'vscode'; import Converter from './converter'; import Document from './document'; +import Logger from './logger'; import Options from './options'; -import * as utils from './utils'; +import { prompt, transformHtmlImageUri } from './utils'; /** * Texinfo document preview. @@ -25,11 +26,11 @@ export default class Preview { static async show(editor: vscode.TextEditor) { const document = editor.document; const documentContext = Document.get(document); - if (documentContext === undefined) { - return; - } + if (documentContext === undefined) return; + // Only show preview for saved files, as we're not gonna send document content to `makeinfo` via STDIN. + // Instead, the file will be loaded from disk. if (document.isUntitled) { - if (!await utils.prompt('Save this document to display preview.', 'Save')) return; + if (!await prompt('Save this document to display preview.', 'Save')) return; if (!await document.save()) return; } documentContext.initPreview().panel.reveal(); @@ -54,6 +55,7 @@ export default class Preview { constructor(private readonly documentContext: Document) { this.panel = vscode.window.createWebviewPanel('texinfo.preview', '', vscode.ViewColumn.Beside); this.disposables.push(this.panel.onDidDispose(() => this.close())); + this.updateTitle(); this.updateWebview(); } @@ -76,8 +78,8 @@ export default class Preview { } this.updating = true; this.pendingUpdate = false; - this.updateTitle(); - + // Inform the user that the preview is updating if `makeinfo` takes too long. + setTimeout(() => this.updating && this.updateTitle(), 500); let htmlCode = await Converter.convertToHtml(this.document.fileName); if (htmlCode === undefined) { prompt(`Failed to show preview for ${this.document.fileName}.`, 'Show log', true) @@ -86,7 +88,7 @@ export default class Preview { if (Options.displayImage) { const pathName = path.dirname(this.document.fileName); // To display images in webviews, image URIs in HTML should be converted to VSCode-recognizable ones. - htmlCode = utils.transformHtmlImageUri(htmlCode, src => { + htmlCode = transformHtmlImageUri(htmlCode, src => { const srcUri = vscode.Uri.file(pathName + '/' + src); return this.panel.webview.asWebviewUri(srcUri).toString(); }); @@ -95,7 +97,6 @@ export default class Preview { } this.updating = false; this.updateTitle(); - this.pendingUpdate && this.updateWebview(); } } diff --git a/src/utils.ts b/src/utils.ts index 4d3d8b2..e33c956 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -24,7 +24,7 @@ export async function prompt(message: string, confirm: string, error = false) { } /** - * Execute command and get output. + * Execute command and fetch output. * * @param path Path to the executable file. * @param args Arguments to be passed to the command. @@ -32,7 +32,7 @@ export async function prompt(message: string, confirm: string, error = false) { * @returns The output data, or `undefined` if execution fails. */ export function exec(path: string, args: string[], maxBuffer: number) { - return new Promise(resolve => { + return new Promise>(resolve => { child_process.execFile(path, args, { maxBuffer: maxBuffer }, (error, stdout, stderr) => { if (error) { Logger.log(stderr ? stderr : error.message); @@ -62,3 +62,7 @@ export function transformHtmlImageUri(htmlCode: string, transformer: (src: strin // If nothing is transformed, return the original HTML code, for better performance. return elements.length === 0 ? htmlCode : dom.outerHTML; } + +export type Optional = T | undefined; + +export type Range = { start: number, end: number };