mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-25 00:03:08 +00:00
fix(vcs): open a multi-line selection's lines on the Git remote from Open In (#4844)
Editor > Open In > GitHub / GitLab / Bitbucket opened only the caret line even with several lines selected. The editor menu passed the caret line only and vcs.openOnRemote dropped endLine; the menu now takes its lines from the helper Copy Path/Reference uses (moved to selectedLines.ts) and Open In keeps endLine. Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com> Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
This commit is contained in:
@@ -68,6 +68,7 @@ import { cloneCaretAbove, cloneCaretBelow } from "./cm/verticalMotion";
|
||||
import { pasteAsPlainText, pasteFromHistory, unwrapAtCaret } from "./extras";
|
||||
import { cyclicExpandWord } from "./wordCompletion";
|
||||
import { copyReferenceToClipboard, declaredName, qualifiedReference, referenceOffset } from "./copyReference";
|
||||
import { selectedLineContext, selectedLines } from "./selectedLines";
|
||||
import { declarationChain } from "./view/outline";
|
||||
import { recordClipboard } from "./clipboardHistory";
|
||||
import { useDocMeta, useOutline } from "./model/document";
|
||||
@@ -589,15 +590,6 @@ function copyReference(reference: Promise<string>) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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;
|
||||
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);
|
||||
@@ -606,11 +598,9 @@ function pathWithLines(state: EditorState, path: string): string {
|
||||
|
||||
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,
|
||||
endLine: last > first ? last - 1 : undefined,
|
||||
...selectedLineContext(view.state),
|
||||
}).flatMap((item) =>
|
||||
(item.kind ?? "item") === "item" && "run" in item && item.id && item.run && item.preview
|
||||
? [
|
||||
|
||||
@@ -115,6 +115,7 @@ import { isClassFile } from "@/platform/classFiles";
|
||||
import { isVirtualUri, virtualDocument } from "@/platform/virtualDocs";
|
||||
import type { VirtualDocument } from "@/platform/ipc";
|
||||
import type { Scheduler } from "./sync/DocSync";
|
||||
import { selectedLineContext } from "./selectedLines";
|
||||
|
||||
export const fileKey = (path: string) => `file:${path}`;
|
||||
export const fileInput = (path: string): EditorInput => ({ kind: "file", key: fileKey(path), path });
|
||||
@@ -994,7 +995,7 @@ export class EditorController implements SlotHost {
|
||||
] as MenuItem[])
|
||||
: ([
|
||||
{ kind: "separator" },
|
||||
openInMenu(slot.model.path, view.state.doc.lineAt(view.state.selection.main.head).number - 1),
|
||||
openInMenu(slot.model.path, selectedLineContext(view.state)),
|
||||
{ kind: "separator" },
|
||||
localHistoryMenu(slot.model.path),
|
||||
gitMenu(slot.model.path),
|
||||
@@ -2352,7 +2353,7 @@ export class EditorController implements SlotHost {
|
||||
}
|
||||
|
||||
/** Open In › for a file: Project View, Finder / Explorer / Files, associated app, Terminal. */
|
||||
function openInMenu(path: string, line?: number): MenuItem {
|
||||
function openInMenu(path: string, lines?: { line: number; endLine?: number }): MenuItem {
|
||||
return {
|
||||
label: "Open In",
|
||||
submenu: [
|
||||
@@ -2372,7 +2373,7 @@ function openInMenu(path: string, line?: number): MenuItem {
|
||||
: []),
|
||||
{ kind: "action", action: "project.openInTerminal", arg: path, label: "Terminal" },
|
||||
{ kind: "separator" },
|
||||
...menuGroupItems("openIn", { paths: [path], line }),
|
||||
...menuGroupItems("openIn", { paths: [path], ...lines }),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -239,6 +239,36 @@ describe("editor Copy Path/Reference chooser", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("gives Open In contributions the selected lines", () => {
|
||||
const contexts: MenuGroupContext[] = [];
|
||||
disposers.push(
|
||||
registerMenuGroup({
|
||||
id: "test.openIn",
|
||||
menus: ["openIn"],
|
||||
items: (_menu, ctx) => {
|
||||
contexts.push(ctx);
|
||||
return [];
|
||||
},
|
||||
}),
|
||||
);
|
||||
const view = ctrl.activeSlot!.view!;
|
||||
vi.spyOn(view, "posAtCoords").mockReturnValue(null);
|
||||
// The caret; a selection from the second line back into the first; one ending at the start of the second.
|
||||
for (const selection of [
|
||||
{ anchor: documentText.indexOf("A {}") },
|
||||
{ anchor: documentText.indexOf("A {}"), head: 0 },
|
||||
{ anchor: 0, head: documentText.indexOf("class") },
|
||||
]) {
|
||||
view.dispatch({ selection });
|
||||
ctrl.onContextMenu(ctrl.activeSlot!, new MouseEvent("contextmenu"));
|
||||
}
|
||||
expect(contexts.map(({ paths, line, endLine }) => [paths, line, endLine])).toEqual([
|
||||
[[path], 1, undefined],
|
||||
[[path], 0, 1],
|
||||
[[path], 0, undefined],
|
||||
]);
|
||||
});
|
||||
|
||||
it("adds the chooser to Copy / Paste Special", () => {
|
||||
vi.spyOn(ctrl.activeSlot!.view!, "posAtCoords").mockReturnValue(null);
|
||||
ctrl.onContextMenu(ctrl.activeSlot!, new MouseEvent("contextmenu"));
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { EditorState } from "@codemirror/state";
|
||||
|
||||
/** The main selection's lines (1-based), leaving out the one a selection ends at the start of, as IDEA. */
|
||||
export function selectedLines(state: EditorState): { first: number; last: number } {
|
||||
const { doc, selection } = state;
|
||||
const { from, to } = selection.main;
|
||||
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 };
|
||||
}
|
||||
|
||||
/** The selected lines as a menu group's 0-based `line` / `endLine` (absent for a single line). */
|
||||
export function selectedLineContext(state: EditorState): { line: number; endLine?: number } {
|
||||
const { first, last } = selectedLines(state);
|
||||
return { line: first - 1, endLine: last > first ? last - 1 : undefined };
|
||||
}
|
||||
@@ -24,6 +24,9 @@ vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
const openUrl = vi.hoisted(() => vi.fn(async (_url: string) => undefined));
|
||||
vi.mock("../issueLinks", () => ({ openUrl }));
|
||||
|
||||
import { openOnRemoteActions, registerOpenOnRemote } from "../openOnRemote";
|
||||
import { useVcs } from "../store";
|
||||
|
||||
@@ -127,6 +130,24 @@ describe("Copy Path/Reference… › <hosting> Link", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("opens the file on its remote at the selected lines from Open In", async () => {
|
||||
await start();
|
||||
const open = (ctx: Parameters<typeof menuGroupItems>[1]) =>
|
||||
menuGroupItems("openIn", ctx).filter((i): i is Item => i.kind !== "separator" && "run" in i)[0]!;
|
||||
open({ paths: ["/p/src/a.ts"], line: 9, endLine: 19 }).run!();
|
||||
await vi.waitFor(() =>
|
||||
expect(openUrl).toHaveBeenLastCalledWith(
|
||||
"https://gitlab.example.com/group/sub/app/-/blob/feature/x/src/a.ts#L10-20",
|
||||
),
|
||||
);
|
||||
open({ paths: ["/p/src/a.ts"], line: 9 }).run!();
|
||||
await vi.waitFor(() =>
|
||||
expect(openUrl).toHaveBeenLastCalledWith(
|
||||
"https://gitlab.example.com/group/sub/app/-/blob/feature/x/src/a.ts#L10",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("warns instead of copying when the file has no page on a remote", async () => {
|
||||
await executeAction("vcs.copyRemoteLink", { path: "/elsewhere/a.ts", line: 0 });
|
||||
await vi.waitFor(() =>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Open In › GitHub / GitLab / Bitbucket: the file (at the caret line) or directory on the web UI
|
||||
* Open In › GitHub / GitLab / Bitbucket: the file (at the selected lines) or directory on the web UI
|
||||
* of its repository's remote, at the current branch's upstream (else the HEAD commit).
|
||||
* Copy Path/Reference… › <hosting> Link copies that page's address, with the selected lines.
|
||||
* Copy Path/Reference… › <hosting> Link copies that page's address.
|
||||
*/
|
||||
import { executeAction, type Action } from "@/platform/actions";
|
||||
import { writeClipboardText } from "@/platform/clipboard";
|
||||
@@ -133,9 +133,9 @@ export function registerOpenOnRemote(): () => void {
|
||||
const url = root ? remoteUrls.get(root) : null;
|
||||
const hosting = url ? remoteHosting(url) : null;
|
||||
if (!root || !hosting) return [];
|
||||
const target: Target = { path, dir: dirs?.[0] ?? false, line };
|
||||
const target: Target = { path, dir: dirs?.[0] ?? false, line, endLine };
|
||||
if (menu === "copyPath") {
|
||||
const page = pageFor({ ...target, endLine }, root, url);
|
||||
const page = pageFor(target, root, url);
|
||||
if (!page) return [];
|
||||
return [
|
||||
{
|
||||
@@ -143,7 +143,7 @@ export function registerOpenOnRemote(): () => void {
|
||||
label: tx`${hosting.name} Link`,
|
||||
verbatim: true,
|
||||
preview: page,
|
||||
run: () => void executeAction("vcs.copyRemoteLink", { ...target, endLine }),
|
||||
run: () => void executeAction("vcs.copyRemoteLink", target),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user