Skip to content

Commit

Permalink
auto-format using the latest intellij-awk : adjust minify.awk
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Dec 21, 2023
1 parent 36689f1 commit 196be3c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions minify.awk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ in_begin && /^}/ { in_begin = 0 }
in_begin && $1 ~ /^delete/{ next }
{ minifyLine() }

function minifyLine( l) {
function minifyLine( l,subs) {
if (!/"#"/ && !/\*#\// && !/\*\(#/) gsub("[ \t\r\n]*#.*$", "")
gsub(/ == /, "==")
gsub(/ = /, "=")
Expand Down Expand Up @@ -35,27 +35,31 @@ function minifyLine( l) {
gsub(/print +"/, "print\"")
gsub(/printf +"/, "printf\"")
if (!/^ +}/) gsub(/ +}/, "}")
gsubKeepStrings(", +", ",")
gsubKeepStrings(" *- ", "-")
gsubKeepStrings("] +", "]")
gsubKeepStrings("^ +in ", "in ")
gsubKeepStrings(" +$", "")
subs[", +"] = ","
subs[" *- "] = "-"
subs["] +"] = "]"
subs["^ +in "] = "in "
subs[" +$"] = ""
gsubKeepStrings(subs)
gsub(Q, Q "\\" Q Q)
if (l = trim($0)) { decreaseIndent(); printf "%s", (l == "}" ? l : (NR == 1 ? "" : "\n") $0) }
}
function decreaseIndent() {
match($0, /^ */)
$0 = sprintf("%" (RLENGTH - 1) / 2 "s", "") substr($0, RLENGTH)
}
function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) {
function gsubKeepStrings(subs, nonString,s,isString,i,c,regex) {
nonString = ""
s = ""
isString = 0
for (i=1;(c=substr($0,i,1))!="";i++) {
# print "> " i " " c
if ("\"" == c && (substr($0,i-1,1) != "\\" || substr($0,i-2,1) == "\\")) {
if (!isString) {
gsub(regex, replacement, nonString)
for (regex in subs)
gsub(regex, subs[regex], nonString)
s = s nonString
nonString = ""
} else {
Expand All @@ -70,7 +74,8 @@ function gsubKeepStrings(regex, replacement, nonString,s,isString,i,c) {
nonString = nonString c
}
}
gsub(regex, replacement, nonString)
for (regex in subs)
gsub(regex, subs[regex], nonString)
s = s nonString
$0 = s
}
Expand Down

0 comments on commit 196be3c

Please sign in to comment.