fix(editor): gutter icons move with their line's text on Enter (#3726)

Line-start gutter icons were mapped with the default association, so Enter at the start of their line left them on the new empty line until each source placed them again. They now move with their line's text at once (mapLineStart), and HTTP endpoint markers are mapped through edits instead of cleared. Sibling of #3722.
This commit is contained in:
l0ng-ai
2026-11-16 17:40:40 +08:00
committed by GitHub
parent 8b7318e59a
commit e598936529
12 changed files with 48 additions and 21 deletions
+1 -1
View File
@@ -34,7 +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";
export { iconAreaSource, mapLineStart, 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;
+16 -1
View File
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent } from "@testing-library/react";
import { EditorState, RangeSet, StateEffect, StateField } from "@codemirror/state";
import { EditorView, GutterMarker } from "@codemirror/view";
import { SETTLE_MS, iconAreaExtension, iconAreaSource } from "./iconArea";
import { SETTLE_MS, iconAreaExtension, iconAreaSource, mapLineStart } from "./iconArea";
class Dot extends GutterMarker {
constructor(readonly name: string) {
@@ -154,3 +154,18 @@ describe("while edits place icons again", () => {
expect(spacerIcons(v)).toBe(2);
});
});
it("moves a line's marker down with its text when Enter is pressed at the line's start", () => {
const state = EditorState.create({ doc: "a\nclass B {}\nc" });
const lineB = state.doc.line(2).from;
const tr = state.update({ changes: { from: lineB, insert: "\n" } });
expect(mapLineStart(tr, lineB)).toBe(tr.state.doc.line(3).from);
const after = (from: number, insert: string, to = from) =>
mapLineStart(state.update({ changes: { from, to, insert } }), lineB);
// Other edits keep it at the start of its line.
expect(after(lineB, " ")).toBe(lineB);
expect(after(1, "\nnew")).toBe(lineB + 4);
expect(after(lineB + 5, "\n")).toBe(lineB);
// Joined into the line above, it goes with its text.
expect(after(1, "", lineB)).toBe(0);
});
+11 -1
View File
@@ -3,7 +3,7 @@
* 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 { Facet, type EditorState, type Extension, type RangeSet, type Transaction } from "@codemirror/state";
import {
EditorView,
GutterMarker,
@@ -28,6 +28,16 @@ export interface IconAreaSource {
mousedown?(view: EditorView, block: BlockInfo, event: MouseEvent): boolean;
}
/**
* Where a marker on the line starting at `pos` goes after `tr`: the start of the line its text is
* on now. An Enter at the start of the line moves the marker down with its text, as every source
* must agree; one left on the new blank line until its source refreshes would split a line's icons
* and briefly narrow the area, shifting the code.
*/
export function mapLineStart(tr: Transaction, pos: number): number {
return tr.state.doc.lineAt(tr.changes.mapPos(pos, 1)).from;
}
export const iconAreaSource = Facet.define<IconAreaSource, readonly IconAreaSource[]>({
combine: (sources) => [...sources].sort((a, b) => a.order - b.order),
});
@@ -16,7 +16,7 @@
*/
import { RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state";
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
import { iconAreaSource } from "@/editor/core/cm/iconArea";
import { iconAreaSource, mapLineStart } 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";
@@ -572,7 +572,7 @@ const markersField = StateField.define<PlacedMarker[]>({
update(value, tr) {
for (const e of tr.effects) if (e.is(setMarkers)) return e.value;
if (!tr.docChanged || !value.length) return value;
return value.map((m) => ({ ...m, pos: tr.state.doc.lineAt(tr.changes.mapPos(m.pos, -1)).from }));
return value.map((m) => ({ ...m, pos: mapLineStart(tr, m.pos) }));
},
});
+2 -2
View File
@@ -7,7 +7,7 @@ import { RangeSet, StateEffect, StateField, type EditorState, type Extension } f
import { EditorView, GutterMarker, ViewPlugin, gutter, showPanel, type Panel, type ViewUpdate } from "@codemirror/view";
import { useEffect } from "react";
import { createRoot } from "react-dom/client";
import type { EditorDocContext } from "@/editor/api";
import { mapLineStart, type EditorDocContext } from "@/editor/api";
import { shortcutLabel } from "@/platform/keymap";
import { isVirtualUri } from "@/platform/virtualDocs";
import type { HttpRequestBlock } from "@/platform/ipc";
@@ -40,7 +40,7 @@ export const requestsField = StateField.define<Placed[]>({
for (const e of tr.effects) if (e.is(setRequests)) return e.value;
if (!tr.docChanged || !value.length) return value;
return value.map((r) => {
const pos = tr.state.doc.lineAt(tr.changes.mapPos(r.pos, -1)).from;
const pos = mapLineStart(tr, r.pos);
return { ...r, pos, line: tr.state.doc.lineAt(pos).number - 1 };
});
},
+4 -2
View File
@@ -1,7 +1,7 @@
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 { iconAreaSource, type EditorDocContext } from "@/editor/api";
import { iconAreaSource, mapLineStart, type EditorDocContext } from "@/editor/api";
import { executeAction } from "@/platform/actions";
import { tx } from "@/platform/i18n";
import { useProject } from "@/platform/project";
@@ -78,7 +78,9 @@ const marks = StateField.define<Mark[]>({
create: () => [],
update: (value, tr) => {
for (const e of tr.effects) if (e.is(setMarks)) return e.value;
return tr.docChanged ? [] : value;
// Kept on their text until the refresh, so the icon area keeps its width while typing.
if (!tr.docChanged || !value.length) return value;
return value.map((m) => ({ ...m, pos: mapLineStart(tr, m.pos) }));
},
});
+2 -1
View File
@@ -9,6 +9,7 @@ import {
activeEditor,
iconAreaSource,
intentionProviders,
mapLineStart,
type ContributedIntention,
type EditorDocContext,
} from "@/editor/api";
@@ -169,7 +170,7 @@ const marksField = StateField.define<Mark[]>({
update(value, tr) {
for (const e of tr.effects) if (e.is(setMarks)) return e.value;
if (!tr.docChanged || !value.length) return value;
return value.map((m) => ({ ...m, pos: tr.state.doc.lineAt(tr.changes.mapPos(m.pos, -1)).from }));
return value.map((m) => ({ ...m, pos: mapLineStart(tr, m.pos) }));
},
});
+2 -2
View File
@@ -10,7 +10,7 @@ import {
} from "@codemirror/view";
import { highlightingFor } from "@codemirror/language";
import { highlightTree } from "@lezer/highlight";
import { iconAreaSource, shownAgain, type EditorDocContext } from "@/editor/api";
import { iconAreaSource, mapLineStart, 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";
@@ -94,7 +94,7 @@ const actions = StateField.define<{ pos: number; marker: ActionMarker }[]>({
update(v, tr) {
for (const e of tr.effects) if (e.is(setActions)) return e.value;
if (!tr.docChanged) return v;
return v.map((a) => ({ ...a, pos: tr.state.doc.lineAt(tr.changes.mapPos(a.pos, 1)).from }));
return v.map((a) => ({ ...a, pos: mapLineStart(tr, a.pos) }));
},
});
/** In the shared icon area, beside the plugins' line markers. */
+2 -2
View File
@@ -6,7 +6,7 @@
*/
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 { iconAreaSource, mapLineStart, shownAgain, type EditorDocContext } from "@/editor/api";
import { navigationButton } from "./navigationButton";
import { tx } from "@/platform/i18n";
import { pluginsBackend } from "./backend";
@@ -64,7 +64,7 @@ const markersField = StateField.define<Placed[]>({
update(value, tr) {
for (const e of tr.effects) if (e.is(setMarkers)) return e.value;
if (!tr.docChanged || !value.length) return value;
return value.map((m) => ({ ...m, pos: tr.state.doc.lineAt(tr.changes.mapPos(m.pos, -1)).from }));
return value.map((m) => ({ ...m, pos: mapLineStart(tr, m.pos) }));
},
});
+2 -2
View File
@@ -5,7 +5,7 @@
*/
import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/state";
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
import { iconAreaSource, type EditorDocContext } from "@/editor/api";
import { iconAreaSource, mapLineStart, type EditorDocContext } from "@/editor/api";
import { executeAction } from "@/platform/actions";
import { tx } from "@/platform/i18n";
import { call, commands, type RunConfig } from "@/platform/ipc";
@@ -358,7 +358,7 @@ export function entryGutter(
update(value, tr) {
for (const e of tr.effects) if (e.is(setEntries)) return e.value;
if (!tr.docChanged) return value;
return value.map((p) => ({ ...p, pos: tr.state.doc.lineAt(tr.changes.mapPos(p.pos, -1)).from }));
return value.map((p) => ({ ...p, pos: mapLineStart(tr, p.pos) }));
},
});
const detector = ViewPlugin.fromClass(
+2 -3
View File
@@ -6,7 +6,7 @@
*/
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 { iconAreaSource, mapLineStart, type EditorDocContext } from "@/editor/api";
import { editorPrefs } from "@/platform/editorSettings";
import { useProject } from "@/platform/project";
import { isVirtualUri } from "@/platform/virtualDocs";
@@ -54,8 +54,7 @@ const targetsField = StateField.define<PlacedTarget[]>({
if (!tr.docChanged || !value.length) return value;
const out: PlacedTarget[] = [];
for (const t of value) {
const pos = tr.changes.mapPos(t.pos, -1);
const line = tr.state.doc.lineAt(pos);
const line = tr.state.doc.lineAt(mapLineStart(tr, t.pos));
if (out.some((o) => o.pos === line.from)) continue;
out.push({ ...t, pos: line.from, line: line.number - 1 });
}
+2 -2
View File
@@ -5,7 +5,7 @@
*/
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 { iconAreaSource, mapLineStart, type EditorDocContext } from "@/editor/api";
import { executeAction } from "@/platform/actions";
import { editorPrefs } from "@/platform/editorSettings";
import { useProject } from "@/platform/project";
@@ -36,7 +36,7 @@ const targetsField = StateField.define<Placed[]>({
update(value, tr) {
for (const e of tr.effects) if (e.is(setTargets)) return e.value;
if (!tr.docChanged) return value;
return value.map((p) => ({ ...p, pos: tr.state.doc.lineAt(tr.changes.mapPos(p.pos, -1)).from }));
return value.map((p) => ({ ...p, pos: mapLineStart(tr, p.pos) }));
},
});