Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(editor): Copy Reference emits bare member name, '#' for Kotlin/Scala (#1894)

arookieofc61b12269a1d3
diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts
index 4574a17e7..e6410539e 100644
--- a/src/editor/core/actions.ts
+++ b/src/editor/core/actions.ts
@@ -62,7 +62,7 @@ import { isColumnMode, toggleColumnMode } from "./cm/columnMode";
import { cloneCaretAbove, cloneCaretBelow } from "./cm/verticalMotion";
import { pasteAsPlainText, pasteFromHistory, unwrapAtCaret } from "./extras";
import { cyclicExpandWord } from "./wordCompletion";
-import { copyReferenceToClipboard, qualifiedReference, referenceOffset } from "./copyReference";
+import { copyReferenceToClipboard, declaredName, qualifiedReference, referenceOffset } from "./copyReference";
import { declarationChain } from "./view/outline";
import { recordClipboard } from "./clipboardHistory";
import { useDocMeta, useOutline } from "./model/document";
@@ -439,7 +439,7 @@ async function resolveReference(view: EditorView, slot: EditorSlot): Promise<str
path: slot.model.path,
root,
text: state.doc.toString(),
- chain: localChain.map((item) => item.name),
+ chain: localChain.map(declaredName),
line: position.number - 1,
});
} else {
@@ -462,7 +462,7 @@ async function resolveReference(view: EditorView, slot: EditorSlot): Promise<str
path: target.path,
root,
text: lines.join("\n"),
- chain: chain.map((item) => item.name),
+ chain: chain.map(declaredName),
line: pos.line,
});
}
diff --git a/src/editor/core/copyReference.ts b/src/editor/core/copyReference.ts
index 618952573..160ac3ee3 100644
--- a/src/editor/core/copyReference.ts
+++ b/src/editor/core/copyReference.ts
@@ -78,24 +78,32 @@ export function pythonModulePath(path: string, root: string | null): string {
const declared = (text: string, re: RegExp) => re.exec(text)?.[1] ?? null;
+/** `method(int, String)` → `method`: a language server may bake the signature into a callable's name; IDEA's reference is the bare name. */
+export function declaredName(item: { name: string; kind: string }): string {
+ const n = item.name;
+ if (!["function", "method", "constructor"].includes(item.kind)) return n;
+ const paren = n.indexOf("(");
+ return paren > 0 ? n.slice(0, paren).trim() : n;
+}
+
+/** `pkg.Outer.Inner#member` (the last symbol is the member unless the caret is on a type's name), as IDEA's Copy Reference. */
+function jvmReference(pkg: string | null, chain: string[]): string {
+ const [type, ...rest] = chain;
+ const typePath = [pkg, type].filter(Boolean).join(".");
+ return rest.length ? `${[typePath, ...rest.slice(0, -1)].join(".")}#${rest[rest.length - 1]}` : typePath;
+}
+
export function qualifiedReference(r: ReferenceInput): string {
const fallback = `${relativePath(r.path, r.root)}:${r.line + 1}`;
const chain = r.chain.filter(Boolean);
if (!chain.length) return fallback;
switch (r.language) {
case "java":
- case "groovy": {
- const pkg = declared(r.text, /^\s*package\s+([\w.]+)\s*;?/m);
- const [type, ...rest] = chain;
- const typePath = [pkg, type].filter(Boolean).join(".");
- // `pkg.Outer.Inner#member`: the last symbol is the member unless the caret is on a type's name.
- return rest.length ? `${[typePath, ...rest.slice(0, -1)].join(".")}#${rest[rest.length - 1]}` : typePath;
- }
+ case "groovy":
+ return jvmReference(declared(r.text, /^\s*package\s+([\w.]+)\s*;?/m), chain);
case "kotlin":
- case "scala": {
- const pkg = declared(r.text, /^\s*package\s+([\w.]+)/m);
- return [pkg, ...chain].filter(Boolean).join(".");
- }
+ case "scala":
+ return jvmReference(declared(r.text, /^\s*package\s+([\w.]+)/m), chain);
case "csharp":
return chain.join(".");
case "rust":
diff --git a/src/editor/core/editingExtras.test.ts b/src/editor/core/editingExtras.test.ts
index 335acad9e..ab0542a10 100644
--- a/src/editor/core/editingExtras.test.ts
+++ b/src/editor/core/editingExtras.test.ts
@@ -5,6 +5,7 @@ import { cyclicExpandWord, expandCandidates, prefixStart } from "./wordCompletio
import type { OutlineItem } from "@/platform/ipc";
import {
copyReferenceToClipboard,
+ declaredName,
pythonModulePath,
qualifiedReference,
referenceOffset,
@@ -105,6 +106,25 @@ describe("copy reference", () => {
).toBe("A.B#m");
});
+ it("qualifies Kotlin and Scala members like IDEA", () => {
+ const text = "package com.acme.util";
+ expect(
+ qualifiedReference({ ...base, language: "kotlin", path: "/p/src/Svc.kt", text, chain: ["Svc", "get"] }),
+ ).toBe("com.acme.util.Svc#get");
+ expect(
+ qualifiedReference({ ...base, language: "scala", path: "/p/src/Svc.scala", text, chain: ["Svc", "get"] }),
+ ).toBe("com.acme.util.Svc#get");
+ });
+
+ it("strips a language server's signature from a callable's name", () => {
+ expect(declaredName({ name: "getUserById(int)", kind: "method" })).toBe("getUserById");
+ expect(declaredName({ name: "UserService(int)", kind: "constructor" })).toBe("UserService");
+ expect(declaredName({ name: "run()", kind: "function" })).toBe("run");
+ // No signature, or a non-callable kind: the name is kept as is.
+ expect(declaredName({ name: "trim", kind: "method" })).toBe("trim");
+ expect(declaredName({ name: "Strings", kind: "class" })).toBe("Strings");
+ });
+
it("builds Rust paths from the module tree", () => {
expect(rustModulePath("/p/crates/x/src/lib.rs")).toBe("crate");
expect(rustModulePath("/p/src/a/b.rs")).toBe("crate::a::b");