JSON/YAML LSP Example
Minimal language server that combines Volar’s Vue intelligence with the JSON and YAML language services. It mirrors the steps in docs/building-lsp-json-yaml.md and gives you a runnable playground.
Commands
npm install
npm run dev # starts the LSP over stdio
npm run build && npm start compiles to dist/ and runs the emitted JavaScript.
Use VS Code’s “Run Extension” launch config or any LSP-compatible client and point it at node ./dist/server.js (stdio). The server advertises completions, hovers, and diagnostics for .vue, .json, and .yaml files.
Files
src/jsonYaml.ts– wraps the JSON and YAML language services with lightweight helpers, schema caching, dynamic configuration, code actions, and definition helpers.src/server.ts– entry point that wires routing, diagnostics, completion/hover/definition/code-action handlers, configuration reloads, and cancellation-aware plumbing on top of Volar’s connection utilities.
Feature Highlights
- Diagnostics + auto-fixes: validations are debounced and version-gated; YAML code actions come straight from
yaml-language-serverand JSON documents offer a quick fix to add a$schemaentry (configurable via settings). - Hover & completion context: handlers pass every request through the structured services, ensuring schema-derived detail bubbles up in tooltips and suggestions.
- Follow
$refdefinitions: definition requests are routed to the JSON/YAML services so$reftargets and YAML anchors resolve to their sources. - Workspace-aware schemas: relative schema URLs are resolved via
server.workspaceFolders, so configs like./schemas/service.schema.jsonjust work. - Workspace diagnostics: implements
languages.diagnostics.onWorkspace, letting supporting editors pull batched diagnostic snapshots for all tracked files.
Configure behaviour from your editor by sending the volarJsonYaml setting:
{
"volarJsonYaml": {
"json": {
"defaultSchemaUri": "./schemas/service.schema.json",
"schemas": [
{ "fileMatch": ["config/*.json"], "url": "./schemas/service.schema.json" }
]
},
"yaml": {
"customTags": ["!secret scalar"]
}
}
}
You can extend this sample by adding more volar-service-* plugins or extra third-party backends (e.g., XML). Refer to the troubleshooting tips in the main docs if routing behaves unexpectedly.