UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

74 lines (73 loc) 2.2 kB
"use strict"; import { LineType } from "./LineType"; import { NetworkChildNodeType } from "../../../../poly/NodeContext"; const LINE_SUFFIXES = { [LineType.MEMBER]: "", [LineType.CONSTRUCTOR]: "", [LineType.DEFINE]: "", [LineType.BODY]: ";" // [LineType.TRIGGER]: ';', // [LineType.TRIGGERABLE]: ';', }; const LINE_PREFIXES = { [LineType.MEMBER]: "", [LineType.CONSTRUCTOR]: "", [LineType.DEFINE]: "", [LineType.BODY]: " " // [LineType.TRIGGER]: ' ', // [LineType.TRIGGERABLE]: ' ', }; const BLOCK_START_LAST_CHAR = "{"; const BLOCK_END_LAST_CHAR = "}"; export class CodeFormatter { static nodeComment(node, lineType) { let line = `// ${node.path()}`; let prefix = LINE_PREFIXES[lineType]; if (lineType == LineType.BODY) { let distance = this.nodeDistanceToMaterial(node); if (node.type() == NetworkChildNodeType.OUTPUT) { distance += 1; } prefix = prefix.repeat(distance); } if (lineType == LineType.BODY) { line = `${prefix}${line}`; } return line; } static lineWrap(node, line, lineType) { let add_suffix = true; if (line.includes("#if") || line.includes("#endif") || line.includes("#pragma unroll_loop_")) { add_suffix = false; } let prefix = LINE_PREFIXES[lineType]; if (lineType == LineType.BODY) { prefix = prefix.repeat(this.nodeDistanceToMaterial(node)); } line = `${prefix}${line}`; if (add_suffix) { const last_char = line[line.length - 1]; const suffix = LINE_SUFFIXES[lineType]; const lineIsEmpty = line.trim().length == 0; const lineIsComment = line.trim().startsWith("//"); if (last_char != suffix && last_char != BLOCK_START_LAST_CHAR && last_char != BLOCK_END_LAST_CHAR && !lineIsEmpty && !lineIsComment) { line += suffix; } } return line; } static post_line_separator(lineType) { return lineType == LineType.BODY ? " " : ""; } static nodeDistanceToMaterial(node) { const parent = node.parent(); if (!parent) { return 0; } if (parent.context() != node.context()) { return 1; } else { return 1 + this.nodeDistanceToMaterial(parent); } } }