add: textEditor LaTeX compile error line highlighting

This commit is contained in:
Daniel Kapla 2018-01-31 16:10:41 +01:00
parent ceab16f93a
commit eeea3268a0
3 changed files with 83 additions and 21 deletions

View File

@ -9,6 +9,7 @@ export default {
gather: { insertText: "gather", detail: "math environment", documentation: "gather", kind: latex_types.environment },
"gather*": { insertText: "gather*", detail: "math environment", documentation: "gather*", kind: latex_types.environment },
equation: { insertText: "equation", detail: "math environment", documentation: "equation", kind: latex_types.environment },
subequation: { insertText: "subequation", detail: "\\begin{subequation}\\begin{<eqenv>} ...", documentation: "An equation to encapsulate a multi-equation environemnt for sub labeling", kind: latex_types.environment },
multline: { insertText: "multline", detail: "math environment", documentation: "multline", kind: latex_types.environment },
split: { insertText: "split", detail: "math environment", documentation: "split", kind: latex_types.environment },

View File

@ -1,12 +1,73 @@
import * as vscode from 'vscode';
import * as child_process from 'child_process';
import documentclass_symbols from '../dictionary/documentclass_symbols';
import { window } from 'vscode';
import { window, Range } from 'vscode';
// /**
// ${workspaceFolder} - the path of the workspace folder that contains the tasks.json file
// ${workspaceFolderBasename} - the name of the workspace folder that contains the tasks.json file without any slashes (/)
// ${file} - the current opened file
// ${relativeFile} - the current opened file relative to the workspace folder containing the file
// ${fileBasename} - the current opened file's basename
// ${fileBasenameNoExtension} - the current opened file's basename without the extension
// ${fileDirname} - the current opened file's dirname
// ${fileExtname} - the current opened file's extension
// ${cwd} - the task runner's current working directory on startup
// ${lineNumber} - the current selected line number in the active file
// */
// function commandVariableSubstitution(variable: string, document: vscode.TextDocument): string {
// switch (variable) {
// case "${workspaceFolder}":
// throw new Error("[ commandVariableSubstitution ] Not Implemented.");
// case "${workspaceFolderBasename}":
// throw new Error("[ commandVariableSubstitution ] Not Implemented.");
// case "${file}":
// return document.fileName;
// case "${relativeFile}":
// throw new Error("[ commandVariableSubstitution ] Not Implemented.");
// case "${fileBasename}":
// return document.fileName.substring(document.fileName.lastIndexOf('/') + 1);
// case "${fileBasenameNoExtension}":
// return document.fileName.substring(document.fileName.lastIndexOf('/') + 1, document.fileName.lastIndexOf('.'));
// case "${fileDirname}":
// return document.fileName.substring(0, document.fileName.lastIndexOf('/'));
// case "${fileExtname}":
// return document.fileName.substring(document.fileName.lastIndexOf('.') + 1);
// case "${cwd}":
// throw new Error("[ commandVariableSubstitution ] Not supported.");
// case "${lineNumber}":
// throw new Error("[ commandVariableSubstitution ] Not supported.");
// default:
// throw new Error("[ commandVariableSubstitution ] variable not found.");
// }
// }
var decorationOptions: vscode.DecorationRenderOptions = {
borderRadius: "3px",
borderWidth: "1px",
borderStyle: "solid",
backgroundColor: "rgba(255,0,0,0.3)",
borderColor: "rgba(255,100,100,0.15)"
};
var LaTeX_error_decoration_type: vscode.TextEditorDecorationType = vscode.window.createTextEditorDecorationType(decorationOptions);
export default {
// pdflatex -synctex=1 -interaction=nonstopmode -output-directory="$FILEDIRNAME" "$FILE" > ~/.vscode/.pdflatex_log
latex_pdflatex: function latex_pdflatex(textEditor: vscode.TextEditor, textEditorEdit: vscode.TextEditorEdit): void {
function getHighliteLineNumbers(output: string): number[] {
var lineNumbers: number[] = [];
var outputLines: string[] = output.split("\n");
for (let line = outputLines[0], i = 0; i < outputLines.length; line = outputLines[i++]) {
if (line.startsWith("l.")) {
var ln = Number.parseInt(line.substring(2, (line.indexOf(" ") + 1 || Infinity) - 1 ));
if (lineNumbers.lastIndexOf(ln) < 0) {
lineNumbers.push(ln);
}
}
}
return lineNumbers;
}
var document: vscode.TextDocument = textEditor.document;
var fileDir: string = document.fileName.substring(0, document.fileName.lastIndexOf('/'));
var outputChannel: vscode.OutputChannel = this.outputChannel;
@ -17,15 +78,27 @@ export default {
"pdflatex -synctex=1 -interaction=nonstopmode -output-directory=\"" + fileDir + "\" \"" + document.fileName + "\"",
{ cwd: fileDir }, // set the working directory
(error: Error, stdout: string, stderr: string) => {
var decorationRanges: vscode.Range[] = [];
if (error) {
var output: string = stdout + "\n" + stderr;
var lineNumbers = getHighliteLineNumbers(output);
for (var i = 0; i < lineNumbers.length; i++) {
decorationRanges.push(new vscode.Range(
new vscode.Position(lineNumbers[i] - 1, 0),
new vscode.Position(lineNumbers[i] - 1, 1000)
));
}
vscode.window.showErrorMessage("pdflatex Failed: see output.");
outputChannel.show();
outputChannel.appendLine("pdflatex Failed: " + error.message);
outputChannel.append(stdout + "\n" + stderr);
outputChannel.append(output);
} else {
vscode.window.setStatusBarMessage("pdflatex succeeded: see output", 5000);
outputChannel.appendLine("pdflatex succeeded: created file " + document.fileName.replace('.tex', '.pdf'));
}
textEditor.setDecorations(LaTeX_error_decoration_type, decorationRanges);
}
);
},
@ -33,7 +106,7 @@ export default {
var document: vscode.TextDocument = textEditor.document;
var fileDir: string = document.fileName.substring(0, document.fileName.lastIndexOf('/'));
var outputChannel: vscode.OutputChannel = this.outputChannel;
textEditor.document.save(); // save the document before makeindex
child_process.exec(
"makeindex " + document.fileName.substring(document.fileName.lastIndexOf('/') + 1, document.fileName.lastIndexOf('.')),

View File

@ -8,26 +8,14 @@ import Commands from './commands';
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
var outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel("LaTeX build output");
var settings: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('latex');
var commands_this_context = { outputChannel: outputChannel, settings: vscode.workspace.getConfiguration('latex') };
// Register Build Commands
// The Command was defined in package.json and the commandId parameter has
// to be equal to the 'command' field in the package.json file
context.subscriptions.push(vscode.commands.registerTextEditorCommand(
'latex.pdflatex',
Commands.latex_pdflatex,
{ extensionPath: context.extensionPath, outputChannel: outputChannel, settings: settings }
));
context.subscriptions.push(vscode.commands.registerTextEditorCommand(
'latex.makeindex',
Commands.latex_makeindex,
{ extensionPath: context.extensionPath, outputChannel: outputChannel, settings: settings }
));
context.subscriptions.push(vscode.commands.registerTextEditorCommand(
'latex.cleanup',
Commands.latex_cleanup,
{ extensionPath: context.extensionPath, outputChannel: outputChannel, settings: settings }
));
context.subscriptions.push(vscode.commands.registerTextEditorCommand( 'latex.pdflatex', Commands.latex_pdflatex, commands_this_context ));
context.subscriptions.push(vscode.commands.registerTextEditorCommand( 'latex.makeindex', Commands.latex_makeindex, commands_this_context ));
context.subscriptions.push(vscode.commands.registerTextEditorCommand( 'latex.cleanup', Commands.latex_cleanup, commands_this_context ));
// Register the Completion Item Provider
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(