-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRust.py
More file actions
1525 lines (1319 loc) · 63 KB
/
Rust.py
File metadata and controls
1525 lines (1319 loc) · 63 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 os
import sys
import json
import shutil
from functools import partial
# --- Graphics / WebEngine Fixes --- U15 DMS
os.environ["QT_OPENGL"] = "desktop"
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu"
# Prevent Qt from using an internal qt.conf that can break plugin/DLL resolution in onefile builds,
# but ONLY when running as a frozen executable. When running from source, keep the internal qt.conf enabled
# so PySide6 can locate its plugins (platforms, etc.).
if getattr(sys, "frozen", False):
os.environ["PYSIDE_DISABLE_INTERNAL_QT_CONF"] = "1"
else:
os.environ.pop("PYSIDE_DISABLE_INTERNAL_QT_CONF", None)
from PySide6.QtCore import (
Qt, QDir, QFileInfo, QUrl, QRegularExpression, QCoreApplication, QRect, QSize, QProcess, Slot, QTimer, QRunnable, QThreadPool, QObject, Signal, QPoint
)
from PySide6.QtGui import (
QFont, QSyntaxHighlighter, QTextCharFormat, QColor, QPalette, QPainter, QTextFormat, QTextCursor, QIcon, QPixmap, QDesktopServices, QPen, QLinearGradient, QKeySequence, QShortcut
)
from PySide6.QtWidgets import (
QApplication, QMainWindow, QSplitter, QTreeView, QTextEdit,
QVBoxLayout, QWidget, QFileDialog, QTabWidget, QPlainTextEdit,
QMessageBox, QFileSystemModel, QMenuBar, QHeaderView,
QHBoxLayout, QPushButton, QCompleter,
QDialog, QDialogButtonBox, QFontComboBox, QSpinBox, QFormLayout, QComboBox,
QMenu, QInputDialog, QLineEdit,
QStackedWidget, QLabel, QTabBar, QStyledItemDelegate, QStyle
)
# Optional WebEngine Import
try:
from PySide6.QtWebEngineWidgets import QWebEngineView
WEB_ENGINE_AVAILABLE = True
except ImportError:
WEB_ENGINE_AVAILABLE = False
QWebEngineView = None
# Force-disable WebEngine in frozen executables to avoid recursive subprocess spawning
if getattr(sys, "frozen", False):
WEB_ENGINE_AVAILABLE = False
QWebEngineView = None
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
from PySide6.QtWidgets import QSizePolicy
# Import existing modules
from Details.Main_Code_Editor import CodeEditor
from file_showen import FileTreeDelegate, CustomFileSystemModel, FileSorterProxyModel, KeyboardDisplayWidget
from running_app import InteractiveTerminal
from manage_native import ManageWidget
from Manage.document_io import SaveLoadManager
from Details.Header_Setting import create_main_menu_bar
from Details.dialogs import CustomInputDialog, CustomMessageBox, LicenseDialog, KeyboardShortcutsDialog, CustomTitleBarDialog
from Details.welcome_page import WelcomePageWidget
from Details.file_tree_with_shortcuts import EnhancedFileTreeView
# Import new modular components
from Main.settings_dialogs import SettingsDialog
from Main.title_bar import CustomTitleBar
from Main.file_operations import FileOperationsManager
from Main.menu_actions import MenuActionsManager
from Main.terminal_manager import TerminalManager
from Main.editor_actions import EditorActionsManager
from Main.menu_style_right_click import apply_default_menu_style, show_file_tree_context_menu
from Main.color_mode import ColorModeManager
from Main.rust_runner import RustRunnerManager
from Main.rust_error_checker import RustErrorChecker, CargoCheckManager
from Main.ui_setup import UISetupManager
from Main.settings_manager import SettingsManager
from Main.window_state_manager import WindowStateManager
# Main Window
class MainWindow(QMainWindow):
"""
The main application window for the code editor.
Includes file tree, code editor, and preview panes.
"""
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowTitle("A³ Rust Editor")
try:
self.setWindowIcon(QIcon("img/icon.ico"))
except Exception:
pass
self.setGeometry(100, 100, 1400, 900)
self.setStyleSheet("background-color: #131314;")
self.current_file_path = None
self.open_files = {} # path: editor_widget
self.closable_tabs_with_buttons = {}
# Persistent color change mode state and current color for toolbar icon
self.color_change_mode_active = False
self.current_color_hex = "#1E1F22"
self._cursor_move_conn = None
# Rust execution state handled by manager
self.rust_runner = None # initialized later after UI
self.settings_dir = os.path.join(os.environ.get('LOCALAPPDATA') or os.environ.get('APPDATA') or os.path.expanduser("~"), "A3PyEditor")
try:
os.makedirs(self.settings_dir, exist_ok=True)
except Exception:
pass
self.settings_file = os.path.join(self.settings_dir, "settings.json")
# Session persistence file (tracks open tabs, active tab, and unsaved buffers)
self.session_file = os.path.join(self.settings_dir, "session.json")
self.recent_files = []
self.settings = {
'font_family': 'Consolas',
'font_size': 12,
# Do NOT use the frozen EXE as a Python interpreter
'python_interpreter_path': (sys.executable if not getattr(sys, 'frozen', False) else ''),
'theme': 'Default',
'restore_last_folder': True,
'last_folder': ''
}
# Initialize managers FIRST (before loading settings)
self.file_ops = FileOperationsManager(self)
self.menu_actions = MenuActionsManager(self)
self.terminal_manager = TerminalManager(self)
self.editor_actions = EditorActionsManager(self)
self.ui_setup = UISetupManager(self)
self.settings_manager = SettingsManager(self)
self.window_state_manager = WindowStateManager(self)
# Initialize project manager (will be set up after UI is ready)
self.project_manager = None
# Now load settings after managers are initialized
self.load_settings()
# Determine initial workspace root (restore last folder if available)
try:
lf = (self.settings.get('last_folder') or '').strip()
self.initial_root_path = lf if self.settings.get('restore_last_folder') and lf and os.path.isdir(lf) else ''
except Exception:
self.initial_root_path = ''
# Sanitize interpreter path when running as a frozen EXE to avoid self-recursion
try:
if getattr(sys, 'frozen', False):
p = (self.settings.get('python_interpreter_path') or '').strip()
if p and os.path.abspath(p).lower() == os.path.abspath(sys.executable).lower():
self.settings['python_interpreter_path'] = ''
except Exception:
pass
# Clipboard for file operations (copy/cut/paste)
self.file_clipboard = {'action': None, 'paths': []}
# Initialize split editor state
self.editor_splitter = None
# Vertical adjustment for settings menu position (pixels). Positive moves down, negative moves up.
self.settings_menu_y_offset = 0
# Color mode controller
self.color_mode = ColorModeManager(self)
self.ui_setup.setup_ui()
# Init rust runner and cargo check manager now that self is ready
self.rust_runner = RustRunnerManager(self)
self.cargo_check_manager = CargoCheckManager(self)
# Initialize project manager after UI is ready
QTimer.singleShot(200, self._setup_project_manager)
self.settings_manager.apply_settings()
# Restore window state (size, position, maximized state, panel sizes)
try:
self.window_state_manager.restore_window_state()
except Exception:
pass
# Restore session (open tabs and unsaved buffers like VS Code)
try:
self.restore_session()
except Exception:
pass
# Accept drag-and-drop of files/folders into the window
self.setAcceptDrops(True)
# Keyboard shortcut for Change Color toggle (F1)
try:
self.color_change_shortcut = QShortcut(QKeySequence("F1"), self)
self.color_change_shortcut.activated.connect(self.toggle_color_change_mode)
except Exception:
pass
# Periodic session autosave to persist open tabs and unsaved edits
try:
self._session_autosave_timer = QTimer(self)
self._session_autosave_timer.setInterval(2000)
self._session_autosave_timer.timeout.connect(self.save_session_state)
self._session_autosave_timer.start()
except Exception:
pass
def _setup_project_manager(self):
"""Initialize the project manager for Add Project functionality"""
try:
from Manage2.project_manager import ProjectManager
project_root = os.path.dirname(os.path.abspath(__file__))
# Get the manage widget's canvas and toolbar
if hasattr(self, 'manage_widget'):
canvas = self.manage_widget.canvas
# Get the toolbar that was already created by ManageWidget
self.top_toolbar = getattr(self.manage_widget, 'top_toolbar', None)
# Create project manager
self.project_manager = ProjectManager(self, canvas, project_root)
# Connect canvas double-click to check for Add node
canvas.node_double_clicked.connect(self._on_node_double_clicked)
except Exception:
pass
def _on_node_double_clicked(self, node):
"""Handle node double-click - check if it's the Add node"""
try:
# Check if this is the Add node
is_add_tool = getattr(node, 'is_add_tool', False)
has_add_icon = hasattr(node, 'icon_path') and node.icon_path and 'Add.png' in str(node.icon_path)
if is_add_tool or has_add_icon:
# Trigger add function dialog
if self.project_manager:
self.project_manager.on_add_function_requested()
return
# Otherwise, use default behavior (show code viewer)
if hasattr(self.manage_widget, 'on_node_double_clicked'):
self.manage_widget.on_node_double_clicked(node)
except Exception:
pass
def _on_menu_action(self, action_name: str):
"""Handle menu action from top right toolbar"""
# Handle project switching
if action_name.startswith("_load_project_"):
try:
project_id = int(action_name.replace("_load_project_", ""))
from Manage2.project_loader import load_project_canvas
load_project_canvas(self.manage_widget, self.top_toolbar.project_state, project_id)
self.top_toolbar.refresh_projects_list()
except Exception:
pass
return
if action_name == "Add Project":
if self.project_manager:
self.project_manager.start_add_project()
else:
QMessageBox.warning(self, "Error", "Project manager not initialized")
elif action_name == "Select Box":
# Toggle selection box mode on the canvas
if hasattr(self.manage, 'canvas'):
current_mode = getattr(self.manage.canvas, '_selection_box_mode', False)
self.manage.canvas.toggle_selection_box_mode(not current_mode)
elif action_name == "Search":
# Trigger the search box in manage widget
try:
if hasattr(self, 'manage_widget'):
self.manage_widget.trigger_search()
except Exception:
QMessageBox.information(self, "Search", "Search functionality")
elif action_name == "Save A3 Project":
self._save_layout_to_file()
elif action_name == "Load A3 Project":
self._open_layout_from_file()
def highlight_main_python_file(self, file_path):
"""Highlight the main Python file in the file tree"""
self.highlighted_file = file_path
# Update the whole tree view to ensure all columns are highlighted
self.tree_view.viewport().update()
# Ensure the file is visible
index = self.proxy_model.mapFromSource(self.file_model.index(file_path))
parent_index = index.parent()
while parent_index.isValid():
self.tree_view.expand(parent_index)
parent_index = parent_index.parent()
self.tree_view.scrollTo(index)
# Clear the highlight after 3 seconds
QTimer.singleShot(3000, self.clear_main_python_highlight)
def clear_main_python_highlight(self):
"""Clear the highlight from the main Python file"""
if hasattr(self, 'highlighted_file'):
source_index = self.file_model.index(self.highlighted_file)
self.highlighted_file = None
# Update all columns in the row
for column in range(self.file_model.columnCount()):
old_index = self.proxy_model.mapFromSource(source_index.sibling(source_index.row(), column))
self.tree_view.update(old_index)
# Also update the viewport to ensure complete refresh
self.tree_view.viewport().update()
# ++++++++++++++++++++++++++ Panel Management ++++++++++++++++++++++++++
def show_files_panel(self):
"""Shows the file tree and restores the main editor view."""
self.main_content_stack.setCurrentIndex(0)
self.left_pane_stack.setCurrentIndex(0)
# Restore preview tabs when returning to Files view
try:
self.preview_tabs.setVisible(True)
self.preview_tabs.show()
h = max(200, self.right_pane_splitter.height())
self.right_pane_splitter.setSizes([int(h * 0.4), int(h * 0.6)])
self.right_pane_splitter.update()
except Exception:
pass
def show_search_panel(self):
"""Shows the search panel and restores the main editor view."""
self.main_content_stack.setCurrentIndex(0)
self.left_pane_stack.setCurrentIndex(1)
# Update search panel root path to current workspace
try:
src_idx = self.proxy_model.mapToSource(self.tree_view.rootIndex())
path = self.file_model.filePath(src_idx)
if path and os.path.isdir(path):
self.search_panel.set_root_path(path)
except Exception:
pass
# Hide preview tabs when showing search (like VS Code)
try:
self.preview_tabs.setVisible(False)
self.right_pane_splitter.setSizes([self.right_pane_splitter.height(), 0])
except Exception:
pass
def show_chat_panel(self):
"""Shows the AI chat and restores the main editor view."""
self.main_content_stack.setCurrentIndex(0)
self.left_pane_stack.setCurrentIndex(2)
# Hide preview tabs to maximize AI Chat height
try:
self.preview_tabs.setVisible(False)
self.right_pane_splitter.setSizes([self.right_pane_splitter.height(), 0])
except Exception:
pass
def show_manage_panel(self):
"""Shows the native manage panel in full screen."""
self.main_content_stack.setCurrentIndex(1)
def _save_layout_to_file(self):
"""Save current canvas layout AND all Layer menu projects to a .mndoc file"""
try:
from Manage.document_io import SaveLoadManager
# Ensure Manage panel is visible
self.show_manage_panel()
# Collect current canvas state WITH all Layer menu projects
save_manager = SaveLoadManager()
state = save_manager.collect_state(self.manage_widget, include_projects=True)
# Ask user where to save
file_path, _ = QFileDialog.getSaveFileName(
self,
"Save A3 Project",
"",
save_manager.suggested_filter()
)
if file_path:
# Ensure .mndoc extension
file_path = save_manager.ensure_extension(file_path)
# Save to file (includes all projects)
save_manager.save_to_file(file_path, state)
# Mark all projects as saved
if hasattr(self, 'manage_widget') and hasattr(self.manage_widget, 'top_toolbar'):
project_state = self.manage_widget.top_toolbar.project_state
for project in project_state.get_all_projects():
project_state.mark_project_saved(project.id)
# Show custom success dialog
from Manage2.project_dialogs import SaveSuccessDialog
success_dialog = SaveSuccessDialog(
file_path,
len(state.get('projects', {})),
False, # Rust.py doesn't use whole_file feature
self
)
success_dialog.exec()
except Exception as e:
print(f"[Rust.py] Error saving layout: {e}")
import traceback
traceback.print_exc()
QMessageBox.critical(self, "Error", f"Failed to save project:\n{e}")
def _open_layout_from_file(self):
"""Load canvas layout AND all Layer menu projects from a .mndoc file"""
try:
from Manage.document_io import SaveLoadManager
from Manage.data_analysis import FunctionNode
save_manager = SaveLoadManager()
# Ask user which file to load
file_path, _ = QFileDialog.getOpenFileName(
self,
"Load A3 Project",
"",
save_manager.suggested_filter()
)
if not file_path or not os.path.exists(file_path):
return
# Ensure Manage panel is visible
self.show_manage_panel()
# Load the document
doc = save_manager.load_from_file(file_path)
# Restore Layer menu projects if present
projects_data = doc.get('projects', {})
if projects_data and hasattr(self, 'manage_widget') and hasattr(self.manage_widget, 'top_toolbar'):
print(f"[Rust.py] Restoring {len(projects_data)} Layer menu projects")
# Get project state manager
project_state = self.manage_widget.top_toolbar.project_state
# Clear existing projects
project_state.projects = {}
# Restore each project
from Manage2.project_state import ProjectData
for project_id_str, project_dict in projects_data.items():
try:
project = ProjectData.from_dict(project_dict)
project_state.projects[project.id] = project
print(f"[Rust.py] ✓ Restored project: {project.name} (ID: {project.id})")
except Exception as e:
print(f"[Rust.py] ERROR restoring project {project_id_str}: {e}")
# Restore next_project_id and active_project_id
if 'next_project_id' in doc:
project_state.next_project_id = doc['next_project_id']
if 'active_project_id' in doc:
project_state.active_project_id = doc['active_project_id']
# Refresh Layer menu
self.manage_widget.top_toolbar.refresh_projects_list()
print(f"[Rust.py] ✓ Restored {len(project_state.projects)} projects to Layer menu")
# Clear canvas first
self.manage_widget.canvas.clear()
# Get nodes from document (current canvas state)
nodes_data = doc.get('nodes', [])
if nodes_data:
print(f"[Rust.py] Loading {len(nodes_data)} nodes from file")
# Recreate nodes from saved data
for idx, node_data in enumerate(nodes_data):
try:
node_name = node_data.get('name', '')
node_x = node_data.get('x', 0.0)
node_y = node_data.get('y', 0.0)
# Create node data dict with all saved fields
data = {
'name': node_name,
'lineno': node_data.get('lineno', 0),
'end_lineno': node_data.get('end_lineno', 0),
'args': [],
'docstring': node_data.get('docstring', ''),
'returns': '',
'complexity': 1,
'file_path': node_data.get('file_path'),
'type': node_data.get('type', 'Function'),
}
# Add source_code if present (for Rust functions)
if node_data.get('source_code'):
data['source_code'] = node_data['source_code']
# Add content_type if present (for text/image/video nodes)
if node_data.get('content_type'):
data['content_type'] = node_data['content_type']
# Also restore text/image/video content
if 'text_content' in node_data:
data['text_content'] = node_data['text_content']
if 'image_path' in node_data:
data['image_path'] = node_data['image_path']
if 'video_path' in node_data:
data['video_path'] = node_data['video_path']
# Create FunctionNode
node = FunctionNode(data, node_x, node_y)
# Restore color and icon if available
if node_data.get('color'):
node.color = node_data['color']
if node_data.get('icon_path'):
node.icon_path = node_data['icon_path']
# Restore is_add_tool flag if present
if node_data.get('is_add_tool'):
node.is_add_tool = True
# Add to canvas
self.manage_widget.canvas.nodes.append(node)
# Index the node
if hasattr(self.manage_widget.canvas, '_index_node'):
self.manage_widget.canvas._index_node(node)
print(f"[Rust.py] ✓ Loaded node: {node.name}")
except Exception as e:
print(f"[Rust.py] ERROR loading node: {e}")
import traceback
traceback.print_exc()
# Apply viewport and other settings
save_manager.apply_to_canvas(self.manage_widget.canvas, doc)
# Update canvas
self.manage_widget.canvas.update()
print(f"[Rust.py] ✓ Loaded {len(self.manage_widget.canvas.nodes)} nodes from: {file_path}")
# Show custom success dialog
num_projects = len(projects_data)
num_nodes = len(nodes_data)
from Manage2.project_dialogs import LoadSuccessDialog
success_dialog = LoadSuccessDialog(
num_projects,
num_nodes,
False, # Rust.py doesn't use whole_file feature
self
)
success_dialog.exec()
except Exception as e:
print(f"[Rust.py] Error loading layout: {e}")
import traceback
traceback.print_exc()
QMessageBox.critical(self, "Error", f"Failed to load project:\n{e}")
# ++++++++++++++++++++++++++ Core Methods ++++++++++++++++++++++++++
def get_current_editor(self):
"""Returns the currently active CodeEditor widget."""
return self.editor_tabs.currentWidget()
def get_editor_for_path(self, file_path):
"""Returns the CodeEditor widget for a given file path."""
return self.open_files.get(file_path)
def close_editor_tab(self, index):
"""Closes an editor tab and removes it from tracking."""
editor = self.editor_tabs.widget(index)
if editor:
self.close_editor_by_widget(editor)
def close_editor_by_widget(self, editor):
"""Closes an editor tab by widget reference and removes it from tracking."""
if not editor:
return
# If closing the Welcome tab, keep its index in sync
if editor == getattr(self, 'welcome_page', None):
self.welcome_tab_index = -1
# Find the tab index for this editor
tab_index = self.editor_tabs.indexOf(editor)
if tab_index == -1:
return
# Clean up tracking dictionaries
if editor in self.closable_tabs_with_buttons:
del self.closable_tabs_with_buttons[editor]
path_to_remove = None
for path, e in self.open_files.items():
if e == editor:
path_to_remove = path
break
if path_to_remove:
del self.open_files[path_to_remove]
# Remove the tab and clean up the editor
self.editor_tabs.removeTab(tab_index)
editor.deleteLater()
# Update toolbar visibility
self.update_editor_toolbar_visibility()
# Persist session after a tab is closed
try:
self.save_session_state()
except Exception:
pass
def close_welcome_tab(self):
"""Closes the welcome tab."""
if self.welcome_tab_index >= 0:
self.editor_tabs.removeTab(self.welcome_tab_index)
self.welcome_tab_index = -1
def on_editor_tab_changed(self, index):
if index > -1:
widget = self.editor_tabs.widget(index)
for path, editor in self.open_files.items():
if editor == widget:
self.current_file_path = path
break
# Attach cursor move listener to update color icon when in Change Color mode
try:
if hasattr(self, '_cursor_move_conn') and self._cursor_move_conn:
prev_editor, slot = self._cursor_move_conn
try:
prev_editor.cursorPositionChanged.disconnect(slot)
except Exception:
pass
self._cursor_move_conn = None
# Only connect if this is a CodeEditor instance
if isinstance(widget, CodeEditor):
# Ensure Change Color click is enabled on this editor if global mode active
try:
widget.enable_change_color_click = bool(getattr(self, 'color_change_mode_active', False))
except Exception:
pass
slot = self.update_color_icon_from_cursor
widget.cursorPositionChanged.connect(slot)
self._cursor_move_conn = (widget, slot)
# Update icon immediately based on current cursor position
self.update_color_icon_from_cursor()
except Exception:
pass
def create_menu(self):
"""Creates the application's menu bar."""
return create_main_menu_bar(self)
def on_file_clicked(self, index):
"""Handles file clicks in the file tree view."""
source_index = self.proxy_model.mapToSource(index)
path = self.file_model.filePath(source_index)
if self.file_model.isDir(source_index):
if self.tree_view.isExpanded(index):
self.tree_view.collapse(index)
else:
self.tree_view.expand(index)
self.file_model.dataChanged.emit(source_index, source_index)
else:
self.file_ops.open_file_for_editing(path)
def on_modification_changed(self, editor, modified):
"""Updates the tab's close button to indicate unsaved changes."""
if editor in self.closable_tabs_with_buttons:
button = self.closable_tabs_with_buttons[editor]
if modified:
button.setText("●")
button.setStyleSheet("border:none; background:transparent; color: white; font-weight: normal; font-size: 16px; padding: 0px 4px;")
else:
button.setText("X")
button.setStyleSheet("border:none; background:transparent; color: #BDC1C6; font-weight: bold; font-size: 14px; padding: 2px 6px;")
@Slot(int)
def on_tab_changed(self, index):
"""Handles tab changes in the preview tab widget."""
try:
current_widget = self.preview_tabs.widget(index)
if current_widget == getattr(self, 'python_console_output', None):
if self.current_file_path and os.path.isfile(self.current_file_path):
self.run_rust_for_current_file(self.current_file_path)
else:
self.statusBar().showMessage(f"Switched to '{self.preview_tabs.tabText(index)}' tab.", 2000)
except Exception:
pass
# ++++++++++++++++++++++++++ Linting ++++++++++++++++++++++++++
def setup_completer_for_editor(self, editor):
"""Sets up the autocompleter for a given editor instance."""
self.menu_actions.setup_completer_for_editor(editor)
# ++++++++++++++++++++++++++ Settings ++++++++++++++++++++++++++
def open_settings_dialog(self):
"""Opens the settings dialog and applies changes if accepted."""
self.settings_manager.open_settings_dialog()
def load_settings(self):
"""Load settings from file."""
self.settings_manager.load_settings()
def save_settings(self):
"""Save settings to file."""
self.settings_manager.save_settings()
def apply_settings(self):
"""Apply the current settings to all open editors and terminals."""
self.settings_manager.apply_settings()
# ++++++++++++++++++++++++++ Session Persistence (open tabs and unsaved buffers) ++++++++++++++++++++++++++
def save_session_state(self):
"""Persist open tabs, active tab, and unsaved buffer contents to session_file."""
try:
open_tabs = []
# Map editor widget -> file path for quick lookup
editor_to_path = {}
try:
for p, e in self.open_files.items():
editor_to_path[e] = p
except Exception:
pass
for i in range(self.editor_tabs.count()):
w = self.editor_tabs.widget(i)
# Skip welcome page if present
if w == getattr(self, 'welcome_page', None):
continue
path = None
try:
path = editor_to_path.get(w)
except Exception:
pass
if not path:
# Only persist file-backed tabs for now
continue
try:
cursor = w.textCursor()
pos = cursor.position()
except Exception:
pos = 0
try:
from PySide6.QtWidgets import QAbstractScrollArea
vs = getattr(w, 'verticalScrollBar', None)
scroll_val = vs().value() if callable(vs) else 0
except Exception:
scroll_val = 0
try:
modified = bool(w.document().isModified())
except Exception:
modified = False
try:
text = w.toPlainText() if modified else None
except Exception:
text = None
open_tabs.append({
'path': path,
'cursor_pos': pos,
'v_scroll': scroll_val,
'modified': modified,
'unsaved_text': text,
})
# Determine active path
active_path = None
try:
active_path = self.current_file_path if self.current_file_path in [t['path'] for t in open_tabs] else None
except Exception:
active_path = None
state = {
'open_tabs': open_tabs,
'active_path': active_path,
}
# Ensure settings dir exists
try:
os.makedirs(self.settings_dir, exist_ok=True)
except Exception:
pass
with open(self.session_file, 'w', encoding='utf-8') as f:
json.dump(state, f, indent=2)
except Exception:
pass
def _apply_restored_states(self, saved_state: dict):
"""Apply saved cursor/scroll/unsaved buffers after tabs have been opened."""
try:
tabs = saved_state.get('open_tabs') or []
# Build path->editor mapping
editor_by_path = {}
try:
for p, e in self.open_files.items():
editor_by_path[p] = e
except Exception:
pass
for t in tabs:
p = t.get('path')
if not p:
continue
ed = editor_by_path.get(p)
if not ed:
continue
try:
if t.get('modified') and t.get('unsaved_text') is not None:
ed.blockSignals(True)
ed.setPlainText(t.get('unsaved_text') or "")
try:
ed.document().setModified(True)
except Exception:
pass
ed.blockSignals(False)
# Update unsaved indicator on tab
try:
self.on_modification_changed(ed, True)
except Exception:
pass
except Exception:
pass
# Restore cursor and scroll
try:
cur = ed.textCursor()
cur.setPosition(int(t.get('cursor_pos') or 0))
ed.setTextCursor(cur)
except Exception:
pass
try:
vs = getattr(ed, 'verticalScrollBar', None)
if callable(vs):
vs().setValue(int(t.get('v_scroll') or 0))
except Exception:
pass
# Restore active tab by path
try:
ap = saved_state.get('active_path')
if ap:
for i in range(self.editor_tabs.count()):
w = self.editor_tabs.widget(i)
if self.open_files.get(ap) == w:
self.editor_tabs.setCurrentIndex(i)
break
except Exception:
pass
except Exception:
pass
def restore_session(self):
"""Restore previously open tabs and unsaved edits at startup."""
try:
if not os.path.exists(self.session_file):
return
with open(self.session_file, 'r', encoding='utf-8') as f:
state = json.load(f)
tabs = state.get('open_tabs') or []
if not tabs:
return
# Open folder root to the first tab's directory for a consistent tree
# BUT ONLY if no initial_root_path was already set from settings
try:
if not self.initial_root_path:
first_path = tabs[0].get('path')
if first_path and os.path.isfile(first_path):
folder = os.path.dirname(first_path)
# Prefer the Cargo project root if this file is inside a Cargo project
try:
cargo_root = self.rust_runner.find_cargo_root(folder)
except Exception:
cargo_root = None
root_to_open = cargo_root or folder
if root_to_open and os.path.isdir(root_to_open):
self.file_ops.open_folder_from_path(root_to_open)
except Exception:
pass
# Open each tab
for t in tabs:
p = t.get('path')
if p and os.path.isfile(p):
try:
self.file_ops.open_file_for_editing(p)
except Exception:
pass
# Apply states after event loop cycles to ensure editors are created
QTimer.singleShot(250, lambda: self._apply_restored_states(state))
# Close welcome if we opened at least one file
try:
if self.editor_tabs.count() > 1:
self.close_welcome_tab()
except Exception:
pass
except Exception:
pass
def show_settings_menu(self):
"""Creates and shows a context menu for the settings button."""
self.settings_manager.show_settings_menu()
def open_file_context_menu(self, position):
"""Opens context menu for file/folder operations (delegated to shared module)."""
show_file_tree_context_menu(self, position)
def refresh_file_tree(self):
"""Refresh the file tree view after file operations."""
try:
# Force the file model to refresh
current_root = self.tree_view.rootIndex()
if current_root.isValid():
source_root = self.proxy_model.mapToSource(current_root)
path = self.file_model.filePath(source_root)
if path:
# Re-set the root to force a refresh
self.file_model.setRootPath(path)
# Update the view
self.tree_view.viewport().update()
except Exception as e:
print(f"Error refreshing file tree: {e}")
def show_welcome_page(self):
"""Shows the welcome page tab."""
if self.welcome_tab_index == -1:
self.welcome_page = WelcomePageWidget(self)
self.welcome_tab_index = self.editor_tabs.addTab(self.welcome_page, "Welcome")
self.editor_tabs.setCurrentIndex(self.welcome_tab_index)
else:
self.editor_tabs.setCurrentIndex(self.welcome_tab_index)
# ++++++++++++++++++++++++++ Delegate Methods to Managers ++++++++++++++++++++++++++
# File operations
def new_window(self):
self.menu_actions.new_window()
def create_new_file(self, base_path=None):
self.file_ops.create_new_file(base_path)
def open_file(self):
self.file_ops.open_file()
def open_folder(self):
self.file_ops.open_folder()
def save_file(self):
self.file_ops.save_file()
def save_as_file(self):
self.file_ops.save_as_file()
def save_all_files(self):
self.file_ops.save_all_files()
def close_current_editor(self):
self.file_ops.close_current_editor()
def close_folder(self):
self.file_ops.close_folder()
def create_cargo_project_here(self):
"""Create a new Cargo binary project in the currently opened folder (tree view root)."""
try:
# Determine the current root folder from the file tree
src_idx = self.proxy_model.mapToSource(self.tree_view.rootIndex())
root_path = self.file_model.filePath(src_idx)
if not root_path or not os.path.isdir(root_path):
QMessageBox.warning(self, "New Cargo Project", "Open a folder first (File > Open Folder...) to choose where to create the Cargo project.")
return
cargo_toml = os.path.join(root_path, "Cargo.toml")
src_dir = os.path.join(root_path, "src")
main_rs = os.path.join(src_dir, "main.rs")
# Prevent accidental overwrite if Cargo.toml already exists
if os.path.exists(cargo_toml):
QMessageBox.information(self, "New Cargo Project", "Cargo.toml already exists in this folder. Aborting to avoid overwriting.")
return
os.makedirs(src_dir, exist_ok=True)
# Derive a package name from the folder name (lowercase, hyphens for invalid chars)
folder_name = os.path.basename(os.path.abspath(root_path)) or "app"
pkg = ''.join(ch if (ch.isalnum() or ch in '-_') else '-' for ch in folder_name.lower())
if pkg and pkg[0].isdigit():
pkg = f"app-{pkg}"
if not pkg:
pkg = "app"
cargo_contents = (
f"[package]\n"
f"name = \"{pkg}\"\n"
f"version = \"0.1.0\"\n"
f"edition = \"2021\"\n\n"
f"[dependencies]\n"
)
main_rs_contents = (
"fn main() {\n"
" println!(\"Hello, world!\");\n"
"}\n"
)
with open(cargo_toml, 'w', encoding='utf-8') as f:
f.write(cargo_contents)
with open(main_rs, 'w', encoding='utf-8') as f: