Last active
December 20, 2023 07:09
-
-
Save 0racle/0267f04990beb33d31bcfbbbecab0664 to your computer and use it in GitHub Desktop.
Janitor - J code formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jconsole | |
NB. No space before these tokens | |
bef =: ;: ') @ @. @: & &. &: &.: " ` ;. !. !: / /. \ ~ }' | |
NB. No space after these tokens | |
aft =: ;: '( @ @. @: & &. &: &.: " ` ;. !. !:' | |
Cont =: {{ +./ ((dltb > x) (-: ;)"1 0 y) }} | |
Fmt =: {{ | |
tokens =. ;: y | |
res =. {. tokens | |
for_next. }. tokens do. | |
last =. > {. res | |
if. -. ((last Cont aft) +. (next Cont bef)) do. | |
next =. ' ', dltb ; next | |
end. | |
res =. res ;~ > next | |
end. | |
'' joinstring |. res | |
}} | |
NB. Reads boxed lines from ARGV filenames, otherwise from STDIN | |
ARGLINES =: {{ <;._2 stdin _ }}`{{ ,/ ('b' fread ])@> 2 }. y }}@.(2 < #) | |
MAIN =: {{ | |
for_line. ARGLINES y do. | |
indent =. ' ' #~ {. I. ' ' ~: > line | |
if. ((line_index -: 0) *. ('#' -: {. > line)) do. | |
echo > line | |
continue. | |
end. | |
echo indent , Fmt > line | |
end. | |
}} | |
MAIN ARGV | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better version using
j-lex