Skip to content

Commit

Permalink
#35: glslify support current file as basedir
Browse files Browse the repository at this point in the history
  • Loading branch information
hsimpson committed Oct 9, 2021
1 parent 40ab89a commit dfc4885
Show file tree
Hide file tree
Showing 6 changed files with 667 additions and 309 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to the "vscode-glsllint" extension will be documented in thi

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.5.x]

### Added

- Support glslify current file as basedir [#35](https://github.com/hsimpson/vscode-glsllint/issues/35)

## [1.4.x]

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ All settings are prefixed with `glsllint.`
| `languageSettings.[LANGID]` | see below | Settings per language VScode language ID, there are built in configurations for JS, JSX, TS, TSX and ELM. |
| `glslifyPattern` | `#pragma glslify:` | Regex pattern for glslify pragma |
| `glslifyAutoOpenOnError` | `true` | Opens the glslified code when there is a linting error |
| `glslifyOptions` | `{}` | Specify glslify options used in glslify.compile() if basedir is not set then the workspace root path is used |
| `glslifyOptions.basedir` | `""` | Specify the base directory for glslify.compile() |
| `glslifyUseCurrentFileAsBasedir` | `false` | Use the current file as the base directory for glslify (only if `glslifyOptions.basedir` is not set) |

### additionalStageAssociations example

Expand Down
35 changes: 20 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-glsllint",
"version": "1.4.1",
"version": "1.5.0",
"publisher": "CADENAS",
"repository": {
"type": "git",
Expand Down Expand Up @@ -87,6 +87,11 @@
"default": {},
"description": "Specify glslify options used in glslify.compile if basedir is not set then the workspace root path is used"
},
"glsllint.glslifyUseCurrentFileAsBasedir": {
"type": "boolean",
"default": false,
"description": "Use the current file as the basedir for glslify"
},
"glsllint.additionalStageAssociations": {
"type": "object",
"default": {},
Expand Down Expand Up @@ -148,25 +153,25 @@
"test": "webpack --config ./webpack.config.ts --mode production && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/node": "^15.0.2",
"@types/node": "^16.10.3",
"@types/vscode": "1.38.0",
"@types/webpack": "^5.28.0",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"@typescript-eslint/typescript-estree": "^4.23.0",
"eslint": "^7.26.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"@typescript-eslint/typescript-estree": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"prettier": "^2.3.0",
"ts-loader": "^9.1.2",
"ts-node": "^9.0.0",
"vsce": "^1.88.0",
"vscode-test": "^1.4.1",
"webpack": "^5.37.0",
"webpack-cli": "^4.7.0"
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.4.1",
"ts-loader": "^9.2.6",
"ts-node": "^10.2.1",
"vsce": "^1.100.1",
"vscode-test": "^1.6.1",
"webpack": "^5.58.1",
"webpack-cli": "^4.9.0"
},
"dependencies": {
"glslify": "^7.1.1",
"typescript": "^4.2.4"
"typescript": "^4.4.3"
}
}
27 changes: 17 additions & 10 deletions src/features/glsllintProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ export class GLSLLintingProvider {
}

private getValidatorPath(): string {
const config = vscode.workspace.getConfiguration('glsllint');
let glslangValidatorPath = config.glslangValidatorPath;
let glslangValidatorPath = this.config.glslangValidatorPath;

if (glslangValidatorPath === null || glslangValidatorPath === '') {
glslangValidatorPath = 'glslangValidator';
Expand Down Expand Up @@ -326,19 +325,27 @@ export class GLSLLintingProvider {
return glslifyRegEx.test(content);
}

private compileGlslify(content: string): string {
private compileGlslify(content: string, basedir: string): string {
try {
const glslifyOptions = {
...this.config.glslifyOptions,
};
glslifyOptions.basedir = glslifyOptions.basedir || vscode.workspace.rootPath;
return glslify.compile(content, glslifyOptions);
return glslify.compile(content, { basedir });
} catch (error) {
this.showMessage(`GLSL Lint: failed to compile the glslify file!\n${error.toString()}`, MessageSeverity.Error);
return '';
}
}

private getGlslifyBaseDir(textDocument: vscode.TextDocument): string {
if (this.config.glslifyOptions.basedir) {
return this.config.glslifyOptions.basedir; // use the user defined base dir
}

if (this.config.glslifyUseCurrentFileAsBasedir) {
return path.dirname(textDocument.fileName); // use the current file's directory
}

return vscode.workspace.rootPath;
}

private async openGlslifiedDocument(filename: string, content: string): Promise<void> {
const glslifyUri = vscode.Uri.parse(`${GLSLifyProvider.scheme}:${filename}-${this.GLSLIFIED_SUFFIX}`);
GLSLifyUriMapper.add(glslifyUri, content);
Expand All @@ -365,7 +372,7 @@ export class GLSLLintingProvider {
for (const [i, literal] of stringLiterals.entries()) {
const glslifyUsed = this.useGlslify(literal.text);
if (glslifyUsed) {
literal.text = this.compileGlslify(literal.text);
literal.text = this.compileGlslify(literal.text, this.getGlslifyBaseDir(textDocument));
}

if (literal.text !== '') {
Expand Down Expand Up @@ -401,7 +408,7 @@ export class GLSLLintingProvider {
const glslifyUsed = this.useGlslify(fileContent);

if (glslifyUsed) {
fileContent = this.compileGlslify(fileContent);
fileContent = this.compileGlslify(fileContent, this.getGlslifyBaseDir(textDocument));
}

if (fileContent !== '') {
Expand Down
5 changes: 3 additions & 2 deletions test/test_shaders/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"glsllint.glslifyAutoOpenOnError": true,
"glsllint.glslifyOptions": {
"basedir": "G:\\src\\vscodeextensions\\vscode-glsllint\\test\\test_shaders\\GLSLIFY\\chunks"
}
// "basedir": "G:\\src\\vscodeextensions\\vscode-glsllint\\test\\test_shaders\\GLSLIFY\\chunks"
},
"glsllint.glslifyUseCurrentFileAsBasedir": true
}
Loading

0 comments on commit dfc4885

Please sign in to comment.