Skip to content

Commit 3ed6621

Browse files
committed
v1.2.0
1 parent d67aa9f commit 3ed6621

File tree

10 files changed

+455
-242
lines changed

10 files changed

+455
-242
lines changed

ai_code_context_helper/clipboard_operations.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ def copy_to_clipboard(self):
3434
if text:
3535
self.parent.root.clipboard_clear()
3636
self.parent.root.clipboard_append(text)
37-
self.parent.status_var.set(self.parent.texts["status_copied_to_clipboard"])
37+
38+
# 计算总行数
39+
total_lines = text.count("\n") + 1
40+
41+
self.parent.status_var.set(
42+
f"{self.parent.texts['status_copied_to_clipboard']} | 共 {total_lines} 行"
43+
)
3844
else:
3945
self.parent.status_var.set(self.parent.texts["status_no_selection"])
4046

@@ -99,8 +105,15 @@ def copy_path(self):
99105
combined = "\n".join(results)
100106
self.parent.root.clipboard_clear()
101107
self.parent.root.clipboard_append(combined)
108+
109+
# 计算总行数
110+
total_lines = combined.count("\n") + 1
111+
112+
# 使用带行数的状态消息
102113
self.parent.status_var.set(
103-
self.parent.texts["status_paths_copied"].format(count)
114+
self.parent.texts.get("status_paths_copied_with_lines", "已复制 {0} 个路径 | 共 {1} 行").format(
115+
count, total_lines
116+
)
104117
)
105118
else:
106119
self.parent.status_var.set(self.parent.texts["status_no_paths"])
@@ -121,8 +134,15 @@ def code_processor(path_obj):
121134
combined = "\n\n".join(results)
122135
self.parent.root.clipboard_clear()
123136
self.parent.root.clipboard_append(combined)
137+
138+
# 计算总行数
139+
total_lines = combined.count("\n") + 1
140+
141+
# 使用带行数的状态消息
124142
self.parent.status_var.set(
125-
self.parent.texts["status_code_copied"].format(count)
143+
self.parent.texts.get("status_code_copied_with_lines", "已复制 {0} 个文件的代码 | 共 {1} 行").format(
144+
count, total_lines
145+
)
126146
)
127147
else:
128148
self.parent.status_var.set(self.parent.texts["status_no_text_files"])
@@ -145,8 +165,15 @@ def both_processor(path_obj):
145165
combined = "".join(results)
146166
self.parent.root.clipboard_clear()
147167
self.parent.root.clipboard_append(combined)
168+
169+
# 计算总行数
170+
total_lines = combined.count("\n") + 1
171+
172+
# 使用带行数的状态消息
148173
self.parent.status_var.set(
149-
self.parent.texts["status_path_code_copied"].format(count)
174+
self.parent.texts.get("status_path_code_copied_with_lines", "已复制 {0} 个文件的路径和代码 | 共 {1} 行").format(
175+
count, total_lines
176+
)
150177
)
151178
else:
152179
self.parent.status_var.set(self.parent.texts["status_no_text_files"])
@@ -178,8 +205,15 @@ def copy_filename(self):
178205
combined = "\n".join(filenames)
179206
self.parent.root.clipboard_clear()
180207
self.parent.root.clipboard_append(combined)
208+
209+
# 计算总行数
210+
total_lines = combined.count("\n") + 1
211+
212+
# 使用带行数的状态消息
181213
self.parent.status_var.set(
182-
self.parent.texts["status_filenames_copied"].format(len(filenames))
214+
self.parent.texts.get("status_filenames_copied_with_lines", "已复制 {0} 个文件名 | 共 {1} 行").format(
215+
len(filenames), total_lines
216+
)
183217
)
184218
else:
185219
self.parent.status_var.set(self.parent.texts["status_no_selection"])

ai_code_context_helper/code_context_generator.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from ai_code_context_helper.languages import LANGUAGES
2626
from ai_code_context_helper import __version__
2727

28-
# 导入新的模块
2928
from ai_code_context_helper.gui_components import GUIComponents
3029
from ai_code_context_helper.tree_operations import TreeOperations
3130
from ai_code_context_helper.clipboard_operations import ClipboardOperations
@@ -143,8 +142,31 @@ def __init__(self, root):
143142

144143
# 注册全局快捷键Ctrl+2
145144
self._register_global_hotkey()
146-
147-
145+
146+
self._setup_auto_save()
147+
148+
def _setup_auto_save(self):
149+
"""设置自动保存机制 - 每30秒保存所有设置"""
150+
self._auto_save_interval = 30000 # 30秒
151+
self._schedule_auto_save()
152+
153+
def _schedule_auto_save(self):
154+
"""安排下一次自动保存"""
155+
if hasattr(self, "root") and self.root.winfo_exists():
156+
self.root.after(self._auto_save_interval, self._auto_save_task)
157+
158+
def _auto_save_task(self):
159+
"""执行自动保存任务"""
160+
try:
161+
if self.settings.settings_changed:
162+
print("执行自动保存...")
163+
self.settings.save_settings()
164+
except Exception as e:
165+
print(f"自动保存出错: {str(e)}")
166+
finally:
167+
self._schedule_auto_save()
168+
169+
148170
def _setup_initial_directory(self):
149171
"""设置初始目录并安排加载"""
150172
if self.dir_history and len(self.dir_history) > 0:
@@ -156,7 +178,7 @@ def _setup_initial_directory(self):
156178
# 延迟生成树,确保界面初始化完成
157179
self.root.after(300, self._initial_tree_load)
158180
return
159-
181+
160182
# 如果所有历史目录都无效
161183
print("所有历史目录都无效,清空历史")
162184
self.dir_history = []
@@ -295,13 +317,13 @@ def open_terminal(self):
295317
def _initial_tree_load(self):
296318
"""初始化时加载目录树并应用保存的展开状态,不保存当前状态"""
297319
directory = self.dir_path.get().strip()
298-
320+
299321
# 检查目录是否有效
300322
if not directory or not Path(directory).is_dir():
301323
print("初始化加载: 目录无效或为空")
302324
self._initial_loading = False # 标记初始化完成,即使失败了
303325
return False
304-
326+
305327
print(f"===== 初始化加载目录: {directory} =====")
306328
# 设置当前加载的目录
307329
self._current_loaded_directory = directory
@@ -398,6 +420,9 @@ def update_tree(self):
398420
print("===== 更新目录树 =====")
399421
directory = self.dir_path.get().strip()
400422
if directory and Path(directory).is_dir():
423+
# 更新目录树时强制刷新 .gitignore 缓存
424+
from ai_code_context_helper.file_utils import clear_gitignore_cache
425+
clear_gitignore_cache()
401426
# 先保存当前的滚动位置
402427
try:
403428
# 获取当前可见区域的开始和结束位置
@@ -501,22 +526,17 @@ def update_tree(self):
501526
self.status_var.set(self.texts["status_tree_updated"])
502527

503528
def _save_expanded_state(self):
504-
"""保存当前目录树的展开状态"""
529+
"""立即保存展开状态"""
505530
current_dir = self.dir_path.get().strip()
506531
if not current_dir or not Path(current_dir).is_dir():
507532
return
508533

509-
# 标准化路径
510534
current_dir = normalize_path(current_dir)
511535
expanded_items = []
512536

513-
# 收集所有已展开的目录
514537
def collect_expanded_items(parent=""):
515538
for item_id in self.tree.get_children(parent):
516-
# 检查是否为目录并且是展开状态
517539
is_open = self.tree.item(item_id, "open")
518-
519-
# 获取路径
520540
item_path = None
521541
for path, tree_id in self.tree_items.items():
522542
if tree_id == item_id:
@@ -525,32 +545,28 @@ def collect_expanded_items(parent=""):
525545

526546
if item_path and is_open and Path(item_path).is_dir():
527547
try:
528-
# 计算相对路径
529548
rel_path = str(Path(item_path).relative_to(Path(current_dir)))
530549
if rel_path == ".":
531550
expanded_items.append(".")
532551
else:
533552
expanded_items.append(rel_path)
534-
# 只有展开的节点才需要递归处理子项
535553
collect_expanded_items(item_id)
536554
except (ValueError, TypeError):
537555
pass
538556

539-
# 从根节点开始收集
540557
collect_expanded_items()
541-
542-
# 确保至少包含根目录
558+
543559
if "." not in expanded_items:
544560
expanded_items.append(".")
545561

546-
# 用新的展开状态更新设置
547-
print(f"保存目录 '{current_dir}' 的展开状态: {expanded_items}")
548562
self.settings.expanded_states[current_dir] = expanded_items
549563
self.settings.settings_changed = True
564+
565+
# 立即保存关键状态
566+
self.settings.save_settings()
550567

551568
def change_language(self, *args):
552569
"""更改界面语言"""
553-
# 保留这个方法,因为它是核心功能
554570
selected_display_name = self.language_var.get()
555571
selected_language = self.language_names.get(selected_display_name)
556572

@@ -562,13 +578,11 @@ def change_language(self, *args):
562578
self.settings.settings_changed = True
563579
self.gui.update_ui_texts()
564580

565-
# 重新创建系统托盘图标以更新菜单文本
566581
print("语言已更改,正在更新系统托盘菜单...")
567582
self._create_system_tray()
568583

569584
def on_setting_option_changed(self, *args):
570585
"""当设置选项改变时的处理函数"""
571-
# 保留这个方法,因为它是核心功能
572586
self.settings.show_hidden_value = self.show_hidden.get()
573587
self.settings.show_files_value = self.show_files.get()
574588
self.settings.show_folders_value = self.show_folders.get()
@@ -581,10 +595,11 @@ def on_setting_option_changed(self, *args):
581595

582596
directory = self.dir_path.get().strip()
583597
if directory and Path(directory).is_dir():
598+
# 如果是gitignore相关设置变更,强制刷新缓存
584599
if args and self.settings.settings_changed:
585600
from ai_code_context_helper.file_utils import clear_gitignore_cache
586-
587601
clear_gitignore_cache()
602+
588603
self.tree_ops.generate_tree(preserve_state=True)
589604

590605
def on_dir_changed(self, *args):
@@ -631,7 +646,6 @@ def on_dir_changed(self, *args):
631646
# 如果是不同的目录,保存旧目录状态
632647
if current_dir and current_dir != new_directory and Path(current_dir).is_dir():
633648
print(f"保存旧目录 {current_dir} 的状态")
634-
backup_dir = self.dir_path.get()
635649
# 临时将路径设置为旧目录以正确保存状态
636650
self.dir_path.set(current_dir)
637651
self._save_expanded_state()
@@ -767,7 +781,7 @@ def clear_all_history(self):
767781
# 清空目录地址栏
768782
self.dir_path.set("")
769783
self._current_loaded_directory = None
770-
784+
771785
# 重置初始化标记,以便能够重新加载新目录
772786
self._initial_loading = False
773787

@@ -858,15 +872,7 @@ def _create_system_tray(self):
858872
except Exception as e:
859873
print(f"停止现有托盘图标时出错: {e}")
860874

861-
# 创建一个用于系统托盘的图标
862-
icon_path = None
863-
try:
864-
icon_path = Path(__file__).parent / RESOURCES_DIR / ICON_FILENAME
865-
except Exception as e:
866-
print(f"无法加载图标: {str(e)}")
867-
# 如果找不到图标,创建一个简单的图标
868-
icon = self._create_default_icon()
869-
875+
icon_path = Path(__file__).parent / RESOURCES_DIR / ICON_FILENAME
870876
if icon_path and icon_path.exists():
871877
try:
872878
# 使用现有图标
@@ -921,7 +927,6 @@ def _create_default_icon(self, size=64):
921927
image = Image.new("RGBA", (size, size), color=(0, 0, 0, 0))
922928
dc = ImageDraw.Draw(image)
923929

924-
# 画一个简单的图标
925930
dc.rectangle(
926931
[(8, 8), (size - 8, size - 8)],
927932
fill=(45, 156, 219),

ai_code_context_helper/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@
4444
SETTINGS_FILENAME = "default_settings.json"
4545

4646
# 树形视图设置
47-
TREE_COLUMN_WIDTH = 500
47+
TREE_COLUMN_WIDTH = 400
4848
TREE_COLUMN_MIN_WIDTH = 200
4949
CHECK_COLUMN_WIDTH = 50
5050
CHECK_COLUMN_MIN_WIDTH = 50
51+
LINES_COLUMN_WIDTH = 70
52+
LINES_COLUMN_MIN_WIDTH = 50
53+
SIZE_COLUMN_WIDTH = 80
54+
SIZE_COLUMN_MIN_WIDTH = 60
5155
CHECK_MARK = "✓"
5256
TREE_COLUMN_ID = "#0"
5357
CHECK_COLUMN_ID = "checked"
58+
LINES_COLUMN_ID = "lines"
59+
SIZE_COLUMN_ID = "size"
5460

5561
# 链接
5662
CHANGELOG_URL = (

0 commit comments

Comments
 (0)