Skip to content

yaml language server

Maxim Ivanov edited this page Sep 10, 2020 · 16 revisions

Ansible Example

Is an extendable Language Server providing basic yaml validation and code completion for various well known yaml schemas, and others such as kubernetes and kedge.

Requirements

Before proceeding ensure the following requirements have been met:

Installation

npm install -g yaml-language-server

LanguageClient Configuration

First you need to configure the LanguageClient:

let g:LanguageClient_serverCommands = {
\ 'yaml': ['yaml-language-server', '--stdio']                                                                                                                                                                              
\ }

If the yaml-language-server wasn't installed globally or the command is not on your PATH you could try running the server.js via node directly:

let g:LanguageClient_serverCommands = {
\ 'yaml': ['node', 'yourNodeModulesDirectory/yaml-language-server/out/server/src/server.js', '--stdio']                                                                                                                                                                              
\ }

LanguageServer Configuration

Now need to notify the language server of your settings and/or additional schemas, this can be done in one of several ways.

Method 1

Within your project folder create the .vim/settings.json file and populate (or append) it with the information below:

{
    "yaml": {
            "format": {
                "enable": false
            },
            "validate": true
        },
    "http": {
        "proxyStrictSSL": true
    }
}

You need to make sure the g:LanguageClient_loadSettings is set to 1 for the settings file to be loaded. It is set to 1 by default.

Method 2

You could inline the settings.json file to apply those settings globally. For that you need to notify the language server on LanguageClientStarted event:

let settings = json_decode('
\{
\    "yaml": {
\        "completion": true,
\        "hover": true,
\        "validate": true,
\        "schemas": {
\            "Kubernetes": "/*"
\        },
\        "format": {
\            "enable": true
\        }
\    },
\    "http": {
\        "proxyStrictSSL": true
\    }
\}')
augroup LanguageClient_config
    autocmd!
    autocmd User LanguageClientStarted call LanguageClient#Notify(
        \ 'workspace/didChangeConfiguration', {'settings': settings})
augroup END

Method 3

For this method we will create an autload function in your $VIMCONFIG directory ($HOME/.config/nvim), that can be called at any time after the LanguageServer has been started in order to pass along whichever settings are deemed necessary.

  • Create the autoload directory: mkdir $VIMCONFIG/autoload/$USERNAME
  • Create the autoload script: touch $VIMCONFIG/autoload/$USERNAME/yaml.vim
  • Populate the autoload script with the following, do not forget to adjust the UserName in the function name:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"                        Yaml-Language-Server Helpers                         "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! UserName#yaml#SetSchema()
    if &ft=="yaml"
        let schema = &filetype
        echom "Yaml-Language-Server using " . schema . " schema."
        let config = json_decode(system("cat ~/.config/nvim/yaml/" . schema . ".json"))
        call LanguageClient#Notify('workspace/didChangeConfiguration', { 'settings': config })
    endif
endfunction
  • Setup an autocmd for the LanguageClient when started, again adjust UserName accordingly.
augroup LanguageClient_config
    autocmd!
    autocmd User LanguageClientStarted call UserName#yaml#SetSchema()
augroup END
  • Create the default config for the yaml-language-server touch ~/.config/nvim/yaml/default.json.
  • Populate the default config with the following:
{
    "yaml": {
            "format": {
                "enable": false
            },
            "validate": true
        },
    "http": {
        "proxyStrictSSL": true
    }
}

Now that LanguageServer has been configured with either of the above methods, auto-completion and validation should be working for the default yaml schemas found on the schemastore. This includes thecircleci and travis schemas to name a few.

Examples