-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoustomScript.py
More file actions
1334 lines (1188 loc) · 61.2 KB
/
Copy pathCoustomScript.py
File metadata and controls
1334 lines (1188 loc) · 61.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import tkinter as tk
from tkinter import scrolledtext, filedialog, messagebox
import re
import random
class ModernLanguageIDE:
def __init__(self, root):
self.root = root
self.root.title("CustomScript Developer Studio")
self.root.geometry("1250x780")
self.root.minsize(950, 620)
self.bg = "#0B0E13"
self.chrome = "#10141B"
self.sidebar_bg = "#0D1117"
self.editor_bg = "#0B0F14"
self.terminal_bg = "#080B10"
self.line_bg = "#0D1219"
self.border = "#202733"
self.text = "#DCE3EA"
self.muted = "#687586"
self.accent = "#22D3EE"
self.success = "#4ADE80"
self.error = "#F87171"
self.current_file = None
self.panel_visible = True
self.sidebar_visible = True
self.active_panel = "output"
self.active_tab = None
self.documents = {}
self.tab_buttons = {}
self.problems = []
self.search_visible = False
self._loading_document = False
self._folds = {}
self._diagnostic_count = 0
self._diagnostic_lines = {}
self._autocomplete_popup = None
self._autocomplete_list = None
self.root.configure(bg=self.bg)
self._build_menu()
self._build_ui()
self._load_starter()
self._ensure_documents()
self.reset_memory()
self._update_line_numbers()
self._schedule_highlight(delay=1)
self._set_status("Ready")
self._run_live_diagnostics()
# ========================= UI =========================
def _build_menu(self):
menubar = tk.Menu(self.root, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent,
tearoff=False, bd=0)
file_menu = tk.Menu(menubar, tearoff=False, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent)
file_menu.add_command(label="New", accelerator="Ctrl+N", command=self.new_file)
file_menu.add_command(label="Open...", accelerator="Ctrl+O", command=self.open_file)
file_menu.add_command(label="Save", accelerator="Ctrl+S", command=self.save_file)
file_menu.add_command(label="Save As...", command=self.save_as)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.root.destroy)
menubar.add_cascade(label="File", menu=file_menu)
edit_menu = tk.Menu(menubar, tearoff=False, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent)
edit_menu.add_command(label="Undo", accelerator="Ctrl+Z", command=lambda: self.editor.event_generate("<<Undo>>"))
edit_menu.add_command(label="Redo", accelerator="Ctrl+Y", command=lambda: self.editor.event_generate("<<Redo>>"))
edit_menu.add_separator()
edit_menu.add_command(label="Select All", accelerator="Ctrl+A", command=self.select_all)
menubar.add_cascade(label="Edit", menu=edit_menu)
view_menu = tk.Menu(menubar, tearoff=False, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent)
view_menu.add_command(label="Toggle Explorer", command=self.toggle_sidebar)
view_menu.add_command(label="Toggle Bottom Panel", command=self.toggle_panel)
view_menu.add_command(label="Show Output", command=lambda: self.show_panel("output"))
view_menu.add_command(label="Show Terminal", command=lambda: self.show_panel("terminal"))
menubar.add_cascade(label="View", menu=view_menu)
run_menu = tk.Menu(menubar, tearoff=False, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent)
run_menu.add_command(label="Run CustomScript", accelerator="Ctrl+R", command=self.execute_code)
run_menu.add_command(label="Clear Output", accelerator="Ctrl+L", command=self.clear_output)
menubar.add_cascade(label="Run", menu=run_menu)
help_menu = tk.Menu(menubar, tearoff=False, bg=self.chrome, fg=self.text,
activebackground="#1B2732", activeforeground=self.accent)
help_menu.add_command(label="About CustomScript", command=self.show_about)
menubar.add_cascade(label="Help", menu=help_menu)
self.root.config(menu=menubar)
self.root.bind_all("<Control-r>", lambda e: self.execute_code() or "break")
self.root.bind_all("<Control-l>", lambda e: self.clear_output() or "break")
self.root.bind_all("<Control-s>", lambda e: self.save_file() or "break")
self.root.bind_all("<Control-o>", lambda e: self.open_file() or "break")
self.root.bind_all("<Control-n>", lambda e: self.new_file() or "break")
self.root.bind_all("<Control-f>", lambda e: self.focus_search() or "break")
self.root.bind_all("<Control-Shift-f>", lambda e: self.focus_search() or "break")
self.root.bind_all("<Control-Shift-Up>", lambda e: self.toggle_fold_at_cursor() or "break")
self.root.bind_all("<Control-Shift-Down>", lambda e: self.unfold_all() or "break")
def _build_ui(self):
top = tk.Frame(self.root, bg=self.chrome, height=38)
top.pack(fill=tk.X, side=tk.TOP)
top.pack_propagate(False)
tk.Label(top, text="CS", bg=self.chrome, fg=self.accent,
font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(14, 8))
tk.Label(top, text="CUSTOMSCRIPT", bg=self.chrome, fg="#E7ECF2",
font=("Segoe UI", 9, "bold")).pack(side=tk.LEFT)
tk.Label(top, text=" Developer Studio", bg=self.chrome, fg=self.muted,
font=("Segoe UI", 9)).pack(side=tk.LEFT)
self.run_btn = tk.Button(top, text="▶ RUN", bg="#18232B", fg=self.accent,
activebackground="#20323B", activeforeground="#67E8F9",
font=("Segoe UI", 8, "bold"), relief="flat", bd=0,
padx=12, cursor="hand2", command=self.execute_code)
self.run_btn.pack(side=tk.RIGHT, padx=(4, 10), pady=6)
self.status_dot = tk.Label(top, text="●", bg=self.chrome, fg=self.success,
font=("Segoe UI", 8))
self.status_dot.pack(side=tk.RIGHT, padx=(8, 2))
main = tk.Frame(self.root, bg=self.bg)
main.pack(fill=tk.BOTH, expand=True)
self.rail = tk.Frame(main, bg="#0A0D12", width=44)
self.rail.pack(side=tk.LEFT, fill=tk.Y)
self.rail.pack_propagate(False)
self.explorer_btn = self._rail_button(self.rail, "▣", True, self.toggle_sidebar)
self.search_btn = self._rail_button(self.rail, "⌕", False, self.focus_search)
self.sidebar = tk.Frame(main, bg=self.sidebar_bg, width=190)
self.sidebar.pack(side=tk.LEFT, fill=tk.Y)
self.sidebar.pack_propagate(False)
tk.Label(self.sidebar, text="EXPLORER", bg=self.sidebar_bg, fg="#718096",
font=("Segoe UI", 8, "bold"), anchor="w").pack(fill=tk.X, padx=15, pady=(16, 12))
tk.Label(self.sidebar, text="⌄ CUSTOMSCRIPT PROJECT", bg=self.sidebar_bg,
fg="#AEB8C5", font=("Segoe UI", 8, "bold"), anchor="w").pack(fill=tk.X, padx=12, pady=3)
self.file_row = tk.Button(self.sidebar, text="● main.cs", bg="#151B24", fg="#D5DCE5",
activebackground="#1B2732", activeforeground="#FFFFFF",
font=("Consolas", 9), anchor="w", relief="flat", bd=0,
cursor="hand2", command=lambda: self.editor.focus_set())
self.file_row.pack(fill=tk.X, padx=8, pady=2)
tk.Frame(self.sidebar, bg=self.sidebar_bg).pack(expand=True, fill=tk.BOTH)
tk.Label(self.sidebar, text="CUSTOMSCRIPT", bg=self.sidebar_bg, fg="#4D5968",
font=("Consolas", 7, "bold"), anchor="w").pack(fill=tk.X, padx=14)
self.side_status = tk.Label(self.sidebar, text="Runtime • Ready", bg=self.sidebar_bg,
fg="#657283", font=("Consolas", 8), anchor="w")
self.side_status.pack(fill=tk.X, padx=14, pady=(2, 12))
self.workspace = tk.Frame(main, bg=self.editor_bg)
self.workspace.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
tabbar = tk.Frame(self.workspace, bg="#0C1016", height=34)
tabbar.pack(fill=tk.X)
tabbar.pack_propagate(False)
self.tabbar = tabbar
self.tabs_frame = tk.Frame(tabbar, bg="#0C1016")
self.tabs_frame.pack(side=tk.LEFT, fill=tk.Y)
self.new_tab_btn = tk.Button(tabbar, text="+", bg="#0C1016", fg="#657283",
activebackground="#151B24", activeforeground=self.accent,
relief="flat", bd=0, font=("Consolas", 11),
cursor="hand2", command=self.new_file)
self.new_tab_btn.pack(side=tk.LEFT, padx=4)
self.tab_name = None
crumb = tk.Frame(self.workspace, bg=self.editor_bg, height=25)
crumb.pack(fill=tk.X)
crumb.pack_propagate(False)
self.crumb_label = tk.Label(crumb, text="main.cs › CustomScript › source",
bg=self.editor_bg, fg="#586575", font=("Consolas", 8), anchor="w")
self.crumb_label.pack(fill=tk.X, padx=12, pady=5)
self.editor_area = tk.Frame(self.workspace, bg=self.editor_bg)
self.editor_area.pack(fill=tk.BOTH, expand=True)
editor_area = self.editor_area
self.line_numbers = tk.Text(editor_area, width=5, bg=self.line_bg, fg="#465363",
font=("Consolas", 11), relief="flat", bd=0, padx=8, pady=9,
state=tk.DISABLED, takefocus=0, cursor="arrow", wrap=tk.NONE)
self.line_numbers.pack(side=tk.LEFT, fill=tk.Y)
self.editor = scrolledtext.ScrolledText(
editor_area, bg=self.editor_bg, fg=self.text, insertbackground="#F8FAFC",
font=("Consolas", 11), relief="flat", bd=0, padx=12, pady=9,
selectbackground="#183247", selectforeground="#FFFFFF", undo=True, wrap=tk.NONE)
self.editor.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.editor.bind("<KeyRelease>", self._editor_changed)
self.editor.bind("<ButtonRelease-1>", self._editor_changed)
self.editor.bind("<MouseWheel>", self._sync_line_scroll)
self.editor.bind("<Tab>", self._autocomplete_accept)
self.editor.bind("<Escape>", self._hide_autocomplete)
self.editor.bind("<Up>", self._autocomplete_key)
self.editor.bind("<Down>", self._autocomplete_key)
self.editor.bind("<Return>", self._autocomplete_key)
self._configure_syntax_highlighting()
self.panel = tk.Frame(self.workspace, bg=self.terminal_bg, height=190)
self.panel.pack(fill=tk.X)
self.panel.pack_propagate(False)
panel_head = tk.Frame(self.panel, bg="#0C1016", height=31)
panel_head.pack(fill=tk.X)
panel_head.pack_propagate(False)
self.output_tab_btn = self._panel_button(panel_head, "OUTPUT", "output")
self.terminal_tab_btn = self._panel_button(panel_head, "TERMINAL", "terminal")
self.problems_tab_btn = self._panel_button(panel_head, "PROBLEMS", "problems")
self.clear_btn = tk.Button(panel_head, text="Clear", bg="#0C1016", fg="#657283",
activebackground="#151B24", activeforeground="#FFFFFF",
relief="flat", bd=0, font=("Consolas", 8), cursor="hand2",
command=self.clear_output)
self.clear_btn.pack(side=tk.RIGHT, padx=8)
body = tk.Frame(self.panel, bg=self.terminal_bg)
body.pack(fill=tk.BOTH, expand=True)
self.output_console = scrolledtext.ScrolledText(
body, bg=self.terminal_bg, fg="#C7D0DB", insertbackground="white",
font=("Consolas", 9), relief="flat", bd=0, padx=12, pady=8,
state=tk.DISABLED, wrap=tk.NONE)
self.terminal_console = scrolledtext.ScrolledText(
body, bg=self.terminal_bg, fg="#8D99A8", insertbackground="white",
font=("Consolas", 9), relief="flat", bd=0, padx=12, pady=8,
state=tk.DISABLED, wrap=tk.NONE)
self.problems_console = scrolledtext.ScrolledText(
body, bg=self.terminal_bg, fg="#D8E0E8", insertbackground="white",
font=("Consolas", 9), relief="flat", bd=0, padx=12, pady=8,
state=tk.DISABLED, wrap=tk.NONE)
self.output_console.pack(fill=tk.BOTH, expand=True)
status = tk.Frame(self.root, bg="#0E131A", height=22)
status.pack(fill=tk.X, side=tk.BOTTOM)
status.pack_propagate(False)
tk.Label(status, text=" CustomScript", bg="#0E131A", fg=self.accent,
font=("Consolas", 8, "bold")).pack(side=tk.LEFT)
self.status_text = tk.Label(status, text="Ready", bg="#0E131A", fg="#667486",
font=("Consolas", 8))
self.status_text.pack(side=tk.LEFT, padx=12)
self.cursor_text = tk.Label(status, text="Ln 1, Col 1", bg="#0E131A", fg="#7B8797",
font=("Consolas", 8))
self.cursor_text.pack(side=tk.RIGHT, padx=12)
def _rail_button(self, parent, symbol, active, command):
b = tk.Button(parent, text=symbol, bg="#0A0D12", fg=self.accent if active else "#46515F",
activebackground="#151B24", activeforeground=self.accent,
font=("Segoe UI Symbol", 15), relief="flat", bd=0,
cursor="hand2", command=command)
b.pack(fill=tk.X, pady=(10, 4))
return b
def _panel_button(self, parent, text, name):
b = tk.Button(parent, text=text, bg="#111720" if name == self.active_panel else "#0C1016",
fg=self.accent if name == self.active_panel else "#667486",
activebackground="#151B24", activeforeground="#FFFFFF",
relief="flat", bd=0, font=("Consolas", 8, "bold"),
padx=10, cursor="hand2", command=lambda: self.show_panel(name))
b.pack(side=tk.LEFT, fill=tk.Y)
return b
def show_panel(self, name):
self.active_panel = name
for widget in (self.output_console, self.terminal_console, self.problems_console):
widget.pack_forget()
target = {"output": self.output_console, "terminal": self.terminal_console, "problems": self.problems_console}.get(name, self.output_console)
target.pack(fill=tk.BOTH, expand=True)
for btn, key in ((self.output_tab_btn, "output"), (self.terminal_tab_btn, "terminal"), (self.problems_tab_btn, "problems")):
btn.config(bg="#111720" if name == key else "#0C1016", fg=self.accent if name == key else "#667486")
if name == "problems":
self._render_problems()
def toggle_panel(self):
if self.panel_visible:
self.panel.pack_forget()
self.panel_visible = False
else:
self.panel.pack(fill=tk.X)
self.panel_visible = True
self.show_panel(self.active_panel)
def toggle_sidebar(self):
if self.sidebar_visible:
self.sidebar.pack_forget()
self.sidebar_visible = False
else:
self.sidebar.pack(side=tk.LEFT, fill=tk.Y, before=self.workspace)
self.sidebar_visible = True
def _load_starter(self):
self.editor.delete("1.0", tk.END)
self.editor.insert(tk.END, self.get_starter_code())
def _editor_changed(self, event=None):
self._update_line_numbers()
try:
line, col = self.editor.index(tk.INSERT).split(".")
self.cursor_text.config(text=f"Ln {line}, Col {int(col) + 1}")
except Exception:
pass
if self._loading_document:
return
self._set_status("Modified")
if self.active_tab and self.active_tab in self.documents:
self.documents[self.active_tab]["dirty"] = True
self.documents[self.active_tab]["code"] = self.editor.get("1.0", "end-1c")
self._rebuild_tabs()
self._schedule_highlight()
self._schedule_live_diagnostics()
self._schedule_autocomplete()
def _sync_line_scroll(self, event=None):
self.root.after_idle(self._update_line_numbers)
self.root.after_idle(self._highlight_visible)
# ========================= Syntax Highlighting =========================
def _configure_syntax_highlighting(self):
# CustomScript syntax colors. These affect the editor only; the
# interpreter/runtime is completely separate.
styles = {
"cs_keyword": "#60A5FA", # let, maybe, suppose, otherwise...
"cs_command": "#22D3EE", # reveal, rewind, fast_ram
"cs_subcommand": "#A78BFA", # byte, write, read
"cs_variable": "#E5E7EB", # declared variable names
"cs_number": "#B5CEA8", # numbers / numeric literals
"cs_string": "#CE9178", # reveal text
"cs_comment": "#5E6A78", # comments
"cs_operator": "#F59E0B", # + - * / comparisons / =
"cs_unit": "#4EC9B0", # meters, seconds, etc.
"cs_scope": "#C084FC", # |>
"cs_identifier": "#DCE3EA", # normal identifiers
"cs_delimiter": "#6B7280", # slash/brackets/parentheses
}
for tag, color in styles.items():
self.editor.tag_configure(tag, foreground=color)
# NOVA diagnostics: CustomScript-native error visuals.
self.editor.tag_configure("nova_error_line", background="#24151A")
self.editor.tag_configure("nova_error_token", foreground="#FF8A9A", underline=True)
self.line_numbers.tag_configure("nova_error_number", foreground="#F87171", background="#24151A")
self._highlight_after_id = None
self._highlighting = False
self._schedule_highlight(delay=1)
def _schedule_highlight(self, delay=35):
if not hasattr(self, "editor"):
return
if self._highlight_after_id is not None:
try:
self.root.after_cancel(self._highlight_after_id)
except Exception:
pass
self._highlight_after_id = self.root.after(delay, self._highlight_visible)
def _highlight_visible(self):
self._highlight_after_id = None
if self._highlighting:
return
self._highlighting = True
try:
self._highlight_syntax()
finally:
self._highlighting = False
def _highlight_syntax(self):
"""Lex CustomScript for editor coloring without changing program text."""
text = self.editor.get("1.0", "end-1c")
tags = ("cs_keyword", "cs_command", "cs_subcommand", "cs_variable",
"cs_number", "cs_string", "cs_comment", "cs_operator",
"cs_unit", "cs_scope", "cs_identifier", "cs_delimiter")
for tag in tags:
self.editor.tag_remove(tag, "1.0", tk.END)
keyword_set = {"let", "maybe", "suppose", "otherwise", "attempt", "rescue", "ask"}
command_set = {"reveal", "rewind", "fast_ram"}
subcommand_set = {"byte", "write", "read"}
# Process line-by-line so comment and reveal-string boundaries stay
# simple and predictable.
for line_no, line in enumerate(text.split("\n"), 1):
base = f"{line_no}.0"
i = 0
n = len(line)
expect_declared_name = False
while i < n:
# Comment: // outside a reveal//variable// construct.
if line.startswith("//", i):
self.editor.tag_add("cs_comment", f"{base}+{i}c", f"{base}+{n}c")
break
# Scope operator.
if line.startswith("|>", i):
self.editor.tag_add("cs_scope", f"{base}+{i}c", f"{base}+{i+2}c")
i += 2
continue
# reveal/text/ — highlight command + everything between slashes
# as a string. This is checked before generic slash handling.
if line.startswith("reveal/", i) and (i == 0 or line[i-1].isspace()):
self.editor.tag_add("cs_command", f"{base}+{i}c", f"{base}+{i+6}c")
start = i + 7
end = line.find("/", start)
if end >= 0:
self.editor.tag_add("cs_delimiter", f"{base}+{i+6}c", f"{base}+{i+7}c")
if end > start:
self.editor.tag_add("cs_string", f"{base}+{start}c", f"{base}+{end}c")
self.editor.tag_add("cs_delimiter", f"{base}+{end}c", f"{base}+{end+1}c")
i = end + 1
continue
# reveal//name// — command, delimiters, then identifier.
if line.startswith("reveal//", i) and (i == 0 or line[i-1].isspace()):
self.editor.tag_add("cs_command", f"{base}+{i}c", f"{base}+{i+6}c")
self.editor.tag_add("cs_delimiter", f"{base}+{i+6}c", f"{base}+{i+8}c")
start = i + 8
end = line.find("//", start)
if end >= 0:
if end > start:
self.editor.tag_add("cs_identifier", f"{base}+{start}c", f"{base}+{end}c")
self.editor.tag_add("cs_delimiter", f"{base}+{end}c", f"{base}+{end+2}c")
i = end + 2
continue
ch = line[i]
# Identifiers / keywords / units.
if ch.isalpha() or ch == "_":
j = i + 1
while j < n and (line[j].isalnum() or line[j] == "_"):
j += 1
word = line[i:j]
if word in keyword_set:
self.editor.tag_add("cs_keyword", f"{base}+{i}c", f"{base}+{j}c")
expect_declared_name = word in {"let", "maybe"}
elif word in command_set:
self.editor.tag_add("cs_command", f"{base}+{i}c", f"{base}+{j}c")
elif word in subcommand_set:
self.editor.tag_add("cs_subcommand", f"{base}+{i}c", f"{base}+{j}c")
elif expect_declared_name:
self.editor.tag_add("cs_variable", f"{base}+{i}c", f"{base}+{j}c")
expect_declared_name = False
elif i > 0 and line[i-1] not in "." and re.match(r"\s*$", line[:i]):
self.editor.tag_add("cs_identifier", f"{base}+{i}c", f"{base}+{j}c")
else:
self.editor.tag_add("cs_identifier", f"{base}+{i}c", f"{base}+{j}c")
i = j
continue
# Numeric literal, including a unit immediately after it.
if ch.isdigit() or (ch == "-" and i + 1 < n and line[i+1].isdigit()):
j = i + 1
while j < n and (line[j].isdigit() or line[j] == "."):
j += 1
self.editor.tag_add("cs_number", f"{base}+{i}c", f"{base}+{j}c")
u = j
while u < n and line[u].isalpha():
u += 1
if u > j:
self.editor.tag_add("cs_unit", f"{base}+{j}c", f"{base}+{u}c")
i = u
continue
# Operators. Long operators first.
if line[i:i+2] in {"==", "!=", "<=", ">="}:
self.editor.tag_add("cs_operator", f"{base}+{i}c", f"{base}+{i+2}c")
i += 2
continue
if ch in "+-*/=<>~":
self.editor.tag_add("cs_operator", f"{base}+{i}c", f"{base}+{i+1}c")
i += 1
continue
if ch in "[]()":
self.editor.tag_add("cs_delimiter", f"{base}+{i}c", f"{base}+{i+1}c")
i += 1
continue
i += 1
# ========================= end Syntax Highlighting =========================
def _update_line_numbers(self):
try:
count = int(self.editor.index("end-1c").split(".")[0])
numbers = "\n".join(str(i) for i in range(1, count + 1))
self.line_numbers.config(state=tk.NORMAL)
self.line_numbers.delete("1.0", tk.END)
self.line_numbers.insert("1.0", numbers)
self.line_numbers.config(state=tk.DISABLED)
except Exception:
pass
def _set_status(self, text):
self.status_text.config(text=text)
self.side_status.config(text=f"Runtime • {text}")
def show_message(self, text):
messagebox.showinfo("CustomScript Developer Studio", text)
def focus_search(self):
if not self.search_visible:
self.search_visible = True
if not hasattr(self, "search_bar"):
self._build_search_bar()
self.search_bar.pack(fill=tk.X, before=self.editor_area)
self.search_entry.focus_set()
self.search_entry.select_range(0, tk.END)
def _build_search_bar(self):
self.search_bar = tk.Frame(self.workspace, bg="#111720", height=34)
self.search_bar.pack_propagate(False)
tk.Label(self.search_bar, text="SEARCH", bg="#111720", fg=self.accent, font=("Consolas", 8, "bold")).pack(side=tk.LEFT, padx=(10, 6))
self.search_entry = tk.Entry(self.search_bar, bg="#0B0F14", fg=self.text, insertbackground="white", relief="flat", font=("Consolas", 9))
self.search_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=4, pady=6)
self.search_entry.bind("<Return>", lambda e: self.find_next())
self.search_entry.bind("<Shift-Return>", lambda e: self.find_previous())
for label, cmd in (("↑", self.find_previous), ("↓", self.find_next), ("×", self.close_search)):
tk.Button(self.search_bar, text=label, bg="#111720", fg="#8290A1", activebackground="#18232B", activeforeground=self.accent, relief="flat", bd=0, command=cmd, cursor="hand2", font=("Segoe UI", 9)).pack(side=tk.LEFT, padx=2)
def close_search(self):
if hasattr(self, "search_bar"):
self.search_bar.pack_forget()
self.search_visible = False
self.editor.tag_remove("search_hit", "1.0", tk.END)
self.editor.focus_set()
def find_next(self):
query = self.search_entry.get() if hasattr(self, "search_entry") else ""
if not query:
return
start = self.editor.index(tk.INSERT)
pos = self.editor.search(query, start, stopindex=tk.END, nocase=False)
if not pos:
pos = self.editor.search(query, "1.0", stopindex=tk.END, nocase=False)
self._select_search_hit(pos, query)
def find_previous(self):
query = self.search_entry.get() if hasattr(self, "search_entry") else ""
if not query:
return
start = self.editor.index(tk.INSERT)
pos = self.editor.search(query, start, stopindex="1.0", backwards=True, nocase=False)
if not pos:
pos = self.editor.search(query, tk.END, stopindex="1.0", backwards=True, nocase=False)
self._select_search_hit(pos, query)
def _select_search_hit(self, pos, query):
self.editor.tag_remove("search_hit", "1.0", tk.END)
if pos:
end = f"{pos}+{len(query)}c"
self.editor.tag_configure("search_hit", background="#3A3215", foreground="#FDE68A")
self.editor.tag_add("search_hit", pos, end)
self.editor.mark_set(tk.INSERT, end)
self.editor.see(pos)
# ========================= File/UI actions =========================
def _make_doc(self, name, code=""):
return {"name": name, "path": None, "code": code, "dirty": False}
def _ensure_documents(self):
if not self.documents:
doc = self._make_doc("main.cs", self.editor.get("1.0", "end-1c"))
self.documents["main.cs"] = doc
self.active_tab = "main.cs"
self._rebuild_tabs()
def _save_current_doc_state(self):
if self.active_tab and self.active_tab in self.documents:
self.documents[self.active_tab]["code"] = self.editor.get("1.0", "end-1c")
def _switch_tab(self, name):
self._save_current_doc_state()
self.active_tab = name
doc = self.documents[name]
self._loading_document = True
self.editor.delete("1.0", tk.END)
self.editor.insert("1.0", doc["code"])
self._loading_document = False
self.current_file = doc["path"]
self._update_file_labels(doc["name"])
self._editor_changed()
self._rebuild_tabs()
def _rebuild_tabs(self):
if not hasattr(self, "tabs_frame"):
return
for child in self.tabs_frame.winfo_children():
child.destroy()
self.tab_buttons.clear()
for name, doc in self.documents.items():
active = name == self.active_tab
label = ("● " if doc["dirty"] else "") + name
b = tk.Button(self.tabs_frame, text=label, bg=self.editor_bg if active else "#0C1016", fg="#D8E0E8" if active else "#687586", activebackground="#151B24", activeforeground="#FFFFFF", relief="flat", bd=0, font=("Consolas", 9), cursor="hand2", padx=10, command=lambda n=name: self._switch_tab(n))
b.pack(side=tk.LEFT, fill=tk.Y)
self.tab_buttons[name] = b
def new_file(self):
self._ensure_documents()
base = "untitled.cs"
i = 1
name = base
while name in self.documents:
name = f"untitled{i}.cs"; i += 1
self._save_current_doc_state()
self.documents[name] = self._make_doc(name, "// New CustomScript file\nreveal/Hello, CustomScript!/\n")
self._switch_tab(name)
self._set_status("New file")
def open_file(self):
path = filedialog.askopenfilename(filetypes=[("CustomScript", "*.cs"), ("Text", "*.txt"), ("All files", "*.*")])
if not path:
return
try:
with open(path, "r", encoding="utf-8") as f:
code = f.read()
name = path.split("/")[-1].split("\\")[-1]
self._ensure_documents()
self._save_current_doc_state()
self.documents[name] = {"name": name, "path": path, "code": code, "dirty": False}
self._switch_tab(name)
self.terminal_log(f"Opened: {path}")
except Exception as e:
messagebox.showerror("Open failed", str(e))
def save_file(self):
self._ensure_documents()
self._save_current_doc_state()
doc = self.documents[self.active_tab]
if not doc["path"]:
return self.save_as()
try:
with open(doc["path"], "w", encoding="utf-8") as f:
f.write(doc["code"])
doc["dirty"] = False
self.current_file = doc["path"]
self._rebuild_tabs()
self._set_status("Saved")
self.terminal_log(f"Saved: {doc['path']}")
except Exception as e:
messagebox.showerror("Save failed", str(e))
def save_as(self):
self._ensure_documents()
path = filedialog.asksaveasfilename(defaultextension=".cs", filetypes=[("CustomScript", "*.cs"), ("All files", "*.*")])
if not path:
return
name = path.split("/")[-1].split("\\")[-1]
self._save_current_doc_state()
old_key = self.active_tab
doc = self.documents.pop(old_key)
doc.update({"name": name, "path": path, "dirty": False})
self.documents[name] = doc
self.active_tab = name
self.current_file = path
self._rebuild_tabs()
self._update_file_labels(name)
self.save_file()
def _update_file_labels(self, name):
self.crumb_label.config(text=f"{name} › CustomScript › source")
self.file_row.config(text=f"● {name}")
def select_all(self):
self.editor.tag_add(tk.SEL, "1.0", tk.END)
self.editor.mark_set(tk.INSERT, "1.0")
self.editor.see(tk.INSERT)
def show_about(self):
messagebox.showinfo("About CustomScript", "CustomScript Developer Studio NOVA v2\n\nA CustomScript-native development environment.")
# ========================= NOVA v2 Smart Editor =========================
def _schedule_live_diagnostics(self):
if hasattr(self, "_live_diag_after") and self._live_diag_after is not None:
try: self.root.after_cancel(self._live_diag_after)
except Exception: pass
self._live_diag_after = self.root.after(180, self._run_live_diagnostics)
def _run_live_diagnostics(self):
self._live_diag_after = None
text = self.editor.get("1.0", "end-1c")
declared = set(re.findall(r"\b(?:let|maybe)\s+([A-Za-z_]\w*)", text))
known = set(["reveal","let","maybe","rewind","suppose","otherwise","attempt","rescue","ask","fast_ram","byte","write","read"])
problems=[]
for ln, raw in enumerate(text.splitlines(),1):
line=raw.strip()
if not line or line.startswith("//") or line.startswith("|>"): continue
m=re.fullmatch(r"reveal//(.+)//", line)
if m and not (m.group(1) in declared or re.fullmatch(r"-?\d+(?:\.\d+)?",m.group(1))):
problems.append((ln,"MEM-001",f"'{m.group(1)}' is not defined in this source."))
continue
m=re.match(r"let\s+\w+\s*=\s*(.+)",line)
if m:
for token in re.findall(r"\b[A-Za-z_]\w*\b",m.group(1)):
if token not in declared and token not in known and token not in {"meters","seconds"} and not token.isdigit():
problems.append((ln,"MEM-001",f"'{token}' is used before it is defined.")); break
m=re.match(r"suppose\s+(\w+)\s+(?:==|!=|<=|>=|<|>)\s+(\w+)",line)
if m:
for token in m.groups():
if not re.fullmatch(r"-?\d+(?:\.\d+)?",token) and token not in declared:
problems.append((ln,"MEM-001",f"'{token}' is not defined in this source.")); break
if line.split()[0] not in known and not line.startswith("reveal/"):
problems.append((ln,"SYN-001",f"Unknown CustomScript instruction: {line.split()[0]}"))
self.problems = problems
self._apply_live_problem_marks()
self._render_problems()
def _apply_live_problem_marks(self):
self.editor.tag_remove("nova_live_line","1.0",tk.END)
self.line_numbers.config(state=tk.NORMAL)
self.line_numbers.tag_remove("nova_live_number","1.0",tk.END)
self.editor.tag_configure("nova_live_line",background="#1D171B")
self.line_numbers.tag_configure("nova_live_number",foreground="#F59E0B",background="#1D171B")
for ln,_,_ in self.problems:
self.editor.tag_add("nova_live_line",f"{ln}.0",f"{ln}.end")
self.line_numbers.tag_add("nova_live_number",f"{ln}.0",f"{ln}.end")
self.line_numbers.config(state=tk.DISABLED)
def _render_problems(self):
if not hasattr(self,"problems_console"): return
self.problems_console.config(state=tk.NORMAL)
self.problems_console.delete("1.0",tk.END)
if not self.problems:
self.problems_console.insert(tk.END," ✓ No problems detected in the current source.\n","nova_ok")
self.problems_console.tag_configure("nova_ok",foreground=self.success)
else:
self.problems_console.insert(tk.END,f" NOVA found {len(self.problems)} problem(s)\n\n","nova_head")
self.problems_console.tag_configure("nova_head",foreground=self.accent)
for i,(ln,code,msg) in enumerate(self.problems,1):
tag=f"problem_{i}"
self.problems_console.tag_configure(tag,foreground="#F3A6B4")
self.problems_console.tag_bind(tag,"<Button-1>",lambda e,n=ln:self._jump_to_diagnostic(n))
self.problems_console.tag_bind(tag,"<Enter>",lambda e:self.problems_console.config(cursor="hand2"))
self.problems_console.tag_bind(tag,"<Leave>",lambda e:self.problems_console.config(cursor="xterm"))
self.problems_console.insert(tk.END,f" × Line {ln:<4} {code:<8} {msg}\n",tag)
self.problems_console.config(state=tk.DISABLED)
def _completion_context(self, prefix):
line = self.editor.get(f"{self.editor.index(tk.INSERT).split('.')[0]}.0", tk.INSERT).strip()
base=["reveal","let","maybe","rewind","suppose","otherwise","attempt","rescue","ask","fast_ram","byte","write","read"]
source=self.editor.get("1.0","end-1c")
vars_found=re.findall(r"\b(?:let|maybe)\s+([A-Za-z_]\w*)",source)
if line.startswith("fast_ram"):
base=["byte","write","read"]
elif line.startswith("reveal//"):
base=vars_found
pool=list(dict.fromkeys(base+vars_found))
return [x for x in pool if x.lower().startswith(prefix.lower())][:10]
def _autocomplete_candidates(self,prefix):
return self._completion_context(prefix)
def toggle_fold_at_cursor(self):
ln=int(self.editor.index(tk.INSERT).split('.')[0])
lines=self.editor.get("1.0","end-1c").splitlines()
if ln<1 or ln>len(lines): return
# CustomScript-native folding: fold a contiguous |> scope after this line.
if not (lines[ln-1].strip().startswith(("suppose ","otherwise","attempt","rescue"))):
return
end=ln
while end<len(lines) and lines[end].strip().startswith("|>"): end+=1
if end==ln: return
key=(ln,end)
hidden=self._folds.get(key,False)
for n in range(ln+1,end+1):
self.editor.tag_configure("nova_fold",elide=not hidden)
self.editor.tag_add("nova_fold",f"{n}.0",f"{n}.0 lineend +1c")
self._folds[key]=not hidden
def unfold_all(self):
self.editor.tag_remove("nova_fold","1.0",tk.END)
self._folds.clear()
# ========================= Output/Terminal =========================
def _write(self, widget, text, color):
widget.config(state=tk.NORMAL)
tag = f"c{color.replace('#', '')}"
widget.tag_config(tag, foreground=color)
widget.insert(tk.END, text + "\n", tag)
widget.see(tk.END)
widget.config(state=tk.DISABLED)
def output(self, text, color="#C7D0DB"):
self.show_panel("output")
self._write(self.output_console, text, color)
def terminal_log(self, text, color="#8D99A8"):
self._write(self.terminal_console, text, color)
def clear_output(self):
for widget in (self.output_console, self.terminal_console):
widget.config(state=tk.NORMAL)
widget.delete("1.0", tk.END)
widget.config(state=tk.DISABLED)
self.show_panel("output")
self._set_status("Output cleared")
# ========================= Runtime =========================
def reset_memory(self):
self.vars = {}
self.history = {}
self.maybe_vars = {}
self.hardware_memory = {}
self.skip_next_block = False
self.last_condition_met = False
self.in_attempt = False
self.error_caught = False
self.input_values = {}
def get_starter_code(self):
return """// CustomScript demo
reveal/Hello, World!/
let systemScore = 9500
reveal//systemScore//
let distance = 50meters
let time = 5seconds
let speed = distance / time
reveal/Speed calculated:/
reveal//speed//
maybe wind = 10 ~ 25
reveal/Random wind generated:/
reveal//wind//
let health = 100
let health = 50
let health = 10
rewind health by 2 steps
reveal/Health rewound to:/
reveal//health//
suppose health == 50
|> reveal/Player is alive!/
otherwise
|> reveal/Player took damage!/
fast_ram byte memory[4]
fast_ram write memory[2] = 255
fast_ram read memory[2]
"""
# ========================= NOVA Autocomplete =========================
def _schedule_autocomplete(self):
if hasattr(self, "_autocomplete_after_id") and self._autocomplete_after_id is not None:
try:
self.root.after_cancel(self._autocomplete_after_id)
except Exception:
pass
self._autocomplete_after_id = self.root.after(90, self._show_autocomplete)
def _current_word(self):
pos = self.editor.index(tk.INSERT)
line_start = f"{pos.split('.')[0]}.0"
before = self.editor.get(line_start, pos)
m = re.search(r"([A-Za-z_][A-Za-z0-9_]*)$", before)
return (m.group(1), f"{pos.split('.')[0]}.{int(pos.split('.')[1]) - len(m.group(1))}") if m else ("", pos)
def _autocomplete_candidates(self, prefix):
return self._completion_context(prefix)
def _show_autocomplete(self):
self._autocomplete_after_id = None
prefix, _ = self._current_word()
if len(prefix) < 2:
self._hide_autocomplete()
return
candidates = self._autocomplete_candidates(prefix)
if not candidates:
self._hide_autocomplete()
return
if self._autocomplete_popup is None or not self._autocomplete_popup.winfo_exists():
self._autocomplete_popup = tk.Toplevel(self.root)
self._autocomplete_popup.overrideredirect(True)
self._autocomplete_popup.configure(bg="#202733")
self._autocomplete_list = tk.Listbox(
self._autocomplete_popup, bg="#0F151C", fg="#DCE3EA",
selectbackground="#183247", selectforeground="#67E8F9",
highlightthickness=1, highlightbackground="#202733",
relief="flat", font=("Consolas", 10), height=min(8, len(candidates)),
activestyle="none", bd=0
)
self._autocomplete_list.pack(padx=1, pady=1)
self._autocomplete_list.bind("<ButtonRelease-1>", self._autocomplete_accept)
else:
self._autocomplete_list.delete(0, tk.END)
self._autocomplete_list.config(height=min(8, len(candidates)))
for item in candidates:
self._autocomplete_list.insert(tk.END, item)
self._autocomplete_list.selection_set(0)
bbox = self.editor.bbox(tk.INSERT)
if not bbox:
self._hide_autocomplete()
return
x, y, w, h = bbox
screen_x = self.editor.winfo_rootx() + x
screen_y = self.editor.winfo_rooty() + y + h + 2
self._autocomplete_popup.geometry(f"230x{min(8, len(candidates))*22 + 2}+{screen_x}+{screen_y}")
self._autocomplete_popup.lift()
def _autocomplete_accept(self, event=None):
if not self._autocomplete_list or not self._autocomplete_popup or not self._autocomplete_popup.winfo_exists():
return None if event else False
selection = self._autocomplete_list.curselection()
if not selection:
return "break"
word = self._autocomplete_list.get(selection[0])
prefix, start = self._current_word()
end = self.editor.index(tk.INSERT)
self.editor.delete(start, end)
self.editor.insert(start, word)
self.editor.mark_set(tk.INSERT, f"{start}+{len(word)}c")
self._hide_autocomplete()
self._editor_changed()
return "break"
def _autocomplete_key(self, event):
if not self._autocomplete_list or not self._autocomplete_popup or not self._autocomplete_popup.winfo_exists():
return None
if event.keysym == "Up":
self._autocomplete_list.event_generate("<Up>")
return "break"
if event.keysym == "Down":
self._autocomplete_list.event_generate("<Down>")
return "break"
if event.keysym == "Return":
return self._autocomplete_accept(event)
return None
def _hide_autocomplete(self, event=None):
if self._autocomplete_popup is not None:
try:
self._autocomplete_popup.destroy()
except Exception:
pass
self._autocomplete_popup = None
self._autocomplete_list = None
return "break" if event else None
# ========================= NOVA Diagnostics =========================
def _clear_diagnostics(self):
self._diagnostic_count = 0
self._diagnostic_lines.clear()
self.editor.tag_remove("nova_error_line", "1.0", tk.END)
self.editor.tag_remove("nova_error_token", "1.0", tk.END)
self.line_numbers.config(state=tk.NORMAL)
self.line_numbers.tag_remove("nova_error_number", "1.0", tk.END)
self.line_numbers.config(state=tk.DISABLED)
def _error_code(self, error):
msg = str(error)
if msg.startswith("Undefined variable") or msg.startswith("Unknown variable"):
return "MEM-001", "Memory lookup failed"
if "Invalid expression" in msg or "Invalid operator" in msg or "Division by zero" in msg or "Unit mismatch" in msg:
return "EXPR-001", "Expression could not be resolved"
if msg.startswith("Syntax Error") or msg.startswith("Unknown command") or msg.startswith("Invalid "):
return "SYN-001", "Command structure is not valid"
if msg.startswith("Time paradox"):
return "TIME-001", "Rewind request cannot be completed"
if msg.startswith("RAM") or msg.startswith("RAM index") or msg.startswith("RAM byte"):
return "RAM-001", "Hardware memory request failed"
return "RUN-001", "Runtime could not complete this instruction"
def _friendly_error_detail(self, error):
msg = str(error)
if msg.startswith("Undefined variable"):
name = msg.split("'", 2)[1] if "'" in msg else "that name"
return f"'{name}' is not defined in this run."
if msg.startswith("Unknown variable"):
name = msg.split(":", 1)[1].strip() if ":" in msg else "that name"
return f"'{name}' is not available in active memory."
if msg.startswith("Syntax Error: Unknown command"):
return "CustomScript does not recognize this instruction."
if "Division by zero" in msg:
return "A calculation attempted to divide by zero."
if "Unit mismatch" in msg:
return msg
if msg.startswith("Time paradox"):
return msg
return msg
def _mark_error_line(self, line_num, source_line):
self.editor.tag_remove("nova_error_line", "1.0", tk.END)
self.editor.tag_remove("nova_error_token", "1.0", tk.END)
self.line_numbers.config(state=tk.NORMAL)
self.line_numbers.tag_remove("nova_error_number", "1.0", tk.END)
self.editor.tag_add("nova_error_line", f"{line_num}.0", f"{line_num}.end")
self.line_numbers.tag_add("nova_error_number", f"{line_num}.0", f"{line_num}.end")