vscode-texinfo/src/utils/misc.ts

111 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-10-03 18:04:18 +00:00
/**
2020-10-24 21:45:32 +00:00
* utils/misc.ts
2021-03-16 12:01:13 +00:00
*
* Copyright (C) 2020,2021 CismonX <admin@cismon.net>
*
* This file is part of vscode-texinfo.
*
* vscode-texinfo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* vscode-texinfo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* vscode-texinfo. If not, see <https://www.gnu.org/licenses/>.
2020-10-03 18:04:18 +00:00
*/
import * as child_process from 'child_process';
import * as vscode from 'vscode';
2020-10-24 21:45:32 +00:00
import { ExecResult } from './types';
2020-10-03 18:04:18 +00:00
/**
2020-10-22 22:40:41 +00:00
* Execute command and fetch output.
2020-10-03 18:04:18 +00:00
*
* @param path Path to the executable file.
* @param args Arguments to be passed to the command.
* @param maxBuffer Max output buffer size.
2020-10-10 17:36:05 +00:00
* @returns The output data, or `undefined` if execution fails.
2020-10-03 18:04:18 +00:00
*/
export function exec(path: string, args: string[], maxBuffer: number) {
2020-10-24 15:50:20 +00:00
return new Promise<ExecResult>(resolve => {
2020-10-03 18:04:18 +00:00
child_process.execFile(path, args, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
if (error) {
2020-10-24 15:50:20 +00:00
resolve({ error: stderr ? stderr : error.message });
2020-10-03 18:04:18 +00:00
} else {
2020-10-24 15:50:20 +00:00
resolve({ data: stdout, error: stderr });
2020-10-03 18:04:18 +00:00
}
});
});
}
/**
2020-10-25 17:52:13 +00:00
* Open a prompt with a button, and wait for user action.
*
2020-10-24 21:45:32 +00:00
* @param message The message to be displayed on the prompt.
2020-10-25 17:52:13 +00:00
* @param label Text to be displayed on the button.
2020-10-24 21:45:32 +00:00
* @param error Whether the prompt is shown as an error message. Default false.
2020-10-25 17:52:13 +00:00
* @returns Whether the user clicked the button.
*/
2020-10-25 17:52:13 +00:00
export async function prompt(message: string, label: string, error = false) {
2020-10-24 21:45:32 +00:00
const func = error ? vscode.window.showErrorMessage : vscode.window.showInformationMessage;
2020-10-25 17:52:13 +00:00
return label === await func(message, label);
}
2020-10-22 22:40:41 +00:00
2020-10-24 15:51:44 +00:00
/**
* Convert line numbers to VSCode range.
*
* @param startLine
* @param endLine Default to `startLine`.
*/
export function lineNumToRange(startLine: number, endLine = startLine) {
const startPosition = new vscode.Position(startLine, 0);
const endPosition = new vscode.Position(endLine, Number.MAX_SAFE_INTEGER);
return new vscode.Range(startPosition, endPosition);
}
2020-11-11 12:29:30 +00:00
/**
* Check whether character is an alphabet.
*
* @param charCode ASCII code of character.
*/
export function isAlpha(charCode: number) {
return charCode >= 97 && charCode <= 122 || charCode >= 65 && charCode <= 90;
}
/**
* Check whether character is alphanumeric.
*
* @param charCode ASCII code of character.
*/
export function isAlnum(charCode: number) {
return isAlpha(charCode) || charCode >= 48 && charCode <= 57;
}
/**
* Get corresponding HTML cross-reference name by node name.
*
* See section *HTML Cross-reference Node Name Expansion* in the Texinfo manual.
*
* TODO: Node name is not displayed verbatim, leading to wrong HTML xref when containing commands.
* Fix this when migrating to LSP.
*
* @param nodeName
*/
export function getNodeHtmlRef(nodeName: string) {
const result = nodeName.trim().split(/\s+/)
.map(word => word.split('')
.map(ch => {
const charCode = ch.charCodeAt(0);
return isAlnum(charCode) ? ch : '_00' + charCode.toString(16);
})
.join(''))
.join('-');
const firstCharCode = result.charCodeAt(0);
return isAlpha(firstCharCode) ? result : 'g_t_00' + firstCharCode.toString(16) + result.substring(1);
}