Files
volar-docs/examples/json-yaml-lsp
2025-11-09 22:22:52 -06:00
..
2025-11-09 22:22:52 -06:00
2025-11-09 22:22:52 -06:00
2025-11-09 22:22:52 -06:00
2025-11-09 22:22:52 -06:00
2025-11-09 22:22:52 -06:00

JSON/YAML LSP Example

Minimal language server that combines Volars 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 Codes “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 Volars connection utilities.

Feature Highlights

  • Diagnostics + auto-fixes: validations are debounced and version-gated; YAML code actions come straight from yaml-language-server and JSON documents offer a quick fix to add a $schema entry (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 $ref definitions: definition requests are routed to the JSON/YAML services so $ref targets and YAML anchors resolve to their sources.
  • Workspace-aware schemas: relative schema URLs are resolved via server.workspaceFolders, so configs like ./schemas/service.schema.json just 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.