YAML Language Server

repository·main·Indexed 23 days ago

https://github.com/redhat-developer/yaml-language-server

A Language Server Protocol (LSP) implementation providing advanced YAML editing features including validation, autocompletion, formatting, and hover intelligence. It utilizes JSON Schema to drive intelligence and supports various YAML versions (defaulting to 1.2) and Kubernetes custom resources. Key capabilities include schema association via SchemaStore, custom tag extensions, and diagnostic suppression using # yaml-language-server-disable.

Tokens
5.1K
Snippets
14
Records
30
Agent score
80%

What's inside yaml-language-server

  1. Overview of YAML Language Server features

    main

    The YAML Language Server provides YAML language features over the Language Server Protocol (LSP). Key features include:

    • YAML validation: Detects valid YAML syntax and reports diagnostics like invalid keys, invalid types, or missing nodes.
    • Document symbols: Provides a hierarchical view of YAML nodes.
    • Completion: Suggests keys, values, and structures based on associated JSON schemas, including schema defaults.
    • Hover: Displays schema descriptions, anchor information (if yaml.hoverAnchor is enabled), and schema source information (if yaml.hoverSchemaSource is enabled).
    • Formatting: Formats YAML documents and supports on-type formatting (e.g., automatic indentation on newline).

    Note: Completion and hover content are driven by schemas. The server uses eemeli/yaml for parsing, defaulting to YAML spec version 1.2.

  2. Schema resolution priority

    main

    When multiple schema sources or disabling mechanisms are present, the server resolves them in this order (highest to lowest):

    1. Modeline
    2. Inline $schema property
    3. Registered custom schema provider
    4. yaml.disableSchemaDetection
    5. yaml.schemas
    6. json/schemaAssociations notification
    7. SchemaStore
  3. Build module formats for different environments

    main

    The build process generates multiple module formats to support various server-side loaders and browser bundlers (like webpack):

    • CommonJS: Generated in the out/server/src directory.
    • UMD (Universal Module Definition): Generated in the lib directory.
    • ES Modules (ESM): Generated in the lib directory.
  4. Install and run the YAML Language Server

    main

    You can integrate the server into your tools using several methods:

    Via npm (Global Install)

    npm install -g yaml-language-server
    yaml-language-server --stdio

    Via Local Build

    Clone the repository, build it, and run the output file:

    git clone https://github.com/redhat-developer/yaml-language-server.git
    cd yaml-language-server
    npm install
    npm run build
    node ./out/server/src/server.js --stdio

    Via Docker

    docker run -i --rm quay.io/redhat-developer/yaml-language-server:latest

    Supported communication channels:

    • --stdio
    • --socket=<port>
    • --node-ipc
  5. Set up the yaml-language-server development environment

    main

    To develop on the yaml-language-server repository, ensure you have Node.js (v18.18.0 or higher) and npm installed. Follow these steps to prepare the environment:

    1. Fork and clone the repository.
    2. Install dependencies using npm install.
    3. Build the server using npm run build.

    After building, the main server output is located in out/server/src.

    cd yaml-language-server
    npm install
    npm run build
  6. Suppress diagnostics in YAML files

    main

    You can hide specific diagnostics or all diagnostics on a line by adding a suppression comment immediately before the line.

    To suppress all diagnostics on a line, use: # yaml-language-server-disable

    To suppress specific diagnostics, append one or more comma-separated substrings that match the diagnostic message. The matching is case-insensitive.

    # yaml-language-server-disable
    version: 123
    
    # Suppress specific messages (case-insensitive)
    # yaml-language-server-disable Incorrect type, not accepted
    version: 123
  7. Disable schema validation

    main

    To stop schema-based diagnostics for a file while still allowing YAML syntax error reporting, you can use one of the following methods:

    1. Using a modeline

    Add a comment at the top of the file using either the standard or IntelliJ-compatible format:

    • # yaml-language-server: $schema=none
    • # $schema: none

    2. Using yaml.disableSchemaDetection

    Configure the yaml.disableSchemaDetection setting in your editor/client with glob patterns to prevent schemas from being applied to specific files.

    {
      "yaml.disableSchemaDetection": "**/.github/workflows/*.yaml"
    }
  8. Associate schemas with YAML files

    main

    The language server uses JSON Schema (draft-04, draft-07, 2019-09, and 2020-12) to provide intelligence. You can associate schemas using three methods:

    1. Using a modeline

    Add a comment at the very top of your YAML file. Relative paths are resolved from the file's location.

    # yaml-language-server: $schema=<schema-url-or-path>

    Or use the IntelliJ-compatible format:

    # $schema: <schema-url-or-path>

    2. Using an inline $schema property

    Add a top-level $schema property to the YAML document. Relative paths are resolved from the file's location.

    $schema: <schema-url-or-path>

    3. Using yaml.schemas in client settings

    Map schemas to file patterns in your LSP client configuration.

    • Remote schemas: Use a URL as the key.
    • Local schemas: Use absolute paths, file URIs, or relative paths (relative to workspace root).
    • Multi-root workspaces: Prefix schema paths with the workspace folder name.
    • Kubernetes: Use the kubernetes keyword to map files to versioned Kubernetes schemas.
    {
      "yaml.schemas": {
        "https://getcomposer.org/schema.json": "composer.yaml",
        "https://example.com/api-schema.json": ["api/*.yml", "api/*.yaml"],
        "project-a/schema.json": "project-a/test.yaml",
        "kubernetes": "k8s/*.yaml"
      }
    }
  9. Configure custom YAML tags

    main

    You can extend YAML with application-specific syntax using the yaml.customTags setting. Each entry follows one of these formats:

    • !Tag: Treats the tag as a scalar.
    • !Tag nodeType: Specifies the YAML node type (scalar, sequence, or mapping).
    • !Tag nodeType:returnType: Specifies the node type and the schema type for validation (e.g., string, number, integer, boolean, null, array, object).

    Note: scalar, sequence, and mapping are aliases for string, array, and object respectively.

    {
      "yaml.customTags": [
        "!Scalar-example",
        "!Seq-example sequence",
        "!Mapping-example mapping",
        "!Seq-as-string-example sequence:string"
      ]
    }
  10. Suppress diagnostics using yaml-language-server-disable

    main

    You can suppress YAML diagnostics (errors, warnings, etc.) by adding a special comment on the line immediately preceding the line containing the diagnostic.

    Usage Patterns

    • Suppress all diagnostics on the next line: Use the comment without any additional text.
    • Suppress specific diagnostics: Provide one or more comma-separated substrings. A diagnostic will be suppressed if its message contains any of these substrings (case-insensitive).

    Examples

    # yaml-language-server-disable
    key: value  # All diagnostics on this line are suppressed
    
    # yaml-language-server-disable Incorrect type
    key: 123    # Only diagnostics containing "Incorrect type" are suppressed
    
    # yaml-language-server-disable Incorrect type, not accepted
    key: unknown # Diagnostics containing "Incorrect type" OR "not accepted" are suppressed