-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFormating.py
More file actions
82 lines (65 loc) · 2.39 KB
/
Copy pathCodeFormating.py
File metadata and controls
82 lines (65 loc) · 2.39 KB
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
class CodeFormating():
def __init__(self, f_dir, f_name, lines, content):
self.f_dir = f_dir
self.f_name = f_name
self.lines = lines
self.content = content
def tab2space(self, spaces_per_tab):
i = 0
for line in self.lines:
chk = line.find("\t")
if chk != -1:
space = spaces_per_tab * " "
self.lines[i] = line.replace("\t", space)
i += 1
def make_pretty_comment(self):
i = 0
for line in self.lines:
chk = line.lstrip().find("--")
if chk > 0:
# remove multiple spaces at commentbegin
chk = line.find("-- ")
while chk != -1:
line = line.replace("-- ", "-- ")
chk = line.find("-- ")
# remove space before colon
chk = line.find(" ;")
while chk != -1:
line = line.replace(" ;", ";")
chk = line.find(" ;")
# check correctness
chk = line.find("; -- ")
if chk == -1:
# add space between ; and --
chk = line.find(";--")
if chk != -1:
line = line.replace(";--", "; --")
# remove multiple spaces between ; and --
chk = line.find(" --")
while chk != -1:
line = line.replace(" --", " --")
chk = line.find(" --")
# add space between -- and comment
chk = line.find("-- ")
if chk == -1:
line = line.replace("--", "-- ")
self.lines[i] = line
i += 1
def rm_bad_whitespaces(self):
i = 0
for line in self.lines:
# Colon without space
chk1 = line.lstrip().find(":")
chk2 = line.lstrip().find(": ")
if chk1 != -1 and chk2 == -1:
line = line.replace(":", ": ")
self.lines[i] = line
i += 1
def edit_file(self):
content = ""
for line in self.lines:
content += line.rstrip()+"\n"
f_out_path = self.f_dir + self.f_name
f_cf = open(f_out_path, "w")
f_cf.write(content)
f_cf.close()