From cc6ed6b5fd5ae5112e4f65624f7eeb1d4120cbf6 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 17 Nov 2026 21:39:11 +0800 Subject: [PATCH] feat(editor): copy a multi-line selection's path as path:first-last (#4011) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copy Path with Line Number (and its entry in Copy Path/Reference…) copies path:12-18 when the selection spans several lines, leaving out a last line the selection only reaches the start of; a caret or single-line selection still copies path:12. Closes #3962 --- src/editor/core/actions.ts | 29 +++++++++++++++----------- src/editor/core/copyPathPopup.test.tsx | 12 +++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/editor/core/actions.ts b/src/editor/core/actions.ts index 591cc1103..6144cbf08 100644 --- a/src/editor/core/actions.ts +++ b/src/editor/core/actions.ts @@ -570,14 +570,24 @@ function copyReference(reference: Promise) { } } -function copyPathReferencePopup(view: EditorView, slot: EditorSlot) { - const path = slot.model.path; - const { doc, selection } = view.state; - const line = doc.lineAt(selection.main.head).number; +/** The selected lines (1-based), leaving out the one a selection ends at the start of. */ +function selectedLines(state: EditorState): { first: number; last: number } { + const { doc, selection } = state; const { from, to } = selection.main; - // The selected lines, leaving out the one a selection ends at the start of. const first = doc.lineAt(from).number; const last = Math.max(first, doc.lineAt(to).number - (to > from && doc.lineAt(to).from === to ? 1 : 0)); + return { first, last }; +} + +/** `path:12`, or `path:12-18` for a selection over several lines. */ +function pathWithLines(state: EditorState, path: string): string { + const { first, last } = selectedLines(state); + return `${pathToCopy("relative", path)}:${first}${last > first ? `-${last}` : ""}`; +} + +function copyPathReferencePopup(view: EditorView, slot: EditorSlot) { + const path = slot.model.path; + const { first, last } = selectedLines(view.state); const contributed = menuGroupItems("copyPath", { paths: [path], line: first - 1, @@ -603,7 +613,7 @@ function copyPathReferencePopup(view: EditorView, slot: EditorSlot) { { id: "editor.copyPathWithLineNumber", label: "Path with line number", - value: `${pathToCopy("relative", path)}:${line}`, + value: pathWithLines(view.state, path), }, ]; const popup: Extract = { @@ -1280,12 +1290,7 @@ export function registerEditorActions(): () => void { when: TEXT_WHEN, run: ({ arg }) => { if (typeof arg === "string") copyEditorPath(arg); - else - withView((view, slot) => - copyEditorPath( - `${pathToCopy("relative", slot.model.path)}:${view.state.doc.lineAt(view.state.selection.main.head).number}`, - ), - )(); + else withView((view, slot) => copyEditorPath(pathWithLines(view.state, slot.model.path)))(); }, }, { diff --git a/src/editor/core/copyPathPopup.test.tsx b/src/editor/core/copyPathPopup.test.tsx index 4a7483309..1d7672039 100644 --- a/src/editor/core/copyPathPopup.test.tsx +++ b/src/editor/core/copyPathPopup.test.tsx @@ -154,6 +154,18 @@ describe("editor Copy Path/Reference chooser", () => { expect(writeText).toHaveBeenCalledExactlyOnceWith(path); }); + it("copies a selection over several lines as path:first-last", async () => { + const view = ctrl.activeSlot!.view!; + view.dispatch({ selection: { anchor: 0, head: documentText.indexOf("A {}") } }); + await executeAction("editor.copyPathReference"); + const item = popup().items.find((item) => item.id === "editor.copyPathWithLineNumber")!; + expect(item.preview).toBe("src/A.java:1-2"); + // A selection that ends at the start of a line leaves that line out. + view.dispatch({ selection: { anchor: 0, head: documentText.indexOf("class") } }); + await executeAction("editor.copyPathWithLineNumber"); + await vi.waitFor(() => expect(writeText).toHaveBeenLastCalledWith("src/A.java:1")); + }); + it("uses path:line off a symbol and ignores stale reference resolution", async () => { ctrl.activeSlot!.view!.dispatch({ selection: { anchor: documentText.length - 1 } }); await executeAction("editor.copyPathReference");