-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompletion_service.py
More file actions
41 lines (31 loc) 路 1.12 KB
/
Copy pathcompletion_service.py
File metadata and controls
41 lines (31 loc) 路 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Load eqlib symbol hints for POST /completion (MVP)."""
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, List
@lru_cache
def _symbols_path() -> Path:
return Path(__file__).resolve().parent / "data" / "eqlib_symbols.json"
@lru_cache
def _load_symbols() -> List[Dict[str, Any]]:
p = _symbols_path()
if not p.is_file():
return []
return json.loads(p.read_text(encoding="utf-8"))
def suggest(source: str, cursor_line: int, cursor_col: int) -> List[Dict[str, Any]]:
lines = source.splitlines()
if cursor_line < 1 or cursor_line > len(lines):
line = ""
prefix = ""
else:
line = lines[cursor_line - 1]
prefix = line[:cursor_col] if cursor_col <= len(line) else line
# crude token: last word-ish segment
token = prefix.strip().split()[-1] if prefix.strip() else ""
token = token.replace("(", "").replace(".", "")
syms = _load_symbols()
if not token:
return syms[:30]
tlow = token.lower()
return [s for s in syms if tlow in s.get("label", "").lower()][:40]