Skip to content

Commit 2421aeb

Browse files
committed
Added support for ../ includes
1 parent 243eb67 commit 2421aeb

File tree

6 files changed

+48
-47
lines changed

6 files changed

+48
-47
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Minecraft GLSL Shaders
22
## vscode-mc-shader
33

4-
[![CI](https://ci.netsoc.co/api/badges/Strum355/vscode-mc-shader/status.svg?branch=master)](https://ci.netsoc.co/Strum355/vscode-mc-shader)
4+
<!-- [![CI](https://ci.netsoc.co/api/badges/Strum355/vscode-mc-shader/status.svg?branch=master)](https://ci.netsoc.co/Strum355/vscode-mc-shader)
55
[![Issues](https://img.shields.io/github/issues-raw/Strum355/vscode-mc-shader.svg)](https://github.com/Strum355/vscode-mc-shader/issues)
66
[![license](https://img.shields.io/github/license/Strum355/vscode-mc-shader.svg)](https://github.com/Strum355/vscode-mc-shader)
77
[![Maintainability](https://api.codeclimate.com/v1/badges/c2c813cb0a42a8aad483/maintainability)](https://codeclimate.com/github/Strum355/vscode-mc-shader/maintainability)
8-
[![Waffle.io - Columns and their card count](https://badge.waffle.io/Strum355/vscode-mc-shader.svg?columns=all)](https://waffle.io/Strum355/vscode-mc-shader)
8+
[![Waffle.io - Columns and their card count](https://badge.waffle.io/Strum355/vscode-mc-shader.svg?columns=all)](https://waffle.io/Strum355/vscode-mc-shader) -->
99

1010
VSCode-MC-Shader is a [Visual Studio Code](https://code.visualstudio.com/) extension for developing Minecraft GLSL Shaders for [Optifine](http://optifine.net). It currently provides linting and syntax highlighting (by stef-levesque/vscode-shader dependency).
1111

@@ -31,7 +31,7 @@ Got a feature request? Chuck it into an Issue!
3131
- Visual Studio Code (v1.17.0 or higher - minimum requirement untested)
3232
- The [Shader languages support for VS Code](https://marketplace.visualstudio.com/items?itemName=slevesque.shader) extension. This should automatically install when you install this extension.
3333
- That the shader(s) you're editing are in the `shaderpacks` folder in `.minecraft`.
34-
- The [OpenGL / OpenGL ES Reference Compiler](https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang/Install/) (for convenience, put it in your PATH, this is the assumed location if not specified). If, for some reason, you're using MacOS, there are no pre-compiled binaries of this.
34+
- The [OpenGL / OpenGL ES Reference Compiler](https://github.com/KhronosGroup/glslang) (for convenience, put it in your PATH, this is the assumed location if not specified). If, for some reason, you're using MacOS, there are no pre-compiled binaries of this.
3535
- [MacOS] Not MacOS. Not that you're making MC Shaders on/for MacOS anyways...right?
3636

3737
## Extension Settings

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-mc-shader",
33
"displayName": "Minecraft GLSL Shaders",
44
"description": "A Visual Studio Code extension for linting/etc Minecraft GLSL Shaders",
5-
"version": "0.9.0",
5+
"version": "0.8.1",
66
"publisher": "Strum355",
77
"author": "Noah Santschi-Cooney (Strum355)",
88
"license": "MIT",

server/src/comment.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ export namespace Comment {
3333

3434
function empty(i: number, line: string, twice: boolean): string {
3535
line = line.substr(0, i) + ' ' + line.substr(i + 1)
36-
if (twice) {
37-
i++
38-
line = line.substr(0, i) + ' ' + line.substr(i + 1)
39-
}
36+
if (twice) line = line.substr(0, i + 1) + ' ' + line.substr(i + 3)
4037
return line
4138
}
4239
}

server/src/config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { execSync } from 'child_process'
88
import { serverLog } from './logging'
99
import { dirname } from 'path'
1010
import { DidChangeConfigurationParams } from 'vscode-languageserver/lib/main'
11+
import { win } from './linter';
1112

1213
const url = {
1314
'win32': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-windows-x64-Release.zip',
@@ -87,16 +88,16 @@ async function downloadGlslang() {
8788
createReadStream(conf.shaderpacksPath + '/glslangValidator.zip')
8889
.pipe(unzip.Parse())
8990
.on('entry', entry => {
90-
if (entry.path === 'bin/glslangValidator') {
91-
entry.pipe(createWriteStream(conf.shaderpacksPath + '/glslangValidator'))
91+
if (entry.path === 'bin/glslangValidator' + win ? '.exe' : '') {
92+
entry.pipe(createWriteStream(conf.shaderpacksPath + '/glslangValidator' + win ? '.exe' : ''))
9293
return
9394
}
9495
entry.autodrain()
9596
})
9697
.on('close', () => {
97-
chmodSync(conf.shaderpacksPath + '/glslangValidator', 0o775)
98+
chmodSync(conf.shaderpacksPath + '/glslangValidator' + win ? '.exe' : '', 0o775)
9899
unlinkSync(conf.shaderpacksPath + '/glslangValidator.zip')
99-
connection.sendNotification('update-config', conf.shaderpacksPath + '/glslangValidator')
100+
connection.sendNotification('update-config', conf.shaderpacksPath + '/glslangValidator' + win ? '.exe' : '')
100101
connection.window.showInformationMessage('glslangValidator has been downloaded to ' + conf.shaderpacksPath + '/glslangValidator. Your config should be updated automatically.')
101102
glslangReady = true
102103
})

server/src/linter.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const reVersion = /#version [\d]{3}/
1515
const reInclude = /^(?:\s)*?(?:#include) "(.+)"\r?/
1616
const reIncludeExt = /#extension GL_GOOGLE_include_directive ?: ?require/
1717
const include = '#extension GL_GOOGLE_include_directive : require'
18-
const win = platform() === 'win32'
18+
export const win = platform() === 'win32'
1919

2020
const filters = [
2121
/stdin/,
@@ -111,23 +111,22 @@ function includeDirective(lines: string[], docURI: string): boolean {
111111
break
112112
}
113113

114-
if (i === lines.length - 1) {
114+
/* if (i === lines.length - 1) {
115115
linterLog.warn(() => `no version found for ${docURI}. inserting at top`)
116116
lines.splice(0, 0, include)
117117
break
118-
}
118+
} */
119119
}
120120
return hasDirective
121121
}
122122

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
127-
const includes = getIncludes(incStack[0], lines)
126+
const parent = incStack[incStack.length - 1]
127+
const includes = getIncludes(parent, lines)
128128
includes.forEach(i => allIncludes.add(i))
129129

130-
const parent = incStack[incStack.length - 1]
131130
includeGraph.nodes.get(parent).children.forEach((node, uri) => {
132131
if (!includes.has(uri)) {
133132
includeGraph.nodes.get(parent).children.delete(uri)
@@ -154,14 +153,14 @@ function getIncludes(uri: string, lines: string[]) {
154153
total: -1,
155154
comment: Comment.State.No,
156155
parStack: [uri],
157-
count: [0],
156+
count: [-1],
158157
}
159158

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

163162
// TODO can surely be reworked
164-
function processLine(includes: IncludeObj[], line: string, lines: string[], i: number, linesInfo: LinesProcessingInfo): IncludeObj[] {
163+
function processLine(includes: Map<string, IncludeObj>, line: string, lines: string[], i: number, linesInfo: LinesProcessingInfo): Map<string, IncludeObj> {
165164
const updated = Comment.update(line, linesInfo.comment)
166165
linesInfo.comment = updated[0]
167166

@@ -177,7 +176,6 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
177176

178177
if (line.startsWith('#line')) {
179178
const inc = line.slice(line.indexOf('"') + 1, line.lastIndexOf('"'))
180-
181179
if (inc === linesInfo.parStack[linesInfo.parStack.length - 2]) {
182180
linesInfo.count.pop()
183181
linesInfo.parStack.pop()
@@ -191,13 +189,16 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
191189
const match = line.match(reInclude)
192190

193191
if (match) {
194-
includes.push({
195-
path: formatURI(absPath(linesInfo.parStack[linesInfo.parStack.length - 1], match[1])),
196-
lineNum: linesInfo.count[linesInfo.count.length - 1],
197-
lineNumTopLevel: linesInfo.total,
198-
parent: formatURI(linesInfo.parStack[linesInfo.parStack.length - 1]),
199-
match
200-
})
192+
includes.set(
193+
formatURI(absPath(linesInfo.parStack[linesInfo.parStack.length - 1], match[1])),
194+
{
195+
path: formatURI(absPath(linesInfo.parStack[linesInfo.parStack.length - 1], match[1])),
196+
lineNum: linesInfo.count[linesInfo.count.length - 1],
197+
lineNumTopLevel: linesInfo.total,
198+
parent: formatURI(linesInfo.parStack[linesInfo.parStack.length - 1]),
199+
match
200+
}
201+
)
201202
}
202203
return includes
203204
}
@@ -210,7 +211,7 @@ function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], dia
210211
const file = incStack[incStack.length - 1]
211212
const diag: Diagnostic = {
212213
severity: DiagnosticSeverity.Error,
213-
range: calcRange(inc.lineNum, file),
214+
range: calcRange(inc.lineNum - ((!hasDirective && includeGraph.get(file).parents.size === 0) ? 1 : 0), file),
214215
message: msg,
215216
source: 'mc-glsl'
216217
}
@@ -227,9 +228,13 @@ function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], dia
227228
}
228229

229230
function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
230-
let stats: Stats
231231
try {
232-
stats = statSync(inc.path)
232+
const stats = statSync(inc.path)
233+
if (!stats.isFile()) {
234+
const err = new Error()
235+
err['code'] = 'ENOENT'
236+
throw err
237+
}
233238
} catch (e) {
234239
if (e.code === 'ENOENT') {
235240
ifInvalidFile(inc, lines, incStack, diagnostics, hasDirective)
@@ -238,11 +243,6 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
238243
throw e
239244
}
240245

241-
if (!stats.isFile()) {
242-
ifInvalidFile(inc, lines, incStack, diagnostics, hasDirective)
243-
return
244-
}
245-
246246
const dataLines = readFileSync(inc.path).toString().split('\n')
247247

248248
// if the includes parent is the top level (aka where the include directive is placed)
@@ -293,9 +293,8 @@ function processErrors(out: string, docURI: string, diagnostics: Map<string, Dia
293293

294294
const diag: Diagnostic = {
295295
severity: error.type,
296-
// had to do - 2 here instead of - 1, windows only perhaps?
297296
range: calcRange(error.line - ((!hasDirective && includeGraph.get(fileName).parents.size === 0) ? 2 : 1), fileName),
298-
message: `Line ${error.line} ${replaceWords(error.msg)}`,
297+
message: `Line ${error.line + 1} ${replaceWords(error.msg)}`,
299298
source: 'mc-glsl'
300299
}
301300

@@ -311,8 +310,8 @@ function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnos
311310
includeGraph.get(parentURI || error.file).parents.forEach((pair, parURI) => {
312311
const diag: Diagnostic = {
313312
severity: error.type,
314-
range: calcRange(pair.first - (pair.second.parents.size > 0 ? 0 : (2 - (hasDirective ? 1 : 0))), parURI),
315-
message: `Line ${error.line} ${trimPath(error.file)} ${replaceWords(error.msg)}`,
313+
range: calcRange(pair.first - ((!hasDirective && pair.second.parents.size === 0) ? 1 : 0), parURI),
314+
message: `Line ${error.line + 1} ${trimPath(error.file)} ${replaceWords(error.msg)}`,
316315
source: 'mc-glsl'
317316
}
318317

@@ -338,6 +337,8 @@ const filterMatches = (output: string) => output
338337
.filter(match => match && match.length === 5)
339338

340339
function calcRange(lineNum: number, uri: string): Range {
340+
linterLog.debug(() => `calculating range for ${trimPath(uri)} at L${lineNum}`)
341+
341342
const lines = getDocumentContents(uri).split('\n')
342343
const line = lines[lineNum]
343344
const startOfLine = line.length - line.trimLeft().length
@@ -353,9 +354,11 @@ function absPath(currFile: string, includeFile: string): string {
353354
}
354355

355356
// TODO add explanation comment
356-
if (includeFile.charAt(0) === '/') {
357+
if (includeFile.charAt(0) === '/' || (includeFile.charAt(0) === '.' && includeFile.charAt(1) === '.')) {
357358
const shaderPath = trimPath(currFile).split('/').slice(0, 3).join('/')
358359
return path.join(conf.shaderpacksPath, shaderPath, includeFile)
359-
}
360+
} /* else if (includeFile.charAt(0) === '.' && includeFile.charAt(1) === '.') {
361+
362+
} */
360363
return path.join(path.dirname(currFile), includeFile)
361364
}

server/src/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ export function onEvent(document: vsclangproto.TextDocument) {
4545

4646
const uri = formatURI(document.uri)
4747
if (includeGraph.get(uri).parents.size > 0) {
48-
lintBubbleDown(uri, document)
48+
lintBubbleDown(uri)
4949
return
5050
}
5151

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

5757
try {
5858
preprocess(document.getText().split('\n'), uri)
@@ -61,10 +61,10 @@ export function onEvent(document: vsclangproto.TextDocument) {
6161
}
6262
}
6363

64-
function lintBubbleDown(uri: string, document: vsclangproto.TextDocument) {
64+
function lintBubbleDown(uri: string) {
6565
includeGraph.get(uri).parents.forEach((parent, parentURI) => {
6666
if (parent.second.parents.size > 0) {
67-
lintBubbleDown(parentURI, document)
67+
lintBubbleDown(parentURI)
6868
} else {
6969
const lines = getDocumentContents(parentURI).split('\n')
7070
// feel like we could perhaps do better? Hope no one puts #version at the top of their includes..

0 commit comments

Comments
 (0)