Skip to content

Add a built-in D2 language server for editors - #3059

Merged
alixander merged 4 commits into
masterfrom
alixander/d2-lsp
Nov 26, 2026
Merged

alixander merged 4 commits into
masterfrom
alixander/d2-lsp

Conversation

@alixander

@alixander alixander commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Human


AI

Adds d2 lsp [--stdio], a built-in LSP 3.17 server for D2. The editor guide includes setup for Neovim, Helix, and Claude Code.

Editors get live diagnostics, formatting, completion, hover, an outline, folding, document links, color pickers, go-to-definition, find-references, and shape rename. Imports see open buffers, so navigation and diagnostics track unsaved changes. Renaming a shape also updates its references in imported files and inherited scenario boards.

The server uses UTF-16 positions and full document sync. Syntax features remain available during incomplete edits. Navigation uses a cached partial IR. Board-local declarations take precedence over inherited ones, and rename checks for collisions in each affected container.

Find-references and rename follow shape paths in literal near values. For example, renaming a to b edits both the declaration and note: {near: a}. The outline groups x.shape and x.style.fill under one x entry and omits separate entries for connection style assignments.

References and rename include open importers of a shared diagram. Rename refreshes closed dependencies before calculating edit ranges and sends document versions to clients that support them. When a selective import, a board override, or conflicting unsaved buffers make a complete edit uncertain, the server returns a reason.

Go-to-definition follows locally shadowed variables. Incomplete syntax still has an outline and diagnostics while the user types.

Closes #2550.

Verification

  • ./make.sh fmt, ./make.sh lint, and ./make.sh test pass locally.

  • go test -race ./d2lsp/lspserver/ passes.

  • All tests L1 to L78 exist and pass. L78 compares the shapes, connections, and placement targets after applying LSP edits with the compiled result of d2oracle.Rename across seven cases. The framing reader passed a ten-second fuzz run with more than 66,000 inputs.

  • go.mod and go.sum are unchanged.

  • Outside the server package, changes cover CLI integration, the partial IR option, completion context in the existing helper, tests, documentation, and CI. CI runs the server suite on Windows and under the Linux race detector.

  • The existing d2lsp package and D2.js dependency graph exclude the server. Checked with go list -deps ./d2js/... and with GOOS=js GOARCH=wasm.

  • The Appendix C transcript ran against the built binary. Stdout contained two framed JSON-RPC responses, stderr was empty, and the process exited 0.

  • Existing test fixtures and expected outputs are unchanged.

  • Tested in Neovim 0.11.6 with the documented configuration. It attached, reported and cleared diagnostics on unsaved edits, formatted the buffer, jumped to a definition, applied a rename, and shut down cleanly. Helix 25.07.1 also launched the server using the documented configuration, received a live diagnostic at the correct UTF-16 range, and shut down cleanly.

  • A live stdio session exercised 2,650 feature requests against 265 partial edits, with no protocol errors or stderr. Separate runs checked 32 open buffers and a diagram with 1,500 shapes and 500 edges.

  • With the documented Neovim configuration, renaming a shared declaration updated an unsaved near target and an edge in a second open buffer.

  • sent from alixander's Codex

- sent from alixander's Codex
Compare single-file language-server renames against d2oracle on compiled diagrams, including near targets and connections.

- sent from alixander's Codex
- sent from alixander's Codex
@alixander
alixander merged commit 032ef6b into master Nov 26, 2026
20 checks passed
@alixander
alixander deleted the alixander/d2-lsp branch November 26, 2026 16:39
alixander added a commit that referenced this pull request Nov 26, 2026
Master adds the CLI's MCP and LSP servers (#3058, #3059), uvx
distribution (#3061) and a setup-d2 doc (#3062), and makes native builds
embed the TTFs the way master's WebAssembly initializer did (9064887).

d2fonts_embed.go and d2fonts_woff.go are master's, limited to native
builds, so the native CLI's font bytes match master's exactly.
WebAssembly builds keep this branch's d2fonts_embed_wasm.go, which loads
the same TTFs from ttf_stream.br. go.sum is master's, tidied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setup LSP server outside VSCode

1 participant