Skip to content

Commit 362e3bc

Browse files
committed
Refactor CodeContextStructure to support multiple requested elements and improve index handling in from_list_of_elements method
1 parent 68f1f1f commit 362e3bc

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

codetide/core/models.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class CodeContextStructure(BaseModel):
254254
classes :Dict[str, ClassDefinition] = Field(default_factory=dict)
255255
class_attributes :Dict[str, ClassAttribute] = Field(default_factory=dict)
256256
class_methods :Dict[str, MethodDefinition] = Field(default_factory=dict)
257-
requested_elemtent :Optional[Union[ImportStatement, VariableDeclaration, FunctionDefinition, ClassDefinition]] = None
257+
requested_elements :Optional[List[Union[ImportStatement, VariableDeclaration, FunctionDefinition, ClassDefinition]]] = Field(default_factory=list)
258258

259259
_cached_elements :Dict[str, Any] = dict()
260260
_unique_class_elements_ids :List[str] = list()
@@ -334,25 +334,39 @@ def as_list_str(self)->List[str]:
334334
for partial in partially_filled_classes.values():
335335
raw_elements_by_file[partial.filepath].append(partial.raw)
336336

337-
if isinstance(self.requested_elemtent, (ClassAttribute, MethodDefinition)):
338-
classObj :ClassDefinition = self._cached_elements.get(self.requested_elemtent.class_id)
339-
self.requested_elemtent.raw = f"{classObj.raw.split('\n')[0]}\n ...\n\n{self.requested_elemtent.raw}"
337+
for requested_elemtent in self.requested_elements:
338+
if isinstance(requested_elemtent, (ClassAttribute, MethodDefinition)):
339+
classObj :ClassDefinition = self._cached_elements.get(requested_elemtent.class_id)
340+
requested_elemtent.raw = f"{classObj.raw.split('\n')[0]}\n ...\n\n{requested_elemtent.raw}"
340341

341342
wrapped_list = [
342343
wrap_content(content="\n\n".join(elements), filepath=filepath)
343344
for filepath, elements in raw_elements_by_file.items()
344-
] + [wrap_content(content=self.requested_elemtent.raw, filepath=self.requested_elemtent.file_path)]
345+
] + [
346+
wrap_content(content=requested_elemtent.raw, filepath=requested_elemtent.file_path)
347+
for requested_elemtent in self.requested_elements
348+
]
345349

346350
return wrapped_list
347351

348352
@classmethod
349-
def from_list_of_elements(cls, elements: list, requested_element_index :int=0) -> 'CodeContextStructure':
353+
def from_list_of_elements(cls, elements: list, requested_element_index :List[int]=[0]) -> 'CodeContextStructure':
350354
instance = cls()
351-
if requested_element_index < 0:
352-
requested_element_index = len(elements) + requested_element_index
355+
# Normalize negative indices to positive
356+
normalized_indices = [
357+
idx if idx >= 0 else len(elements) + idx
358+
for idx in requested_element_index
359+
]
360+
361+
# Optional: Ensure indices are within bounds
362+
normalized_indices = [
363+
idx for idx in normalized_indices
364+
if 0 <= idx < len(elements)
365+
]
366+
353367
for i, element in enumerate(elements):
354-
if i == requested_element_index:
355-
instance.requested_elemtent = element
368+
if i in requested_element_index:
369+
instance.requested_elements.append(element)
356370
elif isinstance(element, ImportStatement):
357371
instance.add_import(element)
358372
elif isinstance(element, ClassDefinition) :
@@ -624,11 +638,14 @@ def _render_class_contents(self, class_def: 'ClassDefinition', prefix: str,
624638

625639
lines.append(f"{prefix}{current_prefix}{name}")
626640

627-
def get(self, unique_id :str, degree :int=0, as_string :bool=False, as_list_str :bool=False)->Union[CodeContextStructure, str, List[str]]:
641+
def get(self, unique_id :Union[str, List[str]], degree :int=0, as_string :bool=False, as_list_str :bool=False)->Union[CodeContextStructure, str, List[str]]:
628642
if not self._cached_elements:
629643
self._build_cached_elements()
630-
631-
references_ids = [unique_id]
644+
645+
if isinstance(unique_id, str):
646+
unique_id = [unique_id]
647+
648+
references_ids = unique_id
632649
retrieved_elements = []
633650
retrieved_ids = []
634651

@@ -652,7 +669,7 @@ def get(self, unique_id :str, degree :int=0, as_string :bool=False, as_list_str
652669

653670
degree -= 1
654671

655-
codeContext = CodeContextStructure.from_list_of_elements(retrieved_elements)
672+
codeContext = CodeContextStructure.from_list_of_elements(retrieved_elements, requested_element_index=[i for i in range(len(unique_id))])
656673
codeContext._cached_elements = self._cached_elements
657674

658675
if as_string:
@@ -667,11 +684,4 @@ def get(self, unique_id :str, degree :int=0, as_string :bool=False, as_list_str
667684
return codeContext.as_list_str()
668685

669686
else:
670-
return codeContext
671-
672-
### TODO implement schema to return each type as string with emphasis in on building partiall class representation with required attributees and imports only
673-
### can create a template for return as string and fill it with imports, class[A+M], functions varaibles
674-
#### actually can extend that template even to normal returns to ensure consitent experienc
675-
### todo if id is class no need to search for references that are classmethods or classattributes with if class_id = self
676-
677-
### TODO for retrueveal / search / embeddings / whatever use a map of raw_content vs id to retrieve the required_id!
687+
return codeContext

0 commit comments

Comments
 (0)