diff --git a/src/editor/navigation/gotoTest.test.ts b/src/editor/navigation/gotoTest.test.ts index f21efaae1..efb98cc31 100644 --- a/src/editor/navigation/gotoTest.test.ts +++ b/src/editor/navigation/gotoTest.test.ts @@ -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); diff --git a/src/editor/navigation/gotoTest.ts b/src/editor/navigation/gotoTest.ts index d4ed31429..56ee49bee 100644 --- a/src/editor/navigation/gotoTest.ts +++ b/src/editor/navigation/gotoTest.ts @@ -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; +} + +/** 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 { + 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 { 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)) { diff --git a/src/editor/navigation/index.ts b/src/editor/navigation/index.ts index a6ba872f1..72a58ba8e 100644 --- a/src/editor/navigation/index.ts +++ b/src/editor/navigation/index.ts @@ -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: () => { diff --git a/src/editor/navigation/structureNav.test.ts b/src/editor/navigation/structureNav.test.ts index dcfaf4eae..9373e0cd1 100644 --- a/src/editor/navigation/structureNav.test.ts +++ b/src/editor/navigation/structureNav.test.ts @@ -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"); + }); +}); diff --git a/src/editor/navigation/structureNav.ts b/src/editor/navigation/structureNav.ts index 32cc38448..349702615 100644 --- a/src/editor/navigation/structureNav.ts +++ b/src/editor/navigation/structureNav.ts @@ -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;