-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeEditor.py
More file actions
274 lines (208 loc) · 10.5 KB
/
codeEditor.py
File metadata and controls
274 lines (208 loc) · 10.5 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from tkinter import *
from tkinter.font import *
from windowSetting import setCenter # type: ignore
import subprocess
import re
def openEditor(editor, window):
editorWindow= editor.openEditor()
window.withdraw()
window.wait_window(editorWindow)
window.deiconify()
class Editor():
windowHeight = 700
windowWidth = 700
completed = False
numFailCompile = 0
numFailTests = 0
numRunTests = 0
def __init__(self, title, filePath, passedMessage = ""):
self.title = title
self.filePath = filePath
self.passedMessage = passedMessage
def openEditor(self):
self.window = Toplevel()
self.window.title(self.title)
setCenter(self.window, self.windowWidth, self.windowHeight)
self.filePath = self.filePath
# Text editor features
self.textEditorFrame = Frame(self.window, width=self.windowWidth, height=self.windowHeight)
self.textEditorFrame.pack(fill="both", expand=True)
font = Font(family="Courier New", size=12)
self.lineNumbers = Text(self.textEditorFrame, width=4, bg="#2e2e2e", fg="#858585", font=font, state='disabled')
self.lineNumbers.pack(side="left", fill="y")
self.textScrollFrame = Frame(self.textEditorFrame)
self.textScrollFrame.pack(side="right", fill="both", expand=True)
self.textBox = Text(self.textScrollFrame, wrap="none", font=font, bg="#1e1e1e", fg="#d4d4d4", insertbackground="#d4d4d4")
self.textBox.pack(side="left", fill="both", expand=True)
tabWidth = font.measure(" " * 4)
self.textBox.config(tabs=tabWidth)
self.scroll = Scrollbar(self.textScrollFrame, command=self.textBox.yview)
self.scroll.pack(side="right", fill="y")
self.scroll.config(command=self.onScroll)
self.textBox.config(yscrollcommand=self.onTextScroll)
self.textBox.bind("<KeyRelease>", self.onKeyRelease)
self.textBox.bind("<MouseWheel>", self.updateLineNumbers)
self.textBox.bind("<Button-1>", self.updateLineNumbers)
self.textBox.bind("<Configure>", self.updateLineNumbers)
self.updateLineNumbers()
self.highlightSyntax()
self.readFile()
# Buttons
self.buttonFrame = Frame(self.window, width=self.windowWidth, height=self.windowHeight // 10)
self.buttonFrame.pack(padx=10, pady=10)
self.testButton = Button(self.buttonFrame, text="Run Tests", font=font, bg="#1e1e1e", fg="#d4d4d4", command=lambda: self.runTests())
self.saveButton = Button(self.buttonFrame, text="Save", font=font, bg="#1e1e1e", fg="#d4d4d4", command=lambda: self.saveFile())
self.testButton.grid(row=0, column=0)
self.saveButton.grid(row=0, column=1)
# Output
self.outputTextBox = Text(self.window, wrap="word", font=font, bg="#1e1e1e", fg="#d4d4d4", state=DISABLED)
self.outputTextBox.pack(padx=10,pady=10, fill="both", expand=True)
return self.window
def generateMain(self, className, funcName, returnType, tests):
text = "int main(void) {\n"
text += f" {className} solution;\n"
text += f" {returnType} res;\n\n"
addQuotes = returnType != "string"
for input, output in tests:
text += f" res = solution.{funcName}({input});\n"
text += f" if (res != {output}) "
text += "{\n cout << \"Input: \" << " + ('\"' if addQuotes else "") + f"{input}" + ('\"' if addQuotes else "") + "<< endl;\n"
text += " cout << \"Your Output: \" << res << endl;\n"
text += "\n cout << \"Expected Output: \" << " + ('\"' if addQuotes else "") + f"{output}" + ('\"' if addQuotes else "") + "<< endl;\n"
text += " return -1;\n"
text += " }\n\n"
text += " return 0;\n}"
return text
def setFile(self, className, returnType, funcName, params, tests):
text = ""
self.main = self.generateMain(className, funcName, returnType, tests)
text += "#include \"ALL_H_FILES.h\"\n"
text += "using namespace std;\n\n"
text += f"\nclass {className} "
text += "{\npublic:\n"
text += f" {returnType} {funcName}({params})"
text += " {\n\n }\n};"
try:
with open(self.filePath, 'w') as file:
file.write(text)
file.write("\n\n" + self.main)
except:
print(f"Failed to open file {self.filePath}")
def readFile(self):
with open(self.filePath, "r") as file:
content = file.read()
mainIndex = content.find("int main")
if mainIndex != -1:
self.main = content[mainIndex:]
content = content[:mainIndex]
self.textBox.delete("1.0", END)
self.textBox.insert("1.0", content)
def saveFile(self):
with open(self.filePath, "w") as file:
file.write(self.textBox.get("1.0", END))
file.write("\n\n" + self.main)
def compileCode(self):
self.saveFile()
compileResults = subprocess.run(["g++", "-Wall", "-Werror", self.filePath, "-o", f"{self.filePath[:-4]}.o"],
capture_output=True,
text=True)
if (compileResults.returncode != 0):
return compileResults.stderr
return None
def updateOutputText(self, text):
self.outputTextBox.config(state=NORMAL)
self.outputTextBox.delete("1.0", END)
self.outputTextBox.insert("1.0", text)
self.outputTextBox.config(state=DISABLED)
def runTests(self):
self.numRunTests += 1
compRes = self.compileCode()
if compRes:
self.numFailCompile += 1
self.numFailTests += 1
self.updateOutputText(compRes)
return
testResults = subprocess.run(f"./{self.filePath[:-4]}.o",
capture_output=True,
text=True)
if testResults.stdout:
self.numFailTests += 1
self.updateOutputText("Test Failed:\n" + testResults.stdout)
return
self.updateOutputText("All Tests Passed!\n" + self.passedMessage)
self.completed = True
def completedTests(self):
return self.completed
def getStats(self):
return (self.numRunTests, self.numFailTests, self.numFailCompile)
def onKeyRelease(self, event=None):
self.updateLineNumbers()
self.highlightSyntax()
def updateLineNumbers(self, event=None):
self.lineNumbers.config(state="normal")
self.lineNumbers.delete("1.0", "end")
first_visible_line = int(self.textBox.index("@0,0").split(".")[0])
last_visible_line = int(self.textBox.index("@0,%d" % self.textBox.winfo_height()).split(".")[0])
line_number_text = "\n".join(str(i) for i in range(first_visible_line, last_visible_line + 1))
self.lineNumbers.insert("1.0", line_number_text)
self.lineNumbers.config(state="disabled")
def highlightSyntax(self):
self.textBox.tag_remove("keyword", "1.0", END)
self.textBox.tag_remove("string", "1.0", END)
self.textBox.tag_remove("comment", "1.0", END)
self.textBox.tag_remove("preprocessor", "1.0", END)
self.textBox.tag_remove("number", "1.0", END)
self.textBox.tag_remove("function", "1.0", END)
content = self.textBox.get("1.0", END)
# blue Keywords
keywords = [
"int", "return", "else", "if", "void", "for", "while", "do",
"class", "public", "private", "protected", "using", "namespace",
"static", "const", "constexpr", "virtual", "override", "true", "false",
"bool", "char", "double", "float", "long", "short", "signed", "unsigned",
"switch", "case", "break", "continue", "new", "delete", "try", "catch", "throw", "string",
"vector", "unordered_map", "deque", "queue", "set", "unordered_set", "map", self.filePath[:-4], "std"
]
for kw in keywords:
for match in re.finditer(rf'\b{kw}\b', content):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("keyword", start, end)
self.textBox.tag_config("keyword", foreground="#569CD6")
# Purple Strings
for match in re.finditer(r'"[^"\n]*"|\'[^\']*\'', content):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("string", start, end)
self.textBox.tag_config("string", foreground="#C586C0")
# Green Comments
for match in re.finditer(r'//.*?$|/\*.*?\*/', content, re.MULTILINE | re.DOTALL):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("comment", start, end)
self.textBox.tag_config("comment", foreground="#6A9955")
# Orange Preprocessor
for match in re.finditer(r'#\s*\w+', content):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("preprocessor", start, end)
self.textBox.tag_config("preprocessor", foreground="#CE9178")
# Red Numbers
for match in re.finditer(r'\b\d+(\.\d+)?\b', content):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("number", start, end)
self.textBox.tag_config("number", foreground="#D16969")
# Yellow Function names (basic guess: word followed by `(`)
for match in re.finditer(r'\b\w+(?=\s*\()', content):
start = f"1.0 + {match.start()} chars"
end = f"1.0 + {match.end()} chars"
self.textBox.tag_add("function", start, end)
self.textBox.tag_config("function", foreground="#DCDCAA")
def onScroll(self, *args):
self.textBox.yview(*args)
self.lineNumbers.yview(*args)
def onTextScroll(self, *args):
self.lineNumbers.yview_moveto(args[0])
self.scroll.set(*args)
self.updateLineNumbers()