mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(editor): copy a multi-line selection's path as path:first-last (#4011)
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
This commit is contained in:
+17
-12
@@ -570,14 +570,24 @@ function copyReference(reference: Promise<string>) {
|
||||
}
|
||||
}
|
||||
|
||||
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<LspPopup, { kind: "list" }> = {
|
||||
@@ -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)))();
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user