A single indexable unit of code.
| 27 | |
| 28 | @dataclass(frozen=True, slots=True) |
| 29 | class Chunk: |
| 30 | """A single indexable unit of code.""" |
| 31 | |
| 32 | content: str |
| 33 | file_path: str |
| 34 | start_line: int |
| 35 | end_line: int |
| 36 | language: str | None = None |
| 37 | |
| 38 | @property |
| 39 | def location(self) -> str: |
| 40 | """File path and line range as a string.""" |
| 41 | return f"{self.file_path}:{self.start_line}-{self.end_line}" |
| 42 | |
| 43 | def to_dict(self) -> dict[str, Any]: |
| 44 | """Convert the dataclass to a dict.""" |
| 45 | d = asdict(self) |
| 46 | d["location"] = self.location |
| 47 | return d |
| 48 | |
| 49 | @classmethod |
| 50 | def from_dict(cls: type[Chunk], data: dict[str, Any]) -> Chunk: |
| 51 | """Create a Chunk from a dict.""" |
| 52 | data.pop("location", None) |
| 53 | return cls(**data) |
| 54 | |
| 55 | |
| 56 | @dataclass(frozen=True, slots=True) |
no outgoing calls