diff --git a/src/features/color-preview/gutter.test.ts b/src/features/color-preview/gutter.test.ts index 2351cd17a7..b0f9e3cbb4 100644 --- a/src/features/color-preview/gutter.test.ts +++ b/src/features/color-preview/gutter.test.ts @@ -141,7 +141,7 @@ it("samples a screen color and offers it again as a recent color", async () => { expect(view.state.doc.toString()).toBe("color: #4c78ce;"); }); -it("ignores color-like Vue template text while previewing embedded styles", () => { +it("ignores color-like Vue template text while previewing embedded styles", async () => { view = new EditorView({ state: EditorState.create({ doc: '\n', @@ -152,8 +152,30 @@ it("ignores color-like Vue template text while previewing embedded styles", () = }), parent: document.body, }); - const swatches = [...view.dom.querySelectorAll(".cm-openidea-gutter-colors .cm-gutterElement span[title]")]; - expect(swatches.map((swatch) => swatch.getAttribute("title"))).toEqual(["#abc", "#ff0080"]); + // Shown once the background parse reaches the styles (it may finish after the editor opened). + await vi.waitFor(() => { + const swatches = [...view!.dom.querySelectorAll(".cm-openidea-gutter-colors .cm-gutterElement span[title]")]; + expect(swatches.map((swatch) => swatch.getAttribute("title"))).toEqual(["#abc", "#ff0080"]); + }); +}); + +it("shows a markup file's style colors once a parse that outlasted opening the editor finishes", async () => { + const styles = Array.from({ length: 4000 }, (_, i) => `.c${i} { margin: ${i}px; }`).join("\n"); + view = new EditorView({ + state: EditorState.create({ + doc: ``, + extensions: [ + vue(), + colorPreviewGutter({ doc: 1, path: "/p/a.vue", language: "vue", uiMode: null, readOnly: false }), + ], + }), + parent: document.body, + }); + view.scrollDOM.style.height = "100px"; + view.dispatch({ effects: EditorView.scrollIntoView(view.state.doc.length) }); + await vi.waitFor(() => + expect(view!.dom.querySelector('.cm-openidea-gutter-colors span[title="#ff0080"]')).not.toBeNull(), + ); }); it("ignores color-like HTML text while previewing inline styles", () => { diff --git a/src/features/color-preview/gutter.ts b/src/features/color-preview/gutter.ts index 477af162b1..c78328f7c5 100644 --- a/src/features/color-preview/gutter.ts +++ b/src/features/color-preview/gutter.ts @@ -5,7 +5,7 @@ * its declaration, and modern function syntaxes show the resolved value without a picker. */ import { Facet, StateEffect, StateField, type EditorState, type Extension, type RangeSet } from "@codemirror/state"; -import { Decoration, EditorView, GutterMarker, WidgetType, gutter } from "@codemirror/view"; +import { Decoration, EditorView, GutterMarker, WidgetType, gutter, type ViewUpdate } from "@codemirror/view"; import { isolateHistory } from "@codemirror/commands"; import { syntaxTree } from "@codemirror/language"; import type { EditorDocContext } from "@/editor/api"; @@ -320,6 +320,15 @@ function swatchCount(view: EditorView, held: number): number { return held; } +/** + * In markup (HTML, Vue…) a literal counts only inside a style, which the syntax tree tells: the + * background parse finishing after the editor opened changes the swatches of lines it reached. + */ +function parsedFurther(update: ViewUpdate): boolean { + const ctx = update.state.facet(colorContext); + return !!ctx && isMarkupLanguage(ctx.language) && syntaxTree(update.startState) !== syntaxTree(update.state); +} + export const colorGutter = gutter({ class: `${styles.gutter} cm-openidea-gutter-colors`, markers: (view) => view.state.facet(colorMarkers).map((markers) => markers(view)), @@ -330,7 +339,8 @@ export const colorGutter = gutter({ !update.viewportChanged && !update.geometryChanged && update.startState.facet(colorContext) === update.state.facet(colorContext) && - !update.transactions.some((tr) => tr.effects.length > 0) + !update.transactions.some((tr) => tr.effects.length > 0) && + !parsedFurther(update) ) return previous; const held = previous instanceof ColorSpacer ? previous.count : 0; @@ -345,6 +355,7 @@ export const colorGutter = gutter({ }, lineMarkerChange: (update) => update.docChanged || + parsedFurther(update) || update.startState.facet(colorContext) !== update.state.facet(colorContext) || update.startState.readOnly !== update.state.readOnly || update.transactions.some((tr) => tr.effects.some((e) => e.is(setVars))),