Skip to content

yaml language server

Carlos Hernandez edited this page Mar 31, 2018 · 16 revisions

Imgur

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

At the time of writing the official Yaml-Language-Server is not supported by LanguageClient-neovim, because of a custom extension to the LSP. If you wish to know more or track the issue you can follow it at redhat-developer/yaml-language-server#56. So, for this example a fork of the project will be used.

npm i -g https://github.com/hurricanehrndz/yaml-language-server

LanguageClient Configuration

First you need to configure the LanguageClient:

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

LanguageServer Configuration

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

Method 1:

Within your project folder create a .vim directory, if this is a git directory you may wish to add this folder to your ignore list. Within this newly created directory, create a file name settings.json and populated and/or append it with the information below:

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

Method2:

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(schema)
    if &ft=="yaml"
        echom "Yaml-Language-Server using " . a:schema . " schema."
        let config = json_decode(system("cat ~/.config/nvim/yaml/" . a: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 LanguageClientStarted call UserName#yaml#SetSchema("default")
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.

Yaml-Language-Server Schemas:

COMING SOON

Examples