VSCode_LaTeX/src/commands.ts

158 lines
6.9 KiB
TypeScript

import * as vscode from 'vscode';
import * as child_process from 'child_process';
import documentclass_symbols from '../dictionary/documentclass_symbols';
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 {
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;
outputChannel.clear(); // clear the outputChannel to only show the last output
document.save(); // save the document before compile
child_process.exec(
"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(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);
}
);
},
latex_makeindex: function latex_makeindex(textEditor: vscode.TextEditor, textEditorEdit: vscode.TextEditorEdit): void {
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('.')),
{ cwd: fileDir }, // set the working directory
(error: Error, stdout: string, stderr: string) => {
if (error) {
vscode.window.showErrorMessage("makeindex Failed: see output.");
outputChannel.show();
outputChannel.appendLine("makeindex Failed: " + error.message);
outputChannel.append(stdout + "\n" + stderr);
} else {
vscode.window.setStatusBarMessage("makeindex succeeded", 5000);
outputChannel.appendLine("makeindex succeeded: created *.ind file\n" + stdout);
}
}
);
},
latex_cleanup: function latex_cleanup(textEditor: vscode.TextEditor, textEditorEdit: vscode.TextEditorEdit): void {
var document: vscode.TextDocument = textEditor.document;
var fileDir: string = document.fileName.substring(0, document.fileName.lastIndexOf('/'));
var fileName: string = document.fileName.substring(document.fileName.lastIndexOf('/') + 1, document.fileName.lastIndexOf('.'));
var outputChannel: vscode.OutputChannel = this.outputChannel;
child_process.exec("\
for file in \"" + fileName + "\".*; do \
for end in \"aux\" \"idx\" \"ilg\" \"ind\" \"log\" \"out\" \"toc\" \"synctex.gz\"; do \
if [[ \"$file\" == *.\"$end\" ]]; then \
rm \"$file\"; \
echo removed: \"$file\"; \
fi; \
done; \
done;",
{ cwd: fileDir, shell: this.settings["latex.shell"] },
(error: Error, stdout: string, stderr: string) => {
if (error) {
vscode.window.showErrorMessage("cleanup Failed: see output.");
outputChannel.show();
outputChannel.appendLine("cleanup Failed: " + error.message);
outputChannel.append(stdout + "\n" + stderr);
} else {
vscode.window.setStatusBarMessage("cleanup succeeded", 5000);
outputChannel.appendLine('Directory "' + fileDir + '" cleaned');
outputChannel.append(stdout);
}
}
);
}
}