-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff_engine.py
More file actions
1640 lines (1315 loc) · 71.1 KB
/
Copy pathdiff_engine.py
File metadata and controls
1640 lines (1315 loc) · 71.1 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
# Part 1 of diff_viewer_final.py
import pygame
import sys
import os
import json
import ctypes
import time
import pyperclip
import re
import traceback
import difflib
from ctypes import wintypes
import threading
import winreg
import locale
import platform
version = "v2.66"
def __init__(self, parent_pid=None, monitors=None, pos_x=None, pos_y=None, height=None, font_name=None, font_size=None, is_pro=False):
os.environ['SDL_HINT_RENDER_BATCHING'] = '1'
os.environ['SDL_MOUSE_FOCUS_CLICKTHROUGH'] = "1"
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
os.environ['SDL_WINDOWS_DPI_AWARENESS'] = 'permonitorv2'
if 'SDL_RENDER_DRIVER' in os.environ:
del os.environ['SDL_RENDER_DRIVER']
try:
def _pre_log(m):
try:
appdata = os.environ.get('APPDATA', '')
if appdata:
pre_dir = os.path.join(appdata, "CodeStitcher")
os.makedirs(pre_dir, exist_ok=True)
else:
if getattr(sys, "frozen", False) or "__compiled__" in globals():
pre_dir = os.path.dirname(os.path.abspath(sys.executable))
else:
pre_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(pre_dir, "stitch_viewer_debug.log"), "a", encoding="utf-8") as f:
f.write(f"[{time.strftime('%H:%M:%S')}] [PRE-INIT] {m}\n")
except:
pass
_pre_log(f"__init__ started with parent_pid={parent_pid}, is_pro={is_pro}")
if os.name == 'nt':
try:
kernel32 = ctypes.windll.kernel32
user32 = ctypes.windll.user32
shell32 = ctypes.windll.shell32
kernel32.CreateMutexW.argtypes = [ctypes.c_void_p, wintypes.BOOL, wintypes.LPCWSTR]
kernel32.CreateMutexW.restype = wintypes.HANDLE
shell32.SetCurrentProcessExplicitAppUserModelID.argtypes = [wintypes.LPCWSTR]
shell32.SetCurrentProcessExplicitAppUserModelID.restype = ctypes.c_long
kernel32.GetLongPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
kernel32.GetLongPathNameW.restype = wintypes.DWORD
user32.LoadImageW.argtypes = [wintypes.HINSTANCE, wintypes.LPCWSTR, wintypes.UINT, ctypes.c_int, ctypes.c_int, wintypes.UINT]
user32.LoadImageW.restype = wintypes.HANDLE
user32.SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM]
user32.SendMessageW.restype = wintypes.LPARAM
if platform.architecture()[0] == '64bit':
user32.GetWindowLongPtrW.argtypes = [wintypes.HWND, ctypes.c_int]
user32.GetWindowLongPtrW.restype = ctypes.c_void_p
user32.SetWindowLongPtrW.argtypes = [wintypes.HWND, ctypes.c_int, ctypes.c_void_p]
user32.SetWindowLongPtrW.restype = ctypes.c_void_p
self._GetWindowLong = user32.GetWindowLongPtrW
self._SetWindowLong = user32.SetWindowLongPtrW
else:
user32.GetWindowLongW.argtypes = [wintypes.HWND, ctypes.c_int]
user32.GetWindowLongW.restype = ctypes.c_long
user32.SetWindowLongW.argtypes = [wintypes.HWND, ctypes.c_int, ctypes.c_long]
user32.SetWindowLongW.restype = ctypes.c_long
self._GetWindowLong = user32.GetWindowLongW
self._SetWindowLong = user32.SetWindowLongW
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, wintypes.UINT]
user32.SetWindowPos.restype = wintypes.BOOL
user32.ShowWindow.argtypes = [wintypes.HWND, ctypes.c_int]
user32.ShowWindow.restype = wintypes.BOOL
user32.SetForegroundWindow.argtypes = [wintypes.HWND]
user32.SetForegroundWindow.restype = wintypes.BOOL
user32.SetLayeredWindowAttributes.argtypes = [wintypes.HWND, wintypes.COLORREF, ctypes.c_byte, wintypes.DWORD]
user32.SetLayeredWindowAttributes.restype = wintypes.BOOL
user32.GetCursorPos.argtypes = [ctypes.POINTER(wintypes.POINT)]
user32.GetCursorPos.restype = wintypes.BOOL
user32.GetWindowRect.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.RECT)]
user32.GetWindowRect.restype = wintypes.BOOL
except Exception as e:
_pre_log(f"Warning: Failed to setup strict ctypes argtypes: {e}")
if os.name == 'nt':
try:
self.mutex_name = "Global\\CodeStitcherCodeStitcher_MUTEX"
self.mutex = ctypes.windll.kernel32.CreateMutexW(None, False, self.mutex_name)
if ctypes.windll.kernel32.GetLastError() == 183:
_pre_log("FATAL: Another Stitcher Viewer instance is already running. Enforcing single instance.")
sys.exit(0)
except Exception as e:
_pre_log(f"Warning: Failed to create Mutex lock: {e}")
self.parent_pid = parent_pid
self.is_pro = is_pro
if os.name == 'nt':
try:
myappid = 'uep.codestitcher.version.1.0'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
except Exception as e:
_pre_log(f"Warning: SetCurrentProcessExplicitAppUserModelID failed: {e}")
try:
pygame.init()
pygame.font.init()
pygame.key.set_repeat(400, 60)
_pre_log("Pygame initialized successfully with key repeat enabled.")
except Exception as e:
_pre_log(f"FATAL: Pygame init failed: {e}")
self.width = 1260
self.height = 750
self.title_bar_height = 19
self.GAP_WIDTH = 50
self.minimap_width = 24
if getattr(sys, "frozen", False) or "__compiled__" in globals():
self.script_dir = os.path.dirname(os.path.abspath(sys.executable))
if hasattr(sys, "_MEIPASS"):
self.asset_dir = sys._MEIPASS
else:
self.asset_dir = os.path.dirname(os.path.abspath(__file__))
else:
self.script_dir = os.path.dirname(os.path.abspath(__file__))
self.asset_dir = self.script_dir
if os.name == 'nt':
try:
buf = ctypes.create_unicode_buffer(1024)
if ctypes.windll.kernel32.GetLongPathNameW(self.script_dir, buf, 1024):
self.script_dir = buf.value
if ctypes.windll.kernel32.GetLongPathNameW(self.asset_dir, buf, 1024):
self.asset_dir = buf.value
except Exception as e:
_pre_log(f"Warning: Failed to expand short path: {e}")
self._log = self._log
self._log(f"Path verification -> script_dir: {self.script_dir} | asset_dir: {self.asset_dir}")
self.config = {
"search_dirs": monitors if monitors else [self.script_dir],
"CONF_WINDOW_HEIGHT": height if height else 750,
"CONF_FONT_NAME": font_name if font_name else 'consolas',
"CONF_FONT_SIZE": font_size if font_size else 13,
"CONF_REMEMBER_WINDOW_POS": True,
"CONF_VIEWER_ALWAYS_ON_TOP": True,
"CONF_VIEWER_WIDTH": 1260,
}
appdata = os.environ.get('APPDATA', '')
conf_dir = os.path.join(appdata, "CodeStitcher") if appdata else self.script_dir
ini_path = os.path.join(conf_dir, "cs_settings.ini")
ini_found_topmost = False
if os.path.exists(ini_path):
try:
with open(ini_path, 'r', encoding='utf-8') as f:
for line in f:
line_stripped = line.strip()
if line_stripped.startswith('CONF_VIEWER_ALWAYS_ON_TOP'):
val = line.split('=', 1)[1].strip().strip('"\'').lower()
self.config['CONF_VIEWER_ALWAYS_ON_TOP'] = (val == 'true')
ini_found_topmost = True
elif line_stripped.startswith('CONF_VIEWER_WIDTH'):
val = line.split('=', 1)[1].strip().strip('"\'')
try:
self.config['CONF_VIEWER_WIDTH'] = int(float(val))
except: pass
except: pass
if not ini_found_topmost:
self._update_ini_setting('CONF_VIEWER_ALWAYS_ON_TOP', 'True')
try:
self.width = max(800, int(float(self.config.get("CONF_VIEWER_WIDTH", 1260))))
except (ValueError, TypeError):
self.width = 1260
try:
conf_h = int(float(self.config.get('CONF_WINDOW_HEIGHT', 750))) - 1
self.height = max(500, conf_h)
self._log(f"Applied CONF_WINDOW_HEIGHT (with -1 offset): {self.height}")
except (ValueError, TypeError):
pass
self.tree_data = {}
self.render_list = []
self.active_entry_id = None
self._load_history()
pos_valid = False
parent_rect = self._get_parent_window_rect()
if parent_rect:
try:
lx = int(parent_rect[0]) - self.width - 10
ly = int(parent_rect[1])
if -4000 < lx < 12000 and -4000 < ly < 12000:
os.environ['SDL_VIDEO_WINDOW_POS'] = f"{lx},{ly}"
pos_valid = True
self._log(f"Spawning window on left hand side of parent: {lx},{ly}")
except (ValueError, TypeError):
pass
if not pos_valid:
os.environ['SDL_VIDEO_WINDOW_POS'] = "100,100"
self._log("Spawning window at default pos 100, 100")
self._log("Creating Pygame display surface...")
flags = pygame.NOFRAME | pygame.DOUBLEBUF
if hasattr(pygame, 'HIDDEN'):
flags |= getattr(pygame, 'HIDDEN')
try:
self.screen = pygame.display.set_mode((self.width, self.height), flags)
pygame.display.set_caption(f"Stitch Viewer {version}")
except Exception as e:
self._log(f"Warning: Hardware Doublebuf failed, reverting: {e}")
flags_fallback = pygame.NOFRAME
if hasattr(pygame, 'HIDDEN'): flags_fallback |= getattr(pygame, 'HIDDEN')
self.screen = pygame.display.set_mode((self.width, self.height), flags_fallback)
self.running = True
self.hwnd = None
if os.name == 'nt':
try:
self.hwnd = pygame.display.get_wm_info()['window']
self._log(f"Retrieved Window HWND: {self.hwnd}")
icon_path = os.path.join(self.asset_dir, "stitch_viewer.ico")
if not os.path.exists(icon_path):
icon_path = os.path.join(self.script_dir, "stitch_viewer.ico")
if not os.path.exists(icon_path):
icon_path = os.path.join(self.asset_dir, "code_stitcher.ico")
if not os.path.exists(icon_path):
icon_path = os.path.join(self.script_dir, "code_stitcher.ico")
if os.path.exists(icon_path):
WM_SETICON = 0x0080
hIcon = ctypes.windll.user32.LoadImageW(None, icon_path, 1, 0, 0, 0x0010 | 0x00000040)
if hIcon:
ctypes.windll.user32.SendMessageW(self.hwnd, WM_SETICON, 0, hIcon)
ctypes.windll.user32.SendMessageW(self.hwnd, WM_SETICON, 1, hIcon)
GWL_EXSTYLE = -20
WS_EX_TOOLWINDOW = 0x00000080
WS_EX_APPWINDOW = 0x00040000
SWP_NOMOVE = 0x0002
SWP_NOSIZE = 0x0001
SWP_NOZORDER = 0x0004
SWP_FRAMECHANGED = 0x0020
SWP_SHOWWINDOW = 0x0040
if hasattr(self, '_GetWindowLong') and hasattr(self, '_SetWindowLong'):
exstyle = self._GetWindowLong(self.hwnd, GWL_EXSTYLE)
if exstyle is not None:
new_exstyle = (exstyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW
self._SetWindowLong(self.hwnd, GWL_EXSTYLE, new_exstyle)
if self.config.get('CONF_VIEWER_ALWAYS_ON_TOP', True):
HWND_TOPMOST = -1
ctypes.windll.user32.SetWindowPos(self.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW)
def keep_on_top():
while getattr(self, 'running', True):
try:
if self.config.get('CONF_VIEWER_ALWAYS_ON_TOP', True):
ctypes.windll.user32.SetWindowPos(self.hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
else:
ctypes.windll.user32.SetWindowPos(self.hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
except BaseException:
pass
time.sleep(2)
threading.Thread(target=keep_on_top, daemon=True).start()
else:
ctypes.windll.user32.SetWindowPos(self.hwnd, None, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW)
SW_SHOW = 5
ctypes.windll.user32.ShowWindow(self.hwnd, SW_SHOW)
ctypes.windll.user32.SetForegroundWindow(self.hwnd)
self._log("Applied extended styles and brought to foreground successfully.")
def maintain_transparency():
while getattr(self, 'running', True):
try:
if getattr(self, '_current_alpha_byte', 255) < 255 and getattr(self, 'hwnd', None):
ctypes.windll.user32.SetLayeredWindowAttributes(self.hwnd, 0x00FF00FF, self._current_alpha_byte, 3)
except BaseException:
pass
time.sleep(5)
threading.Thread(target=maintain_transparency, daemon=True).start()
except Exception as e:
_pre_log(f"Warning: HWND modification failed: {e}")
font_name = self.config.get('CONF_FONT_NAME', 'consolas')
try:
font_size = int(float(self.config.get('CONF_FONT_SIZE', 13)))
except (ValueError, TypeError):
font_size = 13
self.font = pygame.font.SysFont(font_name, font_size)
self.font_bold = pygame.font.SysFont(font_name, font_size, bold=True)
self.title_font = pygame.font.SysFont(font_name, 14, bold=False)
self.close_font = pygame.font.SysFont(font_name, 18, bold=False)
self.button_font = pygame.font.SysFont(font_name, 11, bold=False)
self.tree_stat_font = pygame.font.SysFont(font_name, max(9, font_size - 2))
self.line_height = self.font.get_linesize() + 4
self.tree_row_height = 22
self.focused = True
self.left_panel_width = 150
self.scroll_left = 0
self.scroll_right = 0
self.scroll_right_target = 0.0
self.scroll_right_float = 0.0
self.scroll_right_x = 0
self.scroll_right_x_target = 0.0
self.scroll_right_x_float = 4490.0
self.max_right_width = 1
self.is_dragging_scroll_x = True
self._scroll_x_drag_offset = 0
self.max_edit_width = 0
self.is_dragging_editor_scroll_x = False
self._editor_scroll_x_offset = 0
self.hovered_history_idx = -1
self.hovered_line_idx = None
self.hovered_line_pane = None
self.old_lines = []
self.new_lines = []
self.diff_blocks = []
self.selection_start = None
self.selection_end = None
self.is_selecting = False
self.selecting_pane = None
self.is_dragging_minimap = False
self.search_text = ""
self.search_active = False
self.search_results = []
self.search_current_idx = -1
self.is_folded = False
self.visible_indices = []
self.actual_to_visual = []
self.search_rect = pygame.Rect(0,0,0,0)
self.fold_rect = pygame.Rect(0,0,0,0)
self.in_context_view = False
self.scroll_context = 0
self.substantial_changes = []
self.is_editing = False
self.edit_lines = []
self.edit_cursor_row = 0
self.edit_cursor_col = 0
self.edit_scroll_y = 0
self.edit_scroll_x = 0
self.edit_selection_start = None
self.edit_is_dragging = False
self.edit_undo_stack = []
self.edit_redo_stack = []
if self.render_list:
for item in self.render_list:
if item['type'] == 'entry':
self._select_entry(item['data'])
break
self._log("__init__ fully completed.")
except BaseException as fatal_e:
try:
appdata = os.environ.get('APPDATA', '')
if appdata:
err_dir = os.path.join(appdata, "CodeStitcher")
os.makedirs(err_dir, exist_ok=True)
else:
if getattr(sys, "frozen", False) or "__compiled__" in globals():
err_dir = os.path.dirname(os.path.abspath(sys.executable))
else:
err_dir = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(err_dir, "stitch_viewer_debug.log")
with open(log_path, "a", encoding="utf-8") as f:
f.write(f"[{time.strftime('%H:%M:%S')}] FATAL INIT CRASH: {fatal_e}\n{traceback.format_exc()}\n")
except:
pass
raise
def _restricted_walk(self, directory: str):
target_base = os.path.abspath(directory)
for root, dirs, files in os.walk(directory):
curr_path = os.path.abspath(root)
dirs[:] = [d for d in dirs if d not in ('.git', '__pycache__', 'node_modules', 'venv', 'env', '.redo')]
if not getattr(self, 'is_pro', False):
if curr_path == target_base:
dirs[:] = [d for d in dirs if d == "ep_backups"]
elif curr_path == os.path.join(target_base, "ep_backups"):
dirs[:] = []
else:
dirs[:] = []
yield root, dirs, files
def _move_selection_to_current(self):
"""Copies custom selection range from historical pane directly to corresponding spot in current version."""
self.context_menu = None
pane = "old"
selected_indices = set()
for (r1, r2) in getattr(self, 'extra_selections', {}).get(pane, []):
selected_indices.update(range(r1, r2 + 1))
if getattr(self, 'selection_start', None) is not None and getattr(self, 'selection_end', None) is not None:
r1 = min(self.selection_start, self.selection_end)
r2 = max(self.selection_start, self.selection_end)
selected_indices.update(range(r1, r2 + 1))
if not selected_indices:
return
sorted_indices = sorted(list(selected_indices))
old_lns = [self.old_lines[i][2] for i in sorted_indices if self.old_lines[i][2] is not None]
if not old_lns: return
old_min, old_max = min(old_lns), max(old_lns)
new_lns = [self.new_lines[i][2] for i in sorted_indices if self.new_lines[i][2] is not None]
bak_ref = getattr(self, 'current_entry', {}).get("backup_reference", "")
if not bak_ref or not hasattr(self, 'active_file_path') or not self.active_file_path:
return
parent_dir = os.path.dirname(self.active_file_path)
old_file_path = os.path.join(parent_dir, "ep_backups", bak_ref)
if not os.path.exists(old_file_path) or not os.path.exists(self.active_file_path):
return
def _get_raw_lines(filepath):
try:
with open(filepath, 'rb') as f: raw = f.read()
for enc in ('utf-8', 'utf-8-sig', 'utf-16', 'windows-1252', 'latin-1'):
try:
text = raw.decode(enc)
text = text.replace('\r\n', '\n').replace('\r', '\n')
return text.splitlines()
except: pass
text = raw.decode('utf-8', errors='ignore')
text = text.replace('\r\n', '\n').replace('\r', '\n')
return text.splitlines()
except: return []
old_file_lines = _get_raw_lines(old_file_path)
new_file_lines = _get_raw_lines(self.active_file_path)
if not old_file_lines or not new_file_lines: return
old_block_lines = old_file_lines[old_min - 1 : old_max]
if new_lns:
new_min, new_max = min(new_lns), max(new_lns)
final_lines = new_file_lines[:new_min - 1] + old_block_lines + new_file_lines[new_max:]
else:
insert_idx = 0
for i in range(sorted_indices[0] - 1, -1, -1):
if self.new_lines[i][2] is not None:
insert_idx = self.new_lines[i][2]
break
final_lines = new_file_lines[:insert_idx] + old_block_lines + new_file_lines[insert_idx:]
try:
with open(self.active_file_path, 'w', encoding='utf-8', newline='\n') as f:
f.write("\n".join(final_lines) + "\n")
self._log("Moved selected code to current version.")
self._select_entry(self.current_entry)
except Exception as e:
self._log(f"Failed to move selection: {e}")
def _copy_selection(self):
self.context_menu = None
pane = getattr(self, 'selecting_pane', None)
if not pane: return
lines = self.old_lines if pane == "old" else self.new_lines
selected_indices = set()
for (r1, r2) in getattr(self, 'extra_selections', {}).get(pane, []):
selected_indices.update(range(r1, r2 + 1))
if getattr(self, 'selection_start', None) is not None and getattr(self, 'selection_end', None) is not None:
r1 = min(self.selection_start, self.selection_end)
r2 = max(self.selection_start, self.selection_end)
selected_indices.update(range(r1, r2 + 1))
copied_text = []
for i in sorted(list(selected_indices)):
if 0 <= i < len(lines):
text, status, ln = lines[i]
if status not in ("blank_del", "blank_add", "header"):
copied_text.append(text)
if copied_text:
try: pyperclip.copy("\n".join(copied_text))
except: pass
def _render_syntax_line(self, text: str, base_color: tuple) -> pygame.Surface:
if not hasattr(self, '_syntax_cache'):
self._syntax_cache = {}
cache_key = (text, base_color)
if cache_key in self._syntax_cache:
return self._syntax_cache[cache_key]
text = text.replace('\t', ' ')
total_width, h = self.font.size(text)
if total_width == 0: total_width = 1
surf = pygame.Surface((total_width, max(h, self.line_height)), pygame.SRCALPHA)
C_KW = (204, 120, 50)
C_CLASS = (255, 170, 60)
C_STR = (106, 135, 89)
C_NUM = (104, 151, 187)
C_COM = (128, 128, 128)
C_BOOL = (86, 156, 214)
classes_kw = {'class', 'struct', 'interface', 'enum', 'namespace'}
keywords = {
'if', 'for', 'while', 'try', 'except', 'finally', 'with', 'def',
'return', 'yield', 'import', 'from', 'as', 'pass', 'break', 'continue',
'async', 'await', 'raise', 'global', 'nonlocal', 'del', 'in', 'is', 'and', 'or', 'not',
'let', 'const', 'var', 'function', 'static', 'public', 'private', 'void', 'new', 'switch', 'case'
}
booleans = {'True', 'False', 'None', 'true', 'false', 'null', 'undefined'}
x = 0
comment_part = ""
code_part = text
if '#' in text and text.find('#') >= 0:
in_str = False
str_char = ''
for i, char in enumerate(text):
if char in "\"'":
if not in_str:
in_str = True
str_char = char
elif str_char == char:
in_str = False
elif char == '#' and not in_str:
code_part = text[:i]
comment_part = text[i:]
break
if not comment_part and '//' in text:
in_str = False
str_char = ''
for i in range(len(text)-1):
char = text[i]
if char in "\"'":
if not in_str:
in_str = True
str_char = char
elif str_char == char:
in_str = False
elif text[i:i+2] == '//' and not in_str:
code_part = text[:i]
comment_part = text[i:]
break
tokens = re.split(r'(\s+|[().,\[\]{}:+\-*/=<>!]+|"[^"]*"|\'[^\']*\')', code_part)
for token in tokens:
if not token: continue
color = base_color
if token in classes_kw:
color = C_CLASS
elif token in keywords:
color = C_KW
elif token in booleans:
color = C_BOOL
elif token.isdigit():
color = C_NUM
elif token.startswith('"') or token.startswith("'"):
color = C_STR
t_surf = self.font.render(token, True, color)
surf.blit(t_surf, (x, 0))
x += self.font.size(token)[0]
if comment_part:
c_surf = self.font.render(comment_part, True, C_COM)
surf.blit(c_surf, (x, 0))
self._syntax_cache[cache_key] = surf
return surf
def _execute_linter_syntax_check(self):
"""Executes the heavy compilation / stack matching syntax check asynchronously/debounced."""
self.edit_linter_errors = []
if not hasattr(self, 'active_file_path') or not self.active_file_path:
return
ext = os.path.splitext(self.active_file_path)[1].lower()
clean_lines = self.edit_lines[:-4] if len(self.edit_lines) >= 4 else self.edit_lines
code_text = "\n".join(clean_lines)
if ext == '.py':
try:
compile(code_text, "<editor>", "exec")
except SyntaxError as e:
row = (e.lineno - 1) if e.lineno is not None else 0
msg = e.msg or "Syntax Error"
self.edit_linter_errors.append((row, msg))
elif ext == '.json':
try:
json.loads(code_text)
except ValueError as e:
m = re.search(r'line (\d+)', str(e))
row = (int(m.group(1)) - 1) if m else 0
self.edit_linter_errors.append((row, str(e)))
else:
open_chars = {'(': ')', '{': '}', '[': ']'}
stack = []
for row_idx, line in enumerate(self.edit_lines):
for col_idx, char in enumerate(line):
if char in open_chars:
stack.append((char, row_idx))
elif char in open_chars.values():
if not stack:
self.edit_linter_errors.append((row_idx, f"Mismatched closing bracket '{char}'"))
break
last_open, last_row = stack.pop()
if open_chars[last_open] != char:
self.edit_linter_errors.append((row_idx, f"Mismatched bracket: expected '{open_chars[last_open]}', got '{char}'"))
break
if len(self.edit_linter_errors) > 0:
break
if stack and not self.edit_linter_errors:
char, r = stack[-1]
self.edit_linter_errors.append((r, f"Unclosed bracket '{char}'"))
def _shift_editor_folds(self, start_row, delta):
"""Adjusts editor folded block indices when lines are added or removed."""
if not hasattr(self, 'edit_folded_blocks'):
self.edit_folded_blocks = set()
new_folds = set()
for r in self.edit_folded_blocks:
if r >= start_row:
new_val = r + delta
if new_val >= 0:
new_folds.add(new_val)
else:
new_folds.add(r)
self.edit_folded_blocks = new_folds
def _copy_for_llm_selection(self):
if self.selection_start is None or self.selection_end is None or not self.selecting_pane:
self.context_menu = None
return
r1 = min(self.selection_start, self.selection_end)
r2 = max(self.selection_start, self.selection_end)
lines = self.old_lines if self.selecting_pane == "old" else self.new_lines
copied_text = []
start_ln = None
end_ln = None
for i in range(r1, r2 + 1):
if 0 <= i < len(lines):
text, status, ln = lines[i]
if status not in ("blank_del", "blank_add", "header"):
copied_text.append(text)
if ln is not None:
if start_ln is None: start_ln = ln
end_ln = ln
if copied_text:
fname = os.path.basename(getattr(self, 'active_file_path', ''))
if not fname: fname = "snippet"
ext = fname.split('.')[-1] if '.' in fname else 'text'
ln_str = f" (Lines {start_ln}-{end_ln})" if start_ln and end_ln else ""
header = f"In `{fname}`{ln_str}:\n```{ext}\n"
footer = "\n```"
try:
pyperclip.copy(header + "\n".join(copied_text) + footer)
except: pass
self.context_menu = None
def _show_modal_confirm(self, title: str, message_lines: list) -> bool:
"""Draws a custom, blocking modal dialog over the Pygame UI."""
bg_surface = self.screen.copy()
dim_surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
dim_surface.fill((0, 0, 0, 180))
bg_surface.blit(dim_surface, (0, 0))
box_w = 480
box_h = 160 + (len(message_lines) * 20)
box_x = (self.width - box_w) // 2
box_y = (self.height - box_h) // 2
box_rect = pygame.Rect(box_x, box_y, box_w, box_h)
btn_w = 140
btn_h = 30
btn_yes = pygame.Rect(box_x + box_w - (btn_w * 2) - 20, box_y + box_h - 45, btn_w, btn_h)
btn_no = pygame.Rect(box_x + box_w - btn_w - 10, box_y + box_h - 45, btn_w, btn_h)
clock = pygame.time.Clock()
while True:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
elif event.key == pygame.K_RETURN:
return True
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if btn_yes.collidepoint(mx, my):
return True
elif btn_no.collidepoint(mx, my):
return False
self.screen.blit(bg_surface, (0, 0))
pygame.draw.rect(self.screen, (35, 35, 40), box_rect, border_radius=6)
pygame.draw.rect(self.screen, (100, 100, 110), box_rect, 1, border_radius=6)
title_surf = self.font_bold.render(title, True, (240, 80, 80))
self.screen.blit(title_surf, (box_x + 20, box_y + 20))
pygame.draw.line(self.screen, (80, 80, 90), (box_x + 20, box_y + 45), (box_x + box_w - 20, box_y + 45))
for i, line in enumerate(message_lines):
txt_surf = self.font.render(line, True, (200, 200, 200))
self.screen.blit(txt_surf, (box_x + 20, box_y + 60 + (i * 20)))
hover_yes = btn_yes.collidepoint(mx, my)
pygame.draw.rect(self.screen, (160, 60, 60) if hover_yes else (120, 50, 50), btn_yes, border_radius=4)
yes_text = self.font_bold.render("Yes, Overwrite", True, (255, 255, 255))
self.screen.blit(yes_text, (btn_yes.centerx - yes_text.get_width()//2, btn_yes.centery - yes_text.get_height()//2))
hover_no = btn_no.collidepoint(mx, my)
pygame.draw.rect(self.screen, (80, 80, 90) if hover_no else (60, 60, 65), btn_no, border_radius=4)
no_text = self.font.render("Cancel", True, (220, 220, 220))
self.screen.blit(no_text, (btn_no.centerx - no_text.get_width()//2, btn_no.centery - no_text.get_height()//2))
pygame.display.flip()
clock.tick(60)
def _activate_search(self):
self.search_active = True
self.context_menu = None
self.search_sel_start = 0
self.search_sel_end = len(self.search_text)
self.search_cursor_pos = len(self.search_text)
def _edit_fold_unchanged_blocks(self):
self.edit_folded_blocks.clear()
old_clean = [l[0] for l in self.old_lines if l[1] != 'blank_del']
sm = difflib.SequenceMatcher(None, old_clean, self.edit_lines)
changed_lines = set()
for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag != 'equal':
changed_lines.update(range(j1, j2))
for start_i, end_i in getattr(self, 'edit_code_blocks', {}).items():
block_lines = set(range(start_i, end_i + 1))
if not block_lines.intersection(changed_lines):
self.edit_folded_blocks.add(start_i)
if not self.edit_folded_blocks:
self.edit_folded_blocks = set(getattr(self, 'edit_code_blocks', {}).keys())
def _handle_mouse_button_down_event(self, event):
mx, my = event.pos
pane = self._get_pane_for_x(mx)
is_pressed = pygame.mouse.get_pressed()[0]
if getattr(self, 'show_commands_help', False):
if getattr(self, 'help_close_rect', pygame.Rect(0,0,0,0)).collidepoint(mx, my):
self.show_commands_help = False
return
cm = getattr(self, 'context_menu', None)
if cm:
if cm['rect'].collidepoint(mx, my):
if event.button == 1:
for opt in cm['options']:
if opt.get('rect') and opt['rect'].collidepoint(mx, my):
if opt.get('action'): opt['action']()
break
return
else:
self.context_menu = None
if my < self.title_bar_height and event.button == 1:
if mx > self.width - 40:
self.running = False
elif mx > self.width - 80:
if getattr(self, 'hwnd', None):
ctypes.windll.user32.ShowWindow(self.hwnd, 6)
else:
self._is_dragging = True
if os.name == 'nt' and getattr(self, 'hwnd', None):
pt = wintypes.POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(pt))
rect = wintypes.RECT()
ctypes.windll.user32.GetWindowRect(self.hwnd, ctypes.byref(rect))
self._drag_offset_x = pt.x - rect.left
self._drag_offset_y = pt.y - rect.top
return
if not getattr(self, 'is_editing', False) and not getattr(self, 'in_context_view', False):
right_w = max(1, self.width - self.left_panel_width - self.minimap_width)
pane_w = max(1, (right_w - self.GAP_WIDTH) // 2)
pane_old_x = self.left_panel_width
pane_new_x = pane_old_x + pane_w + self.GAP_WIDTH
visible_w = pane_w - 65
max_scroll_x = max(0, getattr(self, 'max_right_width', 0) - visible_w + 50)
if max_scroll_x > 0:
scrollbar_h = 12
scrollbar_rect = pygame.Rect(pane_new_x + 60, self.height - scrollbar_h - 5, pane_w - 65, scrollbar_h)
ratio = visible_w / max(1, getattr(self, 'max_right_width', 0))
thumb_w = min(50, max(15, int(scrollbar_rect.width * ratio)))
max_thumb_travel = scrollbar_rect.width - thumb_w
thumb_x = scrollbar_rect.x + int((self.scroll_right_x / max_scroll_x) * max_thumb_travel)
thumb_rect = pygame.Rect(thumb_x, scrollbar_rect.y, thumb_w, scrollbar_h)
if scrollbar_rect.collidepoint(mx, my):
if thumb_rect.collidepoint(mx, my):
self.is_dragging_scroll_x = True
self._scroll_x_drag_offset = mx - thumb_x
else:
click_ratio = (mx - scrollbar_rect.x - thumb_w / 2) / max(1, max_thumb_travel)
self.scroll_right_x = max(0, min(max_scroll_x, int(click_ratio * max_scroll_x)))
self.scroll_right_x_target = float(self.scroll_right_x)
self.scroll_right_x_float = float(self.scroll_right_x)
return
if event.button == 1 and pane == "minimap" and my > (self.title_bar_height + 25):
diff_y_start = self.title_bar_height + 25
map_h = self.height - diff_y_start
is_ed = getattr(self, 'is_editing', False) or getattr(self, 'edit_anim_t', 0.0) > 0.0
if is_ed:
total_lines = len(getattr(self, 'edit_visible_indices', []))
scroll_val = getattr(self, 'edit_scroll_y_float', float(self.edit_scroll_y))
max_lines = (self.height - diff_y_start - 5) // self.line_height
else:
total_lines = len(getattr(self, 'visible_indices', getattr(self, 'old_lines', [])))
scroll_val = getattr(self, 'scroll_right_float', float(self.scroll_right))
max_lines = map_h // self.line_height
scale_y = map_h / total_lines if total_lines > 0 else 0
vp_y = diff_y_start + (scroll_val * scale_y)
vp_h = max(10, max_lines * scale_y)
if vp_y <= my <= vp_y + vp_h:
self._minimap_drag_offset = my - vp_y
else:
self._minimap_drag_offset = None
self.is_dragging_minimap = True
self._scroll_to_minimap(my)
return
if event.button == 3:
if pane == "left" and (self.title_bar_height + 25) < my < self.height - 25:
idx = self.scroll_left + ((my - (self.title_bar_height + 25)) // self.tree_row_height)
if 0 <= idx < len(self.render_list):
item = self.render_list[idx]
if item['type'] == 'file':
full_path = self.tree_data[item['name']]['full_path']
entry = {
'file': item['name'],
'full_path': full_path,
'backup_reference': "",
'is_unversioned': True
}
options = [
{"label": "Copy Entire File", "action": lambda e=entry: self._copy_version_to_clipboard(e)},
{"label": "Attach to compose window", "action": lambda e=entry: self._attach_to_compose(e)}
]
w, h = 210, len(options) * 22
draw_x = mx if mx + w < self.width else self.width - w
draw_y = my if my + h < self.height else self.height - h
self.context_menu = {'rect': pygame.Rect(draw_x, draw_y, w, h), 'options': []}
for i_opt, opt in enumerate(options):
opt_rect = pygame.Rect(draw_x, draw_y + (i_opt * 22), w, 22)
self.context_menu['options'].append({'label': opt['label'], 'action': opt['action'], 'rect': opt_rect})
return
elif item['type'] == 'entry':