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';
|
2021-10-22 18:35:49 +00:00
|
|
|
import * as path from 'path';
|
2020-10-04 17:26:21 +00:00
|
|
|
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
|
|
|
|
2021-10-22 18:35:49 +00:00
|
|
|
/**
|
|
|
|
* Escape string to match verbatim in regular expression.
|
|
|
|
*
|
|
|
|
* @param str The string to be escaped.
|
|
|
|
* @returns The escaped string.
|
|
|
|
*/
|
|
|
|
export function escapeStringForRegExp(str: string) {
|
|
|
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-05-25 09:00:27 +00:00
|
|
|
return new Promise<ExecResult>(
|
|
|
|
resolve => child_process.execFile(path, args,
|
|
|
|
{
|
2021-10-22 18:58:40 +00:00
|
|
|
env: { ...process.env, LC_MESSAGES: 'en_US.UTF-8' },
|
2021-05-25 09:00:27 +00:00
|
|
|
maxBuffer: maxBuffer,
|
|
|
|
},
|
|
|
|
(error, stdout, stderr) => resolve(error
|
|
|
|
? { error: stderr ? stderr : error.message }
|
|
|
|
: { data: stdout, error: stderr }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2020-10-03 18:04:18 +00:00
|
|
|
}
|
2020-10-04 17:26:21 +00:00
|
|
|
|
|
|
|
/**
|
2020-10-25 17:52:13 +00:00
|
|
|
* Open a prompt with a button, and wait for user action.
|
2020-10-04 17:26:21 +00:00
|
|
|
*
|
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-04 17:26:21 +00:00
|
|
|
*/
|
2020-10-25 17:52:13 +00:00
|
|
|
export async function prompt(message: string, label: string, error = false) {
|
2021-05-25 09:00:27 +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-04 17:26:21 +00:00
|
|
|
}
|
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) {
|
2021-05-25 09:00:27 +00:00
|
|
|
return (charCode >= 97 && charCode <= 122)
|
|
|
|
|| (charCode >= 65 && charCode <= 90);
|
2020-11-11 12:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether character is alphanumeric.
|
|
|
|
*
|
|
|
|
* @param charCode ASCII code of character.
|
|
|
|
*/
|
|
|
|
export function isAlnum(charCode: number) {
|
2021-05-25 09:00:27 +00:00
|
|
|
return isAlpha(charCode) || (charCode >= 48 && charCode <= 57);
|
2020-11-11 12:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get corresponding HTML cross-reference name by node name.
|
|
|
|
*
|
2021-10-22 18:35:49 +00:00
|
|
|
* See section *HTML Cross-reference Node Name Expansion* in the
|
|
|
|
* GNU Texinfo manual.
|
2020-11-11 12:29:30 +00:00
|
|
|
*
|
2021-05-25 09:00:27 +00:00
|
|
|
* TODO: Node name is not displayed verbatim, leading to wrong HTML xref when
|
|
|
|
* containing commands. Fix this when migrating to LSP.
|
2020-11-11 12:29:30 +00:00
|
|
|
*
|
|
|
|
* @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);
|
2021-08-11 17:23:04 +00:00
|
|
|
return isAlpha(firstCharCode)
|
|
|
|
? result
|
|
|
|
: 'g_t_00' + firstCharCode.toString(16) + result.substring(1);
|
2020-11-11 12:29:30 +00:00
|
|
|
}
|
2021-10-22 18:35:49 +00:00
|
|
|
|
|
|
|
export function normalizePath(pathName: string) {
|
|
|
|
pathName = path.normalize(pathName);
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
// On Windows, when passing the path of input file to makeinfo,
|
|
|
|
// using backslashes in path name breaks some other command line
|
|
|
|
// options (notably, -I).
|
|
|
|
// Not sure if this is a bug of makeinfo, or perl, or neither.
|
|
|
|
//
|
|
|
|
// TODO: We should look into this issue sometime later.
|
|
|
|
return pathName.replace(/\\/g, '/');
|
|
|
|
} else {
|
|
|
|
return pathName;
|
|
|
|
}
|
|
|
|
}
|