Skip to content

feat(lsp): add document color chips with regex fallback#2533

Merged
bajrangCoder merged 3 commits into
mainfrom
feat/lsp-document-colors
Jul 24, 2026
Merged

feat(lsp): add document color chips with regex fallback#2533
bajrangCoder merged 3 commits into
mainfrom
feat/lsp-document-colors

Conversation

@bajrangCoder

Copy link
Copy Markdown
Member

Adds full LSP document color support (textDocument/documentColor + textDocument/colorPresentation) and keeps the existing regex color preview as a fallback for colors the language server misses.

Implement textDocument/documentColor and colorPresentation for LSP color
previews, share chip UI with the regex colorView, and keep regex as a
fallback for ranges the server does not cover.
@greptile-apps

greptile-apps Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds full LSP textDocument/documentColor and textDocument/colorPresentation support alongside the existing regex-based color preview, with the regex path serving as a fallback for spans the language server does not cover. A new shared colorChip.ts module consolidates the widget, WeakMap payload store, and CSS↔LSP color conversion helpers used by both paths.

  • src/cm/colorChip.ts — new shared widget/utility module; ColorChipWidget, colorChipDecoration, conversion helpers (lspColorToCss, lspColorToHex, cssToLspColor), and colorChipTheme are extracted here and reused by both colorView.ts and documentColors.ts.
  • src/cm/lsp/documentColors.ts — new 541-line extension implementing the full LSP color round-trip: debounced textDocument/documentColor fetch with per-request reqId cancellation, position mapping through unsyncedChanges, textDocument/colorPresentation on chip click with pickPresentation scoring, and a lspDocumentColorsField StateField that the regex scanner reads to skip already-covered spans.
  • src/cm/colorView.ts — refactored to use shared chip utilities; adds getLspCoveredRanges dedup so the regex scanner skips LSP-covered positions, and responds to didReplaceLspDocumentColors (effect-based, not reference-equality-based) to trigger a targeted re-scan.

Confidence Score: 5/5

Safe to merge — the LSP color round-trip, regex fallback dedup, retry counter separation, and post-picker position re-validation are all implemented correctly.

All previously flagged issues (shared retry counters, stale docLen snapshot, clamp01 duplication, lspChanged reference-equality false positives) are addressed. The two remaining findings are a dead null check in mapLspRange (caught downstream by from < 0) and a no-op debounced fetch on every keystroke when the server has no color provider — neither affects correctness or user-visible behavior.

src/cm/lsp/documentColors.ts — the mapLspRange null check and the unconditional docChanged schedule are worth a second look, but neither blocks the feature.

Important Files Changed

Filename Overview
src/cm/colorChip.ts New shared module extracting ColorChipWidget, WeakMap payload lookup, CSS↔LSP color conversion helpers, and theme definition — cleanly consolidates logic used by both the regex and LSP paths.
src/cm/colorView.ts Refactored to use shared colorChip utilities; adds getLspCoveredRanges dedup and didReplaceLspDocumentColors trigger for re-scan — logic is correct and the previously flagged lspChanged-on-every-edit issue is now resolved.
src/cm/lsp/documentColors.ts New 541-line LSP document color extension; all previously flagged issues (shared retry counters, stale docLen, clamp01 duplication, lspChanged reference-equality) are addressed. Two minor issues remain: dead null check in mapLspRange and a no-op schedule on every keystroke when provider is absent.
src/cm/lsp/clientManager.ts Adds documentColors to the builtin extension list, defaulting to true — straightforward integration mirroring the existing inlayHints pattern.
src/cm/lsp/index.ts Public API surface additions for the new documentColors module — exports are complete and consistent.
src/cm/lsp/types.ts Adds documentColors?: boolean to BuiltinExtensionsConfig — minimal, correct change.

Sequence Diagram

sequenceDiagram
    participant User
    participant ColorViewPlugin as ColorViewPlugin (regex)
    participant LSPPlugin as DocumentColors Plugin (LSP)
    participant LspField as lspDocumentColorsField
    participant LSPServer as LSP Server

    User->>ColorViewPlugin: open file / edit
    User->>LSPPlugin: open file / edit (docChanged)
    LSPPlugin->>LSPPlugin: schedule(debounce 150ms)
    LSPPlugin->>LSPServer: textDocument/documentColor
    LSPServer-->>LSPPlugin: ColorInformation[]
    LSPPlugin->>LSPPlugin: process() map positions
    LSPPlugin->>LspField: dispatch setColors effect
    LspField-->>ColorViewPlugin: didReplaceLspDocumentColors true
    ColorViewPlugin->>ColorViewPlugin: add visibleRanges to pendingDirty, scheduleFlush
    ColorViewPlugin->>ColorViewPlugin: colorRanges() skips LSP-covered spans
    ColorViewPlugin->>User: render regex chips (non-LSP spans only)
    LSPPlugin->>User: render LSP chips (viewport bounds)

    User->>LSPPlugin: click LSP color chip
    LSPPlugin->>User: openColorPicker(seed)
    User-->>LSPPlugin: picked color string
    LSPPlugin->>LSPPlugin: findLiveColor() re-validates position
    LSPPlugin->>LSPServer: textDocument/colorPresentation
    LSPServer-->>LSPPlugin: ColorPresentation[]
    LSPPlugin->>LSPPlugin: pickPresentation() scores by format match
    LSPPlugin->>LSPPlugin: applyTextEdits() or label fallback
    LSPPlugin->>User: dispatch doc change
Loading

Reviews (3): Last reviewed commit: "handle edge case" | Re-trigger Greptile

Comment thread src/cm/lsp/documentColors.ts Outdated
Comment thread src/cm/colorView.ts Outdated
Comment thread src/cm/lsp/documentColors.ts Outdated
@bajrangCoder

This comment was marked as outdated.

Comment thread src/cm/lsp/documentColors.ts Outdated
@bajrangCoder

This comment was marked as outdated.

@bajrangCoder
bajrangCoder added this pull request to the merge queue Jul 24, 2026
Merged via the queue into main with commit d79fdcf Jul 24, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in The Code Board - Acode Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant