diff --git a/src/editor/api.ts b/src/editor/api.ts index 8257fb36f..59e8f1ac1 100644 --- a/src/editor/api.ts +++ b/src/editor/api.ts @@ -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; diff --git a/src/editor/core/cm/iconArea.test.ts b/src/editor/core/cm/iconArea.test.ts index 70d0fbca6..16b308cb5 100644 --- a/src/editor/core/cm/iconArea.test.ts +++ b/src/editor/core/cm/iconArea.test.ts @@ -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); +}); diff --git a/src/editor/core/cm/iconArea.ts b/src/editor/core/cm/iconArea.ts index a7b804617..7a837a5b1 100644 --- a/src/editor/core/cm/iconArea.ts +++ b/src/editor/core/cm/iconArea.ts @@ -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({ combine: (sources) => [...sources].sort((a, b) => a.order - b.order), }); diff --git a/src/editor/navigation/implementationGutter.ts b/src/editor/navigation/implementationGutter.ts index 6bd125e0f..c3bce7c45 100644 --- a/src/editor/navigation/implementationGutter.ts +++ b/src/editor/navigation/implementationGutter.ts @@ -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({ 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) })); }, }); diff --git a/src/features/http-client/editor.tsx b/src/features/http-client/editor.tsx index 32e6a5637..c543e3a87 100644 --- a/src/features/http-client/editor.tsx +++ b/src/features/http-client/editor.tsx @@ -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({ 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 }; }); }, diff --git a/src/features/http-client/endpoints.ts b/src/features/http-client/endpoints.ts index 5ffde4d6a..169d7bdb0 100644 --- a/src/features/http-client/endpoints.ts +++ b/src/features/http-client/endpoints.ts @@ -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({ 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) })); }, }); diff --git a/src/features/http-client/spring/editor.ts b/src/features/http-client/spring/editor.ts index 08ae07c42..531834dd4 100644 --- a/src/features/http-client/spring/editor.ts +++ b/src/features/http-client/spring/editor.ts @@ -9,6 +9,7 @@ import { activeEditor, iconAreaSource, intentionProviders, + mapLineStart, type ContributedIntention, type EditorDocContext, } from "@/editor/api"; @@ -169,7 +170,7 @@ const marksField = StateField.define({ 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) })); }, }); diff --git a/src/features/plugins/languageVisuals.ts b/src/features/plugins/languageVisuals.ts index 809675951..b93dc1663 100644 --- a/src/features/plugins/languageVisuals.ts +++ b/src/features/plugins/languageVisuals.ts @@ -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. */ diff --git a/src/features/plugins/lineMarkerGutter.ts b/src/features/plugins/lineMarkerGutter.ts index 98fb14c44..24558aa78 100644 --- a/src/features/plugins/lineMarkerGutter.ts +++ b/src/features/plugins/lineMarkerGutter.ts @@ -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({ 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) })); }, }); diff --git a/src/features/run/npmGutter.ts b/src/features/run/npmGutter.ts index f389b1bd2..82ae79f3a 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, 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( diff --git a/src/features/run/runGutter.ts b/src/features/run/runGutter.ts index 7b762d7c2..4414ee1d7 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, 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({ 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 }); } diff --git a/src/features/services/dockerGutter.ts b/src/features/services/dockerGutter.ts index 85615ea78..3775d58f0 100644 --- a/src/features/services/dockerGutter.ts +++ b/src/features/services/dockerGutter.ts @@ -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({ 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) })); }, });