183 lines
5.8 KiB
Python
183 lines
5.8 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from .document_tree import CODE_DOCUMENT_EXTENSIONS, DocumentEntry
|
|
from .text_io import DEFAULT_SEARCH_INDEX_BYTES, is_within_size_limit, read_text_with_fallback
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SearchMatch:
|
|
entry: DocumentEntry
|
|
snippet: str
|
|
match_count: int = 0
|
|
match_kind: str = "正文"
|
|
|
|
|
|
class SearchIndex:
|
|
def __init__(self, entries: list[DocumentEntry] | None = None) -> None:
|
|
self._records: list[tuple[DocumentEntry, str, str]] = []
|
|
self._body_cache: dict[Path, tuple[int, int, str]] = {}
|
|
if entries:
|
|
self.rebuild(entries)
|
|
|
|
def rebuild(self, entries: list[DocumentEntry]) -> None:
|
|
self._records = [(entry, entry.title, entry.relative_path) for entry in entries]
|
|
current_paths = {entry.path for entry in entries}
|
|
self._body_cache = {path: value for path, value in self._body_cache.items() if path in current_paths}
|
|
|
|
def matches_entry(self, entry: DocumentEntry, query: str) -> bool:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return True
|
|
|
|
for indexed_entry, title, relative_path in self._records:
|
|
if indexed_entry.path == entry.path:
|
|
if normalized in self._haystack(title, relative_path, "").casefold():
|
|
return True
|
|
return normalized in self._body_for(indexed_entry).casefold()
|
|
if normalized in self._haystack(entry.title, entry.relative_path, "").casefold():
|
|
return True
|
|
return normalized in self._body_for(entry).casefold()
|
|
|
|
def search(self, query: str, limit: int = 80) -> list[SearchMatch]:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return []
|
|
|
|
matches: list[SearchMatch] = []
|
|
for entry, title, relative_path in self._records:
|
|
result = _match_details(title, relative_path, "", normalized)
|
|
if result is None:
|
|
result = _match_details(title, relative_path, self._body_for(entry), normalized)
|
|
if result is None:
|
|
continue
|
|
|
|
snippet, match_count, match_kind = result
|
|
matches.append(
|
|
SearchMatch(
|
|
entry=entry,
|
|
snippet=snippet,
|
|
match_count=match_count,
|
|
match_kind=match_kind,
|
|
)
|
|
)
|
|
if len(matches) >= limit:
|
|
break
|
|
return matches
|
|
|
|
def _haystack(self, title: str, relative_path: str, body: str) -> str:
|
|
return f"{title}\n{relative_path}\n{body}"
|
|
|
|
def _body_for(self, entry: DocumentEntry) -> str:
|
|
signature = _file_signature(entry.path)
|
|
if signature is None:
|
|
return ""
|
|
cached = self._body_cache.get(entry.path)
|
|
if cached is not None and cached[:2] == signature:
|
|
return cached[2]
|
|
body = _read_text(entry)
|
|
self._body_cache[entry.path] = (*signature, body)
|
|
return body
|
|
|
|
|
|
def matches_entry(entry: DocumentEntry, query: str) -> bool:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return True
|
|
haystack = f"{entry.title}\n{entry.relative_path}\n{_read_text(entry)}".casefold()
|
|
return normalized in haystack
|
|
|
|
|
|
def search_entries(entries: list[DocumentEntry], query: str, limit: int = 80) -> list[SearchMatch]:
|
|
normalized = query.strip().casefold()
|
|
if not normalized:
|
|
return []
|
|
|
|
matches: list[SearchMatch] = []
|
|
for entry in entries:
|
|
text = _read_text(entry)
|
|
result = _match_details(entry.title, entry.relative_path, text, normalized)
|
|
if result is None:
|
|
continue
|
|
|
|
snippet, match_count, match_kind = result
|
|
matches.append(
|
|
SearchMatch(
|
|
entry=entry,
|
|
snippet=snippet,
|
|
match_count=match_count,
|
|
match_kind=match_kind,
|
|
)
|
|
)
|
|
if len(matches) >= limit:
|
|
break
|
|
return matches
|
|
|
|
|
|
def _read_text(entry: DocumentEntry) -> str:
|
|
supported_text_extensions = {
|
|
".md",
|
|
".markdown",
|
|
".txt",
|
|
".html",
|
|
".htm",
|
|
".xhtml",
|
|
".css",
|
|
".js",
|
|
".json",
|
|
".yaml",
|
|
".yml",
|
|
} | CODE_DOCUMENT_EXTENSIONS
|
|
if entry.path.suffix.lower() not in supported_text_extensions:
|
|
return ""
|
|
if not is_within_size_limit(entry.path, DEFAULT_SEARCH_INDEX_BYTES):
|
|
return ""
|
|
text = read_text_with_fallback(entry.path, max_bytes=DEFAULT_SEARCH_INDEX_BYTES)
|
|
return "" if text.startswith("读取失败:") else text
|
|
|
|
|
|
def _file_signature(path: Path) -> tuple[int, int] | None:
|
|
try:
|
|
stat = path.stat()
|
|
except OSError:
|
|
return None
|
|
return stat.st_mtime_ns, stat.st_size
|
|
|
|
|
|
def _snippet(text: str, index: int, length: int) -> str:
|
|
start = max(0, index - 42)
|
|
end = min(len(text), index + length + 86)
|
|
snippet = text[start:end].replace("\r", " ").replace("\n", " ")
|
|
snippet = " ".join(snippet.split())
|
|
if start > 0:
|
|
snippet = "..." + snippet
|
|
if end < len(text):
|
|
snippet += "..."
|
|
return snippet
|
|
|
|
|
|
def _match_details(title: str, relative_path: str, body: str, normalized_query: str) -> tuple[str, int, str] | None:
|
|
fields = (
|
|
("标题", title),
|
|
("路径", relative_path),
|
|
("正文", body),
|
|
)
|
|
total = sum(_count_matches(text, normalized_query) for _, text in fields)
|
|
if total <= 0:
|
|
return None
|
|
|
|
for kind, text in fields:
|
|
index = text.casefold().find(normalized_query)
|
|
if index >= 0:
|
|
return _snippet(text, index, len(normalized_query)), total, kind
|
|
|
|
return None
|
|
|
|
|
|
def _count_matches(text: str, normalized_query: str) -> int:
|
|
if not normalized_query:
|
|
return 0
|
|
return text.casefold().count(normalized_query)
|