misdocumeno

Untitled

Nov 28th, 2020
1,093
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // The module 'vscode' contains the VS Code extensibility API
  2. // Import the module and reference it with the alias vscode in your code below
  3. import { posix } from 'path';
  4. import * as vscode from 'vscode';
  5.  
  6. // this method is called when your extension is activated
  7. // your extension is activated the very first time the command is executed
  8. export function activate(context: vscode.ExtensionContext) {
  9.  
  10.     // Use the console to output diagnostic information (console.log) and errors (console.error)
  11.     // This line of code will only be executed once when your extension is activated
  12.     console.log('Congratulations, your extension "testexttypescript" is now active!');
  13.  
  14.     const diagnosticCollection = vscode.languages.createDiagnosticCollection('test');
  15.  
  16.     if (vscode.window.activeTextEditor) {
  17.         getCompileErrorsAndWarnings(vscode.window.activeTextEditor.document, diagnosticCollection);
  18.     }
  19.  
  20.     context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor((editor) => {
  21.         if (editor) {
  22.             getCompileErrorsAndWarnings(editor.document, diagnosticCollection);
  23.         }
  24.     }));
  25.  
  26.     context.subscriptions.push(vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
  27.         getCompileErrorsAndWarnings(document, diagnosticCollection);
  28.     }));
  29.  
  30.     context.subscriptions.push(vscode.workspace.onDidChangeTextDocument((e) => {
  31.         let debounceTimer: NodeJS.Timeout;
  32.         clearTimeout(debounceTimer);
  33.         debounceTimer = setTimeout(() => getCompileErrorsAndWarnings.apply(this, [e.document, diagnosticCollection]), 3.0);
  34.     }));
  35. }
  36.  
  37. // this method is called when your extension is deactivated
  38. export function deactivate() {}
  39.  
  40. // run compiler and read errors and warnings
  41. function getCompileErrorsAndWarnings(document: vscode.TextDocument, diagnosticCollection: vscode.DiagnosticCollection) {
  42.  
  43.     const compiler = 'D:\\Documentos\\scripting sourcemod 1.10\\unifix\\spcomp.exe';
  44.  
  45.     const file = document.fileName.replace(/\\/g, '/');
  46.  
  47.     console.log(file);
  48.  
  49.     let destinationFolder = String(require('path').join(require('os').tmpdir(), '_vscode.smx.tmp'));
  50.  
  51.     require('child_process').execFile(compiler, [file, '-o', destinationFolder], (err: any, stdout: string, stderr: string) => {
  52.  
  53.         // create diagnostics list object
  54.         // let oldDiagnosticCollection = vscode.languages.getDiagnostics(vscode.Uri.file(file));
  55.         // console.log('oldDiagnostics:');
  56.         // console.log(oldDiagnosticCollection);
  57.         let diagnosticList : vscode.Diagnostic[] = [];
  58.  
  59.         stdout.split('\n').forEach((line) => {
  60.  
  61.             if (line.startsWith(file)) {
  62.  
  63.                 let matches = line.match(new RegExp(`^${file}\\(([0-9]+)\\) : (error|warning|fatal error) ([0-9]+): (.*)`));
  64.  
  65.                 // separate each part
  66.                 let linenum = parseInt(matches![1]) - 1;
  67.                 let typemsg = matches![2];
  68.                 let typenum = parseInt(matches![3]);
  69.                 let message = matches![4];
  70.  
  71.                 // fix missing quotes on error 105
  72.                 if (typenum === 105) { // cannot find method or property
  73.                     let propertyOrMethod = message.substring(31, message.length);
  74.                     message = message.substring(0, 31) + '"' + propertyOrMethod + '"';
  75.                 }
  76.  
  77.                 // get the piece of code with the warning/error
  78.                 let pieces: string[] = [];
  79.  
  80.                 Array.from(message.matchAll(/["'](.*?)["']/g)).forEach((array) => {
  81.                     pieces.push(array[0].substring(1, array[0].length - 1));
  82.                 });
  83.  
  84.                 // again fixing error 105
  85.                 if (typenum === 105) {
  86.                     pieces.forEach((piece, i) => {
  87.                         let index = piece.indexOf('.');
  88.                         pieces[i] = piece.substring(index, piece.length);
  89.                     });
  90.                 }
  91.  
  92.                 let documentLine = getLineFromDocument(linenum);
  93.  
  94.                 let range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
  95.  
  96.                 if (pieces.length === 0) {
  97.                     if (typenum === 217) { // loose identation
  98.                         let identationLength = 0;
  99.                         for (let i = 0; i < documentLine.length; i++) {
  100.                             if (documentLine.charAt(i) === '\t' || documentLine.charAt(i) === ' ') {
  101.                                 identationLength++;
  102.                             } else {
  103.                                 break;
  104.                             }
  105.                         }
  106.                         range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, identationLength));
  107.                     }
  108.                     else {
  109.                         range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, 0));
  110.                     }
  111.                 } else {
  112.                     pieces.forEach((piece) => {
  113.  
  114.                         let appearances = documentLine.split(piece).length - 1;
  115.  
  116.                         if (appearances === 0 && piece.startsWith('.')) {
  117.                             // maybe its Object::Property instead of Object.Property
  118.                             piece = ':' + piece.substring(1, piece.length);
  119.                             appearances = documentLine.split(piece).length - 1;
  120.                         }
  121.  
  122.                         if (appearances === 1) {
  123.                             let startPos = documentLine.split(piece)[0].length;
  124.                             let endPos = startPos + piece.length;
  125.  
  126.                             if (piece.startsWith('.') || piece.startsWith(':')) {
  127.                                 startPos++;
  128.                             }
  129.  
  130.                             range = new vscode.Range(new vscode.Position(linenum, startPos), new vscode.Position(linenum, endPos));
  131.                         } else {
  132.                             range = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum, 0));
  133.                         }
  134.                     });
  135.                 }
  136.  
  137.                 let diagLvl: vscode.DiagnosticSeverity;
  138.  
  139.                 if (typemsg === 'error' || typemsg === 'fatal error') {
  140.                     diagLvl = vscode.DiagnosticSeverity.Error;
  141.                 } else {
  142.                     diagLvl = vscode.DiagnosticSeverity.Warning;
  143.                 }
  144.  
  145.                 let thisLogDiagnostic = new vscode.Diagnostic(range, message, diagLvl);
  146.                 diagnosticList.push(thisLogDiagnostic);
  147.             }
  148.         });
  149.  
  150.         diagnosticCollection.clear();
  151.         diagnosticCollection.set(vscode.Uri.file(file), diagnosticList);
  152.     });
  153. }
  154.  
  155. let getLineFromDocument = ((linenum: number) => {
  156.  
  157.     let lineRange = new vscode.Range(new vscode.Position(linenum, 0), new vscode.Position(linenum + 1, 0));
  158.     let lineStr = vscode.window.activeTextEditor?.document.getText(lineRange);
  159.  
  160.     if (typeof lineStr !== undefined) {
  161.         return lineStr!.substring(0, lineStr!.length - 1);
  162.     }
  163.     else {
  164.         return "";
  165.     }
  166. });
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×