Skip to content

Commit

Permalink
Merge pull request #272 from JannikGM/conditionalBeautify
Browse files Browse the repository at this point in the history
Fix preprocessor directives during GLSL beautify
  • Loading branch information
sebavan committed Aug 30, 2023
2 parents 9c193ce + a489cfe commit 638284f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
11 changes: 10 additions & 1 deletion sample/js/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ var vertexShaderSource = "attribute vec3 aVertexPosition;" +

var fragmentShaderSource = "varying lowp vec4 vColor;" +

"void main(void) {" +
"void main(void) {\n" +

"#define TEST {}\n" +
"\n" +
"\n" +
"#ifdef TEST\n" +
"{}\n" +
"#endif\n" +
"{}\n" +

" gl_FragColor = vColor;" +
"}";

Expand Down
43 changes: 33 additions & 10 deletions src/embeddedFrontend/resultView/sourceCode/sourceCodeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,41 @@ export class SourceCodeComponent extends BaseComponent<ISourceCodeState> {
* Beautify the given string : correct indentation according to brackets
*/
private _beautify(glsl: string, level: number = 0): string {
let spaces = "";
for (let i = 0; i < level; i++) {
spaces += " "; // 4 spaces
}

// return condition : no brackets at all
const untrimmedGlsl = glsl;
glsl = glsl.trim();

// If preprocessor, indent the preprocessor line and beautify the rest
if (glsl[0] === "#") {

// Figure out if we trimmed away a newline
const preprocessorStart = untrimmedGlsl.indexOf("#");
const newline = untrimmedGlsl.indexOf("\n");
let preservedNewline: string = "";
if (newline !== -1) {
if (newline < preprocessorStart) {
preservedNewline = spaces + "\n";
}
}

const firstLineEnd = glsl.indexOf("\n");
const preprocessorLineEnd = (firstLineEnd !== -1) ? firstLineEnd : glsl.length;
const preprocessorLine = glsl.substr(0, preprocessorLineEnd);
const rest = glsl.substr(preprocessorLineEnd + 1);

return preservedNewline + spaces + preprocessorLine + "\n" + this._beautify(rest, level);
}

// return condition : no brackets at all
glsl = this._adaptComments(glsl);
const brackets = this._getBracket(glsl);
const firstBracket = brackets.firstIteration;
const lastBracket = brackets.lastIteration;

let spaces = "";
for (let i = 0; i < level; i++) {
spaces += " "; // 4 spaces
}

let result: string;
// If no brackets, return the indented string
if (firstBracket === -1) {
Expand All @@ -181,13 +203,14 @@ export class SourceCodeComponent extends BaseComponent<ISourceCodeState> {
else {
// if brackets, beautify the inside
// let insideWithBrackets = glsl.substr(firstBracket, lastBracket-firstBracket+1);
const left = glsl.substr(0, firstBracket);
const right = glsl.substr(lastBracket + 1, glsl.length);
const left = glsl.substr(0, firstBracket).trim();
const right = glsl.substr(lastBracket + 1, glsl.length).trim();
const inside = glsl.substr(firstBracket + 1, lastBracket - firstBracket - 1).trim();
const prettyLeft = (left === "") ? spaces + "{" : this._beautify(left, level) + " {\n";
const prettyInside = this._beautify(inside, level + 1);
result = this._beautify(left, level) + " {\n" + prettyInside + "\n" + spaces + "}\n" + this._beautify(right, level);
const prettyRight = this._beautify(right, level);
result = prettyLeft + prettyInside + "\n" + spaces + "}\n" + prettyRight;
result = result.replace(/\s*\n+\s*;/g, ";"); // Orphan ;
result = result.replace(/#endif[\t \f\v]*{/g, "\n {"); // Curly after #Endig
}

result = result.replace(SourceCodeComponent.semicolonReplacementKeyRegex, ";");
Expand Down

0 comments on commit 638284f

Please sign in to comment.