Skip to content

Instantly share code, notes, and snippets.

@heydamianc
Created October 5, 2011 22:04
Show Gist options
  • Save heydamianc/1265881 to your computer and use it in GitHub Desktop.
Save heydamianc/1265881 to your computer and use it in GitHub Desktop.
Formats Objective-C code (unfinished)
#! /usr/bin/env awk -f
BEGIN {
shiftWidth = 4
expandedTab = createExpandedTab(4)
}
{
s = stripTrailingWhitespace($0)
s = expandTabs(s, expandedTab)
if ($0 ~ /^[ \t]*@synthesize/) {
formatSynthesizeStatements()
}
print
}
function stripTrailingWhitespace(s) {
sub(/[ \t]+$/, "", s)
return s
}
function expandTabs(s, expandedTab) {
gsub(/[\t]/, expandedTab, s)
return s
}
function createExpandedTab(shiftWidth) {
expandedTab = ""
for (i = 0; i < shiftWidth; i++) {
expandedTab = expandedTab " "
}
return expandedTab
}
function formatSynthesizeStatements() {
i = 1
i = storeSynthesizedProperty(names, aliases, i)
while (getline) {
if ($0 ~ /^[ \t]*@synthesize/ || $0 == "\n") {
i = storeSynthesizedProperty(names, aliases, i)
}
else {
maxNameLength = 0
for (j = 1; j < i; j++) {
maxNameLength = max(maxNameLength, length(names[j]))
}
format = "@synthesize %-" maxNameLength "s = %s;\n"
for (j = 1; j < i; j++) {
printf(format, names[j], aliases[j])
}
break
}
}
}
function storeSynthesizedProperty(names, aliases, i) {
sub("@synthesize", "", $0)
sub(";", "", $0)
declarationCount = split($0, declarations, ",")
for (j = 1; j <= declarationCount; j++) {
if (split(declarations[j], values, "=") < 2) {
sub(" ", "", values[1])
names[i] = values[1]
aliases[i] = values[1]
}
else {
sub(" ", "", values[1])
sub(" ", "", values[2])
names[i] = values[1]
aliases[i] = values[2]
}
i++
}
return i
}
function max(m, n) {
return m > n ? m : n
}
#! /usr/bin/env awk -f
BEGIN {
PROPERTY_SHOULD_BE_FORMATTED = 1
PROPERTY_SHOULD_BE_FLUSH_AGAINST_PARENS = 1
}
{
if (PROPERTY_SHOULD_BE_FORMATTED) {
if ($0 ~ /^[ \t]*@property/) {
readPropertyDeclarationBlock(properties)
reformatPropertyDeclarationBlock(properties)
}
}
}
function readPropertyDeclarationBlock(properties) {
i = 1
properties[i++] = $0
while (getline) {
if ($0 ~ /^[ ]*@property/) {
properties[i++] = $0
} else {
break
}
}
}
function reformatPropertyDeclarationBlock(properties) {
maxPropertyDeclaration = 0
maxAnnotation = 0
maxVariableType = 0
for (i = 1; i <= length(properties); i++) {
loc = index(properties[i], ")")
propertyDeclaration = trim(substr(properties[i], 0, loc))
if (PROPERTY_SHOULD_BE_FLUSH_AGAINST_PARENS) {
propertyDeclaration = stripSpaceBetweenPropertyAndParens(propertyDeclaration)
}
propertyDeclaration = addSpaceAfterComma(propertyDeclaration)
propertyDeclaration = condenseWhitespace(propertyDeclaration)
maxPropertyDeclaration = max(maxPropertyDeclaration, length(propertyDeclaration))
propertyDeclarations[i] = propertyDeclaration
variableDeclaration = trim(substr(properties[i], loc + 1))
variableDeclaration = stripSpaceBetweenStarAndVariableName(variableDeclaration)
condenseWhitespace(variableDeclaration)
split(variableDeclaration, components)
annotation = ""
j = 1
for (; j <= length(components) - 2; j++) {
annotation = annotation components[j]
}
annotations[i] = annotation
maxAnnotation = max(maxAnnotation, length(annotation))
variableType = components[j++]
variableTypes[i] = variableType
maxVariableType = max(maxVariableType, length(variableType))
variableName = components[j]
variableNames[i] = variableName
}
format = "%-" maxPropertyDeclaration "s %-" maxAnnotation "s %-" maxVariableType "s %s\n"
for (i = 1; i <= length(properties); i++) {
printf(format, propertyDeclarations[i], annotations[i], variableTypes[i], variableNames[i])
}
}
function trim(s) {
sub(/^[ \t]*/, "", s)
sub(/[ \t]*$/, "", s)
return s
}
function stripSpaceBetweenPropertyAndParens(s) {
sub(/@property[ \t]*\(/, "@property(", s)
return s
}
function addSpaceAfterComma(s) {
gsub(/,/, ", ", s)
return s
}
function condenseWhitespace(s) {
gsub(/[ \t]+/, " ", s)
return s
}
function stripSpaceBetweenStarAndVariableName(s) {
sub(/\*[ \t]*/, " *", s)
return s
}
function max(m, n) {
return m > n ? m : n
}
#! /usr/bin/env awk -f
{
if ($0 ~ /^[ \t]*@synthesize/ || $0 == "\n") {
formatSynthesizeStatements()
}
}
function formatSynthesizeStatements() {
i = 1
i = storeSynthesizedProperty(names, aliases, i)
while (getline) {
if ($0 ~ /^[ \t]*@synthesize/ || $0 == "\n") {
i = storeSynthesizedProperty(names, aliases, i)
}
else {
maxNameLength = 0
for (j = 1; j < i; j++) {
maxNameLength = max(maxNameLength, length(names[j]))
}
format = "@synthesize %-" maxNameLength "s = %s;\n"
for (j = 1; j < i; j++) {
printf(format, names[j], aliases[j])
}
break
}
}
}
function storeSynthesizedProperty(names, aliases, i) {
sub("@synthesize", "", $0)
sub(";", "", $0)
declarationCount = split($0, declarations, ",")
for (j = 1; j <= declarationCount; j++) {
if (split(declarations[j], values, "=") < 2) {
sub(" ", "", values[1])
names[i] = values[1]
aliases[i] = values[1]
}
else {
sub(" ", "", values[1])
sub(" ", "", values[2])
names[i] = values[1]
aliases[i] = values[2]
}
i++
}
return i
}
function max(m, n) {
return m > n ? m : n
}
#! /usr/bin/env awk -f
BEGIN {
shiftWidth = 4
expandedTab = createExpandedTab(4)
}
{
$0 = stripTrailingWhitespace($0)
$0 = expandTabs($0, expandedTab)
if ($0 ~ /^[ \t]*@synthesize/ || $0 == "\n") {
synths[$2] = $4
while (getline) {
if ($0 ~ /^[ \t]*@synthesize/ || $0 == "\n") {
synths[$2] = $4
}
else {
len = 0
for (synth in synths) {
len = max(len, length(synth))
}
format = "@synthesize %-" len "s = %s\n"
for (synth in synths) {
printf(format, synth, synths[synth])
}
break
}
}
}
print
}
function stripTrailingWhitespace(line) {
sub(/[ \t]+$/, "", line)
return line
}
function expandTabs(line, expandedTab) {
gsub(/[\t]/, expandedTab, line)
return line
}
function createExpandedTab(shiftWidth) {
expandedTab = ""
for (i = 0; i < shiftWidth; i++) {
expandedTab = expandedTab " "
}
return expandedTab
}
function max(m, n) {
return m > n ? m : n
}
@baalexander
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment