From 5b8fe6cf098cad0f093bded33a80ea2739a66eac Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Sun, 13 May 2018 22:51:06 +0100 Subject: [PATCH] Basic linting works! rejoice!! --- src/linter/glslProvider.ts | 85 ++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/src/linter/glslProvider.ts b/src/linter/glslProvider.ts index 2dfccd3..57afb93 100644 --- a/src/linter/glslProvider.ts +++ b/src/linter/glslProvider.ts @@ -5,12 +5,24 @@ import * as os from 'os' import * as cp from 'child_process' import * as fs from 'fs' import * as shell from 'shelljs' +import * as path from 'path' interface config { glslangPath: string tmpdir: string } +const extensions: { [id: string] : string } = { + '.fsh': '.frag', + '.vsh': '.vert', + '.gsh': '.geom', + '.glsl': '.frag' +} + +const filters: RegExp[] = [ + /(not supported for this version or the enabled extensions)/g +] + export default class GLSLProvider implements vscode.CodeActionProvider { private diagnosticCollection: vscode.DiagnosticCollection private config: config @@ -23,8 +35,8 @@ export default class GLSLProvider implements vscode.CodeActionProvider { this.checkBinary() try { - shell.mkdir('-p', `${this.config.tmpdir}/shaders`) - console.log('[MC-GLSL] Successfully made temp directory', `${this.config.tmpdir}/shaders`) + shell.mkdir('-p', `${this.config.tmpdir}`) + console.log('[MC-GLSL] Successfully made temp directory', `${this.config.tmpdir}`) } catch(e) { console.error('[MC-GLSL] Error creating temp dir', e) vscode.window.showErrorMessage('[MC-GLSL] Error creat ing temp directory. Check developer tools for more info.') @@ -46,11 +58,19 @@ export default class GLSLProvider implements vscode.CodeActionProvider { const c = vscode.workspace.getConfiguration('mcglsl') console.log('[MC-GLSL] glslangValidatorPath set to', c.get('glslangValidatorPath')) - console.log('[MC-GLSL] temp directory root set to', `${os.tmpdir()}/${vscode.workspace.name}`) + console.log('[MC-GLSL] temp directory root set to', path.join(os.tmpdir(), vscode.workspace.name!!, 'shaders')) return { glslangPath: c.get('glslangValidatorPath') as string, - tmpdir: `${os.tmpdir()}/${vscode.workspace.name}` + tmpdir: path.join(os.tmpdir(), vscode.workspace.name!!, 'shaders') + } + } + + private configChange(e: vscode.ConfigurationChangeEvent) { + if (e.affectsConfiguration('mcglsl')) { + console.log('[MC-GLSL] config changed') + this.config = this.initConfig() + this.checkBinary() } } @@ -72,22 +92,63 @@ export default class GLSLProvider implements vscode.CodeActionProvider { this.diagnosticCollection.dispose() } - private configChange(e: vscode.ConfigurationChangeEvent) { - if (e.affectsConfiguration('mcglsl')) { - console.log('[MC-GLSL] config changed') - this.config = this.initConfig() - this.checkBinary() - } - } - private docChange(e: vscode.TextDocumentChangeEvent) { this.lint(e.document) } + private matchesFilters(s: string): boolean { + return filters.some((reg: RegExp, i: number, array: RegExp[]) => { + let m = s.match(reg) + console.log(s) + console.log(m) + return (m && m.length > 1)!! + }) + } + private lint(document: vscode.TextDocument) { if(document.languageId !== 'glsl') { return } + + let linkname = path.join(this.config.tmpdir, `${path.basename(document.fileName, path.extname(document.fileName))}${extensions[path.extname(document.fileName)]}`) + + if(!fs.existsSync(linkname)) { + console.log(`[MC-GLSL] ${linkname} does not exist yet. Creating.`) + shell.ln('-s', document.uri.fsPath, linkname) + } + + let res = cp.spawnSync(this.config.glslangPath, [linkname]).output[1].toString() + let lines = res.split(/(?:\n)/g) + .filter((s: string) => { return s != '' }) + .slice(1, -1) + //.filter((s: string) => { return !this.matchesFilters(s)} ) + + if (lines.length < 1) { + this.diagnosticCollection.set(document.uri, []) + return + } + + let diags: vscode.Diagnostic[] = [] + + lines.forEach((line: string) => { + // Default to error + let matches = line.match(/WARNING:|ERROR:\s\d+:(\d+): (\W.*)/) + if (!matches || (matches && matches.length < 3)) { + return + } + + let [lineNum, message] = matches.slice(1,3) + + let severity: vscode.DiagnosticSeverity = vscode.DiagnosticSeverity.Error + if(!line.startsWith('ERROR:')) { + // for now assume theres either errors or warnings. Maybe thats even the case! + severity = vscode.DiagnosticSeverity.Warning + } + + let range = new vscode.Range(parseInt(lineNum) -1, 0, parseInt(lineNum) - 1, 0) + diags.push(new vscode.Diagnostic(range, message, severity)) + }) + this.diagnosticCollection.set(document.uri, diags) } public provideCodeActions(document: vscode.TextDocument,