Compare commits

...
Author SHA1 Message Date
l0ng-ai 31b076dd32 fix(python): keep inheritance inspections on classes an unsaved edit moved
The Python inheritance inspections matched each class of the open document
to the project's inheritance graph by its exact line and column, and the
graph is re-indexed only when a file is saved. A line typed above a class
moved it and every class below off their indexed positions, so their
warnings (abstract method not implemented, redundant override, ...) vanished
until the file was saved. The document's classes now pair with the graph's
in order while they still read the same names, and by position otherwise;
methods and members of classes in the same document come from the live
document.
2026-11-24 19:55:10 +08:00
2 changed files with 69 additions and 9 deletions

No files matched your search

@@ -26,6 +26,10 @@ interface ClassSpec {
name: string;
line: number;
col?: number;
/** The line the *index* saw (defaults to `line`): an unsaved edit has moved the class. */
indexedLine?: number;
/** Typed since the graph was indexed: the graph lacks it. */
unindexed?: boolean;
/** The bases the *index* saw (defaults to `bases`). */
indexedBases?: string[];
bases: string[];
@@ -103,7 +107,14 @@ class Fixture {
bases: spec.bases,
methods,
});
this.graph.push({ path, line: spec.line, col, name: spec.name, bases: spec.indexedBases ?? spec.bases });
if (!spec.unindexed)
this.graph.push({
path,
line: spec.indexedLine ?? spec.line,
col,
name: spec.name,
bases: spec.indexedBases ?? spec.bases,
});
this.outlines.set(path, [...(this.outlines.get(path) ?? []), classItem(spec, col, methods)]);
}
this.facts.set(path, { classes: [...(this.facts.get(path)?.classes ?? []), ...classFacts], suppressions: [] });
@@ -357,6 +368,30 @@ it("skips a class whose header no longer matches the indexed graph", async () =>
expect(await project.run()).toEqual([]);
});
it("keeps checking classes an unsaved edit moved off their indexed lines", async () => {
// A comment typed above `One` moved it and `Two` down a line; the graph still has the saved lines.
const project = new Fixture("/p/a.py").add("/p/a.py", [
{ name: "Base", line: 2, bases: ["ABC"], methods: [{ name: "report", line: 4, abstract: true }] },
{ name: "One", line: 7, indexedLine: 6, bases: ["Base"] },
{ name: "Two", line: 10, indexedLine: 9, bases: ["Base"] },
{ name: "Done", line: 13, indexedLine: 12, bases: ["Base"], methods: [{ name: "report", line: 14 }] },
]);
expect(codes(await project.run())).toEqual([
["builtin:python-abstract-method", "warning", 7],
["builtin:python-abstract-method", "warning", 10],
]);
});
it("pairs moved classes by position only once their names no longer line up", async () => {
// A class was added above `Two` since the graph was indexed: `Two` stays where it was indexed.
const project = new Fixture("/p/a.py").add("/p/a.py", [
{ name: "Base", line: 0, bases: [], methods: [{ name: "report", line: 1, abstract: true }] },
{ name: "New", line: 3, unindexed: true, bases: ["Base"] },
{ name: "Two", line: 6, bases: ["Base"] },
]);
expect(codes(await project.run())).toEqual([["builtin:python-abstract-method", "warning", 6]]);
});
it("runs only the producers that are enabled", async () => {
const project = new Fixture("/p/a.py").add("/p/a.py", [
{ name: "First", line: 0, bases: ["Second"] },
@@ -95,7 +95,30 @@ class ProjectView {
private outlines = new Map<string, Promise<readonly OutlineItem[]>>();
private ancestry = new Map<string, Promise<AncestryResolution>>();
constructor(private readonly deps: InheritanceInspectionDeps) {}
/**
* The graph's declarations of this document paired with its live classes. An unsaved
* edit moves classes off the lines the graph indexed: while the classes still read the
* same names in the same order, they pair up in order; otherwise only by position.
*/
private readonly declOf = new Map<PythonClassFacts, ClassInheritance>();
private readonly liveOf = new Map<string, PythonClassFacts>();
constructor(private readonly deps: InheritanceInspectionDeps) {
const indexed = [...deps.index.byName.values()]
.flat()
.filter((c) => c.path === deps.path)
.sort(byPosition);
const live = [...deps.facts.classes].sort((a, b) => byPosition(a.selection.start, b.selection.start));
const inOrder = indexed.length === live.length && indexed.every((c, i) => c.name === live[i]!.name);
for (const [i, facts] of live.entries()) {
const decl = inOrder
? indexed[i]!
: indexed.find((c) => c.line === facts.selection.start.line && c.col === facts.selection.start.col);
if (!decl) continue;
this.declOf.set(facts, decl);
this.liveOf.set(declarationKey(decl), facts);
}
}
/** The document's classes, in declaration order, skipping ones the graph does not match. */
async classes(): Promise<ClassView[]> {
@@ -126,11 +149,16 @@ class ProjectView {
}
async methodsOf(decl: ClassInheritance): Promise<PythonMethodFacts[]> {
if (decl.path === this.deps.path) return this.liveOf.get(declarationKey(decl))?.methods ?? [];
const facts = await this.factsOf(decl.path);
return facts?.classes.find((c) => sameDeclaration(c, decl))?.methods ?? [];
}
async membersOf(decl: ClassInheritance): Promise<ReadonlySet<string>> {
if (decl.path === this.deps.path) {
const live = this.liveOf.get(declarationKey(decl));
return memberNames(live ? classItem(this.deps.outline, live) : null);
}
const outline = await this.outlineOf(decl.path);
const item = outline.find(
(i) =>
@@ -140,13 +168,7 @@ class ProjectView {
}
private declarationOf(facts: PythonClassFacts): ClassInheritance | null {
const found = this.deps.index.byName.get(facts.name) ?? [];
return (
found.find(
(c) =>
c.path === this.deps.path && c.line === facts.selection.start.line && c.col === facts.selection.start.col,
) ?? null
);
return this.declOf.get(facts) ?? null;
}
private factsOf(path: string): Promise<PythonFacts | null> {
@@ -162,6 +184,9 @@ class ProjectView {
}
}
const byPosition = (a: { line: number; col: number }, b: { line: number; col: number }) =>
a.line - b.line || a.col - b.col;
function memo<T>(cache: Map<string, Promise<T>>, key: string, read: () => Promise<T>): Promise<T> {
let pending = cache.get(key);
if (!pending) {