-
Notifications
You must be signed in to change notification settings - Fork 0
/
swiftCleaner.py
163 lines (147 loc) · 6.14 KB
/
swiftCleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fileinput
import os
import sys
import copy
shouldFixLongLines = False
for root, dirs, files in os.walk(os.path.dirname(os.path.abspath(__file__))):
for file in files:
if file.endswith(".swift"):
print(os.path.join(root, file))
f = open(os.path.join(root, file),'r')
filedata = f.read()
f.close()
newdata = filedata
# newdata = newdata.replace(" ", "\t")
newdata = newdata.replace("\t\t ", "\t\t\t")
newdata = newdata.replace("\n\n\n", "\n\n")
newdata = newdata.replace("delete this line\n\n", "")
newdata = newdata.replace("delete this line\n", "")
newdata = filedata.split("\n")
for temp in range(len(newdata[14:])):
# This is explain below more. Maybe we should start implementing this.
# newdata[x] = " ".join(newdata[x].split())
x = temp + 14
if not newdata[x].strip():
newdata[x] = ""
# if "\t" == newdata[x] or "\t\t" == newdata[x] or "\t\t\t" == newdata[x] or "\t\t\t\t" == newdata[x] or "\t\t\t\t\t" == newdata[x] or "\t\t\t\t\t\t" == newdata[x] or "\t\t\t\t\t\t\t" == newdata[x] or "\t" * 8 == newdata[x] or "\t" * 9 == newdata[x] or "\t" * 10 == newdata[x] or "\t" * 11 == newdata[x]:
# newdata[x] = ""
if "*" in newdata[x]:
continue
if "///" in newdata[x]:
continue
elif "//" in newdata[x]:
if newdata[x-1] != "" and "//" not in newdata[x-1]:
newdata[x] = "\n" + newdata[x]
elif newdata[x+1] == "":
newdata[x+1] = "delete this line"
if "//" in newdata[x]:
continue
if "\tif" in newdata[x]:
index = newdata[x].find("\tif")
try:
if newdata[x][index + 3] != " ":
newdata[x] = newdata[x][:index + 3] + " " + newdata[x][index + 3:]
except Exception as e:
pass
if "\tif" in newdata[x] and "else" not in newdata[x]:
if newdata[x-1] != "" and "//" not in newdata[x-1]:
print(newdata[x-1])
newdata[x] = "\n"+newdata[x]
elif "else" in newdata[x]:
index = newdata[x].find("else")
try:
if newdata[x][index - 1] == "}":
newdata[x] = newdata[x][:index] + " " + newdata[x][index:]
except Exception as e:
pass
#There appear to be some cases where this does not work. i think it is
#when there are no spaces on either side. I am not sure yet.
charactersToHaveSpacesOnBothSides = [":", ",", "->", "==", "+=", "-=", ">=", "<=", "="]
for character in charactersToHaveSpacesOnBothSides:
if character in newdata[x]:
index = newdata[x].find(character)
while (index != -1):
if (len(newdata[x]) > index + len(character)):
if (not (character == "=" and ("==" in newdata[x] or "!=" in newdata[x] or "+=" in newdata[x] or "-=" in newdata[x] or "<=" in newdata[x] or ">=" in newdata[x]))):
#Is Next Space
if newdata[x][index + len(character)] != " " and newdata[x][index + len(character)] != "\t":
newdata[x] = newdata[x][:index + len(character)] + " " + newdata[x][index + len(character):]
#Is Previous Space
if character != "," and character != ":":
if newdata[x][index - 1] != " " and newdata[x][index - 1] != "\t":
newdata[x] = newdata[x][:index] + " " + newdata[x][index:]
else:
if newdata[x][index - 1] == " ":
newdata[x] = newdata[x][:index-1] + newdata[x][index:]
index = newdata[x].find(character, index + len(character) + 1)
else:
break
if "{" in newdata[x]:
index = newdata[x].find("{")
try:
if newdata[x][index - 1] != " ":
newdata[x] = newdata[x][:index] + " " + newdata[x][index:]
except Exception as e:
pass
#this appears to work in 90% of the cases. in some cases, this just
#will keep adding white space on every single run
#I believe a viable fix will be to start this off after the initial tabs
#and then call " ".join(theline.split()) so we can remove any types of
#whitespace. Maybe this tactic should be done for each line at the top
#and apply to every single part of this script.
numberOfLinesToAlign = 1
charactersToAlignAt = [":", "="]
for chatacter in charactersToAlignAt:
if chatacter in newdata[x] and "==" not in newdata[x]:
index = newdata[x].find(chatacter)
lineNums = [(x, index)]
y = copy.deepcopy(x)
while (True):
y -= 1
if ("==" not in newdata[y]):
index = newdata[y].find(chatacter)
else:
break
if index == -1:
break
lineNums.append((y, index))
if (len(lineNums) > 1):
print(lineNums)
greatest = -1
for line in lineNums:
if line[1] > greatest:
greatest = line[1]
for line in lineNums:
if line[1] < greatest:
diff = greatest - line[1]
if diff < 15:
newdata[line[0]] = newdata[line[0]][:line[1] - 1] + (" "*diff) + newdata[line[0]][line[1] - 1:]
if (shouldFixLongLines):
if len(newdata[x]) > 80 and "//" not in newdata[x]:
if '"' in newdata[x]:
pass
# newdata[x] = newdata[x] + " //Line has too many characters says SwiftCleaner"
elif "," in newdata[x]:
index = newdata[x].find(",")
newdata[x] = newdata[x][:index + 1] + "\n" + ("\t" * (newdata[x].count("\t") + 2)) + newdata[x][index + 1:]
elif "->" in newdata[x]:
index = newdata[x].find("->")
newdata[x] = newdata[x][:index - 1] + "\n" + ("\t" * (newdata[x].count("\t") + 2)) + newdata[x][index - 1:]
elif " = " in newdata[x]:
index = newdata[x].find("=")
newdata[x] = newdata[x][:index + 1] + "\n" + ("\t" * (newdata[x].count("\t") + 2)) + newdata[x][index + 1:]
else:
pass
# print("Could not fix line")
# newdata[x] = newdata[x] + " //Line has too many characters says SwiftCleaner"
newdata = "\n".join(str(x) for x in newdata)
# newdata = newdata.replace(" ", "\t")
newdata = newdata.replace("\t\t ", "\t\t\t")
newdata = newdata.replace("\n\n\n", "\n\n")
newdata = newdata.replace("delete this line\n\n", "")
newdata = newdata.replace("delete this line\n", "")
# newdata = newdata.replace("\n\n\n", "\n\n")
# print(newdata)
f = open(os.path.join(root, file),'w')
f.write(newdata)
f.close()