Skip to content

yaml language server

tterranigma edited this page Mar 10, 2019 · 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 redhat-developer/yaml-language-server

LanguageClient Configuration

First you need to configure the LanguageClient:

let g:LanguageClient_serverCommands = {
\ 'yaml': ['node', '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()
    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

Clone this wiki locally