Initial upload: TermDiagram v0.1.0
This commit is contained in:
55
src/termdiagram/ui/fuzzy_search.py
Normal file
55
src/termdiagram/ui/fuzzy_search.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
from typing import List, Optional
|
||||||
|
from rapidfuzz import fuzz
|
||||||
|
from ..models import Module, ClassSymbol, FunctionSymbol, SymbolType
|
||||||
|
|
||||||
|
|
||||||
|
class SearchResult:
|
||||||
|
def __init__(self, name: str, symbol_type: str, path: str, line_number: int, score: float):
|
||||||
|
self.name = name
|
||||||
|
self.symbol_type = symbol_type
|
||||||
|
self.path = path
|
||||||
|
self.line_number = line_number
|
||||||
|
self.score = score
|
||||||
|
|
||||||
|
|
||||||
|
class FuzzySearcher:
|
||||||
|
def __init__(self):
|
||||||
|
self.symbols: List = []
|
||||||
|
|
||||||
|
def build_index(self, symbols: List):
|
||||||
|
self.symbols = symbols
|
||||||
|
|
||||||
|
def search(self, query: str, limit: int = 10) -> List[SearchResult]:
|
||||||
|
results = []
|
||||||
|
|
||||||
|
for symbol in self.symbols:
|
||||||
|
score = fuzz.ratio(query.lower(), symbol.name.lower())
|
||||||
|
if score > 50:
|
||||||
|
symbol_type = self._get_symbol_type(symbol)
|
||||||
|
path = getattr(symbol, "path", "")
|
||||||
|
line_number = getattr(symbol, "line_number", 0)
|
||||||
|
results.append(
|
||||||
|
SearchResult(
|
||||||
|
name=symbol.name,
|
||||||
|
symbol_type=symbol_type,
|
||||||
|
path=path,
|
||||||
|
line_number=line_number,
|
||||||
|
score=score,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
results.sort(key=lambda x: x.score, reverse=True)
|
||||||
|
return results[:limit]
|
||||||
|
|
||||||
|
def _get_symbol_type(self, symbol) -> str:
|
||||||
|
if isinstance(symbol, Module):
|
||||||
|
return "module"
|
||||||
|
elif isinstance(symbol, ClassSymbol):
|
||||||
|
return "class"
|
||||||
|
elif isinstance(symbol, FunctionSymbol):
|
||||||
|
return "function"
|
||||||
|
else:
|
||||||
|
return "unknown"
|
||||||
|
|
||||||
|
def get_symbol_count(self) -> int:
|
||||||
|
return len(self.symbols)
|
||||||
Reference in New Issue
Block a user