Порівняти коміти

...
1 Коміти
Автор SHA1 Повідомлення Дата
l0ng-ai 58d8776033 feat(editor): copy a multi-line selection's path as path:first-last
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
2026-11-17 21:38:51 +08:00
2 змінених файлів з 29 додано та 12 видалено

No files matched your search

+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)))();
},
},
{
+12
Переглянути файл
@@ -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");