Skip to content

Instantly share code, notes, and snippets.

@mariusae
Created March 20, 2012 16:18
Show Gist options
  • Save mariusae/2137803 to your computer and use it in GitHub Desktop.
Save mariusae/2137803 to your computer and use it in GitHub Desktop.
Format scala imports
#!/bin/bash
norm(){
awk '
/^import/ {
if(current) print current
current = $0
}
$0 !~ /^import/ {
current = current $0
}
END{
if(current) print current
}'
}
ifmt(){
awk '
length > 80 && /{/ {
i = index($0, "{")
pre = substr($0, 1, i)
post = substr($0, i+1, length($0))
sub("}.*$", "", post)
gsub(" +", " ", post)
print pre
line = " "
n=split(post, fs, ", *")
for(i in fs){
sub("^ +", "", fs[i])
sub(" +$", "", fs[i])
}
asort(fs)
for(i=1; i<=n; i++){
# if empty, continue, and n--?
line = line fs[i]
if(i!=n)
line = line ", "
if(i==n)
print line "}"
else if(length(line) > 70){
print line
line = " "
}
}
next;
}
{print}
'
}
norm|sort|ifmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment