Compare commits

...
Author SHA1 Message Date
l0ng-ai ce8f0711cd fix(editor): run icons, inheritance markers and the doc toggle share one gutter column
Each kind of gutter icon had its own column: a file with doc comments kept an
empty column for the rendered-view toggle (shown on hover) beside the
inheritance markers, and run icons took a third, so the gutter looked twice
as wide as its icons.

A shared icon area (`iconAreaSource`, in core) now holds run icons, the
implements/overrides markers and the rendered-doc toggle in one column, as
wide as the most icons on any line; an area without icons takes no width.
Sources supply their markers, an optional reserved icon width (so icons that
come and go don't shift the text) and their own press handling. Hiding run
icons in Settings › Gutter Icons now drops them from the area instead of
hiding a column.
2026-11-15 03:54:51 +08:00
10 changed files with 232 additions and 93 deletions

No files matched your search

+1
View File
@@ -34,6 +34,7 @@ import type { LineEnding } from "@/platform/ipc";
import { create } from "zustand";
import { maybeEditorService } from "@/workbench/contributions";
import { cornerInset } from "@/editor/core/cm/cornerInset";
export { iconAreaSource, type IconAreaSource } from "@/editor/core/cm/iconArea";
/** Documents above this size (UTF-16 units) skip per-line decorations. */
export const HEAVY_LIMIT = 8_000_000;
+75
View File
@@ -0,0 +1,75 @@
import { afterEach, expect, it, vi } from "vitest";
import { fireEvent } from "@testing-library/react";
import { EditorState, RangeSet } from "@codemirror/state";
import { EditorView, GutterMarker } from "@codemirror/view";
import { iconAreaExtension, iconAreaSource } from "./iconArea";
class Dot extends GutterMarker {
constructor(readonly name: string) {
super();
}
override eq(other: GutterMarker) {
return other instanceof Dot && other.name === this.name;
}
override toDOM() {
const el = document.createElement("span");
el.className = `dot dot-${this.name}`;
return el;
}
}
let view: EditorView | null = null;
afterEach(() => {
view?.destroy();
view = null;
});
const DOC = "run()\nclass A implements B {}\n/** doc */\nend";
const lineStart = (state: EditorState, line: number) => state.doc.line(line).from;
function open(extra: Parameters<typeof iconAreaSource.of>[0][]) {
view = new EditorView({
state: EditorState.create({ doc: DOC, extensions: [iconAreaExtension, ...extra.map((s) => iconAreaSource.of(s))] }),
parent: document.body,
});
return view;
}
it("puts every source's icons in one column, in order within a line", () => {
const v = open([
{
order: 50,
markers: (view) =>
RangeSet.of([new Dot("impl").range(lineStart(view.state, 1)), new Dot("impl").range(lineStart(view.state, 2))]),
},
{ order: 10, markers: (view) => RangeSet.of([new Dot("run").range(lineStart(view.state, 1))]) },
]);
expect(v.dom.querySelectorAll(".cm-gutter")).toHaveLength(1);
const rows = [...v.dom.querySelectorAll(".cm-openidea-iconArea .cm-gutterElement")].filter((el) =>
el.querySelector(".dot"),
);
expect(rows.map((el) => [...el.querySelectorAll(".dot")].map((d) => d.className))).toEqual([
["dot dot-run", "dot dot-impl"],
["dot dot-impl"],
]);
});
it("routes a press to the source that handles it", () => {
const first = vi.fn(() => false);
const second = vi.fn(() => true);
const v = open([
{ order: 1, markers: (view) => RangeSet.of([new Dot("a").range(lineStart(view.state, 1))]), mousedown: first },
{ order: 2, markers: () => RangeSet.empty, mousedown: second },
]);
fireEvent.mouseDown(v.dom.querySelector(".dot-a")!, { button: 0 });
expect(first).toHaveBeenCalledTimes(1);
expect(second).toHaveBeenCalledTimes(1);
});
it("keeps an icon's width only while a source asks for it", () => {
const empty = open([{ order: 1, markers: () => RangeSet.empty }]);
expect(empty.dom.querySelector(".cm-openidea-iconSpacer")).toBeNull();
empty.destroy();
const v = open([{ order: 1, markers: () => RangeSet.empty, reserve: () => true }]);
expect(v.dom.querySelector(".cm-openidea-iconArea .cm-openidea-iconSpacer")).toBeTruthy();
});
+76
View File
@@ -0,0 +1,76 @@
/**
* The gutter's icon area (IDEA's line marker area): run icons, inheritance markers and the
* rendered-doc toggle share one column, as wide as the most icons on any line, instead of a
* column per kind that stays empty on the lines where another kind has its icon.
*/
import { Facet, type EditorState, type Extension, type RangeSet } from "@codemirror/state";
import { EditorView, GutterMarker, gutter, type BlockInfo, type ViewUpdate, type WidgetType } from "@codemirror/view";
export interface IconAreaSource {
/** Left to right within a line: lower first. */
order: number;
markers(view: EditorView): RangeSet<GutterMarker>;
/** The marker beside a block widget (a rendered doc comment), which has no text line. */
widgetMarker?(view: EditorView, widget: WidgetType, block: BlockInfo): GutterMarker | null;
/** Markers that changed without `markers` returning a new set (one shown on hover). */
lineMarkerChange?(update: ViewUpdate): boolean;
/** Keeps one icon of width while true, so icons that come and go don't shift the text. */
reserve?(state: EditorState): boolean;
/** A press on the area at `block`; true when the source handled it. */
mousedown?(view: EditorView, block: BlockInfo, event: MouseEvent): boolean;
}
export const iconAreaSource = Facet.define<IconAreaSource, readonly IconAreaSource[]>({
combine: (sources) => [...sources].sort((a, b) => a.order - b.order),
});
/** The width of one icon, for the reserved column. Hidden; not a button. */
class IconSpacer extends GutterMarker {
override toDOM(): Node {
const el = document.createElement("span");
el.className = "cm-openidea-iconSpacer";
return el;
}
}
const iconSpacer = new IconSpacer();
const noSpacer = new (class extends GutterMarker {})();
const reserved = (state: EditorState) => state.facet(iconAreaSource).some((s) => s.reserve?.(state));
const areaGutter = gutter({
class: "cm-openidea-iconArea",
markers: (view) => view.state.facet(iconAreaSource).map((s) => s.markers(view)),
widgetMarker: (view, widget, block) => {
for (const s of view.state.facet(iconAreaSource)) {
const marker = s.widgetMarker?.(view, widget, block);
if (marker) return marker;
}
return null;
},
lineMarkerChange: (update) =>
update.startState.facet(iconAreaSource) !== update.state.facet(iconAreaSource) ||
update.state.facet(iconAreaSource).some((s) => s.lineMarkerChange?.(update)),
initialSpacer: (view) => (reserved(view.state) ? iconSpacer : noSpacer),
updateSpacer: (_spacer, update) => (reserved(update.state) ? iconSpacer : noSpacer),
domEventHandlers: {
mousedown: (view, block, event) =>
view.state.facet(iconAreaSource).some((s) => s.mousedown?.(view, block, event as MouseEvent) ?? false),
},
});
const areaTheme = EditorView.baseTheme({
".cm-openidea-iconArea .cm-gutterElement": {
display: "flex",
alignItems: "center",
justifyContent: "flex-start",
gap: "2px",
},
// Room around the icons, not the element: an area without icons takes no width.
".cm-openidea-iconArea .cm-gutterElement > *": { margin: "0 1px" },
".cm-openidea-iconArea .cm-gutterElement > :first-child": { marginLeft: "5px" },
".cm-openidea-iconSpacer": { display: "inline-block", width: "14px" },
});
/** Installed in every editor; empty (no width) while no source has markers or reserves room. */
export const iconAreaExtension: Extension = [areaGutter, areaTheme];
+11 -6
View File
@@ -472,13 +472,18 @@ const noSurround = EditorView.inputHandler.of((view, from, to, text) => {
return true;
});
/** Gutter icon groups of Settings › Editor › General › Gutter Icons (stable gutter classes). */
/**
* Gutter icon groups of Settings › Editor › General › Gutter Icons. A hidden group sets
* `cm-openidea-hideGutter-<id>` on the editor (the theme hides the bookmark, color and
* script/compose run columns by it); the icon area's run and inheritance icons read the
* setting themselves.
*/
export const GUTTER_ICON_GROUPS = [
{ id: "run", gutterClass: "cm-openidea-gutter-run" },
{ id: "bookmarks", gutterClass: "cm-openidea-bookmarks" },
{ id: "inheritors", gutterClass: "cm-openidea-gutter-lineMarkers" },
{ id: "supers", gutterClass: "cm-openidea-gutter-lineMarkers" },
{ id: "colors", gutterClass: "cm-openidea-gutter-colors" },
{ id: "run" },
{ id: "bookmarks" },
{ id: "inheritors" },
{ id: "supers" },
{ id: "colors" },
] as const;
/**
@@ -5,6 +5,7 @@ import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { java } from "@codemirror/lang-java";
import { useI18n } from "@/platform/i18n";
import { iconAreaExtension } from "./iconArea";
import { renderDocComments, renderedDocsExtension, renderedRanges, toggleRenderedViewAtCaret } from "./renderedDocs";
let view: EditorView | null = null;
@@ -21,7 +22,7 @@ function open(reader: boolean, doc = DOC) {
view = new EditorView({
state: EditorState.create({
doc,
extensions: [java(), renderedDocsExtension, renderDocComments.of(reader)],
extensions: [java(), iconAreaExtension, renderedDocsExtension, renderDocComments.of(reader)],
}),
parent: document.body,
});
@@ -38,11 +39,8 @@ async function hover(v: EditorView, line: number) {
const pressed = (v: EditorView) => toggles(v).map((t) => t.getAttribute("aria-pressed"));
/** Whether the gutter gives the toggles a column: its spacer has their width, else none. */
const hasColumn = (v: EditorView) =>
[...v.dom.querySelectorAll<HTMLElement>(".cm-openidea-docToggles .cm-gutterElement span")].some(
(el) => el.style.width === "14px",
);
/** Whether the icon area keeps the toggles an icon's width: its spacer is the icon spacer, else none. */
const hasColumn = (v: EditorView) => !!v.dom.querySelector(".cm-openidea-iconArea .cm-openidea-iconSpacer");
/** Opens the file rendering its comments, then brings this one's source back with its pencil. */
async function openFlippedToSource() {
+25 -41
View File
@@ -51,13 +51,12 @@ import {
GutterMarker,
ViewPlugin,
WidgetType,
gutter,
type DecorationSet,
type ViewUpdate,
} from "@codemirror/view";
import { syntaxTree } from "@codemirror/language";
import { gutterIconMarkup } from "@/ui/icons";
import { emptyGutterSpacer } from "@/editor/api";
import { iconAreaSource } from "./iconArea";
import { tx, useI18n } from "@/platform/i18n";
import { loadMarkdown, markupToHtml } from "@/editor/lsp/markdown";
import { hasContent, renderDoc, type DocComment, type DocStyle, type RenderedDoc } from "./docRender";
@@ -358,18 +357,6 @@ function toggleMarker(rendered: boolean): ToggleMarker {
return new ToggleMarker(rendered, tx("Toggle Rendered View"));
}
/** Reserves the toggles' width. Not a `ToggleMarker`: the spacer is in the DOM, hidden, and must not read as a button. */
class ToggleSpacer extends GutterMarker {
override toDOM(): Node {
const el = document.createElement("span");
el.style.display = "inline-block";
el.style.width = "14px";
return el;
}
}
const toggleSpacer = new ToggleSpacer();
/** A doc comment, with where its first line starts and its last line ends, and whether it is rendered now. */
type PlacedComment = DocComment & { line: number; end: number; rendered: boolean };
@@ -658,8 +645,8 @@ const docsField = StateField.define<DocsView>({
provide: (f) => EditorView.decorations.from(f, (v) => v.decorations),
});
const toggleGutter = gutter({
class: "cm-openidea-docToggles",
const toggleSource = iconAreaSource.of({
order: 90,
// Comments left as source: their first line is a text line, which reads these markers.
// Asked again on every update, so the icon follows the mouse.
markers: (view) => sourceToggle(view.state),
@@ -674,24 +661,21 @@ const toggleGutter = gutter({
lineMarkerChange: (update) =>
update.startState.field(hovered) !== update.state.field(hovered) ||
update.transactions.some((tr) => tr.effects.some((e) => e.is(relabel))),
// A file without doc comments gets no column at all; one with them keeps its column
// while the icon comes and goes, so the text does not shift.
initialSpacer: (view) => (placedComments(view.state).length ? toggleSpacer : emptyGutterSpacer),
updateSpacer: (_spacer, update) => (placedComments(update.state).length ? toggleSpacer : emptyGutterSpacer),
domEventHandlers: {
mousedown(view, block, event) {
const target = event.target instanceof Element ? event.target.closest(".cm-openidea-docToggle") : null;
if ((event as MouseEvent).button !== 0 || !target) return false;
// A file that renders none may not have been scanned since its last edit: the flip
// must land on where its comment starts now.
const comment = active(view.state)
? commentOverlapping(view.state, block.from, block.to)
: (placeComments(view.state).find((c) => block.from <= c.to && block.to >= c.line)?.from ?? null);
if (comment === null) return false;
event.preventDefault();
view.dispatch({ effects: toggleRenderedView.of(comment) });
return true;
},
// A file with doc comments keeps an icon's width while the icon comes and goes, so the
// text does not shift; it shares the icon area with the run and inheritance icons.
reserve: (state) => placedComments(state).length > 0,
mousedown(view, block, event) {
const target = event.target instanceof Element ? event.target.closest(".cm-openidea-docToggle") : null;
if (event.button !== 0 || !target) return false;
// A file that renders none may not have been scanned since its last edit: the flip
// must land on where its comment starts now.
const comment = active(view.state)
? commentOverlapping(view.state, block.from, block.to)
: (placeComments(view.state).find((c) => block.from <= c.to && block.to >= c.line)?.from ?? null);
if (comment === null) return false;
event.preventDefault();
view.dispatch({ effects: toggleRenderedView.of(comment) });
return true;
},
});
@@ -874,15 +858,15 @@ const docsTheme = EditorView.theme({
".cm-openidea-renderedDocSections dt": { color: "var(--syn-doc-comment)" },
".cm-openidea-renderedDocSections dd": { margin: "0" },
".cm-openidea-renderedDocText p": { display: "inline", margin: "0" },
".cm-openidea-docToggles .cm-gutterElement": {
display: "flex",
alignItems: "flex-start",
justifyContent: "center",
padding: "0 2px",
},
".cm-openidea-docToggle": {
// At the comment's first line, at the right of the icon area (IDEA aligns it right).
alignSelf: "flex-start",
marginLeft: "auto",
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
// An icon's width, like the other icons of the area, so showing it doesn't widen the area.
width: "14px",
height: "var(--openidea-editor-line-height, var(--lh-code))",
padding: "0",
border: "none",
@@ -902,7 +886,7 @@ export const renderedDocsExtension: Extension = [
hovered,
hoverTracker,
showSourceAtCaret,
toggleGutter,
toggleSource,
markdownReady,
languageWatcher,
docMetrics,
+2
View File
@@ -42,6 +42,7 @@ import { rainbowBrackets } from "./rainbowBrackets";
import { stickyMaxLines, stickyScroll } from "./stickyScroll";
import { findExtension } from "./find";
import { codeLensExtension } from "./codeLens";
import { iconAreaExtension } from "./iconArea";
import { renderedDocsExtension } from "./renderedDocs";
import { caretExtension, fontZoom, methodLinesField, prefsExtension, softWrapActive } from "./prefs";
import { commentTokens } from "./languages";
@@ -396,6 +397,7 @@ export function baseExtensions(o: SetupOptions): Extension {
overviewLane,
cornerInset,
codeLensExtension,
iconAreaExtension,
renderedDocsExtension,
EditorView.contentAttributes.of({
spellcheck: "false",
@@ -1,10 +1,4 @@
/* Editor gutter inheritance icons (implemented / overridden / implementing / overriding). */
.gutter :global(.cm-gutterElement) {
display: flex;
align-items: center;
justify-content: center;
gap: 1px;
}
.marker {
width: 14px;
height: 14px;
+15 -15
View File
@@ -15,7 +15,8 @@
* implementation search walks the whole workspace, so asking again is costly.
*/
import { RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state";
import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view";
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
import { iconAreaSource } from "@/editor/core/cm/iconArea";
import { shownAgain, type EditorDocContext } from "@/editor/api";
import { docOf, debounce, isUnavailable, lspRequest } from "@/editor/lsp/lsp";
import { featureBackoff, onServerSettled } from "@/editor/lsp/availability";
@@ -769,21 +770,20 @@ export function implementationGutter(ctx: EditorDocContext): Extension {
return [
markersField,
ViewPlugin.fromClass(MarkersPlugin),
gutter({
class: `${styles.gutter} cm-openidea-gutter-lineMarkers`,
// In the shared icon area, beside run icons and the rendered-doc toggle.
iconAreaSource.of({
order: 50,
markers: (view) => markerSet(view.state.field(markersField)),
domEventHandlers: {
mousedown: (view, block, event) => {
const e = event as MouseEvent;
if (e.button !== 0) return false;
const target = e.target instanceof Element ? e.target : null;
const marker = markerAt(view, block.from, target);
if (!marker) return false;
e.preventDefault();
const rect = (target?.closest(`.${styles.marker}`) ?? target)?.getBoundingClientRect();
void openMarkerTargets(marker, rect ? { x: rect.left, y: rect.bottom + 2 } : { x: e.clientX, y: e.clientY });
return true;
},
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);
if (!marker) return false;
e.preventDefault();
const rect = (target.closest(`.${styles.marker}`) ?? target).getBoundingClientRect();
void openMarkerTargets(marker, { x: rect.left, y: rect.bottom + 2 });
return true;
},
}),
];
+23 -19
View File
@@ -5,8 +5,9 @@
* edits and mapped through changes in between.
*/
import { RangeSet, StateEffect, StateField, type EditorState, type Extension } from "@codemirror/state";
import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view";
import { emptyGutterSpacer, gutterSpacer, type EditorDocContext } from "@/editor/api";
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
import { iconAreaSource, type EditorDocContext } from "@/editor/api";
import { editorPrefs } from "@/platform/editorSettings";
import { useProject } from "@/platform/project";
import { isVirtualUri } from "@/platform/virtualDocs";
import { shortcutLabel } from "@/platform/keymap";
@@ -174,7 +175,11 @@ class RunMarker extends GutterMarker {
}
}
const runSpacer = gutterSpacer(styles.runMarker ?? "");
/** Settings › Editor › General › Gutter Icons › Run icons (on with the gutter icons). */
function runIconsShown(): boolean {
const prefs = editorPrefs();
return prefs.showGutterIcons && !prefs.gutterIconsDisabled.includes("run");
}
/** The gutter asks for markers on every update: rebuild only when targets or test results changed. */
const markerCache = new WeakMap<readonly PlacedTarget[], { version: number; set: RangeSet<GutterMarker> }>();
@@ -262,22 +267,21 @@ export function runGutter(ctx: EditorDocContext): Extension {
return [
targetsField,
detector,
gutter({
class: `${styles.runGutter} cm-openidea-gutter-run`,
markers: (view) => markers(view.state, path),
// One icon of width only while the file has run targets; other files keep a narrow gutter.
initialSpacer: () => emptyGutterSpacer,
updateSpacer: (_spacer, update) => (update.state.field(targetsField).length ? runSpacer : emptyGutterSpacer),
domEventHandlers: {
mousedown: (view, block, event) => {
const target = view.state.field(targetsField).find((t) => t.pos === block.from);
if (!target || (event as MouseEvent).button !== 0) return false;
const el = (event.target as HTMLElement).closest?.(`.${styles.runMarker}`) as HTMLElement | null;
const rect = (el ?? (event.target as HTMLElement)).getBoundingClientRect();
event.preventDefault();
showTargetMenu(view, target, path, { x: rect.left, y: rect.bottom + 2 });
return true;
},
// In the shared icon area, beside the inheritance icons and the rendered-doc toggle.
iconAreaSource.of({
order: 10,
markers: (view) => (runIconsShown() ? markers(view.state, path) : RangeSet.empty),
// One icon of width while the file has run targets, so icons don't shift the text as they load.
reserve: (state) => runIconsShown() && state.field(targetsField).length > 0,
mousedown: (view, block, event) => {
if (event.button !== 0 || !runIconsShown()) return false;
const el = (event.target as HTMLElement).closest?.(`.${styles.runMarker}`) as HTMLElement | null;
const target = el && view.state.field(targetsField).find((t) => t.pos === block.from);
if (!el || !target) return false;
const rect = el.getBoundingClientRect();
event.preventDefault();
showTargetMenu(view, target, path, { x: rect.left, y: rect.bottom + 2 });
return true;
},
}),
];