vscode-texinfo/src/diagnosis.ts

91 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-10-24 15:51:44 +00:00
/**
* diagnosis.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-24 15:51:44 +00:00
*/
2021-10-22 18:35:49 +00:00
import * as path from 'path';
2020-10-24 15:51:44 +00:00
import * as vscode from 'vscode';
2021-10-22 18:35:49 +00:00
import { escapeStringForRegExp, lineNumToRange } from './utils/misc';
2020-10-24 21:45:32 +00:00
import { isDefined } from './utils/types';
2020-10-24 15:51:44 +00:00
/**
* Manage diagnostic information of Texinfo documents.
*/
2021-08-11 17:23:04 +00:00
export default class Diagnosis implements vscode.Disposable
{
2021-04-22 09:07:13 +00:00
/**
* Remove a document's diagnostic entry from the collection.
*
* @param document
*/
2021-04-19 12:43:20 +00:00
delete(document: vscode.TextDocument) {
2021-05-25 04:16:56 +00:00
this._diagnostics.delete(document.uri);
2020-10-24 15:51:44 +00:00
}
/**
* Generate diagnostic information based on error log from `makeinfo`.
*
* @param document
* @param logText
*/
2021-04-19 12:43:20 +00:00
update(document: vscode.TextDocument, logText: string) {
2021-10-22 18:35:49 +00:00
const fileName = path.basename(document.uri.path);
const regex = new RegExp(`${escapeStringForRegExp(fileName)}:\\d+:`);
2021-04-19 12:43:20 +00:00
const diagnostics = logText.split('\n')
2021-10-22 18:35:49 +00:00
.map(line => line.length > 0 ? line.match(regex) : null)
.map(result => {
const index = result?.index;
const line = result?.input;
if (index === undefined || line === undefined) {
return undefined;
}
const logText = line.substring(index + fileName.length + 1);
return logToDiagnostic(logText);
})
2021-04-19 12:43:20 +00:00
.filter(isDefined);
2021-05-25 04:16:56 +00:00
this._diagnostics.set(document.uri, diagnostics);
2020-10-24 21:45:32 +00:00
}
dispose() {
2021-05-25 04:16:56 +00:00
this._diagnostics.dispose();
2020-10-24 15:51:44 +00:00
}
2021-04-19 12:43:20 +00:00
private readonly _diagnostics
= vscode.languages.createDiagnosticCollection('texinfo');
2020-10-24 15:51:44 +00:00
}
2021-04-22 09:07:13 +00:00
/**
* Convert a `makeinfo` error log line to a VSCode `Diagnostic` object.
*
* @param lineText
* @returns
*/
function logToDiagnostic(lineText: string) {
2021-04-19 12:43:20 +00:00
const lineNum = parseInt(lineText) - 1;
2021-08-11 17:23:04 +00:00
// Ignore error that does not correspond to a line in document.
if (isNaN(lineNum)) {
return undefined;
}
2020-10-24 15:51:44 +00:00
const message = lineText.substring(lineNum.toString().length + 2);
const severity = message.startsWith('warning:')
2021-08-11 17:23:04 +00:00
? vscode.DiagnosticSeverity.Warning
: undefined;
2020-10-24 15:51:44 +00:00
return new vscode.Diagnostic(lineNumToRange(lineNum), message, severity);
}