Refactor & bugfix.

This commit is contained in:
CismonX 2020-10-23 06:40:41 +08:00
parent 5ee1e16cd8
commit 9e717a1e7e
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
6 changed files with 28 additions and 22 deletions

View File

@ -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).
<details>
<summary>Screenshots:</summary>
@ -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.

View File

@ -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);
}
}

View File

@ -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()),

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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<string | undefined>(resolve => {
return new Promise<Optional<string>>(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> = T | undefined;
export type Range = { start: number, end: number };