Compare commits

...
Author SHA1 Message Date
l0ng-ai 3bb0c1f4b7 fix(navigation): Python super methods walk up from the class itself 2026-11-16 18:47:30 +08:00
2 changed files with 40 additions and 24 deletions

No files matched your search

+20 -24
View File
@@ -316,7 +316,7 @@ export async function fillMarkers(
const c = up[i]!;
const self = classOf(c);
if (!self) continue;
const found = resolver ? await superMethodsFrom(resolver, self.name, c.symbol, deps) : [];
const found = resolver ? await superMethodsFrom(resolver, self, c.symbol, deps) : [];
if (!found.length) continue;
const known = supers[i]!;
// Same merge rule as ↓: add the graph's super methods to what the server answered.
@@ -369,41 +369,37 @@ function mergeLocations(first: readonly Location[], more: readonly Location[]):
return [...first, ...more.filter((l) => !first.some((f) => sameDeclarationLine(f, l)))];
}
/** Identifies one class declaration in the inheritance graph. */
/**
* Super methods of `method` declared by a class in `byName`, walking the inheritance
* graph's bases up to a few levels. Empty when no base declares the method.
* Super methods of `method` declared by a base of the class declaration `start`, walking the
* inheritance graph's concrete declarations up to a few levels: a base resolves from the file
* that names it, so an unrelated class of the same name never joins the walk. Empty when no
* base declares the method.
*/
async function superMethodsFrom(
resolver: InheritanceIndex,
start: string,
start: ClassInheritance,
method: string,
deps: MarkerDeps,
depth = 3,
): Promise<{ location: Location; owner: string }[]> {
const bare = method.replace(/\s*[(<].*$/, "");
const seen = new Set<string>([start]);
const key = (c: ClassInheritance) => `${c.path}:${c.line}:${c.col}`;
const seen = new Set<string>([key(start)]);
let level = [start];
for (let d = 0; d < depth && level.length; d++) {
const next: string[] = [];
const next: ClassInheritance[] = [];
const found: { location: Location; owner: string }[] = [];
for (const name of level) {
for (const cls of resolver.byName.get(name) ?? []) {
for (const base of cls.bases) {
if (!seen.add(base)) continue;
// Same rule as the graph: a base the imports name, else a unique one.
const declarations = await resolver.resolve(base, cls.path);
let answered = false;
for (const baseCls of declarations) {
const outline = await deps.supers.outline(baseCls.path).catch(() => [] as OutlineItem[]);
const m = methodInOutline(outline, baseCls, bare);
if (m) {
found.push({ location: { path: baseCls.path, range: m.selection_range }, owner: base });
answered = true;
}
}
// Not found in a base that declares nothing: keep walking its own bases.
if (!answered) next.push(base);
for (const cls of level) {
for (const base of cls.bases) {
// Same rule as the graph: a base the imports name, else a unique one.
for (const baseCls of await resolver.resolve(base, cls.path)) {
if (seen.has(key(baseCls))) continue;
seen.add(key(baseCls));
const outline = await deps.supers.outline(baseCls.path).catch(() => [] as OutlineItem[]);
const m = methodInOutline(outline, baseCls, bare);
if (m) found.push({ location: { path: baseCls.path, range: m.selection_range }, owner: base });
// Not declared there: keep walking that class's own bases.
else next.push(baseCls);
}
}
}
+20
View File
@@ -803,6 +803,26 @@ describe("python inheritance fallback", () => {
expect(cachedMarkers(candidates, ALL, cache).map((m) => [m.line, m.kind])).toEqual([[0, "overridden"]]);
});
it("walks up from the class itself, not every class of its name", async () => {
// a.py: `class BaseA` with `foo`, `class Child(BaseA)` with `foo`; b.py the same with BaseB.
const outline = (base: string) => [
item(base, "class", 0, 2, [item("foo", "method", 1, 1)], 6),
item("Child", "class", 3, 5, [item("foo", "method", 4, 4)], 6),
];
const graph: ClassInheritance[] = [
{ path: "/p/a.py", line: 0, col: 6, name: "BaseA", bases: [] },
{ path: "/p/a.py", line: 3, col: 6, name: "Child", bases: ["BaseA"] },
{ path: "/p/b.py", line: 0, col: 6, name: "BaseB", bases: [] },
{ path: "/p/b.py", line: 3, col: 6, name: "Child", bases: ["BaseB"] },
];
const { deps } = pythonServer(graph, { "/p/a.py": outline("BaseA"), "/p/b.py": outline("BaseB") });
const candidates = markerCandidates(outline("BaseA"), () => "");
const cache = emptyCache();
await fillMarkers("/p/a.py", candidates, ALL, deps(), cache);
const up = cachedMarkers(candidates, ALL, cache).find((m) => m.line === 4 && m.kind === "overriding");
expect(up?.targets.map((t) => [t.path, t.range.start.line])).toEqual([["/p/a.py", 1]]);
});
it("resolves an ambiguous base name through the subclass file's imports", async () => {
// Two modules declare `Config`; `from pkg.b import Config` picks one of them, so
// only b.py's Config is marked as subclassed.