fix(navigation): Navigate to Test uses the top-level class at the caret (#4263)

Navigate to Test derived test names from the file name, so with the caret in a second top-level class it offered the file's test instead. For Java, Kotlin, Scala, Groovy and C# it now takes the outermost type around the caret from the document outline, falling back to the file name. Fixes #4171.
This commit is contained in:
l0ng-ai
2026-11-18 19:08:21 +08:00
committed by GitHub
parent d4d9e7d191
commit ffdb5d74f2
5 changed files with 54 additions and 3 deletions
+14
View File
@@ -36,6 +36,20 @@ describe("Go to Test / Test Subject", () => {
expect(d.open).toHaveBeenCalledWith("/p/src/test/java/a/FooTest.java");
});
it("uses the top-level class at the caret when a file declares several", async () => {
const files = [
"/p/src/main/java/a/Widget.java",
"/p/src/test/java/a/WidgetTest.java",
"/p/src/test/java/a/HelperTest.java",
];
const d = { ...deps(files), classAtCaret: async () => "Helper" };
await gotoTestOrSubject("/p/src/main/java/a/Widget.java", "", d);
expect(d.open).toHaveBeenCalledWith("/p/src/test/java/a/HelperTest.java");
const other = { ...deps(files), classAtCaret: async () => null };
await gotoTestOrSubject("/p/src/main/java/a/Widget.java", "", other);
expect(other.open).toHaveBeenCalledWith("/p/src/test/java/a/WidgetTest.java");
});
it("goes back from a test to its subject", async () => {
const d = deps(["/p/src/main/java/a/Foo.java", "/p/src/test/java/a/FooTest.java"]);
await gotoTestOrSubject("/p/src/test/java/a/FooTest.java", "", d);
+14 -1
View File
@@ -36,6 +36,19 @@ export interface TestNavDeps {
/** Create New Test… in `directory` (file templates), or a Rust test module in the file itself. */
createTest: (req: { directory: string; name: string; extension: string; subject: string }) => void;
createRustTestModule: () => void;
/** Name of the top-level class, interface, enum or object around the caret, if any. */
classAtCaret?: () => Promise<string | null>;
}
/** Languages whose files are named after a class but may declare several top-level ones. */
const CLASS_FILE = /\.(java|kt|scala|groovy|cs)$/;
/** The file name as the class at the caret would have it (`Helper.java` inside `Widget.java`), else the file's own. */
async function subjectFileName(path: string, deps: TestNavDeps): Promise<string> {
const name = basename(path);
if (!CLASS_FILE.test(name) || !deps.classAtCaret) return name;
const cls = await deps.classAtCaret().catch(() => null);
return cls ? cls + splitName(name).ext : name;
}
const isRustIntegrationTest = (path: string) => path.endsWith(".rs") && /\/tests\/[^/]+\.rs$/.test(path);
@@ -88,7 +101,7 @@ function relLabel(root: string | null, path: string) {
export async function gotoTestOrSubject(path: string, text: string, deps: TestNavDeps): Promise<void> {
const roots = deps.roots();
const root = deps.projectRoot();
const name = basename(path);
const name = await subjectFileName(path, deps);
const rank = (paths: string[]) => rankCandidates(path, paths, belowRoot(root, roots));
if (isTestSide(path, roots)) {
+5 -1
View File
@@ -22,7 +22,7 @@ import { registerRelatedProvider, relatedItems } from "@/editor/relatedProviders
import { gotoTestOrSubject, type TestNavDeps } from "./gotoTest";
import { headerSourceItems } from "./headerSource";
import { projectPythonHierarchy, pythonSuperDeps } from "./pythonHierarchy";
import { baseName, memberAt, methodStop, superMethods, type SuperDeps } from "./structureNav";
import { baseName, memberAt, methodStop, superMethods, topLevelTypeAt, type SuperDeps } from "./structureNav";
import { tx } from "@/platform/i18n";
const EDITOR_WHEN = "textEditorActive && (editorFocus || !toolWindowFocus)";
@@ -150,6 +150,10 @@ function testDeps(view: EditorView, ctx: EditorDocContext): TestNavDeps {
})),
}),
hint: (text) => showHint(view, text),
classAtCaret: async () => {
const outline = await call(commands.docOutline(ctx.doc));
return topLevelTypeAt(outline, offsetToPos(view.state.doc, view.state.selection.main.head))?.name ?? null;
},
createTest: ({ directory, name, extension }) =>
void executeAction("templates.newFromTemplate", { directory, name, extension, kindId: "test" }),
createRustTestModule: () => {
+10 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { HierarchyItem, OutlineItem, SymbolKind } from "@/platform/ipc";
import { memberAt, methodStop, methodStops, superMethods } from "./structureNav";
import { memberAt, methodStop, methodStops, superMethods, topLevelTypeAt } from "./structureNav";
const r = (l1: number, l2: number) => ({ start: { line: l1, col: 0 }, end: { line: l2, col: 1 } });
function item(name: string, kind: SymbolKind, from: number, to: number, children: OutlineItem[] = []): OutlineItem {
@@ -100,3 +100,12 @@ describe("Super Method", () => {
expect(await lines("eat")).toEqual([2, 4]);
});
});
describe("topLevelTypeAt", () => {
it("answers the outermost type, through namespaces, and nothing outside a type", () => {
expect(topLevelTypeAt(OUTLINE, { line: 13, col: 2 })?.name).toBe("Foo");
expect(topLevelTypeAt(OUTLINE, { line: 23, col: 0 })).toBeNull();
const ns = [item("App", "namespace", 0, 30, [item("Widget", "class", 1, 10), item("Helper", "class", 12, 20)])];
expect(topLevelTypeAt(ns, { line: 15, col: 0 })?.name).toBe("Helper");
});
});
+11
View File
@@ -36,6 +36,17 @@ export function methodStop(outline: readonly OutlineItem[], caret: Pos, dir: 1 |
return null;
}
/** The outermost type around `caret`, looking through namespaces and packages (C#, Kotlin scripts). */
export function topLevelTypeAt(outline: readonly OutlineItem[], caret: Pos): OutlineItem | null {
let level: readonly OutlineItem[] = outline;
for (;;) {
const hit = level.find((i) => inside(i, caret));
if (!hit) return null;
if (hit.kind !== "namespace" && hit.kind !== "package") return TYPES.has(hit.kind) ? hit : null;
level = hit.children;
}
}
export interface CaretMember {
/** Innermost method at the caret. */
method: OutlineItem | null;