YAML Language Server

1.22.0 · active · verified Sun Apr 19

The YAML Language Server provides comprehensive language support for YAML files, adhering to the Language Server Protocol (LSP). It offers features such as robust YAML validation against JSON Schema drafts (04, 07, 2019-09, and 2020-12), intelligent auto-completion, rich hover information, document outlining, and code formatting capabilities. Since version 1.0.0, it utilizes the `eemeli/yaml` parser for strict enforcement of the YAML specification (defaulting to YAML 1.2). Key differentiators include built-in Kubernetes syntax support, integration with the JSON Schema Store for automatic schema fetching, and the ability to parse Kubernetes Custom Resource Definitions (CRDs). The project maintains an active development cycle, with frequent releases often on a monthly or bi-monthly cadence, with version 1.22.0 being the current stable release.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically start and interact with the YAML Language Server as a child process using Node.js, establishing an LSP connection over standard I/O, sending an 'initialize' request, and notifying the server about a new document opening.

import { spawn } from 'child_process';
import { createConnection, MessageConnection, InitializeParams, TextDocumentItem, DidOpenTextDocumentParams } from 'vscode-languageserver/node';
import { TextDocuments } from 'vscode-languageserver-textdocument';

const serverPath = require.resolve('yaml-language-server/bin/yaml-language-server');

async function startYamlLanguageServer() {
    console.log(`Starting YAML Language Server from: ${serverPath}`);
    const serverProcess = spawn('node', [serverPath, '--stdio']);

    serverProcess.stdout.pipe(process.stdout);
    serverProcess.stderr.pipe(process.stderr);
    process.stdin.pipe(serverProcess.stdin);

    const connection: MessageConnection = createConnection(
        serverProcess.stdin,
        serverProcess.stdout
    );

    connection.listen();

    connection.onInitialize(async (params: InitializeParams) => {
        console.log('LSP Client: Initializing...');
        return {
            capabilities: {
                textDocumentSync: 1, // Full
                completionProvider: { resolveProvider: false, triggerCharacters: ['-', ':'] },
                hoverProvider: true,
                documentFormattingProvider: true,
                documentRangeFormattingProvider: true,
                documentSymbolProvider: true,
                workspace: { workspaceFolders: { supported: true } }
            },
            serverInfo: { name: 'yaml-language-client', version: '1.0' }
        };
    });

    // Send initialize request after connection is established
    connection.sendRequest('initialize', {
        processId: process.pid,
        rootUri: null,
        capabilities: {},
        workspaceFolders: null
    } as InitializeParams).then(async () => {
        console.log('LSP Client: Initialized. Sending didOpen notification...');
        // Example: Open a dummy YAML document
        const dummyYamlContent = `---
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: busybox
      command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
`;

        const textDocument: TextDocumentItem = {
            uri: 'file:///tmp/test.yaml',
            languageId: 'yaml',
            version: 1,
            text: dummyYamlContent
        };
        connection.sendNotification('textDocument/didOpen', { textDocument } as DidOpenTextDocumentParams);
        console.log('LSP Client: Sent didOpen for test.yaml. Check server logs for activity.');
    }).catch(error => {
        console.error('LSP Client: Initialization failed:', error);
    });

    process.on('exit', () => {
        serverProcess.kill();
    });
}

startYamlLanguageServer().catch(console.error);

view raw JSON →