Added postError function for short error notifications. Using existsSync to check if binary exists. Seperated onConfigChange into different functions. lineNum -2 for windows instead of -1 for linux, such a pain.

This commit is contained in:
Noah Santschi-Cooney 2018-07-20 22:05:59 +01:00
parent c8c7a29abb
commit 1cec184e04
4 changed files with 69 additions and 57 deletions

View file

@ -1,10 +1,9 @@
import { connection, documents, onEvent } from './server' import { connection, documents, onEvent } from './server'
import { exec, execSync } from 'child_process'
import { extname } from 'path'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { platform } from 'os' import { platform } from 'os'
import { createWriteStream, chmodSync, createReadStream, unlinkSync } from 'fs' import { createWriteStream, chmodSync, createReadStream, unlinkSync, existsSync } from 'fs'
import * as unzip from 'unzip' import * as unzip from 'unzip'
import { postError } from './utils'
const url = { const url = {
'win32': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-windows-x64-Release.zip', 'win32': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-windows-x64-Release.zip',
@ -17,18 +16,22 @@ export interface Config {
readonly glslangPath: string readonly glslangPath: string
} }
export let conf: Partial<Config> = {} export let conf: Config = {shaderpacksPath: '', glslangPath: ''}
export const onConfigChange = async (change) => { export const onConfigChange = async (change) => {
const temp = change.settings.mcglsl as Config const temp = change.settings.mcglsl as Config
conf = {shaderpacksPath: temp['shaderpacksPath'], glslangPath: temp['glslangValidatorPath']} conf = {shaderpacksPath: temp['shaderpacksPath'], glslangPath: temp['glslangValidatorPath']}
try {
execSync(conf.glslangPath) if (existsSync(conf.glslangPath)) {
documents.all().forEach(document => onEvent) documents.all().forEach(onEvent)
} catch (e) { } else {
if (e.status !== 1) { promptDownloadGlslang()
}
}
async function promptDownloadGlslang() {
const chosen = await connection.window.showErrorMessage( const chosen = await connection.window.showErrorMessage(
`[mc-glsl] glslangValidator not found at: '${conf.glslangPath}' or returned non-0 code`, `[mc-glsl] glslangValidator not found at: '${conf.glslangPath}'.`,
{title: 'Download'}, {title: 'Download'},
{title: 'Cancel'} {title: 'Cancel'}
) )
@ -36,10 +39,14 @@ export const onConfigChange = async (change) => {
if (!chosen || chosen.title !== 'Download') return if (!chosen || chosen.title !== 'Download') return
if (conf.shaderpacksPath === '') { if (conf.shaderpacksPath === '') {
connection.window.showErrorMessage('Please set mcglsl.shaderpacksPath') connection.window.showErrorMessage('Please set mcglsl.shaderpacksPath as this is where glslangValidator will be saved to.')
return return
} }
downloadGlslang()
}
async function downloadGlslang() {
const res = await fetch(url[platform()]) const res = await fetch(url[platform()])
try { try {
@ -49,7 +56,7 @@ export const onConfigChange = async (change) => {
zip.on('finish', async () => { zip.on('finish', async () => {
createReadStream(conf.shaderpacksPath + '/glslangValidator.zip') createReadStream(conf.shaderpacksPath + '/glslangValidator.zip')
.pipe(unzip.Parse()) .pipe(unzip.Parse())
.on('entry', (entry) => { .on('entry', entry => {
if (entry.path === 'bin/glslangValidator') { if (entry.path === 'bin/glslangValidator') {
entry.pipe(createWriteStream(conf.shaderpacksPath + '/glslangValidator')) entry.pipe(createWriteStream(conf.shaderpacksPath + '/glslangValidator'))
return return
@ -64,8 +71,6 @@ export const onConfigChange = async (change) => {
}) })
}) })
} catch (e) { } catch (e) {
connection.window.showErrorMessage(e.message) postError(e)
}
}
} }
} }

View file

@ -1,23 +1,27 @@
import { TextDocument, Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver' import { Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver'
import { connection, documents } from './server' import { connection, documents } from './server'
import { execSync } from 'child_process' import { execSync } from 'child_process'
import * as path from 'path' import * as path from 'path'
import { readFileSync, existsSync } from 'fs' import { readFileSync, existsSync } from 'fs'
import { conf } from './config' import { conf } from './config'
import { postError } from './utils'
import { platform } from 'os'
const reDiag = /^(ERROR|WARNING): ([^?<>:*|"]+?):(\d+): (?:'.*?' : )?(.+)$/ const reDiag = /^(ERROR|WARNING): ([^?<>:*|"]+?):(\d+): (?:'.*?' : )?(.+)[\r\n|\n]/
const reVersion = /#version [\d]{3}/ const reVersion = /#version [\d]{3}/
const reInclude = /^(?:\s)*?(?:#include) "((?:\/?[^?<>:*|"]+?)+?\.(?:[a-zA-Z]+?))"$/ const reInclude = /^(?:\s)*?(?:#include) "((?:\/?[^?<>:*|"]+?)+?\.(?:[a-zA-Z]+?))"$/
const reIncludeExt = /#extension GL_GOOGLE_include_directive ?: ?require/ const reIncludeExt = /#extension GL_GOOGLE_include_directive ?: ?require/
const include = '#extension GL_GOOGLE_include_directive : require' const include = '#extension GL_GOOGLE_include_directive : require'
const win = platform() === 'win32'
const filters = [ const filters = [
/stdin/,
/(No code generated)/, /(No code generated)/,
/(compilation terminated)/, /(compilation terminated)/,
/Could not process include directive for header name:/ /Could not process include directive for header name:/
] ]
const files = new Map<string, number>() const includeToParent = new Map<string, string[]>()
type IncludeObj = { type IncludeObj = {
lineNum: number, lineNum: number,
@ -98,7 +102,7 @@ export function preprocess(lines: string[], docURI: string) {
try { try {
lint(docURI, lines, includeMap, diagnostics) lint(docURI, lines, includeMap, diagnostics)
} catch (e) { } catch (e) {
console.log(e) postError(e)
} }
} }
@ -180,6 +184,7 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
// add #line indicating we are entering a new include block // add #line indicating we are entering a new include block
lines[inc.lineNumTopLevel] = `#line 0 "${inc.path}"` lines[inc.lineNumTopLevel] = `#line 0 "${inc.path}"`
// merge the lines of the file into the current document // merge the lines of the file into the current document
// TODO do we wanna use a DLL here?
lines.splice(inc.lineNumTopLevel + 1, 0, ...dataLines) lines.splice(inc.lineNumTopLevel + 1, 0, ...dataLines)
// add the closing #line indicating we're re-entering a block a level up // add the closing #line indicating we're re-entering a block a level up
lines.splice(inc.lineNumTopLevel + 1 + dataLines.length, 0, `#line ${inc.lineNum} "${inc.parent}"`) lines.splice(inc.lineNumTopLevel + 1 + dataLines.length, 0, `#line ${inc.lineNum} "${inc.parent}"`)
@ -205,7 +210,8 @@ function lint(uri: string, lines: string[], includes: Map<string, IncludeObj>, d
let diag: Diagnostic = { let diag: Diagnostic = {
severity: errorType(type), severity: errorType(type),
range: calcRange(parseInt(line) - 1, file.length - 1 ? file : uri), // had to do - 2 here instead of - 1, windows only perhaps?
range: calcRange(parseInt(line) - (win ? 2 : 1), file.length - 1 ? file : uri),
message: replaceWord(msg), message: replaceWord(msg),
source: 'mc-glsl' source: 'mc-glsl'
} }

View file

@ -2,17 +2,12 @@ import * as vsclang from 'vscode-languageserver'
import * as vsclangproto from 'vscode-languageserver-protocol' import * as vsclangproto from 'vscode-languageserver-protocol'
import { completions } from './completionProvider' import { completions } from './completionProvider'
import { preprocess, ext, formatURI } from './linter' import { preprocess, ext, formatURI } from './linter'
import { exec, execSync } from 'child_process'
import { extname } from 'path' import { extname } from 'path'
import fetch from 'node-fetch'
import { platform } from 'os'
import { createWriteStream, chmodSync, createReadStream, unlinkSync } from 'fs'
import * as unzip from 'unzip'
export let connection: vsclang.IConnection export let connection: vsclang.IConnection
connection = vsclang.createConnection(new vsclang.IPCMessageReader(process), new vsclang.IPCMessageWriter(process)) connection = vsclang.createConnection(new vsclang.IPCMessageReader(process), new vsclang.IPCMessageWriter(process))
import { Config, onConfigChange } from './config' import { onConfigChange } from './config'
export const documents = new vsclang.TextDocuments() export const documents = new vsclang.TextDocuments()
documents.listen(connection) documents.listen(connection)

6
server/src/utils.ts Normal file
View file

@ -0,0 +1,6 @@
import { connection } from './server'
export function postError(e: Error) {
connection.window.showErrorMessage(e.message)
console.log(e)
}