|
50 | 50 | ) |
51 | 51 |
|
52 | 52 |
|
53 | | -def file_path_from_qn(qn: str) -> str | None: |
54 | | - """Last-resort heuristic: derive a file-path-shaped string from a |
55 | | - qualified_name when the graph has no (:File)-[]->(:symbol) edge |
56 | | - for it. |
57 | | -
|
58 | | - Many language indexers emit ``qualified_name = "<file>::<sym>"`` |
59 | | - (Python via the AP indexer does this), in which case splitting on |
60 | | - ``::`` and taking the first segment yields the file path. Other |
61 | | - languages (e.g., Rust ``crate::module::Type::method``) do NOT |
62 | | - encode a file path here — the head segment will be a crate or |
63 | | - module name, not a real path. Callers should prefer the |
64 | | - containment-edge mapping; only use this fallback when no edge |
65 | | - exists, and then validate against the known-files set before |
66 | | - trusting the result. |
| 53 | +def file_path_from_qn(qn: str) -> list[str]: |
| 54 | + """Last-resort heuristic: derive plausible file-path candidates |
| 55 | + from a qualified_name when the graph has no (:File)-[]->(:symbol) |
| 56 | + edge for it. |
| 57 | +
|
| 58 | + Returns a list of candidate paths (zero or more), in priority |
| 59 | + order. Callers MUST validate each candidate against the |
| 60 | + known-files set before trusting it — the qn alone cannot |
| 61 | + distinguish a Python module from a Rust crate path. |
| 62 | +
|
| 63 | + Heuristics applied (priority order): |
| 64 | + 1. ``<path/with/extension>::<sym>`` — head already a file path |
| 65 | + (Python via `<file>::<sym>`, e.g. ``deps/aiofile/aio.py::AIOFile``). |
| 66 | + 2. ``<dotted.module>::<sym>`` — convert dots to slashes and |
| 67 | + append ``.py`` (e.g. ``my.pkg.mod::C`` ⇒ ``my/pkg/mod.py``). |
| 68 | + 3. ``<a::b::c>::<sym>`` — convert ``::`` separators to slashes |
| 69 | + and append ``.py`` (Rust-style module path used by some |
| 70 | + Python indexers, e.g. ``mcp_server::handlers::x::handler`` |
| 71 | + ⇒ ``mcp_server/handlers/x.py`` or ``mcp_server/handlers/x/handler.py``). |
| 72 | + 4. Same as (3) but treating the trailing segment as a method |
| 73 | + on a class — drop the last two segments and use ``.py``. |
| 74 | +
|
| 75 | + Returns an empty list when the qn is empty or has no ``::``. |
67 | 76 | """ |
68 | 77 | if not qn or "::" not in qn: |
69 | | - return None |
| 78 | + return [] |
| 79 | + candidates: list[str] = [] |
| 80 | + code_exts = (".py", ".ts", ".tsx", ".rs", ".js") |
70 | 81 | head = qn.split("::", 1)[0] |
71 | | - return head or None |
| 82 | + head_is_path = bool(head) and ("/" in head or head.endswith(code_exts)) |
| 83 | + head_is_dotted_module = ( |
| 84 | + bool(head) |
| 85 | + and "." in head |
| 86 | + and "/" not in head |
| 87 | + and not head.endswith(code_exts) |
| 88 | + ) |
| 89 | + |
| 90 | + # (1) head already looks like a file path — trust it as-is. |
| 91 | + if head_is_path: |
| 92 | + candidates.append(head) |
| 93 | + return candidates |
| 94 | + |
| 95 | + # (2) dotted-module head (classic Python ``pkg.mod::Sym``). |
| 96 | + if head_is_dotted_module: |
| 97 | + candidates.append(head.replace(".", "/") + ".py") |
| 98 | + return candidates |
| 99 | + |
| 100 | + # (3,4) Rust-style ``a::b::c::sym`` module path. Try progressively |
| 101 | + # shorter prefixes so both ``module::function`` (drop 1) and |
| 102 | + # ``module::Class::method`` (drop 2) resolve. |
| 103 | + parts = qn.split("::") |
| 104 | + for drop in (1, 2, 3): |
| 105 | + if len(parts) - drop < 1: |
| 106 | + break |
| 107 | + prefix = parts[: len(parts) - drop] |
| 108 | + if not prefix: |
| 109 | + continue |
| 110 | + cand = "/".join(prefix) + ".py" |
| 111 | + if cand not in candidates: |
| 112 | + candidates.append(cand) |
| 113 | + |
| 114 | + return candidates |
72 | 115 |
|
73 | 116 |
|
74 | 117 | async def _run_query( |
|
0 commit comments