What is yaml-language-server?
The yaml-language-server npm package provides a language server for YAML files, enabling features like validation, autocompletion, hover information, and more within editors that support the Language Server Protocol (LSP). This enhances the development experience by providing real-time feedback and assistance when working with YAML files.
What are yaml-language-server's main functionalities?
Validation
This feature validates YAML content against a schema or general YAML syntax rules, providing error messages and warnings for any issues found.
const { validate } = require('yaml-language-server');
const yamlContent = 'key: value';
const validationResults = validate(yamlContent);
console.log(validationResults);
Autocompletion
This feature provides autocompletion suggestions based on the current context within the YAML file, helping users to write correct and complete YAML configurations more efficiently.
const { getCompletions } = require('yaml-language-server');
const yamlContent = 'key: ';
const completions = getCompletions(yamlContent, { line: 0, character: 5 });
console.log(completions);
Hover Information
This feature provides hover information for YAML keys and values, offering additional context and documentation to help users understand the purpose and usage of different parts of the YAML file.
const { getHover } = require('yaml-language-server');
const yamlContent = 'key: value';
const hoverInfo = getHover(yamlContent, { line: 0, character: 1 });
console.log(hoverInfo);
Other packages similar to yaml-language-server
vscode-yaml
The vscode-yaml extension provides YAML support for Visual Studio Code, including features like validation, autocompletion, and hover information. It is specifically designed for use within the VS Code editor and integrates seamlessly with its ecosystem.
yaml-lint
The yaml-lint package is a simple YAML linter that checks for syntax errors in YAML files. While it does not provide the full range of features offered by yaml-language-server, it is useful for quickly identifying and fixing syntax issues.
yaml-js
The yaml-js package is a YAML parser and stringifier for JavaScript. It focuses on parsing and generating YAML content rather than providing language server features like validation and autocompletion.
YAML Language Server
Supports JSON Schema 7 and below.
Features
- YAML validation:
- Detects whether the entire file is valid yaml
- Validation:
- Detects errors such as:
- Node is not found
- Node has an invalid key node type
- Node has an invalid type
- Node is not a valid child node
- Detects warnings such as:
- Node is an additional property of parent
- Auto completion:
- Auto completes on all commands
- Scalar nodes autocomplete to schema's defaults if they exist
- Hover support:
- Hovering over a node shows description if available
- Document outlining:
- Shows a complete document outline of all nodes in the document
Language Server Settings
The following settings are supported:
yaml.format.enable
: Enable/disable default YAML formatter (requires restart)yaml.format.singleQuote
: Use single quotes instead of double quotesyaml.format.bracketSpacing
: Print spaces between brackets in objectsyaml.format.proseWrap
: Always: wrap prose if it exeeds the print width, Never: never wrap the prose, Preserve: wrap prose as-isyaml.format.printWidth
: Specify the line length that the printer will wrap onyaml.validate
: Enable/disable validation featureyaml.hover
: Enable/disable hoveryaml.completion
: Enable/disable autocompletionyaml.schemas
: Helps you associate schemas with files in a glob patternyaml.schemaStore.enable
: When set to true the YAML language server will pull in all available schemas from JSON Schema Storeyaml.customTags
: Array of custom tags that the parser will validate against. It has two ways to be used. Either an item in the array is a custom tag such as "!Ref" and it will automatically map !Ref to scalar or you can specify the type of the object !Ref should be e.g. "!Ref sequence". The type of object can be either scalar (for strings and booleans), sequence (for arrays), map (for objects).
Adding custom tags
In order to use the custom tags in your YAML file you need to first specify the custom tags in the setting of your code editor. For example, we can have the following custom tags:
"yaml.customTags": [
"!Scalar-example scalar",
"!Seq-example sequence",
"!Mapping-example mapping"
]
The !Scalar-example would map to a scalar custom tag, the !Seq-example would map to a sequence custom tag, the !Mapping-example would map to a mapping custom tag.
We can then use the newly defined custom tags inside our YAML file:
some_key: !Scalar-example some_value
some_sequence: !Seq-example
- some_seq_key_1: some_seq_value_1
- some_seq_key_2: some_seq_value_2
some_mapping: !Mapping-example
some_mapping_key_1: some_mapping_value_1
some_mapping_key_2: some_mapping_value_2
Associating a schema to a glob pattern via yaml.schemas:
yaml.schemas applies a schema to a file. In other words, the schema (placed on the left) is applied to the glob pattern on the right. Your schema can be local or online. Your schema path must be relative to the project root and not an absolute path to the schema.
For example:
If you have project structure
myProject
> myYamlFile.yaml
you can do
yaml.schemas: {
"https://json.schemastore.org/composer": "/myYamlFile.yaml"
}
and that will associate the composer schema with myYamlFile.yaml.
More examples of schema association:
Using yaml.schemas settings
Single root schema association:
When associating a schema it should follow the format below
yaml.schemas: {
"url": "globPattern",
"Kubernetes": "globPattern"
}
e.g.
yaml.schemas: {
"https://json.schemastore.org/composer": "/*"
}
e.g.
yaml.schemas: {
"kubernetes": "/myYamlFile.yaml"
}
e.g.
yaml.schemas: {
"https://json.schemastore.org/composer": "/*",
"kubernetes": "/myYamlFile.yaml"
}
Multi root schema association:
You can also use relative paths when working with multi root workspaces.
Suppose you have a multi root workspace that is laid out like:
My_first_project:
test.yaml
my_schema.json
My_second_project:
test2.yaml
my_schema2.json
You must then associate schemas relative to the root of the multi root workspace project.
yaml.schemas: {
"My_first_project/my_schema.json": "test.yaml",
"My_second_project/my_schema2.json": "test2.yaml"
}
yaml.schemas
allows you to specify json schemas that you want to validate against the yaml that you write. Kubernetes is an optional field. It does not require a url as the language server will provide that. You just need the keyword kubernetes and a glob pattern.
Using inlined schema
It is possible to specify a yaml schema using a modeline.
# yaml-language-server: $schema=<urlToTheSchema>
Clients
This repository only contains the server implementation. Here are some known clients consuming this server:
Developer Support
Getting started
- Install prerequisites:
- Fork and clone this repository
- Install the dependencies
cd yaml-language-server
$ yarn install
- Build the language server
$ yarn run build
- The new built server is now location in out/server/src/server.js.
node (Yaml Language Server Location)/out/server/src/server.js [--stdio]
Connecting to the language server via stdio
We have included the option to connect to the language server via stdio to help with intergrating the language server into different clients.