twigl icon indicating copy to clipboard operation
twigl copied to clipboard

[Feature request] GLSL code formatting feature

Open cx20 opened this issue 4 years ago • 8 comments

I like to look at the twigl code on Twitter. However, much of that code is obfuscated to keep it short. How about a formatting feature to make the obfuscated code easier to read?

I think js-beautify can be used to format the code. https://github.com/beautify-web/js-beautify

The following is an implementation image.

before: image

after: image

I think js-beautify can be called as follows.

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.13/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.13/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.13/beautify-html.js"></script>
beautify.addEventListener('click', () => {
    const opts = {
        indent_size: 4
    };
    editor.setValue(js_beautify(editor.getValue(), opts));
}, false);

cx20 avatar May 19 '21 14:05 cx20

wow! this is a good idea. However, would you mind if I change the position and design of the bracket icons by myself?

doxas avatar May 19 '21 14:05 doxas

Yes. I'll leave it to you to decide how to implement it.

cx20 avatar May 19 '21 15:05 cx20

BTW, I found one problem with JS Beautifier. In the case of #define, when Beautifier is executed, unexpected line breaks may occur.

before)

#define X(s,l)d=s*sqrt(h);if(h>0.&&d>m&&sin(atan(d,P.y)+P.x*3.+t*l*4.)>.9)m=d;

after)

#define X(s, l) d = s * sqrt(h);
if (h > 0. && d > m && sin(atan(d, P.y) + P.x * 3. + t * l * 4.) > .9) m = d;

Unfortunately, I don't know how to solve this. I hope someone can help us.

cx20 avatar Aug 09 '21 13:08 cx20

That possibility occurred to me right away. So I haven't started on this issue, imagining that I would probably have to make the parser myself.

When I get some time, I might give it a shot!

doxas avatar Aug 09 '21 17:08 doxas

I haven't tried it, but the following GLSL parser may be helpful. https://github.com/stackgl/glsl-parser

cx20 avatar Aug 10 '21 12:08 cx20

Here's one: http://evanw.github.io/glslx/

It can both format/prettify, and minify. Might be useful for writing expanded code, then hitting minify to see if it fits in a tweet, then undoing and continuing editing, etc.

trusktr avatar Aug 08 '22 05:08 trusktr

This could certainly be useful.

doxas avatar Aug 09 '22 09:08 doxas

BTW, In the future, AI may help make code easier to read. https://twitter.com/rianflo/status/1618386347810779136

cx20 avatar Jan 27 '23 02:01 cx20