From c088fa8b26d418027d512496668294a60b21c320 Mon Sep 17 00:00:00 2001 From: Muu Date: Wed, 11 Nov 2026 01:39:06 +0800 Subject: [PATCH] fix(editor): collapse empty icon gutters (#2566) The run, npm-script, HTTP-endpoint and bookmark gutters reserved a column even when a file had none of their icons, pushing the code right by an empty strip. Each now emits a zero-width, non-interactive spacer marker when it has nothing to show, so the column collapses; files with icons are unchanged. A shared `gutterSpacer` factory keeps the spacer from reacting to clicks. Co-authored-by: muutot <92625353+muutot@users.noreply.github.com> --- src/editor/api.ts | 16 ++++++++++++++++ src/features/http-client/HttpClient.module.css | 8 ++++---- src/features/http-client/endpoints.ts | 6 ++++++ src/features/run/Run.module.css | 8 +++----- src/features/run/npmGutter.ts | 8 ++++++-- src/features/run/runGutter.ts | 8 ++++++-- src/features/search/bookmarks/gutter.ts | 8 ++++++-- 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/editor/api.ts b/src/editor/api.ts index 3e2ae89c3..266747646 100644 --- a/src/editor/api.ts +++ b/src/editor/api.ts @@ -335,6 +335,22 @@ export class LaneMarker extends GutterMarker { } } +/** + * A gutter spacer that renders nothing: pair it with `gutter`'s `updateSpacer` + * to reserve icon width only while the file has something to show. + */ +export const emptyGutterSpacer = new (class extends GutterMarker {})(); + +/** A gutter spacer reserving one icon of width, styled by `className`. */ +export const gutterSpacer = (className: string) => + new (class extends GutterMarker { + override toDOM(): Node { + const el = document.createElement("span"); + el.className = className; + return el; + } + })(); + /** * The floating row at the editor's top-right (IDEA's inspection widget area): widgets * append their element to it (CSS `order` sorts them; lower first) and remove it when diff --git a/src/features/http-client/HttpClient.module.css b/src/features/http-client/HttpClient.module.css index 2c32796ce..095debd3b 100644 --- a/src/features/http-client/HttpClient.module.css +++ b/src/features/http-client/HttpClient.module.css @@ -1,11 +1,11 @@ /* Editor gutter ▶ per request (same look as run configuration targets). */ -.runGutter { - width: 14px; -} +/* No fixed width: the endpoints gutter is conditional (updateSpacer), so files + without endpoint markers keep a narrow gutter. */ .runGutter :global(.cm-gutterElement) { display: flex; align-items: center; - justify-content: center; + /* Spare width stays left of the icons, which sit next to the code. */ + justify-content: flex-end; } .runMarker { width: 14px; diff --git a/src/features/http-client/endpoints.ts b/src/features/http-client/endpoints.ts index 161481966..d0fbac42f 100644 --- a/src/features/http-client/endpoints.ts +++ b/src/features/http-client/endpoints.ts @@ -7,6 +7,7 @@ import { executeAction } from "@/platform/actions"; import { tx } from "@/platform/i18n"; import { useProject } from "@/platform/project"; import { gutterIconMarkup, showContextMenu } from "@/ui"; +import { emptyGutterSpacer, gutterSpacer } from "@/editor/api"; import { notifications, onEndpointsChanged, searchEndpoints, type EndpointEntry } from "@/workbench/contributions"; import { appendGeneratedRequests } from "./generatedRequests"; import { METHODS } from "./language"; @@ -90,6 +91,8 @@ class EndpointMarker extends GutterMarker { } } +const endpointSpacer = gutterSpacer(styles.runMarker ?? ""); + export function endpointEditorExtension(ctx: EditorDocContext): Extension { if (ctx.language === "http") return EditorState.languageData.of(() => [{ openideaCompletion: endpointCompletion }]); if (ctx.virtual) return []; @@ -137,6 +140,9 @@ export function endpointEditorExtension(ctx: EditorDocContext): Extension { view.state.field(marks).map((m) => new EndpointMarker().range(m.pos)), true, ), + // One icon of width only while the file has endpoints; other files keep a narrow gutter. + initialSpacer: () => emptyGutterSpacer, + updateSpacer: (_spacer, update) => (update.state.field(marks).length ? endpointSpacer : emptyGutterSpacer), domEventHandlers: { mousedown: (view, line, event) => { if ((event as MouseEvent).button !== 0) return false; diff --git a/src/features/run/Run.module.css b/src/features/run/Run.module.css index 4d191a74f..8c3717cf8 100644 --- a/src/features/run/Run.module.css +++ b/src/features/run/Run.module.css @@ -875,14 +875,12 @@ } } -/* Editor gutter ▶ run icons */ -.runGutter { - width: 14px; -} +/* Editor gutter ▶ run icons (the column collapses when the file has none) */ .runGutter :global(.cm-gutterElement) { display: flex; align-items: center; - justify-content: center; + /* Spare width stays left of the icons, which sit next to the code. */ + justify-content: flex-end; } .runMarker { width: 14px; diff --git a/src/features/run/npmGutter.ts b/src/features/run/npmGutter.ts index cf75cbb29..46afa2b97 100644 --- a/src/features/run/npmGutter.ts +++ b/src/features/run/npmGutter.ts @@ -5,7 +5,7 @@ */ import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state"; import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; -import type { EditorDocContext } from "@/editor/api"; +import { emptyGutterSpacer, gutterSpacer, type EditorDocContext } from "@/editor/api"; import { executeAction } from "@/platform/actions"; import { tx } from "@/platform/i18n"; import { call, commands, type RunConfig } from "@/platform/ipc"; @@ -338,6 +338,8 @@ class ScriptMarker extends GutterMarker { } } +const scriptSpacer = gutterSpacer(styles.runMarker ?? ""); + const DEBOUNCE = 300; export function npmGutter(ctx: EditorDocContext): Extension { @@ -378,7 +380,9 @@ export function npmGutter(ctx: EditorDocContext): Extension { view.state.field(entriesField).map((e) => new ScriptMarker(e, path).range(e.pos)), true, ), - initialSpacer: () => new ScriptMarker({ name: "", line: 0 }, path), + // Reserve width only while the file has scripts, like the run gutter. + initialSpacer: () => emptyGutterSpacer, + updateSpacer: (_spacer, update) => (update.state.field(entriesField).length ? scriptSpacer : emptyGutterSpacer), domEventHandlers: { mousedown: (view, block, event) => { const entry = view.state.field(entriesField).find((e) => e.pos === block.from); diff --git a/src/features/run/runGutter.ts b/src/features/run/runGutter.ts index 9d630c965..c40459484 100644 --- a/src/features/run/runGutter.ts +++ b/src/features/run/runGutter.ts @@ -6,7 +6,7 @@ */ import { RangeSet, StateEffect, StateField, type EditorState, type Extension } from "@codemirror/state"; import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; -import type { EditorDocContext } from "@/editor/api"; +import { emptyGutterSpacer, gutterSpacer, type EditorDocContext } from "@/editor/api"; import { useProject } from "@/platform/project"; import { isVirtualUri } from "@/platform/virtualDocs"; import { shortcutLabel } from "@/platform/keymap"; @@ -174,6 +174,8 @@ class RunMarker extends GutterMarker { } } +const runSpacer = gutterSpacer(styles.runMarker ?? ""); + /** The gutter asks for markers on every update: rebuild only when targets or test results changed. */ const markerCache = new WeakMap }>(); @@ -263,7 +265,9 @@ export function runGutter(ctx: EditorDocContext): Extension { gutter({ class: `${styles.runGutter} cm-openidea-gutter-run`, markers: (view) => markers(view.state, path), - initialSpacer: () => new RunMarker({ line: 0, pos: 0, kind: "test", name: "", path: [] }, 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); diff --git a/src/features/search/bookmarks/gutter.ts b/src/features/search/bookmarks/gutter.ts index 32726bd3e..f1b038230 100644 --- a/src/features/search/bookmarks/gutter.ts +++ b/src/features/search/bookmarks/gutter.ts @@ -7,6 +7,7 @@ */ import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state"; import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; +import { emptyGutterSpacer } from "@/editor/api"; import { hasPrimaryModifier } from "@/platform/keymap"; import { marksOf, setFileLines, updateBookmarks, useBookmarks } from "./store"; @@ -157,7 +158,9 @@ export function bookmarkGutter(path: string, toggle: (line: number) => void): Ex gutter({ class: "cm-openidea-bookmarks", markers: (v) => v.state.field(field), - initialSpacer: () => spacer, + // Reserve the bookmark's width only while this file has bookmarks. + initialSpacer: () => emptyGutterSpacer, + updateSpacer: (_spacer, u) => (u.state.field(field).size ? spacer : emptyGutterSpacer), }), theme, ]; @@ -167,7 +170,8 @@ const theme = EditorView.baseTheme({ ".cm-openidea-bookmarks .cm-gutterElement": { display: "flex", alignItems: "center", - justifyContent: "center", + // Spare width stays left of the bookmark, which sits next to the code. + justifyContent: "flex-end", padding: "0 2px", }, ".cm-openidea-bookmark": {