prettier-plugin-glsl
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Prettier (https://prettier.io) plugin for GLSL (OpenGL Shading Language).", | ||
@@ -15,0 +15,0 @@ "exports": "./lib/prettier-plugin.cjs.js", |
@@ -27,10 +27,12 @@ ![npm](https://img.shields.io/npm/v/prettier-plugin-glsl?style=flat-square) | ||
`#define MAX3(genType) genType max3(genType a, genType b, genType c) { /* comment */ return max(max(a, b), c); }` | ||
will be formatted as. | ||
will be formatted as: | ||
<!-- Printed at 60 wide, so it fits on npm.js renderer site. --> | ||
```glsl | ||
#define MAX3(genType) \ | ||
genType max3(genType a, genType b, genType c) { \ | ||
/* comment */ \ | ||
return max(max(a, b), c); \ | ||
} | ||
#define MAX3(genType) \ | ||
genType max3(genType a, genType b, genType c) { \ | ||
/* comment */ \ | ||
return max(max(a, b), c); \ | ||
} | ||
``` | ||
@@ -57,2 +59,9 @@ | ||
Note that `.frag` files are recognized as JavaScript files by default. Add the | ||
following to your Prettier configuration to format them as GLSL. | ||
```json | ||
"overrides": [{"files": ["*.frag"], "options": {"parser": "glsl-parser"}}] | ||
``` | ||
## Limitations due to preprocessor | ||
@@ -59,0 +68,0 @@ |
@@ -300,6 +300,3 @@ // noinspection JSUnusedGlobalSymbols | ||
function formatMacroDefinition( | ||
doc: Doc, | ||
options: ParserOptions<Node | IToken>, | ||
): string { | ||
function formatMacroDefinition(doc: Doc, options: GlslParserOptions): string { | ||
propagateBreaks(doc) | ||
@@ -323,3 +320,3 @@ const formatted = printDocToString( | ||
path: AstPath, | ||
options: any, | ||
options: GlslParserOptions, | ||
sameIndent: any, | ||
@@ -372,4 +369,4 @@ filter: (arg0: any) => any, | ||
path: AstPath<Node | IToken>, | ||
options: ParserOptions<Node | IToken> & { inMacro?: Token[] }, | ||
rightPropertyName: string, | ||
options: GlslParserOptions, | ||
rightNode: Node | undefined, | ||
): | ||
@@ -382,5 +379,2 @@ | "break-after-operator" | ||
| "fluid" { | ||
const node = path.getValue() as Node | ||
const rightNode = (node as any)[rightPropertyName] as Node | ||
if (!rightNode) { | ||
@@ -433,9 +427,63 @@ return "only-left" | ||
/** | ||
* Print an `AssignmentExpression` or a variable `Declarator`. | ||
*/ | ||
function printAssignmentLike( | ||
path: AstPath, | ||
options: GlslParserOptions, | ||
leftDoc: Doc, | ||
operator: Doc, | ||
rightDoc: Doc, | ||
rightNode: Node | undefined, | ||
): Doc { | ||
const layout = chooseAssignmentLayout(path, options, rightNode) | ||
// TODO: need this group? | ||
// return group([ | ||
// name, | ||
// arraySpecifier, | ||
// " =", | ||
// group(indent(line), { id: groupId }), | ||
// indentIfBreak(p<typeof n>("init"), { groupId }), | ||
// ]) | ||
switch (layout) { | ||
// First break after operator, then the sides are broken independently on their own lines | ||
case "break-after-operator": | ||
return group([group(leftDoc), operator, group(indent([line, rightDoc]))]) | ||
// First break right-hand side, then left-hand side | ||
case "never-break-after-operator": | ||
return group([group(leftDoc), operator, " ", rightDoc]) | ||
// First break right-hand side, then after operator | ||
case "fluid": { | ||
const groupId = Symbol("assignment") | ||
return group([ | ||
group(leftDoc), | ||
operator, | ||
group(indent(line), { id: groupId }), | ||
lineSuffixBoundary, | ||
indentIfBreak(rightDoc, { groupId }), | ||
]) | ||
} | ||
// Parts of assignment chains aren't wrapped in groups. | ||
// Once one of them breaks, the chain breaks too. | ||
case "chain": | ||
return [group(leftDoc), operator, line, rightDoc] | ||
case "chain-tail": | ||
return [group(leftDoc), operator, indent([line, rightDoc])] | ||
case "only-left": | ||
return leftDoc | ||
default: | ||
throw new Error() | ||
} | ||
} | ||
type GlslParserOptions = ParserOptions<Node | IToken> & { inMacro?: Token[] } | ||
export const printers: Plugin<Node | IToken>["printers"] = { | ||
"glsl-ast": { | ||
print( | ||
path, | ||
options: ParserOptions<Node | IToken> & { inMacro?: Token[] }, | ||
print, | ||
): Doc { | ||
print(path, options: GlslParserOptions, print): Doc { | ||
const inMacro = options.inMacro | ||
@@ -606,59 +654,12 @@ const n = path.getValue() | ||
case "declarator": { | ||
const name = p<typeof n>("name") | ||
const arraySpecifier = p<typeof n>("arraySpecifier") | ||
if (!n.init) { | ||
return [name, arraySpecifier] | ||
} else { | ||
const layout = chooseAssignmentLayout(path, options, "init") | ||
const groupId = Symbol("declarator") | ||
const leftDoc = name | ||
const operator = " =" | ||
const rightDoc = p<typeof n>("init") | ||
// TODO: need this group? | ||
// return group([ | ||
// name, | ||
// arraySpecifier, | ||
// " =", | ||
// group(indent(line), { id: groupId }), | ||
// indentIfBreak(p<typeof n>("init"), { groupId }), | ||
// ]) | ||
switch (layout) { | ||
// First break after operator, then the sides are broken independently on their own lines | ||
case "break-after-operator": | ||
return group([ | ||
group(leftDoc), | ||
operator, | ||
group(indent([line, rightDoc])), | ||
]) | ||
// First break right-hand side, then left-hand side | ||
case "never-break-after-operator": | ||
return group([group(leftDoc), operator, " ", rightDoc]) | ||
// First break right-hand side, then after operator | ||
case "fluid": { | ||
const groupId = Symbol("assignment") | ||
return group([ | ||
group(leftDoc), | ||
operator, | ||
group(indent(line), { id: groupId }), | ||
lineSuffixBoundary, | ||
indentIfBreak(rightDoc, { groupId }), | ||
]) | ||
} | ||
// Parts of assignment chains aren't wrapped in groups. | ||
// Once one of them breaks, the chain breaks too. | ||
case "chain": | ||
return [group(leftDoc), operator, line, rightDoc] | ||
case "chain-tail": | ||
return [group(leftDoc), operator, indent([line, rightDoc])] | ||
case "only-left": | ||
return leftDoc | ||
default: | ||
throw new Error() | ||
} | ||
} | ||
const leftDoc = [n.name.image, arraySpecifier] | ||
return printAssignmentLike( | ||
path, | ||
options, | ||
leftDoc, | ||
" =", | ||
p<typeof n>("init"), | ||
n.init, | ||
) | ||
} | ||
@@ -677,11 +678,9 @@ case "arraySpecifier": | ||
"struct", | ||
" ", | ||
p<typeof n>("name"), | ||
" ", | ||
"{", | ||
n.name ? " " + n.name.image : "", | ||
" {", | ||
indent([ | ||
softline, | ||
join(softline, path.map(print, "declarations")), | ||
hardline, | ||
join(hardline, path.map(print, "declarations")), | ||
]), | ||
softline, | ||
hardline, | ||
"}", | ||
@@ -870,10 +869,11 @@ ]) | ||
case "assignmentExpression": { | ||
const groupId = Symbol("assignment") | ||
return group([ | ||
p<typeof n>("lhs"), | ||
" ", | ||
p<typeof n>("op"), | ||
group(indent(line), { id: groupId }), | ||
indentIfBreak(p<typeof n>("rhs"), { groupId }), | ||
]) | ||
const leftDoc = p<typeof n>("lhs") | ||
return printAssignmentLike( | ||
path, | ||
options, | ||
leftDoc, | ||
" " + n.op.image, | ||
p<typeof n>("rhs"), | ||
n.rhs, | ||
) | ||
} | ||
@@ -1086,3 +1086,3 @@ case "conditionalExpression": | ||
// Current options | ||
options: ParserOptions<Node | IToken>, | ||
options: GlslParserOptions, | ||
): Doc { | ||
@@ -1089,0 +1089,0 @@ const n = commentPath.getValue() as Token |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130
516779
11603