This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
forked from wsycharles0o/KillStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
killstyle.py
105 lines (97 loc) · 5.43 KB
/
killstyle.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
import subprocess
import sys
import os
DEFAULT_JAVADOC = "This is a Javadoc."
DEFAULT_PARAMDOC = "This is a parameter."
DEFAULT_RETURNDOC = "This thing returns."
#AUTHOR_NAME: string
#VERBOSE: boolean
#FIX_JAVADOC: boolean
#FIX_RETURN: boolean
#FIX_PARAM: boolean
args = lambda s: s.strip().split(":")
arg1 = lambda err: int(args(err)[1])
arg2 = lambda err: int(args(err)[2])
get_line = lambda s, n: s.split("\n")[n - 1]
get_line_num = lambda s,: len(s.split("\n"))
write_line = lambda s, n, l: "".join(map(lambda l: l + "\n", s.split("\n")[:n - 1] + [l] + s.split("\n")[n:]))[:-1]
add_new_line = lambda s, n, l: "".join(map(lambda l: l + "\n", s.split("\n")[:n - 1] + [l] + s.split("\n")[n - 1:]))[:-1]
change_line = lambda s, n, f: write_line(s, n, f(get_line(s, n)))
delete_char_at = lambda s, ln, cn: change_line(s, ln, lambda l: l[:cn - 1] + l[cn:])
find_comment_end_up = lambda s, ln: find_comment_end(s, ln - 1) if get_line(s, ln) is "" else (ln, get_line(s, ln).find("*/"))
find_comment_end_down = lambda s, ln: find_comment_end(s, ln + 1) if ln <= get_line_num(s) and get_line(s, ln).find("*/") is -1 else (ln, get_line(s, ln).find("*/"))
line_is_in_comment = lambda s, ln: False if ln <= 0 or get_line(s, ln).find("*/") is not -1 else (True if get_line(s, ln).find("/**") is not -1 else line_is_in_comment(s, ln -1))
find_comment_end = lambda s, ln: find_comment_end_down(s, ln) if line_is_in_comment(s, ln) else find_comment_end_up(s, ln)
add_new_line_at_end = lambda src: src + "\n"
add_author_tag = lambda src, err: add_new_line(src, arg1(err), "/** @author " + AUTHOR_NAME + " */")
add_javadoc = lambda src, err: add_new_line(src, arg1(err), "/** " + DEFAULT_JAVADOC + "*/")
add_paramdoc = lambda src, err: change_line(src, find_comment_end(src, arg1(err) - 1)[0], lambda l: l.rstrip().rstrip("*/").rstrip() + "\n * @param " + args(err)[3][26:-2] + " " + DEFAULT_PARAMDOC + " */") if find_comment_end(src, arg1(err) - 1)[1] is not -1 else add_new_line(src, arg1(err), "/** @param " + args(err)[3][26:-2] + " " + DEFAULT_PARAMDOC + " */")
add_returndoc = lambda src, err: change_line(src, find_comment_end(src, arg1(err) - 1)[0], lambda l: l.rstrip().rstrip("*/").rstrip() + "\n * @return " + DEFAULT_RETURNDOC + " */") if find_comment_end(src, arg1(err) - 1)[1] is not -1 else add_new_line(src, arg1(err), "/** @return " + DEFAULT_RETURNDOC + " */")
add_space = lambda src, err: change_line(src, arg1(err), lambda l: l[:arg2(err) - 1] + " " + l[arg2(err) - 1:])
delete_unknown_tag = lambda src, err: delete_char_at(src, arg1(err), arg2(err))
delete_trailing_spaces = lambda src, err: change_line(src, arg1(err), lambda l: l.rstrip())
improve_comment_style = lambda src, err: change_line(src, arg1(err), lambda l: l[:arg2(err) - 1] + "/*" + l[arg2(err) + 1:] + "*/")
improve_indent = lambda src, err: change_line(src, arg1(err), lambda l: " " * int(err.split(",")[1]) + l.lstrip())
break_line = lambda src, err: change_line(src, arg1(err), lambda l: l[:len(l) - l[::-1].find(" ") - 1] + "\n" + l[len(l) - l[::-1].find(" ") - 1:])
def fix(err, src):
if err.find("File does not end with a newline.") is not -1:
src = add_new_line_at_end(src)
if err.find("Unknown tag") is not -1:
src = delete_unknown_tag(src, err)
if err.find("Type Javadoc comment is missing an @author tag.") is not -1:
src = add_author_tag(src, err)
if err.find("Line has trailing spaces.") is not -1:
src = delete_trailing_spaces(src, err)
if err.find("'//'-style comments are not allowed.") is not -1:
src = improve_comment_style(src, err)
if err.find("Missing a Javadoc comment.") is not -1 and FIX_JAVADOC:
src = add_javadoc(src, err)
if err.find("Expected @param tag for") is not -1 and FIX_PARAM:
src = add_paramdoc(src, err)
if err.find("Expected an @return tag.") is not -1 and FIX_RETURN:
src = add_returndoc(src, err)
if err.find("is not followed by whitespace.") is not -1:
src = add_space(src, err)
if err.find("is not preceded with whitespace.") is not -1:
src = add_space(src, err)
if err.find("Line is longer than 80 characters") is not -1:
src = break_line(src, err)
if err.find("should be on a new line.") is not -1:
src = break_line(src, err)
if err.find("not at correct indentation") is not -1:
src = improve_indent(src, err)
return src
def fix_file(javafile):
#javafile = args[0]
assert isinstance(javafile,str), "Java file name invalid!"
#try:
# os.remove("./run.bat")
#except Exception:
# #FIX ME
#try:
# os.remove("./res.tmp")
#except Exception:
#FIX ME
subprocess.call("@echo off\n@del /f /s run.bat", shell = True)
subprocess.call("@echo off\n@del /f /s *.tmp", shell = True)
subprocess.call("@echo off > run.bat", shell = True)
f_run = open("run.bat", "w")
f_run.write("@echo off\n@java -cp ucb-checkstyle.jar ucb.checkstyle.Main ")
f_run.write(javafile)
f_run.write(" > res.tmp")
f_run.close()
subprocess.call("@run.bat", shell=True)
f_res = open("res.tmp", "r")
res = f_res.read()
f_res.close()
lines = res.split("\n")
# subprocess.call("cls", shell=True)
f_source = open(javafile, "r")
source = f_source.read()
f_source.close()
for i in range(len(lines)):
source = fix(lines[len(lines) - i - 1], source)
f_source = open(javafile, "w")
f_source.write(source)
f_source.close()
# run(sys.argv[1:] if len(sys.argv) >= 2 else help_page())