From d171a946febb279469b50e1e7f2dbee46bad99f9 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 15 Nov 2026 10:31:42 +0800 Subject: [PATCH] fix(editor): Spring, endpoint, npm, Docker and plugin gutter icons join the shared icon area (#3472) Following #3452, the remaining line-marker kinds kept a gutter column each (a Spring controller showed its mapping icons in a second column beside the run icon). Scripts and Docker/Compose targets join the run icons in the shared icon area (hidden with them), Spring mappings and endpoints follow, then plugin line markers, gutter actions and indexed mappings (now 14px like the others). Presses go to the pressed icon's source. --- e2e/pluginEndpoints.spec.ts | 4 +- e2e/pluginMarkerIcons.spec.ts | 6 +- .../http-client/HttpClient.module.css | 2 - src/features/http-client/endpoints.ts | 56 +++++++++---------- src/features/http-client/spring/editor.ts | 35 +++++++----- src/features/plugins/indexedMarkers.test.ts | 3 +- src/features/plugins/indexedMarkers.ts | 17 +++--- src/features/plugins/languageVisuals.test.ts | 12 ++-- src/features/plugins/languageVisuals.ts | 31 +++------- src/features/plugins/lineMarkerGutter.test.ts | 19 +++++-- src/features/plugins/lineMarkerGutter.ts | 30 ++++------ src/features/run/npmGutter.test.ts | 6 +- src/features/run/npmGutter.ts | 39 ++++++------- src/features/run/runGutter.ts | 2 +- src/features/services/Services.module.css | 8 --- src/features/services/dockerGutter.test.ts | 6 +- src/features/services/dockerGutter.ts | 51 +++++++++-------- 17 files changed, 154 insertions(+), 173 deletions(-) diff --git a/e2e/pluginEndpoints.spec.ts b/e2e/pluginEndpoints.spec.ts index a9a222780..381dd86fa 100644 --- a/e2e/pluginEndpoints.spec.ts +++ b/e2e/pluginEndpoints.spec.ts @@ -33,7 +33,7 @@ test("plugin routes feed search, HTTP completion and the declaration gutter and }); await editorService().openFile("/dev/acme-api/routes.txt"); }); - await expect(page.locator('.cm-openidea-gutter-endpoints button[title="Open in HTTP Client"]')).toBeVisible(); + await expect(page.locator('.cm-openidea-iconArea button[title="Open in HTTP Client"]')).toBeVisible(); await doubleShift(page); const search = page.getByRole("dialog", { name: "Search Everywhere" }); await search.getByRole("combobox").fill("/people"); @@ -59,5 +59,5 @@ test("plugin routes feed search, HTTP completion and the declaration gutter and const path = "/src/workbench/contributions.ts"; await (await import(path)).editorService().openFile("/dev/acme-api/routes.txt"); }); - await expect(page.locator('.cm-openidea-gutter-endpoints button[title="Open in HTTP Client"]')).toBeHidden(); + await expect(page.locator('.cm-openidea-iconArea button[title="Open in HTTP Client"]')).toBeHidden(); }); diff --git a/e2e/pluginMarkerIcons.spec.ts b/e2e/pluginMarkerIcons.spec.ts index 60499e7ef..0e38b5d85 100644 --- a/e2e/pluginMarkerIcons.spec.ts +++ b/e2e/pluginMarkerIcons.spec.ts @@ -67,13 +67,13 @@ test("plugin markers render names and package SVG variants and keep the indexed ); await editorService().openFile(source); }); - const markers = page.locator(".cm-openidea-gutter-pluginMarkers"); + const markers = page.locator(".cm-openidea-iconArea"); await expect(markers.locator('[title="Run marker"] svg')).toHaveAttribute("data-icon", "gutter-run"); await expect(markers.locator('[title="Database marker"] svg')).toHaveAttribute("data-icon", "database"); const custom = markers.locator('[title="Package marker"] svg'); await expect(custom).toHaveAttribute("data-icon", /^ext:pluginIcon\/hello\//); // CodeMirror clips every gutter column to its width (`overflow: hidden`), so an icon wider than - // the column loses its edges: each line-marker icon stays inside the 14px column. + // the column loses its edges: each line-marker icon stays inside the shared icon area. const outside = await markers.evaluate((column) => { const bounds = column.getBoundingClientRect(); return [...column.querySelectorAll("svg")].filter((svg) => { @@ -96,7 +96,7 @@ test("plugin markers render names and package SVG variants and keep the indexed await expect(custom.locator("rect")).toBeVisible(); await expect(custom.locator("circle")).toBeHidden(); await page.screenshot({ path: test.info().outputPath("markers-light.png"), animations: "disabled" }); - await page.locator(".cm-plugin-mapping-gutter button").click(); + await markers.locator('button[aria-label^="Go to mapping:"]').click(); const chooser = page.getByRole("dialog", { name: "Choose Mapping Target" }); await expect(chooser.getByRole("option")).toHaveCount(2); await page.screenshot({ path: test.info().outputPath("markers-chooser.png"), animations: "disabled" }); diff --git a/src/features/http-client/HttpClient.module.css b/src/features/http-client/HttpClient.module.css index a17b43e25..f2fff82c5 100644 --- a/src/features/http-client/HttpClient.module.css +++ b/src/features/http-client/HttpClient.module.css @@ -1,6 +1,4 @@ /* Editor gutter ▶ per request (same look as run configuration targets). */ -/* 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; diff --git a/src/features/http-client/endpoints.ts b/src/features/http-client/endpoints.ts index 054775a87..5ffde4d6a 100644 --- a/src/features/http-client/endpoints.ts +++ b/src/features/http-client/endpoints.ts @@ -1,13 +1,11 @@ import type { CompletionContext, CompletionResult } from "@codemirror/autocomplete"; import { EditorState, RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state"; import { EditorView, GutterMarker, ViewPlugin } from "@codemirror/view"; -import { gutter } from "@codemirror/view"; -import type { EditorDocContext } from "@/editor/api"; +import { iconAreaSource, type EditorDocContext } from "@/editor/api"; 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 { headerCompletion } from "./headers"; @@ -89,6 +87,7 @@ class EndpointMarker extends GutterMarker { const button = document.createElement("button"); button.type = "button"; button.className = styles.runMarker ?? ""; + button.dataset.iconSource = "endpoint"; button.title = tx("Open in HTTP Client"); button.setAttribute("aria-label", button.title); button.innerHTML = gutterIconMarkup("file-http") ?? ""; @@ -96,8 +95,6 @@ class EndpointMarker extends GutterMarker { } } -const endpointSpacer = gutterSpacer(styles.runMarker ?? ""); - export function endpointEditorExtension(ctx: EditorDocContext): Extension { if (ctx.language === "http") return EditorState.languageData.of(() => [ @@ -142,36 +139,35 @@ export function endpointEditorExtension(ctx: EditorDocContext): Extension { } }, ), - gutter({ - class: `${styles.runGutter} cm-openidea-gutter-endpoints`, + // In the shared icon area, after the run icons. + iconAreaSource.of({ + order: 30, markers: (view) => RangeSet.of( 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; - const found = view.state.field(marks).find((m) => m.pos === line.from); - if (!found) return false; - event.preventDefault(); - const rect = (event.target as HTMLElement).getBoundingClientRect(); - showContextMenu( - { x: rect.left, y: rect.bottom }, - found.endpoints.map((e) => ({ - label: `${e.method} ${e.url}`, - verbatim: true, - icon: "file-http", - run: () => { - void executeAction(OPEN_ENDPOINT, e); - }, - })), - ); - return true; - }, + // One icon of width while the file has endpoints, so icons don't shift the text as they load. + reserve: (state) => state.field(marks).length > 0, + mousedown: (view, line, event) => { + if (event.button !== 0) return false; + const el = (event.target as HTMLElement).closest?.('[data-icon-source="endpoint"]') as HTMLElement | null; + const found = el && view.state.field(marks).find((m) => m.pos === line.from); + if (!el || !found) return false; + event.preventDefault(); + const rect = el.getBoundingClientRect(); + showContextMenu( + { x: rect.left, y: rect.bottom }, + found.endpoints.map((e) => ({ + label: `${e.method} ${e.url}`, + verbatim: true, + icon: "file-http", + run: () => { + void executeAction(OPEN_ENDPOINT, e); + }, + })), + ); + return true; }, }), ]; diff --git a/src/features/http-client/spring/editor.ts b/src/features/http-client/spring/editor.ts index 0243b1451..08ae07c42 100644 --- a/src/features/http-client/spring/editor.ts +++ b/src/features/http-client/spring/editor.ts @@ -4,8 +4,14 @@ * requests into the `generated-requests.http` scratch and open it. */ import { RangeSet, StateEffect, StateField, type EditorState, type Extension } from "@codemirror/state"; -import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; -import { activeEditor, intentionProviders, type ContributedIntention, type EditorDocContext } from "@/editor/api"; +import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view"; +import { + activeEditor, + iconAreaSource, + intentionProviders, + type ContributedIntention, + type EditorDocContext, +} from "@/editor/api"; import { appendGeneratedRequests } from "../generatedRequests"; import { call, commands } from "@/platform/ipc"; import { useProject } from "@/platform/project"; @@ -201,6 +207,7 @@ class SpringMarker extends GutterMarker { override toDOM(): Node { const el = document.createElement("div"); el.className = styles.runMarker ?? ""; + el.dataset.iconSource = "spring"; el.title = this.mark.title; el.setAttribute("role", "button"); el.setAttribute("aria-label", this.mark.title); @@ -269,23 +276,23 @@ export function springEditorExtension(ctx: EditorDocContext): Extension { marksField, detector, intentionProviders.of(intentions), - gutter({ - class: `${styles.runGutter} cm-openidea-gutter-spring`, + // In the shared icon area, after the run icons. + iconAreaSource.of({ + order: 30, markers: (view) => RangeSet.of( view.state.field(marksField).map((m) => new SpringMarker(m).range(m.pos)), true, ), - domEventHandlers: { - mousedown: (view, block, event) => { - const mark = view.state.field(marksField).find((m) => m.pos === block.from); - if (!mark || (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(); - showContextMenu({ x: rect.left, y: rect.bottom + 2 }, mark.items(path, view.state.doc.toString())); - return true; - }, + mousedown: (view, block, event) => { + if (event.button !== 0) return false; + const el = (event.target as HTMLElement).closest?.('[data-icon-source="spring"]') as HTMLElement | null; + const mark = el && view.state.field(marksField).find((m) => m.pos === block.from); + if (!el || !mark) return false; + const rect = el.getBoundingClientRect(); + event.preventDefault(); + showContextMenu({ x: rect.left, y: rect.bottom + 2 }, mark.items(path, view.state.doc.toString())); + return true; }, }), ]; diff --git a/src/features/plugins/indexedMarkers.test.ts b/src/features/plugins/indexedMarkers.test.ts index cbb27dda1..1032d33d5 100644 --- a/src/features/plugins/indexedMarkers.test.ts +++ b/src/features/plugins/indexedMarkers.test.ts @@ -1,6 +1,7 @@ import { afterEach, expect, it, vi } from "vitest"; import { EditorState, type RangeSet, type StateEffect } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; +import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import { offsetsToRange } from "@/editor/lsp/positions"; import type { FileMarkers } from "@/platform/ipc"; import { indexedMarkers } from "./indexedMarkers"; @@ -25,7 +26,7 @@ const source = '\n\n'; function setup() { view = new EditorView({ parent: document.body, - state: EditorState.create({ doc: source, extensions: [indexedMarkers("/p/Query.xml")] }), + state: EditorState.create({ doc: source, extensions: [iconAreaExtension, indexedMarkers("/p/Query.xml")] }), }); const range = (from: number, to: number) => offsetsToRange(view!.state.doc, from, to); const target = { path: "/p/Query.java", range: range(0, 4) }; diff --git a/src/features/plugins/indexedMarkers.ts b/src/features/plugins/indexedMarkers.ts index b0608ab7a..f1dafe7f0 100644 --- a/src/features/plugins/indexedMarkers.ts +++ b/src/features/plugins/indexedMarkers.ts @@ -2,11 +2,11 @@ * Gutter markers from plugins' document indexers, resolved by the host from * its cached graph. A click goes to the marker's targets (a chooser when there * are several); an edit keeps them on their lines until the edited text is indexed, - * so the column keeps its width while typing. + * so the icon area keeps its width while typing. */ import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state"; -import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; -import { registerEditorContribution } from "@/editor/api"; +import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view"; +import { iconAreaSource, registerEditorContribution } from "@/editor/api"; import { rangeToOffsets } from "@/editor/lsp/positions"; import { editorPrefs } from "@/platform/editorSettings"; import { useProject } from "@/platform/project"; @@ -87,7 +87,12 @@ export function markersOf( export function indexedMarkers(path: string): Extension { return [ markersField, - gutter({ class: "cm-plugin-mapping-gutter", markers: (v) => v.state.field(markersField) }), + // In the shared icon area, after the plugins' line markers and gutter actions. + iconAreaSource.of({ + order: 70, + markers: (view) => view.state.field(markersField), + reserve: (state) => state.field(markersField).size > 0, + }), EditorView.baseTheme({ ".cm-plugin-mapping-button": { border: "none", @@ -99,10 +104,6 @@ export function indexedMarkers(path: string): Extension { display: "flex", alignItems: "center", }, - // Only the indexed-marker column sizes its icons to 16px. Line markers reuse the button class - // in their own 14px column (lineMarkerGutter.ts, LineMarkers.module.css), whose `overflow: hidden` - // would clip an icon this size at its edges. - ".cm-plugin-mapping-gutter .cm-plugin-mapping-button svg": { width: "16px", height: "16px" }, ".cm-plugin-mapping-button:focus-visible": { outline: "1px solid var(--accent)" }, }), ViewPlugin.fromClass( diff --git a/src/features/plugins/languageVisuals.test.ts b/src/features/plugins/languageVisuals.test.ts index da607dde5..9bdc5c632 100644 --- a/src/features/plugins/languageVisuals.test.ts +++ b/src/features/plugins/languageVisuals.test.ts @@ -2,6 +2,7 @@ import { afterEach, expect, it, vi } from "vitest"; import { EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language"; +import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import { invalidateNavigationProviders } from "@/editor/navigationProviders"; const ipc = vi.hoisted(() => ({ pluginLanguageVisuals: vi.fn(), @@ -54,7 +55,7 @@ it("renders owned gutter commands and drops answers to superseded buffers", asyn }; ipc.pluginLanguageVisuals.mockResolvedValue({ status: "ok", data: visual }); const view = new EditorView({ - state: EditorState.create({ doc: "before", extensions: pluginLanguageVisuals(ctx) }), + state: EditorState.create({ doc: "before", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }), parent: document.body, }); views.push(view); @@ -79,14 +80,14 @@ it("keeps Run buttons mounted while edits and delayed refreshes move their lines const visual = { version: 1, injections: [], colors: [], gutterActions: [["riscv", action]] }; ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual }); const view = new EditorView({ - state: EditorState.create({ doc: "sum: ret", extensions: pluginLanguageVisuals(ctx) }), + state: EditorState.create({ doc: "sum: ret", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }), parent: document.body, }); views.push(view); await vi.advanceTimersByTimeAsync(300); const button = view.dom.querySelector(".cm-plugin-gutter-action")!; expect(button).not.toBeNull(); - const column = view.dom.querySelector(".cm-plugin-gutter-actions"); + const area = view.dom.querySelector(".cm-openidea-iconArea"); let answer!: (value: unknown) => void; ipc.pluginLanguageVisuals.mockImplementationOnce(() => new Promise((resolve) => (answer = resolve))); view.dispatch({ changes: { from: 8, insert: " " } }); @@ -96,7 +97,7 @@ it("keeps Run buttons mounted while edits and delayed refreshes move their lines answer({ status: "ok", data: visual }); await vi.advanceTimersByTimeAsync(0); expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button); - expect(view.dom.querySelector(".cm-plugin-gutter-actions")).toBe(column); + expect(view.dom.querySelector(".cm-openidea-iconArea")).toBe(area); ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual }); invalidateNavigationProviders(); @@ -130,7 +131,7 @@ it("retains buttons on refresh failures and removes them when the provider confi }; ipc.pluginLanguageVisuals.mockResolvedValueOnce({ status: "ok", data: visual }); const view = new EditorView({ - state: EditorState.create({ doc: "main: ret", extensions: pluginLanguageVisuals(ctx) }), + state: EditorState.create({ doc: "main: ret", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }), parent: document.body, }); views.push(view); @@ -147,5 +148,4 @@ it("retains buttons on refresh failures and removes them when the provider confi expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBe(button); await vi.advanceTimersByTimeAsync(300); expect(view.dom.querySelector(".cm-plugin-gutter-action")).toBeNull(); - expect(view.dom.querySelector(".cm-plugin-gutter-actions")).toBeNull(); }); diff --git a/src/features/plugins/languageVisuals.ts b/src/features/plugins/languageVisuals.ts index 465c63b80..809675951 100644 --- a/src/features/plugins/languageVisuals.ts +++ b/src/features/plugins/languageVisuals.ts @@ -1,25 +1,16 @@ /** Embedded highlighting and command gutter actions share the editor-provider refresh. */ -import { - Compartment, - Prec, - RangeSet, - StateEffect, - StateField, - type Extension, - type EditorState, -} from "@codemirror/state"; +import { Prec, RangeSet, StateEffect, StateField, type Extension, type EditorState } from "@codemirror/state"; import { Decoration, EditorView, GutterMarker, ViewPlugin, - gutter, type DecorationSet, type ViewUpdate, } from "@codemirror/view"; import { highlightingFor } from "@codemirror/language"; import { highlightTree } from "@lezer/highlight"; -import { shownAgain, type EditorDocContext } from "@/editor/api"; +import { iconAreaSource, shownAgain, type EditorDocContext } from "@/editor/api"; import { rangeToOffsets } from "@/editor/lsp/positions"; import { pluginVisuals } from "@/editor/lsp/pluginVisuals"; import { findLanguageDescription, languageInfo, loadSupport } from "@/editor/core/cm/languages"; @@ -106,8 +97,9 @@ const actions = StateField.define<{ pos: number; marker: ActionMarker }[]>({ return v.map((a) => ({ ...a, pos: tr.state.doc.lineAt(tr.changes.mapPos(a.pos, 1)).from })); }, }); -const column = gutter({ - class: `${styles.gutter} cm-plugin-gutter-actions`, +/** In the shared icon area, beside the plugins' line markers. */ +const actionSource = iconAreaSource.of({ + order: 65, markers: (view) => RangeSet.of( view.state.field(actions).map((a) => a.marker.range(a.pos)), @@ -116,17 +108,15 @@ const column = gutter({ }); export function pluginLanguageVisuals(ctx: EditorDocContext): Extension { if (ctx.doc < 0) return []; - const compartment = new Compartment(); return [ highlights, actions, - compartment.of([]), + actionSource, ViewPlugin.define( (view) => new (class { timer: ReturnType | undefined; generation = 0; - shown = false; skipped = false; off: () => void; constructor() { @@ -163,14 +153,7 @@ export function pluginLanguageVisuals(ctx: EditorDocContext): Extension { if (generation !== this.generation || view.state.doc !== text) return; this.skipped = !view.dom.isConnected; if (this.skipped) return; - const effects: StateEffect[] = [setHighlight.of(deco), setActions.of(placed)]; - if (this.shown !== !!placed.length) { - this.shown = !!placed.length; - effects.push(compartment.reconfigure(this.shown ? column : [])); - } - view.dispatch({ - effects, - }); + view.dispatch({ effects: [setHighlight.of(deco), setActions.of(placed)] }); } catch (error) { console.debug("[plugin language visuals]", error); } diff --git a/src/features/plugins/lineMarkerGutter.test.ts b/src/features/plugins/lineMarkerGutter.test.ts index 7aa1bf81c..e03ce7f0e 100644 --- a/src/features/plugins/lineMarkerGutter.test.ts +++ b/src/features/plugins/lineMarkerGutter.test.ts @@ -3,6 +3,7 @@ import { EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import type { EditorDocContext } from "@/editor/api"; import type { PluginEvent } from "@/bindings"; +import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import { pluginLineMarkerGutter } from "./lineMarkerGutter"; import { pluginIconName } from "./pluginIcon"; import { projectMarkerIcon } from "./markerIcons"; @@ -41,19 +42,22 @@ it("withdraws old interactive markers on reload and does not refresh on idle sta }); view = new EditorView({ parent: document.body, - state: EditorState.create({ doc: "select", extensions: pluginLineMarkerGutter({ doc: 1 } as EditorDocContext) }), + state: EditorState.create({ + doc: "select", + extensions: [iconAreaExtension, pluginLineMarkerGutter({ doc: 1 } as EditorDocContext)], + }), }); await vi.advanceTimersByTimeAsync(450); - expect(view.dom.querySelector(".cm-openidea-gutter-pluginMarkers")).not.toBeNull(); + expect(view.dom.querySelector(".cm-openidea-iconArea .cm-plugin-mapping-button")).not.toBeNull(); const calls = mock.markers.mock.calls.length; mock.event!({ type: "commandsChanged" }); await vi.advanceTimersByTimeAsync(450); expect(mock.markers).toHaveBeenCalledTimes(calls); mock.markers.mockResolvedValue({ status: "ok", data: [] }); mock.event!({ type: "documentIndexesChanged" }); - expect(view.dom.querySelector(".cm-openidea-gutter-pluginMarkers")).toBeNull(); + expect(view.dom.querySelector(".cm-openidea-iconArea .cm-plugin-mapping-button")).toBeNull(); await vi.advanceTimersByTimeAsync(450); - expect(view.dom.querySelector(".cm-openidea-gutter-pluginMarkers")).toBeNull(); + expect(view.dom.querySelector(".cm-openidea-iconArea .cm-plugin-mapping-button")).toBeNull(); view.destroy(); view = undefined; expect(mock.off).toHaveBeenCalledOnce(); @@ -79,7 +83,7 @@ it("renders ordinary and custom registered icons alongside legacy marker aliases parent: document.body, state: EditorState.create({ doc: "legacy\nordinary\ncustom", - extensions: pluginLineMarkerGutter({ doc: 1 } as EditorDocContext), + extensions: [iconAreaExtension, pluginLineMarkerGutter({ doc: 1 } as EditorDocContext)], }), }); await vi.advanceTimersByTimeAsync(450); @@ -98,7 +102,10 @@ it("refreshes a fallback when the package icon declaration arrives after its mar }); view = new EditorView({ parent: document.body, - state: EditorState.create({ doc: "late", extensions: pluginLineMarkerGutter({ doc: 1 } as EditorDocContext) }), + state: EditorState.create({ + doc: "late", + extensions: [iconAreaExtension, pluginLineMarkerGutter({ doc: 1 } as EditorDocContext)], + }), }); await vi.advanceTimersByTimeAsync(450); expect(view.dom.querySelector(".cm-plugin-mapping-button circle")).toBeNull(); diff --git a/src/features/plugins/lineMarkerGutter.ts b/src/features/plugins/lineMarkerGutter.ts index 5f204fbb5..98fb14c44 100644 --- a/src/features/plugins/lineMarkerGutter.ts +++ b/src/features/plugins/lineMarkerGutter.ts @@ -1,12 +1,12 @@ /** * Gutter icons from plugins' `line-markers` providers. Asked once per document version * (debounced); markers follow their lines in between; a click opens the - * marker's target. The gutter column exists only while there are markers, - * so editors without plugin providers keep their usual width. + * marker's target. They sit in the shared icon area, which takes no width + * while no source has icons. */ -import { Compartment, RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state"; -import { EditorView, GutterMarker, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; -import { shownAgain, type EditorDocContext } from "@/editor/api"; +import { RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state"; +import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view"; +import { iconAreaSource, shownAgain, type EditorDocContext } from "@/editor/api"; import { navigationButton } from "./navigationButton"; import { tx } from "@/platform/i18n"; import { pluginsBackend } from "./backend"; @@ -120,19 +120,16 @@ class MarkersPlugin { private timer: ReturnType | null = null; private generation = 0; private skipped = false; - private shown = false; private off: () => void; constructor( private view: EditorView, private doc: number, - private column: Compartment, ) { const offBackend = pluginsBackend().onEvent((event) => { if (event.type !== "documentIndexesChanged") return; this.generation++; - this.shown = false; - this.view.dispatch({ effects: [setMarkers.of([]), this.column.reconfigure([])] }); + this.view.dispatch({ effects: setMarkers.of([]) }); this.schedule(); }); const offIcons = onMarkerIconsChanged(() => { @@ -174,12 +171,7 @@ class MarkersPlugin { if (this.skipped) return; const placed = placeMarkers(text, resolved); if (!placed.length && !this.view.state.field(markersField).length) return; - const effects: StateEffect[] = [setMarkers.of(placed)]; - if (placed.length > 0 !== this.shown) { - this.shown = placed.length > 0; - effects.push(this.column.reconfigure(this.shown ? markerColumn : [])); - } - this.view.dispatch({ effects }); + this.view.dispatch({ effects: setMarkers.of(placed) }); } catch (e) { console.debug("[plugin line markers]", e); } @@ -192,8 +184,9 @@ class MarkersPlugin { } } -const markerColumn: Extension = gutter({ - class: `${styles.gutter} cm-openidea-gutter-pluginMarkers`, +/** After the built-in run, request and inheritance icons. */ +const markerSource = iconAreaSource.of({ + order: 60, markers: (view) => RangeSet.of( view.state.field(markersField).map((m) => new PluginMarker(m).range(m.pos)), @@ -204,6 +197,5 @@ const markerColumn: Extension = gutter({ export function pluginLineMarkerGutter(ctx: EditorDocContext): Extension { if (ctx.doc < 0) return []; const doc = ctx.doc; - const column = new Compartment(); - return [markersField, column.of([]), ViewPlugin.define((view) => new MarkersPlugin(view, doc, column))]; + return [markersField, markerSource, ViewPlugin.define((view) => new MarkersPlugin(view, doc))]; } diff --git a/src/features/run/npmGutter.test.ts b/src/features/run/npmGutter.test.ts index cff1da5c4..dce77a599 100644 --- a/src/features/run/npmGutter.test.ts +++ b/src/features/run/npmGutter.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { EditorView } from "@codemirror/view"; +import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import { commands, type RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { @@ -248,7 +249,10 @@ describe("scriptMenuItems", () => { const view = new EditorView({ doc: text, parent: document.body, - extensions: npmGutter({ doc: 1, path: "/project/package.json", language: "json", uiMode: null, readOnly: false }), + extensions: [ + iconAreaExtension, + npmGutter({ doc: 1, path: "/project/package.json", language: "json", uiMode: null, readOnly: false }), + ], }); try { await vi.waitFor(() => expect(view.dom.querySelector("button[aria-label=\"Run 'dev'\"]")).not.toBeNull()); diff --git a/src/features/run/npmGutter.ts b/src/features/run/npmGutter.ts index e1d62cebc..f389b1bd2 100644 --- a/src/features/run/npmGutter.ts +++ b/src/features/run/npmGutter.ts @@ -4,14 +4,15 @@ * for the script, or a temporary one when it has none. */ import { RangeSet, StateEffect, StateField, 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 { executeAction } from "@/platform/actions"; import { tx } from "@/platform/i18n"; import { call, commands, type RunConfig } from "@/platform/ipc"; import { shortcutLabel } from "@/platform/keymap"; import { useProject } from "@/platform/project"; import { gutterIconMarkup, showContextMenu, type MenuItem } from "@/ui"; +import { runIconsShown } from "./runGutter"; import { loadConfigs, useRun } from "./store"; import styles from "./Run.module.css"; @@ -334,8 +335,6 @@ class ScriptMarker extends GutterMarker { } } -const scriptSpacer = gutterSpacer(styles.runMarker ?? ""); - const DEBOUNCE = 300; export function npmGutter(ctx: EditorDocContext): Extension { @@ -389,24 +388,22 @@ export function entryGutter( return [ entriesField, detector, - gutter({ - // Shares the run gutter's class so "Show run icons" hides these too. - class: `${styles.runGutter} cm-openidea-gutter-run`, + // In the shared icon area with the other run icons; hidden with them. + iconAreaSource.of({ + order: 10, markers: (view) => - RangeSet.of( - view.state.field(entriesField).map((e) => new ScriptMarker(e, path, menu).range(e.pos)), - true, - ), - // 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); - if (!entry || (event as MouseEvent).button !== 0) return false; - event.preventDefault(); - return true; - }, + runIconsShown() + ? RangeSet.of( + view.state.field(entriesField).map((e) => new ScriptMarker(e, path, menu).range(e.pos)), + true, + ) + : RangeSet.empty, + reserve: (state) => runIconsShown() && state.field(entriesField).length > 0, + // The marker's own click opens the menu; keep the press from moving the caret. + mousedown: (_view, _block, event) => { + if (event.button !== 0 || !(event.target as HTMLElement).closest?.(`.${styles.runMarker}`)) return false; + event.preventDefault(); + return true; }, }), ]; diff --git a/src/features/run/runGutter.ts b/src/features/run/runGutter.ts index ffc7d15e5..d579f94fc 100644 --- a/src/features/run/runGutter.ts +++ b/src/features/run/runGutter.ts @@ -176,7 +176,7 @@ class RunMarker extends GutterMarker { } /** Settings › Editor › General › Gutter Icons › Run icons (on with the gutter icons). */ -function runIconsShown(): boolean { +export function runIconsShown(): boolean { const prefs = editorPrefs(); return prefs.showGutterIcons && !prefs.gutterIconsDisabled.includes("run"); } diff --git a/src/features/services/Services.module.css b/src/features/services/Services.module.css index df5278e5b..0f71d22c7 100644 --- a/src/features/services/Services.module.css +++ b/src/features/services/Services.module.css @@ -143,14 +143,6 @@ font-size: var(--fs-small); } /* Dockerfile / Compose gutter run icons */ -.gutter { - width: 14px; -} -.gutter :global(.cm-gutterElement) { - display: flex; - align-items: center; - justify-content: center; -} .marker { padding: 0; border: 0; diff --git a/src/features/services/dockerGutter.test.ts b/src/features/services/dockerGutter.test.ts index 5c50db365..15ae506ee 100644 --- a/src/features/services/dockerGutter.test.ts +++ b/src/features/services/dockerGutter.test.ts @@ -1,5 +1,6 @@ import { afterEach, expect, it, vi } from "vitest"; import { EditorView } from "@codemirror/view"; +import { iconAreaExtension } from "@/editor/core/cm/iconArea"; import * as ui from "@/ui"; import { dockerGutter } from "./dockerGutter"; import { useComposeFiles } from "./compose"; @@ -14,7 +15,10 @@ it("refreshes markers and menus after edits, removes non-service keys and respec const view = new EditorView({ doc: "services:\n web:\n image: nginx\n x-defaults:\n image: nginx\nvolumes:\n data:\n", parent: document.body, - extensions: dockerGutter({ doc: 1, path: "/p/stack.yml", language: "yaml", uiMode: null, readOnly: false }), + extensions: [ + iconAreaExtension, + dockerGutter({ doc: 1, path: "/p/stack.yml", language: "yaml", uiMode: null, readOnly: false }), + ], }); const markers = () => [...view.dom.querySelectorAll(".cm-gutterElement button")]; try { diff --git a/src/features/services/dockerGutter.ts b/src/features/services/dockerGutter.ts index b4115caf9..85615ea78 100644 --- a/src/features/services/dockerGutter.ts +++ b/src/features/services/dockerGutter.ts @@ -3,10 +3,11 @@ * files (`services:` → up all, each service → up it). As in IDEA they start * temporary Dockerfile / Docker Compose run configurations through `run.runConfig`. */ -import { Compartment, 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 { RangeSet, StateEffect, StateField, type EditorState, type Extension } from "@codemirror/state"; +import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view"; +import { iconAreaSource, type EditorDocContext } from "@/editor/api"; import { executeAction } from "@/platform/actions"; +import { editorPrefs } from "@/platform/editorSettings"; import { useProject } from "@/platform/project"; import { gutterIconMarkup, showContextMenu, type MenuItem } from "@/ui"; import { @@ -101,25 +102,15 @@ class Marker extends GutterMarker { } } -function makeGutter(path: string): Extension { - return gutter({ - // Shares the run gutter's class so "Show run icons" hides these too. - class: `${styles.gutter} cm-openidea-gutter-run`, - markers: (view) => - RangeSet.of( - view.state.field(targetsField).map((p) => new Marker(p.target, path).range(p.pos)), - true, - ), - initialSpacer: () => new Marker({ kind: "composeAll", line: 0 }, path), - }); +/** 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"); } export function dockerGutter(ctx: EditorDocContext): Extension { const path = ctx.path; if (ctx.virtual || !(isDockerfile(path) || isYamlFile(path))) return []; - const gutterSlot = new Compartment(); - const gutterExt = makeGutter(path); - let hasGutter = false; const detector = ViewPlugin.fromClass( class { timer: ReturnType | undefined; @@ -136,14 +127,7 @@ export function dockerGutter(ctx: EditorDocContext): Extension { schedule(delay: number) { clearTimeout(this.timer); this.timer = setTimeout(() => { - const placed = place(this.view.state, path); - const wantGutter = placed.length > 0; - const effects: StateEffect[] = [setTargets.of(placed)]; - if (wantGutter !== hasGutter) { - hasGutter = wantGutter; - effects.push(gutterSlot.reconfigure(wantGutter ? gutterExt : [])); - } - this.view.dispatch({ effects }); + this.view.dispatch({ effects: setTargets.of(place(this.view.state, path)) }); }, delay); } destroy() { @@ -152,5 +136,20 @@ export function dockerGutter(ctx: EditorDocContext): Extension { } }, ); - return [targetsField, gutterSlot.of([]), detector]; + return [ + targetsField, + detector, + // In the shared icon area with the other run icons; hidden with them. + iconAreaSource.of({ + order: 10, + markers: (view) => + runIconsShown() + ? RangeSet.of( + view.state.field(targetsField).map((p) => new Marker(p.target, path).range(p.pos)), + true, + ) + : RangeSet.empty, + reserve: (state) => runIconsShown() && state.field(targetsField).length > 0, + }), + ]; }