feat(ui): Automatically populate AST slices when adding files to context

This commit is contained in:
2026-05-11 18:49:18 -04:00
parent c5ae21dc85
commit a669f92cab
2 changed files with 166 additions and 1 deletions
+74 -1
View File
@@ -507,6 +507,37 @@ class App:
f.write(data)
# ---------------------------------------------------------------- helpers
def _populate_auto_slices(self, f_item: models.FileItem) -> None:
from src import mcp_client
import re
mcp_client.configure([{"path": f_item.path}])
outline = mcp_client.py_get_code_outline(f_item.path)
if outline.startswith("ERROR") or outline.startswith("ACCESS DENIED"):
return
pattern = re.compile(r'^\s*\[(.*?)\] (.*?) \(Lines (\d+)-(\d+)\)', re.MULTILINE)
try:
with open(f_item.path, "r", encoding="utf-8") as f:
text = f.read()
except Exception:
return
try:
from src.fuzzy_anchor import FuzzyAnchor
except ImportError:
FuzzyAnchor = None
for match in pattern.finditer(outline):
kind, name, s_str, e_str = match.groups()
s_line = int(s_str)
e_line = int(e_str)
if any(s.get('start_line') == s_line and s.get('end_line') == e_line for s in f_item.custom_slices):
continue
if FuzzyAnchor:
slice_data = FuzzyAnchor.create_slice(text, s_line, e_line)
else:
slice_data = {"start_line": s_line, "end_line": e_line}
slice_data['tag'] = 'auto-ast'
slice_data['comment'] = name
f_item.custom_slices.append(slice_data)
def _render_text_viewer(self, label: str, content: str, text_type: str = 'text', force_open: bool = False) -> None:
if imgui.button("[+]##" + str(id(content))) or force_open:
self.text_viewer_type = text_type
@@ -1889,6 +1920,46 @@ class App:
imgui.end_popup()
def _render_add_context_files_modal(self) -> None:
if imgui.begin_popup_modal("Select Context Files", None, imgui.WindowFlags_.always_auto_resize)[0]:
imgui.text("Select files from project to add to context:")
imgui.begin_child("ctx_picker_list", imgui.ImVec2(600, 300), True)
from src import models
# Create a temporary selection set if not initialized
if not hasattr(self, '_ui_picker_selected'):
self._ui_picker_selected = set()
for f in self.files:
fpath = f.path if hasattr(f, 'path') else str(f)
# Skip if already in context
if any((cf.path if hasattr(cf, 'path') else str(cf)) == fpath for cf in self.context_files):
continue
is_sel = fpath in self._ui_picker_selected
clicked, new_sel = imgui.checkbox(f"{fpath}##picker_{fpath}", is_sel)
if clicked:
if new_sel:
self._ui_picker_selected.add(fpath)
else:
self._ui_picker_selected.discard(fpath)
imgui.end_child()
imgui.separator()
if imgui.button("Add Selected", imgui.ImVec2(120, 0)):
for fpath in self._ui_picker_selected:
f_item = models.FileItem(path=fpath)
self.context_files.append(f_item)
self._populate_auto_slices(f_item)
self._ui_picker_selected.clear()
imgui.close_current_popup()
imgui.same_line()
if imgui.button("Cancel", imgui.ImVec2(120, 0)):
if hasattr(self, '_ui_picker_selected'):
self._ui_picker_selected.clear()
imgui.close_current_popup()
imgui.end_popup()
def _render_preset_manager_content(self, is_embedded: bool = False) -> None:
avail = imgui.get_content_region_avail()
if not hasattr(self, "_prompt_md_preview"): self._prompt_md_preview = False
@@ -2953,7 +3024,9 @@ class App:
for f in self.files:
f_path = f.path if hasattr(f, "path") else str(f)
if f_path not in context_paths:
self.context_files.append(copy.deepcopy(f))
f_copy = copy.deepcopy(f)
self.context_files.append(f_copy)
self._populate_auto_slices(f_copy)
imgui.same_line()
if imgui.button("Del##batch"):
new_files = []