Skip to content

Commit 6fd02ab

Browse files
committed
Fixed errors still propogating of the include was removed from the parent. Also supressing 'shaderpacks' path not found
1 parent 036b680 commit 6fd02ab

File tree

6 files changed

+52
-26
lines changed

6 files changed

+52
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ VSCode-MC-Shader is a [Visual Studio Code](https://code.visualstudio.com/) exten
2020

2121
## Planned
2222

23+
- Multi-workspaces (currently only one is supported and using multiple is very undefined behaviour)
2324
- Warnings for unused uniforms/varyings
2425
- Some cool `DRAWBUFFERS` stuff
2526

server/src/config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { postError } from './utils'
77
import { execSync } from 'child_process'
88
import { serverLog } from './logging'
99
import { dirname } from 'path'
10+
import { DidChangeConfigurationParams } from 'vscode-languageserver/lib/main'
1011

1112
const url = {
1213
'win32': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-windows-x64-Release.zip',
@@ -23,16 +24,24 @@ export interface Config {
2324

2425
export let conf: Config = {shaderpacksPath: '', glslangPath: ''}
2526

26-
export const onConfigChange = async (change) => {
27+
let supress = false
28+
29+
export async function onConfigChange(change: DidChangeConfigurationParams) {
2730
const temp = change.settings.mcglsl as Config
2831
if (temp.shaderpacksPath === conf.shaderpacksPath && temp.glslangPath === conf.glslangPath) return
2932
conf = {shaderpacksPath: temp['shaderpacksPath'].replace(/\\/g, '/'), glslangPath: temp['glslangValidatorPath'].replace(/\\/g, '/')}
3033
serverLog.debug(() => 'new config: ' + JSON.stringify(temp))
3134
serverLog.debug(() => 'old config: ' + JSON.stringify(conf))
3235

3336
if (conf.shaderpacksPath === '' || conf.shaderpacksPath.replace(dirname(conf.shaderpacksPath), '') !== '/shaderpacks') {
37+
if (supress) return
3438
serverLog.error(() => 'shaderpack path not set or doesn\'t end in \'shaderpacks\'', null)
35-
connection.window.showErrorMessage('mcglsl.shaderpacksPath is not set or doesn\'t end in \'shaderpacks\'. Please set it in your settings.')
39+
supress = true
40+
const clicked = await connection.window.showErrorMessage(
41+
'mcglsl.shaderpacksPath is not set or doesn\'t end in \'shaderpacks\'. Please set it in your settings.',
42+
{title: 'Supress'}
43+
)
44+
supress = (clicked && clicked.title === 'Supress') ? true : false
3645
return
3746
}
3847

server/src/graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type Pair<T, S> = {
44
second: S
55
}
66

7-
type Node = {
7+
export type Node = {
88
parents: Map<string, Pair<number, Node>>
99
children: Map<string, Node>
1010
}

server/src/linter.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { readFileSync, existsSync, statSync, Stats } from 'fs'
66
import { conf } from './config'
77
import { postError, formatURI, getDocumentContents, trimPath } from './utils'
88
import { platform } from 'os'
9-
import { Graph } from './graph'
9+
import { Graph, Node } from './graph'
1010
import { Comment } from './comment'
1111
import { linterLog } from './logging'
1212

@@ -91,7 +91,7 @@ export function preprocess(lines: string[], docURI: string) {
9191

9292
const includeMap = new Map<string, IncludeObj>(Array.from(allIncludes).map(obj => [obj.path, obj]) as [string, IncludeObj][])
9393

94-
lint(docURI, lines, includeMap, diagnostics)
94+
lint(docURI, lines, includeMap, diagnostics, hasDirective)
9595
}
9696

9797
function includeDirective(lines: string[], docURI: string): boolean {
@@ -123,11 +123,24 @@ function includeDirective(lines: string[], docURI: string): boolean {
123123
const buildIncludeGraph = (inc: IncludeObj) => includeGraph.setParent(inc.path, inc.parent, inc.lineNum)
124124

125125
function processIncludes(lines: string[], incStack: string[], allIncludes: Set<IncludeObj>, diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
126+
// todo figure out why incStack[0] here
126127
const includes = getIncludes(incStack[0], lines)
127128
includes.forEach(i => allIncludes.add(i))
128-
if (includes.length > 0) {
129-
linterLog.info(() => `${trimPath(incStack[0])} has ${includes.length} include(s). [${includes.map(i => '\n\t\t' + trimPath(i.path))}\n\t]`)
130-
includes.reverse().forEach(inc => {
129+
130+
const parent = incStack[incStack.length - 1]
131+
includeGraph.nodes.get(parent).children.forEach((node, uri) => {
132+
if (!includes.has(uri)) {
133+
includeGraph.nodes.get(parent).children.delete(uri)
134+
node.parents.delete(parent)
135+
}
136+
})
137+
138+
const includeList = Array.from(includes.values())
139+
140+
if (includeList.length > 0) {
141+
linterLog.info(() => `${trimPath(parent)} has ${includeList.length} include(s). [${includeList.map(i => '\n\t\t' + trimPath(i.path))}\n\t]`)
142+
143+
includeList.reverse().forEach(inc => {
131144
buildIncludeGraph(inc)
132145
mergeInclude(inc, lines, incStack, diagnostics, hasDirective)
133146
})
@@ -137,16 +150,14 @@ function processIncludes(lines: string[], incStack: string[], allIncludes: Set<I
137150
}
138151

139152
function getIncludes(uri: string, lines: string[]) {
140-
// the numbers start at -1 because we increment them as soon as we enter the loop so that we
141-
// dont have to put an incrememnt at each return
142153
const lineInfo: LinesProcessingInfo = {
143154
total: -1,
144155
comment: Comment.State.No,
145156
parStack: [uri],
146-
count: [-1],
157+
count: [0],
147158
}
148159

149-
return lines.reduce<IncludeObj[]>((out, line, i): IncludeObj[] => processLine(out, line, lines, i, lineInfo), [])
160+
return new Map(lines.reduce<IncludeObj[]>((out, line, i) => processLine(out, line, lines, i, lineInfo), []).map(inc => [inc.path, inc] as [string, IncludeObj]))
150161
}
151162

152163
// TODO can surely be reworked
@@ -191,7 +202,7 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
191202
return includes
192203
}
193204

194-
function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>) {
205+
function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
195206
const msg = `${trimPath(inc.path)} is missing or an invalid file.`
196207

197208
linterLog.error(msg, null)
@@ -212,7 +223,7 @@ function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], dia
212223
line: inc.lineNum,
213224
msg, file,
214225
}
215-
propogateDiagnostic(error, diagnostics)
226+
propogateDiagnostic(error, diagnostics, hasDirective)
216227
}
217228

218229
function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
@@ -221,14 +232,14 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
221232
stats = statSync(inc.path)
222233
} catch (e) {
223234
if (e.code === 'ENOENT') {
224-
ifInvalidFile(inc, lines, incStack, diagnostics)
235+
ifInvalidFile(inc, lines, incStack, diagnostics, hasDirective)
225236
return
226237
}
227238
throw e
228239
}
229240

230241
if (!stats.isFile()) {
231-
ifInvalidFile(inc, lines, incStack, diagnostics)
242+
ifInvalidFile(inc, lines, incStack, diagnostics, hasDirective)
232243
return
233244
}
234245

@@ -248,7 +259,7 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
248259
lines.splice(inc.lineNumTopLevel + 1 + dataLines.length, 0, `#line ${inc.lineNum + 1} "${inc.parent}"`)
249260
}
250261

251-
function lint(docURI: string, lines: string[], includes: Map<string, IncludeObj>, diagnostics: Map<string, Diagnostic[]>) {
262+
function lint(docURI: string, lines: string[], includes: Map<string, IncludeObj>, diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
252263
let out: string = ''
253264
try {
254265
execSync(`${conf.glslangPath} --stdin -S ${ext.get(path.extname(docURI))}`, {input: lines.join('\n')})
@@ -261,15 +272,15 @@ function lint(docURI: string, lines: string[], includes: Map<string, IncludeObj>
261272
if (!diagnostics.has(key)) diagnostics.set(key, [])
262273
})
263274

264-
processErrors(out, docURI, diagnostics)
275+
processErrors(out, docURI, diagnostics, hasDirective)
265276

266277
daigsArray(diagnostics).forEach(d => {
267278
if (win) d.uri = d.uri.replace('file://C:', 'file:///c%3A')
268279
connection.sendDiagnostics({uri: d.uri, diagnostics: d.diag})
269280
})
270281
}
271282

272-
function processErrors(out: string, docURI: string, diagnostics: Map<string, Diagnostic[]>) {
283+
function processErrors(out: string, docURI: string, diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
273284
filterMatches(out).forEach(match => {
274285
const error: ErrorMatch = {
275286
type: errorType(match[1]),
@@ -289,16 +300,16 @@ function processErrors(out: string, docURI: string, diagnostics: Map<string, Dia
289300
diagnostics.get(error.file.length - 1 ? error.file : docURI).push(diag)
290301

291302
// if is an include, highlight an error in the parents line of inclusion
292-
propogateDiagnostic(error, diagnostics)
303+
propogateDiagnostic(error, diagnostics, hasDirective)
293304
})
294305
}
295306

296307
//errorFile: string, type: DiagnosticSeverity, line: number, msg: string
297-
function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnostic[]>, parentURI?: string) {
308+
function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean, parentURI?: string) {
298309
includeGraph.get(parentURI || error.file).parents.forEach((pair, parURI) => {
299310
const diag: Diagnostic = {
300311
severity: error.type,
301-
range: calcRange(pair.first - 1, parURI),
312+
range: calcRange(pair.first - (pair.second.parents.size > 0 ? 0 : (2 - (hasDirective ? 1 : 0))), parURI),
302313
message: `Line ${error.line} ${trimPath(error.file)} ${replaceWords(error.msg)}`,
303314
source: 'mc-glsl'
304315
}
@@ -307,7 +318,7 @@ function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnos
307318
diagnostics.get(parURI).push(diag)
308319

309320
if (pair.second.parents.size > 0) {
310-
propogateDiagnostic(error, diagnostics, parURI)
321+
propogateDiagnostic(error, diagnostics, hasDirective, parURI)
311322
}
312323
})
313324
}

server/src/server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ documents.onDidSave((event) => onEvent(event.document))
3434

3535
// what am i saying here
3636
// dont do this for include files, for non-include files, clear diags for all its includes. Cache this maybe
37-
documents.onDidClose((event) => connection.sendDiagnostics({uri: event.document.uri, diagnostics: []}))
37+
documents.onDidClose((event) => {
38+
connection.sendDiagnostics({uri: event.document.uri, diagnostics: []})
39+
})
3840

3941
//documents.onDidChangeContent(onEvent)
4042

@@ -48,7 +50,9 @@ export function onEvent(document: vsclangproto.TextDocument) {
4850
}
4951

5052
// i think we still need to keep this in case we havent found the root of this files include tree
51-
if (!ext.has(extname(document.uri))) return
53+
const lines = document.getText().split('\n')
54+
const hasVersion = lines.filter(l => reVersion.test(l)).length > 0
55+
if (!ext.has(extname(document.uri)) || !hasVersion) return
5256

5357
try {
5458
preprocess(document.getText().split('\n'), uri)

server/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { serverLog } from './logging'
55

66
export function postError(e: Error) {
77
connection.window.showErrorMessage(e.message)
8-
serverLog.error(e.message, new Error())
8+
serverLog.error(e.message, null)
9+
console.log(e)
910
}
1011

1112
export const formatURI = (uri: string) => uri.replace(/^file:\/\//, '').replace(/^(?:\/)c%3A/, 'C:').replace(/\\/g, '/')

0 commit comments

Comments
 (0)