fix(grep): preserve XLSX cell locators for literal tabs

Keep extracted spreadsheet text unchanged and carry actual cell start offsets into grep headers. Add real workbook GrepTool regressions and plain/empty-cell controls. Focused literal-source extraction/ContentPage replay passes; full GrepTool and required CI remain pending.
This commit is contained in:
Rudy Celekli authored and Xubin Ren committed 2026-10-18 16:40:43 +08:00
1 parent a2f9fae8fe
commit af353fae73
3 files changed
+55 -4

No files matched your search

+6 -1
View File
@@ -3,6 +3,7 @@
from __future__ import annotations
import re
from bisect import bisect_right
from collections import deque
from dataclasses import dataclass, field
from itertools import chain, islice
@@ -43,7 +44,11 @@ def _format_line(line: LocatedDocumentLine, start: int | None, limit: int) -> st
def _format_header(path: str, line: LocatedDocumentLine, start: int) -> str:
locator = line.locator
if locator.startswith("sheet="):
column_index = line.text[:start].count("\t") + 1
column_index = (
bisect_right(line.column_starts, start)
if line.column_starts is not None
else line.text[:start].count("\t") + 1
)
column = ""
while column_index:
column_index, remainder = divmod(column_index - 1, 26)
+10 -3
View File
@@ -87,6 +87,8 @@ class LocatedDocumentLine:
extracted_line: int
locator: str
searchable: bool = True
# XLSX cell offsets in text: a tab inside a cell is not a column separator.
column_starts: tuple[int, ...] | None = None
@dataclass(frozen=True, slots=True)
@@ -257,11 +259,15 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource:
worksheet.reset_dimensions()
wrote_header = False
for row_index, row in enumerate(worksheet.iter_rows(values_only=True), 1):
row_text = "\t".join(
str(cell) if cell is not None else "" for cell in row
)
cells = [str(cell) if cell is not None else "" for cell in row]
row_text = "\t".join(cells)
if not row_text.strip():
continue
column_starts = []
offset = 0
for cell in cells:
column_starts.append(offset)
offset += len(cell) + 1
if not wrote_header:
if wrote_document_content:
extracted_line += 1
@@ -282,6 +288,7 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource:
row_text,
extracted_line,
f"sheet={sheet_name!r},row={row_index}",
column_starts=tuple(column_starts),
)
finally:
workbook.close()