mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-23 08:49:33 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
924f046032 |
No files matched your search
@@ -25,6 +25,7 @@ import { isNavigating, onNavigation } from "@/editor/lsp/foreground";
|
||||
import { readManyFileLines, withDocument } from "@/editor/lsp/files";
|
||||
import { locationItems, navigateTo, pinToFind } from "@/editor/lsp/navigation";
|
||||
import { openPopup } from "@/editor/lsp/popupStore";
|
||||
import type { DeclarationTarget } from "@/editor/lsp/syntaxFallback";
|
||||
import {
|
||||
commands,
|
||||
isCancelled,
|
||||
@@ -542,13 +543,32 @@ async function markerTargets(marker: LineMarker, doc: DocId | null): Promise<Loc
|
||||
}
|
||||
}
|
||||
|
||||
/** Opens the single target, or the chooser over several. */
|
||||
/** Resolves each declaration's targets before opening the shared chooser. */
|
||||
export async function openMarkerTargets(
|
||||
marker: LineMarker,
|
||||
markers: readonly [LineMarker, ...LineMarker[]],
|
||||
anchor: { x: number; y: number },
|
||||
doc: DocId | null = null,
|
||||
) {
|
||||
const targets = await markerTargets(marker, doc);
|
||||
const [first] = markers;
|
||||
const merged = markers.length > 1;
|
||||
let targets: DeclarationTarget[] = (
|
||||
await Promise.all(
|
||||
markers.map(async (marker) => {
|
||||
const locations = await markerTargets(marker, doc);
|
||||
// Generated methods can share a source location. Keep their names visible in the chooser.
|
||||
return merged && marker.method ? locations.map((target) => ({ ...target, name: marker.name })) : locations;
|
||||
}),
|
||||
)
|
||||
).flat();
|
||||
if (merged) {
|
||||
const seen = new Set<string>();
|
||||
targets = targets.filter(({ path, range, name }) => {
|
||||
const key = JSON.stringify([path, range.start.line, range.start.col, name]);
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
if (targets.length === 1) {
|
||||
await navigateTo(targets[0]!);
|
||||
return;
|
||||
@@ -556,9 +576,9 @@ export async function openMarkerTargets(
|
||||
openPopup({
|
||||
kind: "list",
|
||||
anchor,
|
||||
title: marker.chooserTitle,
|
||||
title: merged ? tx`Choose Inheritance Target (${targets.length} found)` : first.chooserTitle,
|
||||
items: await locationItems(targets),
|
||||
pin: pinToFind(marker.name, targets),
|
||||
pin: pinToFind([...new Set(markers.map((m) => m.name))].join(", "), targets),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -587,41 +607,50 @@ function place(doc: Text, markers: readonly LineMarker[]): PlacedMarker[] {
|
||||
}
|
||||
|
||||
const sameMarkers = (a: readonly PlacedMarker[], b: readonly PlacedMarker[]) =>
|
||||
a.length === b.length &&
|
||||
a.every(
|
||||
(m, i) => m.pos === b[i]!.pos && m.marker.kind === b[i]!.marker.kind && m.marker.tooltip === b[i]!.marker.tooltip,
|
||||
);
|
||||
a.length === b.length && a.every((m, i) => m.pos === b[i]!.pos && m.marker === b[i]!.marker);
|
||||
|
||||
class InheritanceMarker extends GutterMarker {
|
||||
constructor(readonly marker: LineMarker) {
|
||||
readonly kind: LineMarker["kind"];
|
||||
readonly tooltip: string;
|
||||
|
||||
constructor(readonly markers: readonly [LineMarker, ...LineMarker[]]) {
|
||||
super();
|
||||
const [first] = markers;
|
||||
this.kind = first.kind;
|
||||
this.tooltip =
|
||||
markers.length === 1 ? first.tooltip : [...new Set(markers.map((m) => `${m.name} — ${m.tooltip}`))].join("\n");
|
||||
}
|
||||
|
||||
override eq(other: GutterMarker): boolean {
|
||||
return (
|
||||
other instanceof InheritanceMarker &&
|
||||
other.marker.kind === this.marker.kind &&
|
||||
other.marker.tooltip === this.marker.tooltip
|
||||
);
|
||||
return other instanceof InheritanceMarker && other.kind === this.kind && other.tooltip === this.tooltip;
|
||||
}
|
||||
|
||||
override toDOM(): Node {
|
||||
const el = document.createElement("div");
|
||||
el.className = `${styles.marker} cm-openidea-lineMarker-${this.marker.kind}`;
|
||||
el.dataset.kind = this.marker.kind;
|
||||
el.title = this.marker.tooltip;
|
||||
el.className = `${styles.marker} cm-openidea-lineMarker-${this.kind}`;
|
||||
el.dataset.kind = this.kind;
|
||||
el.title = this.tooltip;
|
||||
el.setAttribute("role", "button");
|
||||
el.setAttribute("aria-label", this.marker.tooltip);
|
||||
el.innerHTML = gutterIconMarkup(MARKER_ICONS[this.marker.kind]) ?? "";
|
||||
el.setAttribute("aria-label", this.tooltip);
|
||||
el.innerHTML = gutterIconMarkup(MARKER_ICONS[this.kind]) ?? "";
|
||||
return el;
|
||||
}
|
||||
}
|
||||
|
||||
const gutterMarkers = new WeakMap<readonly PlacedMarker[], RangeSet<GutterMarker>>();
|
||||
const gutterMarkers = new WeakMap<readonly PlacedMarker[], RangeSet<InheritanceMarker>>();
|
||||
|
||||
function markerSet(placed: readonly PlacedMarker[]): RangeSet<GutterMarker> {
|
||||
function markerSet(placed: readonly PlacedMarker[]): RangeSet<InheritanceMarker> {
|
||||
let set = gutterMarkers.get(placed);
|
||||
if (!set) {
|
||||
const groups = new Map<string, { pos: number; markers: [LineMarker, ...LineMarker[]] }>();
|
||||
for (const { pos, marker } of placed) {
|
||||
const key = `${pos}:${marker.kind}`;
|
||||
const group = groups.get(key);
|
||||
if (group) group.markers.push(marker);
|
||||
else groups.set(key, { pos, markers: [marker] });
|
||||
}
|
||||
set = RangeSet.of(
|
||||
placed.map((m) => new InheritanceMarker(m.marker).range(m.pos)),
|
||||
[...groups.values()].map(({ pos, markers }) => new InheritanceMarker(markers).range(pos)),
|
||||
true,
|
||||
);
|
||||
gutterMarkers.set(placed, set);
|
||||
@@ -829,10 +858,14 @@ export function markerDeps(
|
||||
};
|
||||
}
|
||||
|
||||
function markerAt(view: EditorView, lineFrom: number, el: Element | null): LineMarker | null {
|
||||
const onLine = view.state.field(markersField).filter((m) => m.pos === lineFrom);
|
||||
const kind = (el?.closest(`.${styles.marker}`) as HTMLElement | null)?.dataset.kind;
|
||||
return (onLine.find((m) => m.marker.kind === kind) ?? (onLine.length === 1 ? onLine[0] : null))?.marker ?? null;
|
||||
function markerAt(view: EditorView, lineFrom: number, kind: string | undefined): InheritanceMarker | undefined {
|
||||
let match: InheritanceMarker | undefined;
|
||||
markerSet(view.state.field(markersField)).between(lineFrom, lineFrom, (pos, _to, marker) => {
|
||||
if (pos !== lineFrom || marker.kind !== kind) return;
|
||||
match = marker;
|
||||
return false;
|
||||
});
|
||||
return match;
|
||||
}
|
||||
|
||||
export function implementationGutter(ctx: EditorDocContext): Extension {
|
||||
@@ -846,13 +879,13 @@ export function implementationGutter(ctx: EditorDocContext): Extension {
|
||||
markers: (view) => markerSet(view.state.field(markersField)),
|
||||
mousedown: (view, block, e) => {
|
||||
if (e.button !== 0) return false;
|
||||
const target = e.target instanceof Element ? e.target : null;
|
||||
if (!target?.closest(`.${styles.marker}`)) return false;
|
||||
const marker = markerAt(view, block.from, target);
|
||||
const target = e.target instanceof Element ? e.target.closest<HTMLElement>(`.${styles.marker}`) : null;
|
||||
if (!target) return false;
|
||||
const marker = markerAt(view, block.from, target.dataset.kind);
|
||||
if (!marker) return false;
|
||||
e.preventDefault();
|
||||
const rect = (target.closest(`.${styles.marker}`) ?? target).getBoundingClientRect();
|
||||
void openMarkerTargets(marker, { x: rect.left, y: rect.bottom + 2 }, ctx.doc);
|
||||
const rect = target.getBoundingClientRect();
|
||||
void openMarkerTargets(marker.markers, { x: rect.left, y: rect.bottom + 2 }, ctx.doc);
|
||||
return true;
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { fireEvent } from "@testing-library/react";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { iconAreaExtension } from "@/editor/core/cm/iconArea";
|
||||
import { lspDoc } from "@/editor/lsp/lsp";
|
||||
import type { DeclarationTarget } from "@/editor/lsp/syntaxFallback";
|
||||
import {
|
||||
IpcError,
|
||||
commands,
|
||||
@@ -14,14 +19,15 @@ const nav = vi.hoisted(() => ({ navigateTo: vi.fn(async () => {}), openPopup: vi
|
||||
vi.mock("@/editor/lsp/navigation", async (orig) => ({
|
||||
...(await orig<typeof import("@/editor/lsp/navigation")>()),
|
||||
navigateTo: nav.navigateTo,
|
||||
locationItems: async (locs: Location[]) => locs.map((l, i) => ({ id: `${i}`, label: l.path, run: () => {} })),
|
||||
locationItems: async (locs: DeclarationTarget[]) =>
|
||||
locs.map((l, i) => ({ id: `${i}`, label: l.name ?? l.path, run: () => {} })),
|
||||
}));
|
||||
vi.mock("@/editor/lsp/popupStore", async (orig) => ({
|
||||
...(await orig<typeof import("@/editor/lsp/popupStore")>()),
|
||||
openPopup: nav.openPopup,
|
||||
}));
|
||||
|
||||
import { candidateKey, markerCandidates, type MarkerCandidate } from "./lineMarkers";
|
||||
import { candidateKey, markerCandidates, type LineMarker, type MarkerCandidate } from "./lineMarkers";
|
||||
import { Text } from "@codemirror/state";
|
||||
import {
|
||||
affectsMarkers,
|
||||
@@ -29,6 +35,7 @@ import {
|
||||
emptyCache,
|
||||
enabledGroups,
|
||||
fillMarkers,
|
||||
implementationGutter,
|
||||
libraryCandidates,
|
||||
markerBatches,
|
||||
markerDeps,
|
||||
@@ -539,16 +546,18 @@ describe("clicking a marker", () => {
|
||||
it("navigates to a single target", async () => {
|
||||
const target = loc("/p/Circle.java", 0, 13);
|
||||
await openMarkerTargets(
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 0,
|
||||
col: 17,
|
||||
method: false,
|
||||
name: "Shape",
|
||||
targets: [target],
|
||||
tooltip: "",
|
||||
chooserTitle: "",
|
||||
},
|
||||
[
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 0,
|
||||
col: 17,
|
||||
method: false,
|
||||
name: "Shape",
|
||||
targets: [target],
|
||||
tooltip: "",
|
||||
chooserTitle: "",
|
||||
},
|
||||
],
|
||||
{ x: 1, y: 2 },
|
||||
);
|
||||
expect(nav.navigateTo).toHaveBeenCalledWith(target);
|
||||
@@ -562,16 +571,18 @@ describe("clicking a marker", () => {
|
||||
const method = { ...zero, range: { start: { line: 5, col: 4 }, end: { line: 7, col: 5 } } };
|
||||
const spy = vi.spyOn(commands, "lspImplementationLocations").mockResolvedValue({ status: "ok", data: [method] });
|
||||
await openMarkerTargets(
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 1,
|
||||
col: 9,
|
||||
method: true,
|
||||
name: "save",
|
||||
targets: [zero],
|
||||
tooltip: "",
|
||||
chooserTitle: "",
|
||||
},
|
||||
[
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 1,
|
||||
col: 9,
|
||||
method: true,
|
||||
name: "save",
|
||||
targets: [zero],
|
||||
tooltip: "",
|
||||
chooserTitle: "",
|
||||
},
|
||||
],
|
||||
{ x: 1, y: 2 },
|
||||
7,
|
||||
);
|
||||
@@ -585,16 +596,18 @@ describe("clicking a marker", () => {
|
||||
const targets = [loc("/p/Circle.java", 0, 13), loc("/p/Square.java", 0, 13)];
|
||||
expect(candidateKey(candidate())).toBe("0:17:Shape");
|
||||
await openMarkerTargets(
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 0,
|
||||
col: 17,
|
||||
method: false,
|
||||
name: "Shape",
|
||||
targets,
|
||||
tooltip: "",
|
||||
chooserTitle: "Choose Implementation of Shape (2 found)",
|
||||
},
|
||||
[
|
||||
{
|
||||
kind: "implemented",
|
||||
line: 0,
|
||||
col: 17,
|
||||
method: false,
|
||||
name: "Shape",
|
||||
targets,
|
||||
tooltip: "",
|
||||
chooserTitle: "Choose Implementation of Shape (2 found)",
|
||||
},
|
||||
],
|
||||
{ x: 1, y: 2 },
|
||||
);
|
||||
expect(nav.navigateTo).not.toHaveBeenCalled();
|
||||
@@ -609,6 +622,98 @@ describe("clicking a marker", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("inheritance gutter", () => {
|
||||
it.each([
|
||||
{ kind: "overriding", language: "java", declaration: "@SuperBuilder" },
|
||||
{ kind: "overridden", language: "java", declaration: "@Data" },
|
||||
{ kind: "implementing", language: "go", declaration: "type Square struct{}" },
|
||||
{ kind: "implemented", language: "rust", declaration: "trait Shape { fn area(); fn label(); }" },
|
||||
] as const)("merges $language $kind markers by line and kind", async ({ kind, language, declaration }) => {
|
||||
vi.useFakeTimers();
|
||||
nav.navigateTo.mockClear();
|
||||
nav.openPopup.mockClear();
|
||||
const ctx = { doc: 1, path: `/p/gutter-${kind}`, language, uiMode: null, readOnly: false };
|
||||
const doc = Text.of([declaration, "another declaration"]);
|
||||
const markers = markersOf(ctx.path, doc);
|
||||
const otherKind = kind === "overridden" ? "overriding" : "overridden";
|
||||
const inputs = [
|
||||
{ kind, name: "build", line: 0 },
|
||||
{ kind: otherKind, name: "other", line: 0 },
|
||||
{ kind, name: "self", line: 0 },
|
||||
{ kind, name: "build", line: 0 },
|
||||
{ kind, name: "later", line: 1 },
|
||||
] as const;
|
||||
const candidates = inputs.map(({ name, line }, i): MarkerCandidate => ({
|
||||
name,
|
||||
line,
|
||||
symbol: `${name}(${i})`,
|
||||
col: 0,
|
||||
method: true,
|
||||
abstract: false,
|
||||
inheritable: false,
|
||||
overriding: false,
|
||||
owner: null,
|
||||
}));
|
||||
markers.outline = { items: [], candidates };
|
||||
inputs.forEach((input, i) => {
|
||||
const marker: LineMarker = {
|
||||
...input,
|
||||
col: 0,
|
||||
method: true,
|
||||
targets: [
|
||||
loc(
|
||||
language === "java" && input.kind === kind && input.line === 0 ? "/p/BaseOrder.java" : `/p/${input.name}`,
|
||||
2,
|
||||
),
|
||||
],
|
||||
tooltip: `Target for ${input.name}`,
|
||||
chooserTitle: input.name,
|
||||
};
|
||||
const group = marker.kind === "implemented" || marker.kind === "overridden" ? "inheritors" : "supers";
|
||||
markers.cache.entries.set(candidateKey(candidates[i]!), { [group]: marker });
|
||||
});
|
||||
const view = new EditorView({
|
||||
doc,
|
||||
extensions: [lspDoc.of(ctx), iconAreaExtension, implementationGutter(ctx)],
|
||||
parent: document.body,
|
||||
});
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
const icons = [...view.dom.querySelectorAll<HTMLElement>(`.cm-openidea-lineMarker-${kind}`)];
|
||||
expect(icons).toHaveLength(2);
|
||||
expect(icons[0]!.title).toContain("Target for build");
|
||||
expect(icons[0]!.title).toContain("Target for self");
|
||||
expect(icons[0]!.title).not.toContain("Target for later");
|
||||
expect(icons[0]!.parentElement!.querySelector(`.cm-openidea-lineMarker-${otherKind}`)).not.toBeNull();
|
||||
|
||||
fireEvent.mouseDown(icons[0]!, { button: 0 });
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(nav.navigateTo).not.toHaveBeenCalled();
|
||||
expect(nav.openPopup.mock.calls[0]![0].items.map((item: { label: string }) => item.label)).toEqual([
|
||||
"build",
|
||||
"self",
|
||||
]);
|
||||
|
||||
// CodeMirror finds the clicked line by the gutter row's screen position.
|
||||
const block = view.lineBlockAt(doc.line(2).from);
|
||||
vi.spyOn(icons[1]!.parentElement!, "getBoundingClientRect").mockReturnValue(
|
||||
new DOMRect(0, view.documentTop + block.top, 10, block.height),
|
||||
);
|
||||
fireEvent.mouseDown(icons[1]!, { button: 0 });
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(nav.navigateTo).toHaveBeenCalledWith(loc("/p/later", 2));
|
||||
|
||||
view.dispatch({ changes: { from: doc.line(1).to, to: doc.line(2).from, insert: " " } });
|
||||
const joined = view.dom.querySelectorAll<HTMLElement>(`.cm-openidea-lineMarker-${kind}`);
|
||||
expect(joined).toHaveLength(1);
|
||||
expect(joined[0]!.title).toContain("Target for later");
|
||||
} finally {
|
||||
view.destroy();
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("overriders of a type's methods from its subtypes", () => {
|
||||
// Shapes and names as jdtls answers them (outline names carry the signature).
|
||||
const REPO = [
|
||||
|
||||
@@ -2821,6 +2821,7 @@
|
||||
"Accepts mcpServers, servers, or a single server JSON object.": "Accepts mcpServers, servers, or a single server JSON object.",
|
||||
"Leave empty to enable for all agents, or pick specific coding agents.": "Leave empty to enable for all agents, or pick specific coding agents.",
|
||||
"Related Symbol…": "Related Symbol…",
|
||||
"Choose Inheritance Target ({count} found)": "Choose Inheritance Target ({count} found)",
|
||||
"Choose Target": "Choose Target",
|
||||
"No related symbols": "No related symbols",
|
||||
"Header": "Header",
|
||||
|
||||
@@ -9180,6 +9180,7 @@
|
||||
"Accepts mcpServers, servers, or a single server JSON object.": "支持 mcpServers、servers 或单个服务器 JSON 对象。",
|
||||
"Leave empty to enable for all agents, or pick specific coding agents.": "留空则对所有智能体启用,也可选择特定的编码智能体。",
|
||||
"Related Symbol…": "相关符号…",
|
||||
"Choose Inheritance Target ({count} found)": "选择继承目标(找到 {count} 个)",
|
||||
"Choose Target": "选择目标",
|
||||
"No related symbols": "没有相关符号",
|
||||
"Header": "头文件",
|
||||
|
||||
Reference in new issue
Block a user