From 42ae06a288e3775261847081a6934bc991abd106 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson Date: Wed, 4 Jul 2018 13:36:50 +0200 Subject: [PATCH 1/3] puppet: Add puppet-languageserver linter --- README.md | 2 +- ale_linters/puppet/languageserver.vim | 35 +++++++++++++++++++++++++++ doc/ale-puppet.txt | 11 +++++++++ doc/ale.txt | 3 ++- 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ale_linters/puppet/languageserver.vim diff --git a/README.md b/README.md index caddc590..bfa58428 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ formatting. | Pony | [ponyc](https://github.com/ponylang/ponyc) | | proto | [protoc-gen-lint](https://github.com/ckaznocha/protoc-gen-lint) | | Pug | [pug-lint](https://github.com/pugjs/pug-lint) | -| Puppet | [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) | +| Puppet | [languageserver](https://github.com/lingua-pupuli/puppet-editor-services), [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) | | Python | [autopep8](https://github.com/hhatto/autopep8), [black](https://github.com/ambv/black), [flake8](http://flake8.pycqa.org/en/latest/), [isort](https://github.com/timothycrosley/isort), [mypy](http://mypy-lang.org/), [prospector](http://github.com/landscapeio/prospector), [pycodestyle](https://github.com/PyCQA/pycodestyle), [pyls](https://github.com/palantir/python-language-server), [pyre](https://github.com/facebook/pyre-check), [pylint](https://www.pylint.org/) !!, [yapf](https://github.com/google/yapf) | | QML | [qmlfmt](https://github.com/jesperhh/qmlfmt), [qmllint](https://github.com/qt/qtdeclarative/tree/5.11/tools/qmllint) | | R | [lintr](https://github.com/jimhester/lintr) | diff --git a/ale_linters/puppet/languageserver.vim b/ale_linters/puppet/languageserver.vim new file mode 100644 index 00000000..feff1d53 --- /dev/null +++ b/ale_linters/puppet/languageserver.vim @@ -0,0 +1,35 @@ +" Author: Alexander Olofsson +" Description: Puppet Language Server integration for ALE + +call ale#Set('puppet_languageserver_executable', 'puppet-languageserver') + +function! ale_linters#puppet#languageserver#GetExecutable(buffer) abort + return ale#Var(a:buffer, 'puppet_languageserver_executable') +endfunction + +function! ale_linters#puppet#languageserver#GetCommand(buffer) abort + let l:exe = ale#Escape(ale_linters#puppet#languageserver#GetExecutable(a:buffer)) + + return l:exe . ' --stdio' +endfunction + +function! ale_linters#puppet#languageserver#GetProjectRoot(buffer) abort + " Note: while manifest.json is a strong recommendation, the only + " *required* path for a Puppet module is the manifests folder. + let l:root_path = ale#path#FindNearestFile(a:buffer, 'metadata.json') + + if empty(l:root_path) + let l:root_path = ale#path#FindNearestDirectory(a:buffer, 'manifests') + endif + + return !empty(l:root_path) ? fnamemodify(l:root_path, ':h') : '' +endfunction + +call ale#linter#Define('puppet', { +\ 'name': 'languageserver', +\ 'lsp': 'stdio', +\ 'executable_callback': 'ale_linters#puppet#languageserver#GetExecutable', +\ 'command_callback': 'ale_linters#puppet#languageserver#GetCommand', +\ 'language': 'puppet', +\ 'project_root_callback': 'ale_linters#puppet#languageserver#GetProjectRoot', +\}) diff --git a/doc/ale-puppet.txt b/doc/ale-puppet.txt index 604565e0..7c67484e 100644 --- a/doc/ale-puppet.txt +++ b/doc/ale-puppet.txt @@ -22,5 +22,16 @@ g:ale_puppet_puppetlint_options *g:ale_puppet_puppetlint_options* puppet-lint invocation. +=============================================================================== +puppet-languageserver *ale-puppet-languageserver* + +g:ale_puppet_languageserver_executable *g:ale_puppet_languageserver_executable* + *b:ale_puppet_languageserver_executable* + type: |String| + Default: `'puppet-languageserver'` + + This variable can be used to specify the executable used for + puppet-languageserver. + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 232d7630..6cc0026d 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -186,6 +186,7 @@ CONTENTS *ale-contents* puglint.............................|ale-pug-puglint| puppet................................|ale-puppet-options| puppetlint..........................|ale-puppet-puppetlint| + puppet-languageserver...............|ale-puppet-languageserver| pyrex (cython)........................|ale-pyrex-options| cython..............................|ale-pyrex-cython| python................................|ale-python-options| @@ -390,7 +391,7 @@ Notes: * Pony: `ponyc` * proto: `protoc-gen-lint` * Pug: `pug-lint` -* Puppet: `puppet`, `puppet-lint` +* Puppet: `languageserver`, `puppet`, `puppet-lint` * Python: `autopep8`, `black`, `flake8`, `isort`, `mypy`, `prospector`, `pycodestyle`, `pyls`, `pyre`, `pylint`!!, `yapf` * QML: `qmlfmt`, `qmllint` * R: `lintr` From 9ca133feb0a97785aa5a3a2aa27763e385d6b47c Mon Sep 17 00:00:00 2001 From: Alexander Olofsson Date: Thu, 5 Jul 2018 21:17:14 +0200 Subject: [PATCH 2/3] Start of vader tests for puppet root detection --- .../lib/puppet/types/exampletype.rb | 0 .../new-style-module/metadata.json | 0 .../new-style-module/template/template.epp | 0 .../old-style-module/manifests/init.pp | 0 .../old-style-module/templates/template.epp | 0 test/test_puppet_path_detection.vader | 22 +++++++++++++++++++ 6 files changed, 22 insertions(+) create mode 100644 test/puppet-test-files/new-style-module/lib/puppet/types/exampletype.rb create mode 100644 test/puppet-test-files/new-style-module/metadata.json create mode 100644 test/puppet-test-files/new-style-module/template/template.epp create mode 100644 test/puppet-test-files/old-style-module/manifests/init.pp create mode 100644 test/puppet-test-files/old-style-module/templates/template.epp create mode 100644 test/test_puppet_path_detection.vader diff --git a/test/puppet-test-files/new-style-module/lib/puppet/types/exampletype.rb b/test/puppet-test-files/new-style-module/lib/puppet/types/exampletype.rb new file mode 100644 index 00000000..e69de29b diff --git a/test/puppet-test-files/new-style-module/metadata.json b/test/puppet-test-files/new-style-module/metadata.json new file mode 100644 index 00000000..e69de29b diff --git a/test/puppet-test-files/new-style-module/template/template.epp b/test/puppet-test-files/new-style-module/template/template.epp new file mode 100644 index 00000000..e69de29b diff --git a/test/puppet-test-files/old-style-module/manifests/init.pp b/test/puppet-test-files/old-style-module/manifests/init.pp new file mode 100644 index 00000000..e69de29b diff --git a/test/puppet-test-files/old-style-module/templates/template.epp b/test/puppet-test-files/old-style-module/templates/template.epp new file mode 100644 index 00000000..e69de29b diff --git a/test/test_puppet_path_detection.vader b/test/test_puppet_path_detection.vader new file mode 100644 index 00000000..f8d3a0fa --- /dev/null +++ b/test/test_puppet_path_detection.vader @@ -0,0 +1,22 @@ +Before: + call ale#test#SetDirectory('/testplugin/test') + + runtime ale_linters/puppet/languageserver.vim + +After: + call ale#test#RestoreDirectory() + call ale#linter#Reset() + +Execute(old-style module should find its root correctly): + call ale#test#SetFilename('puppet-test-files/old-style-module/manifests/init.pp') + + AssertEqual + \ ale#path#Simplify(g:dir . '/puppet-test-files/old-style-module'), + \ ale_linters#puppet#languageserver#GetProjectRoot(bufnr('')) + +Execute(new-style module should find its root correctly): + call ale#test#SetFilename('puppet-test-files/new-style-module/lib/puppet/types/exampletype.rb') + + AssertEqual + \ ale#path#Simplify(g:dir . '/puppet-test-files/new-style-module'), + \ ale_linters#puppet#languageserver#GetProjectRoot(bufnr('')) From f78db619d45e54949a8d5d667dffef6ea1e6adb3 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson Date: Thu, 5 Jul 2018 21:35:30 +0200 Subject: [PATCH 3/3] Clean up root detection for puppet languageserver --- ale_linters/puppet/languageserver.vim | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ale_linters/puppet/languageserver.vim b/ale_linters/puppet/languageserver.vim index feff1d53..52880f32 100644 --- a/ale_linters/puppet/languageserver.vim +++ b/ale_linters/puppet/languageserver.vim @@ -14,15 +14,25 @@ function! ale_linters#puppet#languageserver#GetCommand(buffer) abort endfunction function! ale_linters#puppet#languageserver#GetProjectRoot(buffer) abort - " Note: while manifest.json is a strong recommendation, the only - " *required* path for a Puppet module is the manifests folder. + " Note: The metadata.json file is recommended for Puppet 4+ modules, but + " there's no requirement to have it, so fall back to the other possible + " Puppet module directories let l:root_path = ale#path#FindNearestFile(a:buffer, 'metadata.json') - - if empty(l:root_path) - let l:root_path = ale#path#FindNearestDirectory(a:buffer, 'manifests') + if !empty(l:root_path) + return fnamemodify(l:root_path, ':h') endif - return !empty(l:root_path) ? fnamemodify(l:root_path, ':h') : '' + for l:test_path in [ + \ 'manifests', + \ 'templates', + \] + let l:root_path = ale#path#FindNearestDirectory(a:buffer, l:test_path) + if !empty(l:root_path) + return fnamemodify(l:root_path, ':h:h') + endif + endfor + + return '' endfunction call ale#linter#Define('puppet', {