Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The module 'vscode' contains the VS Code extensibility API
- // Import the module and reference it with the alias vscode in your code below
- import { posix } from 'path';
- import * as vscode from 'vscode';
- // this method is called when your extension is activated
- // your extension is activated the very first time the command is executed
- export function activate(context: vscode.ExtensionContext) {
- // Use the console to output diagnostic information (console.log) and errors (console.error)
- // This line of code will only be executed once when your extension is activated
- console.log('Congratulations, your extension "testexttypescript" is now active!');
- const diagnosticCollection = vscode.languages.createDiagnosticCollection('test');
- if (vscode.window.activeTextEditor) {
- getCompileErrorsAndWarnings(vscode.window.activeTextEditor.document, diagnosticCollection);
- }
- context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor((editor) => {
- if (editor) {
- getCompileErrorsAndWarnings(editor.document, diagnosticCollection);
- }
- }));
- context.subscriptions.push(vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
- getCompileErrorsAndWarnings(document, diagnosticCollection);
- }));
- context.subscriptions.push(vscode.workspace.onDidChangeTextDocument((e) => {
- let debounceTimer: NodeJS.Timeout;
- clearTimeout(debounceTimer);
- debounceTimer = setTimeout(() => getCompileErrorsAndWarnings.apply(this, [e.document, diagnosticCollection]), 3.0);
- }));
- }
- // this method is called when your extension is deactivated
- export function deactivate() {}
- // run compiler and read errors and warnings
- function getCompileErrorsAndWarnings(document: vscode.TextDocument, diagnosticCollection: vscode.DiagnosticCollection) {
- const compiler = 'D:\\Documentos\\scripting sourcemod 1.10\\unifix\\spcomp.exe';
- const file = document.fileName.replace(/\\/g, '/');
- console.log(file);
- let destinationFolder = String(require('path').join(require('os').tmpdir(), '_vscode.smx.tmp'));
- require('child_process').execFile(compiler, [file, '-o', destinationFolder], (err: any, stdout: string, stderr: string) => {
- // create diagnostics list object
- // let oldDiagnosticCollection = vscode.languages.getDiagnostics(vscode.Uri.file(file));
- // console.log('oldDiagnostics:');
- // console.log(oldDiagnosticCollection);
- let diagnosticList : vscode.Diagnostic[] = [];
- stdout.split('\n').forEach((line) => {
- if (line.startsWith(file)) {
- let matches = line.match(new RegExp(`^${file}\\(([0-9]+)\\) : (error|warning|fatal error) ([0-9]+): (.*)`));
- // separate each part
- let linenum = parseInt(matches![1]) - 1;
- let typemsg = matches![2];
- let typenum = parseInt(matches![3]);
- let message = matches![4];
- // fix missing quotes on error 105
- if (typenum === 105) { // cannot find method or property
- let propertyOrMethod = message.substring(31, message.length);
- message = message.substring(0, 31) + '"' + propertyOrMethod + '"';
- }
- // get the piece of code with the warning/error
- let pieces: string[] = [];
- Array.from(message.matchAll(/["'](.*?)["']/g)).forEach((array) => {
- pieces.push(array[0].substring(1, array[0].length - 1));
- });
- // again fixing error 105
- if (typenum === 105) {
- pieces.forEach((piece, i) => {
- let index = piece.indexOf('.');
- pieces[i] = piece.substring(index, piece.length);
- });
- }
- let documentLine = getLineFromDocument(linenum);
- let range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
- if (pieces.length === 0) {
- if (typenum === 217) { // loose identation
- let identationLength = 0;
- for (let i = 0; i < documentLine.length; i++) {
- if (documentLine.charAt(i) === '\t' || documentLine.charAt(i) === ' ') {
- identationLength++;
- } else {
- break;
- }
- }
- range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, identationLength));
- }
- else {
- range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, 0));
- }
- } else {
- pieces.forEach((piece) => {
- let appearances = documentLine.split(piece).length - 1;
- if (appearances === 0 && piece.startsWith('.')) {
- // maybe its Object::Property instead of Object.Property
- piece = ':' + piece.substring(1, piece.length);
- appearances = documentLine.split(piece).length - 1;
- }
- if (appearances === 1) {
- let startPos = documentLine.split(piece)[0].length;
- let endPos = startPos + piece.length;
- if (piece.startsWith('.') || piece.startsWith(':')) {
- startPos++;
- }
- range = new vscode.Range(new vscode.Position(linenum, startPos), new vscode.Position(linenum, endPos));
- } else {
- range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, 0));
- }
- });
- }
- let diagLvl: vscode.DiagnosticSeverity;
- if (typemsg === 'error' || typemsg === 'fatal error') {
- diagLvl = vscode.DiagnosticSeverity.Error;
- } else {
- diagLvl = vscode.DiagnosticSeverity.Warning;
- }
- let thisLogDiagnostic = new vscode.Diagnostic(range, message, diagLvl);
- diagnosticList.push(thisLogDiagnostic);
- }
- });
- diagnosticCollection.clear();
- diagnosticCollection.set(vscode.Uri.file(file), diagnosticList);
- });
- }
- let getLineFromDocument = ((linenum: number) => {
- let lineRange = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum + 1, 0));
- let lineStr = vscode.window.activeTextEditor?.document.getText(lineRange);
- if (typeof lineStr !== undefined) {
- return lineStr!.substring(0, lineStr!.length - 1);
- }
- else {
- return "";
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement