fix(editor): group plugin gutter links by line and icon (#2574)

Plugin line markers on the same line with the same resolved icon now render as one
gutter button; duplicate destinations from several providers are dropped and every
distinct target goes to the existing target chooser. Run markers and different icons
stay separate. The default `navigate` icon gets its own navigation glyph instead of
reusing the implementing-method one, as the WIT documents them as distinct icons.

Closes #2573

Co-authored-by: arnaud-ritti <77437157+arnaud-ritti@users.noreply.github.com>
This commit is contained in:
Arnaud Ritti
2026-11-11 03:27:21 +08:00
committed by GitHub
parent 86e8bb5c4d
commit 55fc760abb
6 changed files with 91 additions and 22 deletions
@@ -1039,6 +1039,31 @@ describe("secrets", () => {
});
describe("plugin line markers", () => {
it("groups related destinations on one line without hiding targets or run actions", () => {
const doc = EditorState.create({ doc: "class Post extends Model {}" }).doc;
const location = (path: string) => ({ path, range: { start: { line: 0, col: 0 }, end: { line: 0, col: 4 } } });
const marker = (path: string, icon: string | null = "navigate", pluginId = "laravel") => ({
pluginId,
line: 0,
icon,
tooltip: path,
target: location(path),
});
const placed = placeMarkers(doc, [
marker("/project/create_posts.php"),
marker("/project/PostFactory.php"),
marker("/project/PostFactory.php", null, "another-plugin"),
marker("/project/PostPolicy.php"),
marker("/project/run.php", "run"),
]);
expect(placed).toHaveLength(2);
expect(placed[0]!.markers.map((entry) => entry.target?.path)).toEqual([
"/project/create_posts.php",
"/project/PostFactory.php",
"/project/PostPolicy.php",
]);
expect(placed[1]!.icon).toBe("gutter-run");
});
it("places markers on existing lines and maps their icons", () => {
const doc = EditorState.create({ doc: "a\nbb\nccc" }).doc;
const marker = (line: number, icon: string | null) => ({ pluginId: "m", line, icon, tooltip: "t", target: null });
@@ -1046,8 +1071,8 @@ describe("plugin line markers", () => {
expect(markerIcon("database")).toBe("database");
expect(markerIcon("run")).toBe("gutter-run");
expect(markerIcon("implemented")).toBe("gutter-implemented");
expect(markerIcon(null)).toBe("gutter-implementing");
expect(markerIcon("unknown")).toBe("gutter-implementing");
expect(markerIcon(null)).toBe("gutter-navigate");
expect(markerIcon("unknown")).toBe("gutter-navigate");
});
});
+55 -15
View File
@@ -8,6 +8,7 @@ import { Compartment, RangeSet, StateEffect, StateField, type Extension, type Te
import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view";
import { shownAgain, type EditorDocContext } from "@/editor/api";
import { navigationButton } from "./navigationButton";
import { tx } from "@/platform/i18n";
import { pluginsBackend } from "./backend";
import { commands, type LineMarkerInfo } from "@/platform/ipc";
import { gutterIconMarkup, isIconName } from "@/ui/icons";
@@ -22,7 +23,7 @@ const ICONS: Record<string, string> = {
overridden: "gutter-overridden",
overriding: "gutter-overriding",
run: "gutter-run",
navigate: "gutter-implementing",
navigate: "gutter-navigate",
};
/** Legacy aliases and ordinary registered icon names; navigate when unknown. */
@@ -32,10 +33,30 @@ export function markerIcon(icon: string | null): string {
}
interface Placed {
marker: LineMarkerInfo;
markers: LineMarkerInfo[];
icon: string;
pos: number;
}
function targetKey(marker: LineMarkerInfo): string {
const target = marker.target;
return target
? JSON.stringify([
target.path,
target.range.start.line,
target.range.start.col,
target.range.end.line,
target.range.end.col,
])
: JSON.stringify([marker.pluginId, marker.tooltip]);
}
function markerTooltip(markers: readonly LineMarkerInfo[]): string {
const descriptions = [...new Set(markers.map((marker) => marker.tooltip).filter(Boolean))];
if (markers.length === 1) return descriptions[0] ?? "";
return [`${tx("Related targets")} (${markers.length})`, ...descriptions.slice(0, 5)].join("\n");
}
const setMarkers = StateEffect.define<Placed[]>();
const markersField = StateField.define<Placed[]>({
@@ -47,31 +68,50 @@ const markersField = StateField.define<Placed[]>({
},
});
/** Markers on existing lines, at their line starts. */
/** One marker per line and resolved icon, retaining all distinct destinations. */
export function placeMarkers(doc: Text, markers: readonly LineMarkerInfo[]): Placed[] {
return markers.filter((m) => m.line < doc.lines).map((marker) => ({ marker, pos: doc.line(marker.line + 1).from }));
const groups = new Map<string, { placed: Placed; seen: Set<string> }>();
for (const marker of markers) {
if (!Number.isInteger(marker.line) || marker.line < 0 || marker.line >= doc.lines) continue;
const icon = markerIcon(marker.icon);
const key = `${marker.line}:${icon}`;
let group = groups.get(key);
if (!group) {
group = { placed: { markers: [], icon, pos: doc.line(marker.line + 1).from }, seen: new Set() };
groups.set(key, group);
}
const target = targetKey(marker);
if (!group.seen.has(target)) {
group.seen.add(target);
group.placed.markers.push(marker);
}
}
return [...groups.values()].map(({ placed }) => placed);
}
class PluginMarker extends GutterMarker {
constructor(readonly marker: LineMarkerInfo) {
constructor(readonly placed: Placed) {
super();
}
override eq(other: GutterMarker): boolean {
return (
other instanceof PluginMarker &&
other.marker.tooltip === this.marker.tooltip &&
other.marker.icon === this.marker.icon &&
JSON.stringify(other.marker.target) === JSON.stringify(this.marker.target)
other.placed.icon === this.placed.icon &&
other.placed.markers.length === this.placed.markers.length &&
other.placed.markers.every((marker, index) => {
const current = this.placed.markers[index]!;
return marker.tooltip === current.tooltip && targetKey(marker) === targetKey(current);
})
);
}
override toDOM(): Node {
const el = this.marker.target
? navigationButton([this.marker.target], markerIcon(this.marker.icon))
: document.createElement("span");
const targets = this.placed.markers.flatMap((marker) => (marker.target ? [marker.target] : []));
const tooltip = markerTooltip(this.placed.markers);
const el = targets.length ? navigationButton(targets, this.placed.icon, tooltip) : document.createElement("span");
el.className = `${styles.marker} cm-plugin-mapping-button`;
el.title = this.marker.tooltip;
el.setAttribute("aria-label", this.marker.tooltip);
el.innerHTML = gutterIconMarkup(markerIcon(this.marker.icon)) ?? "";
el.title = tooltip;
el.setAttribute("aria-label", tooltip);
el.innerHTML = gutterIconMarkup(this.placed.icon) ?? "";
return el;
}
}
@@ -156,7 +196,7 @@ const markerColumn: Extension = gutter({
class: `${styles.gutter} cm-openidea-gutter-pluginMarkers`,
markers: (view) =>
RangeSet.of(
view.state.field(markersField).map((m) => new PluginMarker(m.marker).range(m.pos)),
view.state.field(markersField).map((m) => new PluginMarker(m).range(m.pos)),
true,
),
});
+6 -5
View File
@@ -29,10 +29,11 @@ it("resolves declared package paths within their plugin and uses the dark SVG",
it("retains built-in names, legacy aliases and unknown-icon fallback", async () => {
expect(markerIcon(await resolveMarkerIcon("p", "database"))).toBe("database");
expect(markerIcon("overriding")).toBe("gutter-overriding");
expect(markerIcon("not-an-icon")).toBe("gutter-implementing");
expect(markerIcon("__proto__")).toBe("gutter-implementing");
expect(markerIcon("constructor")).toBe("gutter-implementing");
expect(markerIcon(null)).toBe("gutter-implementing");
expect(markerIcon("implementing")).toBe("gutter-implementing");
expect(markerIcon("not-an-icon")).toBe("gutter-navigate");
expect(markerIcon("__proto__")).toBe("gutter-navigate");
expect(markerIcon("constructor")).toBe("gutter-navigate");
expect(markerIcon(null)).toBe("gutter-navigate");
expect(mocks.assets).not.toHaveBeenCalled();
});
@@ -65,7 +66,7 @@ it("falls back when an asset fails to load", async () => {
mocks.assets.mockRejectedValue(new Error("disabled"));
const remove = projectMarkerIcon("p", { id: "resource", icon: "icons/resource.svg" });
try {
expect(markerIcon(await resolveMarkerIcon("p", "icons/resource.svg"))).toBe("gutter-implementing");
expect(markerIcon(await resolveMarkerIcon("p", "icons/resource.svg"))).toBe("gutter-navigate");
} finally {
remove();
warning.mockRestore();
+1
View File
@@ -1,4 +1,5 @@
{
"Related targets": "Related targets",
"GDB (C, C++)": "GDB (C, C++)",
"Unknown error": "Unknown error",
"Retry Loading Conversation": "Retry Loading Conversation",
+1
View File
@@ -1,4 +1,5 @@
{
"Related targets": "相关目标",
"GDB (C, C++)": "GDB(C、C++)",
"Unknown error": "未知错误",
"Retry Loading Conversation": "重新加载对话",
+1
View File
@@ -392,6 +392,7 @@ const ICONS = {
"toolwindow-coverage": "toolwindows/coverage",
"toolwindow-profiler": "toolwindows/profiler",
"toolwindow-logcat": "openidea/fileTypes/log",
"gutter-navigate": "general/autoscrollToSource",
"gutter-run": "gutter/run",
"gutter-run-all": "actions/runAll",
"gutter-run-passed": "gutter/runSuccess",