Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat(around): report a class's declarations, not its type references

KageBinary2b6af6b57645
diff --git a/ix-cli/src/cli/__tests__/around.test.ts b/ix-cli/src/cli/__tests__/around.test.ts
index 873f7bfa..16b22022 100644
--- a/ix-cli/src/cli/__tests__/around.test.ts
+++ b/ix-cli/src/cli/__tests__/around.test.ts
@@ -11,7 +11,7 @@ vi.mock("../resolve.js", () => ({
}));
import {
- DEFAULT_CAPS, estimateTokens, findUseInImporter, fitToBudget, gatherAround, importStatementText, isTestFile,
+ CONTAINER_KINDS, declarationLines, DEFAULT_CAPS, estimateTokens, findUseInImporter, fitToBudget, gatherAround, importStatementText, isTestFile,
outermostDefs, parseAroundTarget, placeDefs, selectEdited, type AroundResult, type FileDef,
} from "../around.js";
import { aroundJson, renderAroundLlm, renderAroundText } from "../around-render.js";
@@ -97,6 +97,55 @@ describe("selectEdited", () => {
});
});
+describe("declarationLines: an edit to a class outside its methods", () => {
+ const placed = placeDefs([
+ def("cls", "Box", 1, 20, { kind: "class" }),
+ def("m1", "open", 2, 5, { kind: "method", topLevel: false, container: "Box" }),
+ def("m2", "close", 7, 10, { kind: "method", topLevel: false, container: "Box" }),
+ def("f", "free", 22, 25),
+ ], []);
+ const cls = placed.find((d) => d.id === "cls")!;
+
+ it("finds the edited lines no member covers -- a field, a declaration", () => {
+ expect(declarationLines(cls, placed, [{ start: 12, end: 14 }])).toEqual({ start: 12, end: 14 });
+ expect(declarationLines(cls, placed, [{ start: 4, end: 6 }])).toEqual({ start: 6, end: 6 });
+ });
+
+ it("is undefined when every edited line is inside a method", () => {
+ expect(declarationLines(cls, placed, [{ start: 3, end: 4 }, { start: 8, end: 9 }])).toBeUndefined();
+ });
+
+ it("is undefined for a function, whose callers are calls", () => {
+ expect(declarationLines(placed.find((d) => d.id === "f")!, placed, [{ start: 23, end: 23 }])).toBeUndefined();
+ });
+
+ it("covers the class-like kinds", () => {
+ for (const k of ["class", "interface", "enum", "struct", "trait"]) expect(CONTAINER_KINDS.has(k), k).toBe(true);
+ for (const k of ["function", "method", "constructor"]) expect(CONTAINER_KINDS.has(k), k).toBe(false);
+ });
+
+ it("renders as declarations, with the type's references counted, not listed", () => {
+ const result = {
+ path: "src/Box.java",
+ symbols: [{
+ id: "cls", name: "Box", kind: "class", path: "src/Box.java", lineStart: 1, lineEnd: 20,
+ callers: { total: 40, rows: [{ path: "src/A.java", line: 3, snippet: "Box b = new Box();" }] },
+ users: { total: 0, rows: [] },
+ tests: { total: 1, rows: [{ path: "test/BoxTest.java", line: 9, snippet: "new Box()" }] },
+ sameName: [],
+ declarations: { lineStart: 12, lineEnd: 14 },
+ }],
+ importers: { total: 0, tests: 0, rows: [] },
+ } as any;
+ const text = renderAroundText(result, { lead: "Ix: you changed" });
+ expect(text).toContain("Ix: you changed declarations in `Box` outside its methods (src/Box.java:12-14).");
+ expect(text).toContain("the class is referenced from 40 places.");
+ expect(text).not.toContain("src/A.java:3");
+ expect(text).toContain("test/BoxTest.java:9");
+ expect(renderAroundLlm(result).filter((l) => l.startsWith("caller "))).toEqual([]);
+ });
+});
+
describe("importers that use the symbol", () => {
const importer = [
"import {", // 1
diff --git a/ix-cli/src/cli/around-render.ts b/ix-cli/src/cli/around-render.ts
index 647e429b..d6774c29 100644
--- a/ix-cli/src/cli/around-render.ts
+++ b/ix-cli/src/cli/around-render.ts
@@ -38,7 +38,9 @@ export interface AroundTextOptions {
/** The symbols as the first line names them: `` `name` (path:lines) ``. */
function symbolList(result: AroundResult): string {
- return result.symbols.map((s) => `\`${s.name}\` (${s.path}:${span(s)})`).join(", ");
+ return result.symbols.map((s) => s.declarations
+ ? `declarations in \`${s.name}\` outside its methods (${s.path}:${span(s.declarations)})`
+ : `\`${s.name}\` (${s.path}:${span(s)})`).join(", ");
}
export function renderAroundText(result: AroundResult, opts: AroundTextOptions = {}): string {
@@ -55,7 +57,12 @@ export function renderAroundText(result: AroundResult, opts: AroundTextOptions =
const many = result.symbols.length > 1;
for (const s of result.symbols) {
const p = many ? `\`${s.name}\` ` : "";
- lines.push(...sectionLines(`${p}callers`, s.callers, " "));
+ if (s.declarations) {
+ // A class's "callers" are mostly references to the type: counted, not listed.
+ if (s.callers.total > 0) lines.push(`${p}the ${s.kind} is referenced from ${s.callers.total} place${s.callers.total === 1 ? "" : "s"}.`);
+ } else {
+ lines.push(...sectionLines(`${p}callers`, s.callers, " "));
+ }
lines.push(...sectionLines(`${p}used by importers`, s.users, " "));
lines.push(...sectionLines(`${p}tests`, s.tests, " "));
if (s.sameName.length > 0) {
@@ -91,9 +98,11 @@ function symbolLlm(s: AroundSymbol, many: boolean): string[] {
llmLine("symbol", [
["name", s.name], ["kind", s.kind], ["path", s.path], ["lines", span(s)],
["moved_from", s.movedFrom],
+ ["declarations", s.declarations ? span(s.declarations) : undefined],
["callers", s.callers.total], ["users", s.users.total], ["tests", s.tests.total],
]),
- ...s.callers.rows.map((r) => llmLine("caller", [...of, ...refFields(r)])),
+ // Edited declarations of a container: its "callers" are type references, counted above.
+ ...(s.declarations ? [] : s.callers.rows.map((r) => llmLine("caller", [...of, ...refFields(r)]))),
...s.users.rows.map((r) => llmLine("user", [...of, ...refFields(r)])),
...s.tests.rows.map((r) => llmLine("test", [...of, ...refFields(r)])),
...s.sameName.map((o) => llmLine("same_name", [...of, ["kind", o.kind], ["lines", span(o)]])),
diff --git a/ix-cli/src/cli/around.ts b/ix-cli/src/cli/around.ts
index b31da2b0..d92d7fad 100644
--- a/ix-cli/src/cli/around.ts
+++ b/ix-cli/src/cli/around.ts
@@ -90,6 +90,47 @@ export interface AroundSymbol {
tests: AroundSection;
/** Other definitions in the same file with the same name. */
sameName: Array<{ kind: string; lineStart: number; lineEnd: number }>;
+ /**
+ * Set when the edit is inside a class-like container but outside every
+ * member the graph has: a field, an annotation, a declaration line. Such a
+ * container's "callers" are mostly references to the type, so they are
+ * counted, not listed.
+ */
+ declarations?: { lineStart: number; lineEnd: number };
+}
+
+/** Definitions whose "callers" are references to a type rather than calls. */
+export const CONTAINER_KINDS = new Set([
+ "class", "interface", "enum", "struct", "trait", "object", "module", "record",
+ "annotation", "union", "namespace", "impl", "type",
+]);
+
+/**
+ * The edited lines inside container `d` that no member of it covers -- its
+ * fields and declarations, which the graph does not index as definitions
+ * (Java and TypeScript fields are not) -- or undefined when every edited line
+ * is inside a member.
+ */
+export function declarationLines(
+ d: PlacedDef, placed: PlacedDef[], ranges: LineRange[],
+): { start: number; end: number } | undefined {
+ if (!CONTAINER_KINDS.has(d.kind.toLowerCase())) return undefined;
+ const members = placed.filter((o) => o.id !== d.id && o.start >= d.start && o.end <= d.end
+ && (o.end - o.start) < (d.end - d.start));
+ let lo = Infinity;
+ let hi = -Infinity;
+ for (const r of ranges) {
+ if (r.insertion) {
+ if (r.start >= d.start && r.start <= d.end && !members.some((m) => m.start <= r.start && m.end >= r.end)) {
+ lo = Math.min(lo, r.start); hi = Math.max(hi, r.start);
+ }
+ continue;
+ }
+ for (let l = Math.max(r.start, d.start); l <= Math.min(r.end, d.end); l++) {
+ if (!members.some((m) => l >= m.start && l <= m.end)) { lo = Math.min(lo, l); hi = Math.max(hi, l); }
+ }
+ }
+ return lo <= hi ? { start: lo, end: hi } : undefined;
}
export interface AroundResult {
@@ -523,6 +564,9 @@ export async function gatherAround(client: AroundClient, req: AroundRequest): Pr
return { kind: o.kind, lineStart: c?.start ?? o.lineStart, lineEnd: c?.end ?? o.lineEnd };
});
const testRows = [...tests.values()];
+ const decl = req.ranges && req.ranges.length > 0 ? declarationLines(d, anchorPlaced, req.ranges) : undefined;
+ // In the current file's coordinates, like the symbol's own span.
+ const shift = here.start - d.start;
return {
id: d.id,
name: d.name,
@@ -535,6 +579,7 @@ export async function gatherAround(client: AroundClient, req: AroundRequest): Pr
users: { total: users.length, rows: users.slice(0, caps.users) },
tests: { total: testRows.length, rows: testRows.slice(0, caps.tests) },
sameName,
+ ...(decl ? { declarations: { lineStart: decl.start + shift, lineEnd: decl.end + shift } } : {}),
};
}));