-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasegen-gui.py
More file actions
1574 lines (1292 loc) · 64 KB
/
basegen-gui.py
File metadata and controls
1574 lines (1292 loc) · 64 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 pathlib
import fnmatch
import threading
import datetime
from typing import List, Optional, Dict, Set, Any
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext, simpledialog
import pathspec
# Import functionality from basegen.py
from basegen import load_config, load_gitignore_specs, should_include_file, generate_markdown, guess_language
class BaseGenGUI:
def __init__(self, root):
self.root = root
self.root.title("BaseGen - Codebase Documentation Generator")
self.root.geometry("1200x800")
self.root.minsize(800, 600)
# State variables
self.workspace_path = None
self.gitignore_spec = None
self.config_data = load_config()
self.selected_files = set() # Stores paths of files to include
self.excluded_files = set() # Stores paths of files to explicitly exclude
self.output_file = "codebase.md"
self.is_generating = False
# Tree list exclusions
self.tree_exclusions = [
"node_modules",
".git",
".svn",
".hg",
"__pycache__",
".venv",
"venv",
"env",
"dist",
"build",
".cache",
".pytest_cache"
]
self.file_exclusions = [
"*.lock",
"package-lock.json",
"yarn.lock",
"*.pyc",
"*.pyo",
"*.pyd",
"*.so",
"*.dylib",
"*.dll"
]
# Create the main layout
self.create_menu()
self.create_main_layout()
# Set up theme
self.style = ttk.Style()
self.style.configure("Treeview", rowheight=25)
self.style.map('Treeview', background=[('selected', '#3366cc')])
# Initial status
self.update_status("Welcome to BaseGen. Open a workspace to start.")
def create_menu(self):
"""Create the application menu bar"""
menubar = tk.Menu(self.root)
# File menu
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="Open Workspace", command=self.open_workspace)
file_menu.add_command(label="Save Configuration", command=self.save_configuration, state=tk.DISABLED)
file_menu.add_command(label="Manage Tree Exclusions",
command=self.manage_tree_exclusions,
state=tk.DISABLED)
file_menu.add_command(label="Load Configuration", command=self.load_configuration)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.root.quit)
menubar.add_cascade(label="File", menu=file_menu)
# Generation menu
gen_menu = tk.Menu(menubar, tearoff=0)
gen_menu.add_command(label="Generate Markdown", command=self.generate_markdown_wrapper, state=tk.DISABLED)
gen_menu.add_command(label="Set Output File", command=self.set_output_file, state=tk.DISABLED)
menubar.add_cascade(label="Generate", menu=gen_menu)
# Help menu
help_menu = tk.Menu(menubar, tearoff=0)
help_menu.add_command(label="About", command=self.show_about)
help_menu.add_command(label="Documentation", command=self.show_docs)
menubar.add_cascade(label="Help", menu=help_menu)
self.root.config(menu=menubar)
self.file_menu = file_menu
self.gen_menu = gen_menu
def create_main_layout(self):
"""Create the main application layout"""
# Main frame with padding
main_frame = ttk.Frame(self.root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Left panel - File tree
left_frame = ttk.LabelFrame(main_frame, text="Project Files")
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
# Search bar above the tree
search_frame = ttk.Frame(left_frame)
search_frame.pack(fill=tk.X, padx=5, pady=5)
ttk.Label(search_frame, text="Filter:").pack(side=tk.LEFT, padx=2)
self.search_var = tk.StringVar()
self.search_var.trace("w", self.filter_tree)
search_entry = ttk.Entry(search_frame, textvariable=self.search_var)
search_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=2)
# Tree buttons
tree_button_frame = ttk.Frame(left_frame)
tree_button_frame.pack(fill=tk.X, padx=5, pady=5)
ttk.Button(tree_button_frame, text="Select All", command=self.select_all).pack(side=tk.LEFT, padx=2)
ttk.Button(tree_button_frame, text="Deselect All", command=self.deselect_all).pack(side=tk.LEFT, padx=2)
ttk.Button(tree_button_frame, text="Toggle Selected", command=self.toggle_selection).pack(side=tk.LEFT, padx=2)
ttk.Button(tree_button_frame, text="Expand All", command=self.expand_all).pack(side=tk.LEFT, padx=2)
ttk.Button(tree_button_frame, text="Collapse All", command=self.collapse_all).pack(side=tk.LEFT, padx=2)
ttk.Button(tree_button_frame, text="Refresh", command=self.refresh_tree).pack(side=tk.LEFT, padx=2)
# Create file tree with scrollbar
tree_frame = ttk.Frame(left_frame)
tree_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.file_tree = ttk.Treeview(tree_frame, selectmode="browse")
self.file_tree.heading("#0", text="Files", anchor=tk.W)
self.file_tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
tree_scrollbar = ttk.Scrollbar(tree_frame, orient="vertical", command=self.file_tree.yview)
tree_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.file_tree.configure(yscrollcommand=tree_scrollbar.set)
# Handle file tree events
self.file_tree.bind("<Double-1>", self.on_tree_double_click)
self.file_tree.bind("<space>", self.on_tree_space)
self.file_tree.bind("<<TreeviewSelect>>", self.on_tree_select)
# Right panel - Configuration and options
right_frame = ttk.Frame(main_frame, width=400)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, padx=5, pady=5)
right_frame.pack_propagate(False)
# Options panel
options_frame = ttk.LabelFrame(right_frame, text="Options")
options_frame.pack(fill=tk.X, pady=5)
# Checkboxes for options
self.respect_gitignore_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Respect .gitignore rules",
variable=self.respect_gitignore_var,
command=self.toggle_gitignore).pack(anchor=tk.W, padx=10, pady=5)
self.use_hardcoded_excludes_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Use hardcoded exclusions from config",
variable=self.use_hardcoded_excludes_var,
command=self.toggle_hardcoded_excludes).pack(anchor=tk.W, padx=10, pady=5)
self.add_toc_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Add table of contents",
variable=self.add_toc_var,
command=self.update_toc_options).pack(anchor=tk.W, padx=10, pady=5)
self.add_dir_structure_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Add directory structure",
variable=self.add_dir_structure_var,
command=self.update_toc_options).pack(anchor=tk.W, padx=10, pady=5)
self.combined_toc_dir_var = tk.BooleanVar(value=False)
ttk.Checkbutton(options_frame, text="Combined TOC/DIR structure",
variable=self.combined_toc_dir_var,
command=self.update_toc_options).pack(anchor=tk.W, padx=10, pady=5)
self.compact_tree_var = tk.BooleanVar(value=False)
ttk.Checkbutton(options_frame, text="Compact tree view (omit empty dirs)",
variable=self.compact_tree_var).pack(anchor=tk.W, padx=10, pady=5)
self.add_file_stats_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Add file statistics (lines, size)",
variable=self.add_file_stats_var).pack(anchor=tk.W, padx=10, pady=5)
# Exclusion patterns frame
exclusion_frame = ttk.LabelFrame(right_frame, text="Exclusion Patterns")
exclusion_frame.pack(fill=tk.BOTH, expand=True, pady=5)
# Exclusion pattern list with add/remove buttons
button_frame = ttk.Frame(exclusion_frame)
button_frame.pack(fill=tk.X, padx=5, pady=5)
ttk.Button(button_frame, text="Add Pattern", command=self.add_exclude_pattern).pack(side=tk.LEFT, padx=2)
ttk.Button(button_frame, text="Remove Selected", command=self.remove_exclude_pattern).pack(side=tk.LEFT, padx=2)
# Listbox for exclusion patterns with scrollbar
patterns_frame = ttk.Frame(exclusion_frame)
patterns_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.exclusion_patterns = tk.Listbox(patterns_frame)
self.exclusion_patterns.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
patterns_scrollbar = ttk.Scrollbar(patterns_frame, orient="vertical", command=self.exclusion_patterns.yview)
patterns_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.exclusion_patterns.configure(yscrollcommand=patterns_scrollbar.set)
# Status bar at the bottom
self.statusbar = ttk.Label(self.root, text="Ready", relief=tk.SUNKEN, anchor=tk.W)
self.statusbar.pack(side=tk.BOTTOM, fill=tk.X)
# Progress bar
self.progress = ttk.Progressbar(self.root, orient=tk.HORIZONTAL, mode='indeterminate')
self.progress.pack(side=tk.BOTTOM, fill=tk.X, before=self.statusbar)
self.progress.pack_forget() # Hide initially
def on_tree_select(self, event):
"""Handle tree item selection"""
# This is a placeholder for future functionality
# Such as showing file details in a side panel
pass
def open_workspace(self):
"""Open a workspace directory"""
directory = filedialog.askdirectory(title="Select Workspace Directory")
if directory:
self.workspace_path = pathlib.Path(directory)
self.update_status(f"Workspace: {self.workspace_path}")
# Enable menu items
self.file_menu.entryconfig("Save Configuration", state=tk.NORMAL)
self.file_menu.entryconfig("Manage Tree Exclusions", state=tk.NORMAL) # Enable the exclusions menu
self.gen_menu.entryconfig("Generate Markdown", state=tk.NORMAL)
self.gen_menu.entryconfig("Set Output File", state=tk.NORMAL)
# Set default output file path
self.output_file = os.path.join(directory, "codebase.md")
# Load gitignore if present and requested
if self.respect_gitignore_var.get():
self.gitignore_spec = load_gitignore_specs(self.workspace_path)
else:
self.gitignore_spec = None
# Populate the tree
self.populate_file_tree()
# Load exclusion patterns from config
self.load_exclusion_patterns()
def load_exclusion_patterns(self):
"""Load exclusion patterns from config into the listbox"""
self.exclusion_patterns.delete(0, tk.END)
if self.use_hardcoded_excludes_var.get() and "HARD_CODED_EXCLUDES" in self.config_data:
for pattern in self.config_data["HARD_CODED_EXCLUDES"]:
self.exclusion_patterns.insert(tk.END, pattern)
def toggle_gitignore(self):
"""Toggle respecting gitignore rules"""
if self.workspace_path:
if self.respect_gitignore_var.get():
self.gitignore_spec = load_gitignore_specs(self.workspace_path)
else:
self.gitignore_spec = None
# Refresh the tree
self.populate_file_tree()
def toggle_hardcoded_excludes(self):
"""Toggle using hardcoded exclusions from config"""
self.load_exclusion_patterns()
def set_output_file(self):
"""Set the output Markdown file path"""
file_path = filedialog.asksaveasfilename(
defaultextension=".md",
filetypes=[("Markdown files", "*.md"), ("All files", "*.*")],
initialdir=self.workspace_path,
initialfile="codebase.md",
title="Save Markdown As"
)
if file_path:
self.output_file = file_path
self.update_status(f"Output file set to: {self.output_file}")
def update_toc_options(self):
"""Update TOC and directory structure options based on combined option"""
if self.combined_toc_dir_var.get():
# Disable individual options when combined option is selected
self.add_dir_structure_var.set(True)
self.add_toc_var.set(True)
# Find the checkbuttons and disable them
for child in self.root.winfo_children():
self._disable_checkbuttons_recursive(child, ["Add directory structure", "Add table of contents"])
else:
# Re-enable individual options
for child in self.root.winfo_children():
self._enable_checkbuttons_recursive(child, ["Add directory structure", "Add table of contents"])
def _disable_checkbuttons_recursive(self, widget, text_list):
"""Recursively find and disable checkbuttons with specific text"""
if isinstance(widget, ttk.Checkbutton):
# Get the text option from the checkbutton if possible
try:
text = widget.cget("text")
if text in text_list:
widget.configure(state="disabled")
except:
pass
# Process children
try:
for child in widget.winfo_children():
self._disable_checkbuttons_recursive(child, text_list)
except:
pass
def _enable_checkbuttons_recursive(self, widget, text_list):
"""Recursively find and enable checkbuttons with specific text"""
if isinstance(widget, ttk.Checkbutton):
# Get the text option from the checkbutton if possible
try:
text = widget.cget("text")
if text in text_list:
widget.configure(state="normal")
except:
pass
# Process children
try:
for child in widget.winfo_children():
self._enable_checkbuttons_recursive(child, text_list)
except:
pass
def save_configuration(self):
"""Save the current configuration to a file"""
# Collect patterns from the listbox
patterns = list(self.exclusion_patterns.get(0, tk.END))
config = {
"workspace": str(self.workspace_path),
"output_file": self.output_file,
"respect_gitignore": self.respect_gitignore_var.get(),
"use_hardcoded_excludes": self.use_hardcoded_excludes_var.get(),
"add_toc": self.add_toc_var.get(),
"add_dir_structure": self.add_dir_structure_var.get(), # New option
"combined_toc_dir": self.combined_toc_dir_var.get(), # New option
"compact_tree": self.compact_tree_var.get(),
"add_file_stats": self.add_file_stats_var.get(),
"exclusion_patterns": patterns,
"selected_files": list(self.selected_files),
"excluded_files": list(self.excluded_files),
"tree_exclusions": self.tree_exclusions,
"file_exclusions": self.file_exclusions
}
file_path = filedialog.asksaveasfilename(
defaultextension=".json",
filetypes=[("JSON files", "*.json"), ("All files", "*.*")],
initialdir=self.workspace_path,
initialfile="basegen_config.json",
title="Save Configuration As"
)
if file_path:
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2)
self.update_status(f"Configuration saved to: {file_path}")
except Exception as e:
messagebox.showerror("Error", f"Could not save configuration: {e}")
def load_configuration(self):
"""Load configuration from a file"""
file_path = filedialog.askopenfilename(
defaultextension=".json",
filetypes=[("JSON files", "*.json"), ("All files", "*.*")],
initialdir=self.workspace_path if self.workspace_path else None,
title="Load Configuration"
)
if file_path:
try:
with open(file_path, 'r', encoding='utf-8') as f:
config = json.load(f)
# Load workspace if specified
if "workspace" in config:
workspace_path = pathlib.Path(config["workspace"])
if workspace_path.exists():
self.workspace_path = workspace_path
# Enable menu items
self.file_menu.entryconfig("Save Configuration", state=tk.NORMAL)
self.file_menu.entryconfig("Manage Tree Exclusions", state=tk.NORMAL)
self.gen_menu.entryconfig("Generate Markdown", state=tk.NORMAL)
self.gen_menu.entryconfig("Set Output File", state=tk.NORMAL)
else:
messagebox.showwarning("Warning", f"Workspace path '{workspace_path}' does not exist.")
# Load output file
if "output_file" in config:
self.output_file = config["output_file"]
# Load options
if "respect_gitignore" in config:
self.respect_gitignore_var.set(config["respect_gitignore"])
if "use_hardcoded_excludes" in config:
self.use_hardcoded_excludes_var.set(config["use_hardcoded_excludes"])
if "add_toc" in config:
self.add_toc_var.set(config["add_toc"])
# Handle new options
if "add_dir_structure" in config:
self.add_dir_structure_var.set(config["add_dir_structure"])
if "combined_toc_dir" in config:
self.combined_toc_dir_var.set(config["combined_toc_dir"])
# For backward compatibility with old config files
# that might have the removed 'add_navigation' option
# We simply ignore it
if "compact_tree" in config:
self.compact_tree_var.set(config["compact_tree"])
if "add_file_stats" in config:
self.add_file_stats_var.set(config["add_file_stats"])
# Update UI state based on combined option
self.update_toc_options()
# Load exclusion patterns
if "exclusion_patterns" in config:
self.exclusion_patterns.delete(0, tk.END)
for pattern in config["exclusion_patterns"]:
self.exclusion_patterns.insert(tk.END, pattern)
# Load tree and file exclusions
if "tree_exclusions" in config:
self.tree_exclusions = config["tree_exclusions"]
if "file_exclusions" in config:
self.file_exclusions = config["file_exclusions"]
# Load selected and excluded files if we have a workspace
if self.workspace_path:
# Load gitignore if needed
if self.respect_gitignore_var.get():
self.gitignore_spec = load_gitignore_specs(self.workspace_path)
else:
self.gitignore_spec = None
# Populate the tree
self.populate_file_tree()
# Restore selection state
if "selected_files" in config:
self.selected_files = set(config["selected_files"])
if "excluded_files" in config:
self.excluded_files = set(config["excluded_files"])
# Update UI to reflect selections
self._update_tree_selections()
self.update_status(f"Configuration loaded from: {file_path}")
except Exception as e:
messagebox.showerror("Error", f"Could not load configuration: {e}")
# Add method to update tree selections based on loaded configuration
def _update_tree_selections(self):
"""Update tree item selections based on loaded configuration"""
def update_item(item_id):
values = self.file_tree.item(item_id, "values")
if not values:
return
path = values[1]
if path in self.selected_files:
self.file_tree.item(item_id, values=(values[0], path, "checked"))
self.file_tree.item(item_id, tags=("checked",))
elif path in self.excluded_files:
self.file_tree.item(item_id, values=(values[0], path, "unchecked"))
self.file_tree.item(item_id, tags=("unchecked",))
# Process children
for child_id in self.file_tree.get_children(item_id):
update_item(child_id)
# Start with root items
for item_id in self.file_tree.get_children():
update_item(item_id)
def generate_markdown_wrapper(self):
"""Wrapper for generate_markdown to run in a thread"""
if self.is_generating:
messagebox.showinfo("Generation in Progress", "Markdown generation is already running.")
return
# Start the generation thread
self.is_generating = True
self.progress.pack(before=self.statusbar)
self.progress.start()
self.update_status("Generating Markdown...")
threading.Thread(target=self._generate_markdown_thread, daemon=True).start()
def _generate_markdown_thread(self):
"""Thread worker for Markdown generation"""
try:
# Instead of generating include patterns, we'll directly pass
# the selected and excluded file paths to a modified version of
# should_include_file
selected_paths = self.selected_files
excluded_paths = self.excluded_files
# Get patterns from exclusion listbox for additional exclusions
exclude_patterns = list(self.exclusion_patterns.get(0, tk.END))
# Determine options for enhanced markdown generation
add_toc = self.add_toc_var.get()
add_dir_structure = self.add_dir_structure_var.get()
combined_toc_dir = self.combined_toc_dir_var.get()
compact_tree = self.compact_tree_var.get()
add_file_stats = self.add_file_stats_var.get()
# Custom extension to generate_markdown with additional features
self._enhanced_generate_markdown(
self.workspace_path,
self.output_file,
selected_paths, # Pass selected paths directly
excluded_paths, # Pass excluded paths directly
exclude_patterns, # Pass additional exclude patterns
self.gitignore_spec,
add_toc,
add_dir_structure,
combined_toc_dir,
compact_tree,
add_file_stats
)
# Update UI in the main thread
self.root.after(0, lambda: self.update_status(f"Markdown generated: {self.output_file}"))
self.root.after(0, self._finish_generation)
except Exception as e:
error_msg = f"Error generating Markdown: {e}"
self.root.after(0, lambda: messagebox.showerror("Error", error_msg))
self.root.after(0, lambda: self.update_status(error_msg))
self.root.after(0, self._finish_generation)
def _finish_generation(self):
"""Finish the generation process"""
self.progress.stop()
self.progress.pack_forget()
self.is_generating = False
# Ask if the user wants to open the file
if messagebox.askyesno("Generation Complete",
f"Markdown file has been generated at:\n{self.output_file}\n\nWould you like to open it?"):
self._open_file(self.output_file)
def _open_file(self, path):
"""Open a file with the default system application"""
try:
import subprocess
if os.name == 'nt': # Windows
os.startfile(path)
elif os.name == 'posix': # macOS, Linux
if sys.platform == 'darwin': # macOS
subprocess.call(('open', path))
else: # Linux
subprocess.call(('xdg-open', path))
except Exception as e:
messagebox.showerror("Error", f"Could not open file: {e}")
def _enhanced_generate_markdown(
self,
root_path: pathlib.Path,
output_file: str,
selected_paths: Set[str],
excluded_paths: Set[str],
exclude_patterns: Optional[List[str]] = None,
gitignore_spec: Optional[pathspec.PathSpec] = None,
add_toc: bool = True,
add_dir_structure: bool = True,
combined_toc_dir: bool = False,
compact_tree: bool = False,
add_file_stats: bool = False
) -> None:
"""
Enhanced version of generate_markdown with additional features for AI consumption:
- Optional table of contents with anchor links
- Optional directory structure representation
- Combined TOC and directory structure
- Compact tree view (omitting empty directories)
- File statistics (lines of code, file size)
"""
base = root_path.parent
included_files = []
try:
for file in sorted(root_path.rglob("*")):
if file.is_file() and self._should_include_file(
file,
root_path,
selected_paths,
excluded_paths,
exclude_patterns,
gitignore_spec
):
try:
rel_file = file.relative_to(base)
except ValueError:
rel_file = file
included_files.append(rel_file)
except Exception as e:
raise RuntimeError(f"Error scanning directory '{root_path}': {e}")
if not included_files:
raise ValueError("No files found matching the criteria.")
# Build tree with optional compaction
if compact_tree:
tree_dict = self._build_compact_tree(included_files)
else:
tree_dict = self._build_tree(included_files)
tree_lines = self._format_tree(tree_dict)
tree_str = "\n".join(tree_lines)
md_lines = []
md_lines.append(f"# Codebase: {root_path.name}")
md_lines.append("")
# Add metadata for AI consumption
md_lines.append("## Metadata")
md_lines.append("")
md_lines.append(f"- **Generated on:** {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
md_lines.append(f"- **Files included:** {len(included_files)}")
if add_file_stats:
total_loc = 0
total_size = 0
for rel_path in included_files:
file_path = base / rel_path
if file_path.exists():
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
total_loc += len(lines)
except UnicodeDecodeError:
# Skip binary files for LOC counting
pass
except Exception:
# Skip files with errors
pass
total_size += file_path.stat().st_size
md_lines.append(f"- **Total lines of code:** {total_loc:,}")
md_lines.append(f"- **Total size:** {self._format_size(total_size)}")
md_lines.append("")
# Add combined TOC and directory structure
if combined_toc_dir:
md_lines.append("## Project Structure")
md_lines.append("")
md_lines.append("```")
# Generate a linked version of the tree
linked_tree_lines = self._format_linked_tree(tree_dict, "", included_files)
md_lines.append("\n".join(linked_tree_lines))
md_lines.append("```")
md_lines.append("")
else:
# Add table of contents with anchor links
if add_toc:
md_lines.append("## Table of Contents")
md_lines.append("")
if add_dir_structure:
md_lines.append("1. [Directory Structure](#directory-structure)")
md_lines.append(f"{1 if not add_dir_structure else 2}. [Files](#files)")
for i, rel_path in enumerate(included_files):
# Create an anchor-friendly ID
anchor = f"file-{i+1}"
md_lines.append(f" - [{rel_path}](#{anchor})")
md_lines.append("")
# Add directory structure as a separate section
if add_dir_structure:
md_lines.append("## Directory Structure")
md_lines.append("")
md_lines.append("```")
md_lines.append(tree_str)
md_lines.append("```")
md_lines.append("")
md_lines.append("## Files")
md_lines.append("")
for i, rel_path in enumerate(included_files):
# Create an anchor-friendly ID
anchor = f"file-{i+1}"
md_lines.append(f"### {rel_path} <a id='{anchor}'></a>")
md_lines.append("")
if add_file_stats:
file_path = base / rel_path
if file_path.exists():
file_size = file_path.stat().st_size
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = len(f.readlines())
md_lines.append(f"- Lines: {lines}")
except UnicodeDecodeError:
md_lines.append("- Binary file")
except Exception as e:
md_lines.append(f"- Error reading file: {e}")
md_lines.append(f"- Size: {self._format_size(file_size)}")
md_lines.append("")
file_path = base / rel_path
ext = file_path.suffix
language = guess_language(ext)
md_lines.append(f"```{language}")
try:
content = file_path.read_text(encoding="utf-8")
except Exception as e:
content = f"Error reading file: {e}"
md_lines.append(content)
md_lines.append("```")
# Always add navigation links for files
md_lines.append("")
if combined_toc_dir:
md_lines.append("<div style='text-align: right;'><a href='#project-structure'>↑ Back to Project Structure</a></div>")
elif add_dir_structure:
md_lines.append("<div style='text-align: right;'><a href='#directory-structure'>↑ Back to Directory Structure</a></div>")
elif add_toc:
md_lines.append("<div style='text-align: right;'><a href='#table-of-contents'>↑ Back to Table of Contents</a></div>")
md_lines.append("")
try:
with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(md_lines))
except Exception as e:
raise RuntimeError(f"Error writing to output file '{output_file}': {e}")
def _format_linked_tree(self, tree, indent, included_files):
"""Format the tree dictionary into a list of strings with linked filenames"""
lines = []
for key in sorted(tree.keys()):
if tree[key]: # Directory
lines.append(f"{indent}{key}/")
lines.extend(self._format_linked_tree(tree[key], indent + " ", included_files))
else: # File
# Find the index of this file in included_files
file_path = pathlib.Path(key)
file_index = None
for i, included_file in enumerate(included_files):
if included_file.name == file_path.name:
# Check if paths match (considering directory structure)
if str(included_file).endswith(str(file_path)):
file_index = i + 1
break
if file_index is not None:
lines.append(f"{indent}[{key}](#file-{file_index})")
else:
lines.append(f"{indent}{key}")
return lines
def _should_include_file(
self,
file: pathlib.Path,
root: pathlib.Path,
selected_paths: Set[str],
excluded_paths: Set[str],
exclude_patterns: Optional[List[str]] = None,
gitignore_spec: Optional[pathspec.PathSpec] = None,
) -> bool:
"""
Decide whether a file should be included based on:
1. If any parent directory is explicitly excluded
2. If the file itself is explicitly excluded
3. If the file or any parent directory is not in selected_paths
4. Gitignore rules (if provided)
5. Exclude glob patterns
"""
file_str = str(file)
# Check if this file or any of its parent directories are explicitly excluded
current = file
while current != root:
if str(current) in excluded_paths:
return False
current = current.parent
# Check if this file (or its parent directory) is specifically selected
is_selected = False
if file_str in selected_paths:
is_selected = True
else:
# Check if any parent directory is selected
current = file.parent
while current != root:
if str(current) in selected_paths:
is_selected = True
break
current = current.parent
# If nothing in the path is explicitly selected, exclude it
if not is_selected and str(root) not in selected_paths:
return False
# Check gitignore rules
try:
rel = file.relative_to(root)
except ValueError:
rel = file
rel_str = str(rel).replace(os.sep, "/")
if gitignore_spec and gitignore_spec.match_file(rel_str):
return False
# Check exclude patterns
if exclude_patterns:
if any(fnmatch.fnmatch(rel_str, pattern) for pattern in exclude_patterns):
return False
return True
def _build_tree(self, paths: List[pathlib.Path]) -> dict:
"""Build a nested dictionary representing a directory tree"""
tree = {}
for path in paths:
parts = path.parts
current = tree
for part in parts:
if part not in current:
current[part] = {}
current = current[part]
return tree
def _build_compact_tree(self, paths: List[pathlib.Path]) -> dict:
"""
Build a compact nested dictionary representing a directory tree,
collapsing directories with only one child.
"""
# First, build the full tree
tree = self._build_tree(paths)
# Then compact it
return self._compact_tree_node(tree)
def _compact_tree_node(self, node: dict) -> dict:
"""Recursively compact a tree node"""
# First, compact all children
for key, child in list(node.items()):
if child: # If not a leaf
node[key] = self._compact_tree_node(child)
# If this node has exactly one child and it's a directory, combine them
if len(node) == 1:
key = list(node.keys())[0]
child = node[key]
# Only combine if the child is a directory
if child:
new_node = {}
for child_key, child_value in child.items():
new_key = f"{key}/{child_key}"
new_node[new_key] = child_value
return new_node
return node
def _format_tree(self, tree: dict, indent: str = "") -> List[str]:
"""Format the tree dictionary into a list of strings"""
lines = []
for key in sorted(tree.keys()):
if tree[key]:
lines.append(f"{indent}{key} >")
lines.extend(self._format_tree(tree[key], indent + " "))
else:
lines.append(f"{indent}{key}")
return lines
def _format_size(self, size_bytes: int) -> str:
"""Format file size in a human-readable format"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024.0:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.2f} TB"
def populate_file_tree(self):
"""Populate the file tree with the workspace directory structure"""
# Clear the tree
for item in self.file_tree.get_children():
self.file_tree.delete(item)
# Clear selections
self.selected_files = set()
self.excluded_files = set()
# Start progress bar
self.progress.pack(before=self.statusbar)
self.progress.start()
self.update_status("Loading file tree...")
# Use a thread to avoid UI freezing
threading.Thread(target=self._populate_tree_thread, daemon=True).start()
def _populate_tree_thread(self):
"""Thread worker for populating the tree"""
try:
# Create root node
root_id = self.file_tree.insert("", "end", text=self.workspace_path.name, open=True,
values=("directory", str(self.workspace_path), "checked"))
self.file_tree.item(root_id, tags=("checked",))
# Track which directories we've added
added_dirs = {self.workspace_path: root_id}
# Add all files to the tree (with performance improvements)
for path in sorted(self.workspace_path.rglob("*")):
try:
# Skip excluded directories for better performance
if any(excluded_dir in path.parts for excluded_dir in self.tree_exclusions):
continue
# Skip excluded file patterns for better performance
if path.is_file() and any(fnmatch.fnmatch(path.name, pattern) for pattern in self.file_exclusions):
continue
# Get relative path for checking against gitignore
rel_path = path.relative_to(self.workspace_path)
rel_path_str = str(rel_path).replace(os.sep, "/")
# Check if it should be included
is_excluded = False
if self.gitignore_spec and self.respect_gitignore_var.get():
is_excluded = self.gitignore_spec.match_file(rel_path_str)
# For directories, we need to ensure the parent path exists in the tree
parent_path = path.parent
if parent_path not in added_dirs:
# Need to build the path
self._build_parent_path(parent_path, added_dirs)
parent_id = added_dirs[parent_path]
# Determine the item properties
if path.is_dir():
# Create directory item
node_id = self.file_tree.insert(parent_id, "end", text=path.name,
values=("directory", str(path), "checked" if not is_excluded else "unchecked"))
# Track this directory
added_dirs[path] = node_id
# Add tags for styling
if is_excluded:
self.file_tree.item(node_id, tags=("unchecked",))
self.excluded_files.add(str(path))
else:
self.file_tree.item(node_id, tags=("checked",))
self.selected_files.add(str(path))
else:
# Create file item
node_id = self.file_tree.insert(parent_id, "end", text=path.name,
values=("file", str(path), "checked" if not is_excluded else "unchecked"))
# Add tags for styling
if is_excluded:
self.file_tree.item(node_id, tags=("unchecked",))
self.excluded_files.add(str(path))