diff --git a/ale_linters/yaml/yq.vim b/ale_linters/yaml/yq.vim new file mode 100644 index 0000000000..35aef654c0 --- /dev/null +++ b/ale_linters/yaml/yq.vim @@ -0,0 +1,22 @@ +" Author: axhav +call ale#Set('yaml_yq_executable', 'yq') +call ale#Set('yaml_yq_options', '') +call ale#Set('yaml_yq_filters', '.') + +" Matches patterns like the following: +let s:pattern = '^Error\:.* line \(\d\+\)\: \(.\+\)$' + +function! ale_linters#yaml#yq#Handle(buffer, lines) abort + return ale#util#MapMatches(a:lines, s:pattern, {match -> { + \ 'lnum': match[1] + 0, + \ 'text': match[2], + \}}) +endfunction + +call ale#linter#Define('yaml', { +\ 'name': 'yq', +\ 'executable': {b -> ale#Var(b, 'yaml_yq_executable')}, +\ 'output_stream': 'stderr', +\ 'command': '%e', +\ 'callback': 'ale_linters#yaml#yq#Handle', +\}) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 57ffa9a3b6..4ff04bac81 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -207,6 +207,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['python'], \ 'description': 'Fix Python files with yapf.', \ }, +\ 'yq': { +\ 'function': 'ale#fixers#yq#Fix', +\ 'suggested_filetypes': ['yaml'], +\ 'description': 'Fix YAML files with yq.', +\ }, \ 'rubocop': { \ 'function': 'ale#fixers#rubocop#Fix', \ 'suggested_filetypes': ['ruby'], diff --git a/autoload/ale/fixers/yq.vim b/autoload/ale/fixers/yq.vim new file mode 100644 index 0000000000..b9bf70070e --- /dev/null +++ b/autoload/ale/fixers/yq.vim @@ -0,0 +1,22 @@ +call ale#Set('yaml_yq_executable', 'yq') +call ale#Set('yaml_yq_options', '') +call ale#Set('yaml_yq_filters', '.') + +function! ale#fixers#yq#GetExecutable(buffer) abort + return ale#Var(a:buffer, 'yaml_yq_executable') +endfunction + +function! ale#fixers#yq#Fix(buffer) abort + let l:options = ale#Var(a:buffer, 'yaml_yq_options') + let l:filters = ale#Var(a:buffer, 'yaml_yq_filters') + + if empty(l:filters) + return 0 + endif + + return { + \ 'command': ale#Escape(ale#fixers#yq#GetExecutable(a:buffer)) + \ . ' ' . l:filters . ' ' + \ . l:options, + \} +endfunction diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 6a11d4aaca..40d9210c30 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -729,6 +729,7 @@ Notes: * `yamlfix` * `yamlfmt` * `yamllint` + * `yq` * YANG * `yang-lsp` * Zeek diff --git a/doc/ale-yaml.txt b/doc/ale-yaml.txt index a6741c8320..fc52dc73bc 100644 --- a/doc/ale-yaml.txt +++ b/doc/ale-yaml.txt @@ -366,5 +366,43 @@ g:ale_yaml_gitlablint_options *g:ale_yaml_gitlablint_options This variable can be set to pass additional options to gll. +=============================================================================== +yq *ale-yaml-yq* + +Website: https://github.com/mikefarah/yq + + +Installation +------------------------------------------------------------------------------- + +Install yq: > + + wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY}.tar.gz -O - | tar xz && mv ${BINARY} /usr/bin/yq + +Options +------------------------------------------------------------------------------- + +g:ale_yaml_yq_executable *g:ale_yaml_yq_executable* + *b:ale_yaml_yq_executable* + Type: |String| + Default: `'yq'` + + This variable can be set to change the path to yq. + + +g:ale_yaml_yq_options *g:ale_yaml_yq_options* + *b:ale_yaml_yq_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to yq. + +g:ale_yaml_yq_filters *g:ale_yaml_yq_filters + *b:ale_yaml_yq_filters* + Type: |String| + Default: `'.'` + + This option can be changed to pass additional filters to yq + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 1d62fd2d6b..3d7e8f5a94 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3517,6 +3517,7 @@ documented in additional help files. yamlfmt...............................|ale-yaml-yamlfmt| yamllint..............................|ale-yaml-yamllint| gitlablint............................|ale-yaml-gitlablint| + yq....................................|ale-yaml-yq| yang....................................|ale-yang-options| yang-lsp..............................|ale-yang-lsp| zeek....................................|ale-zeek-options| diff --git a/supported-tools.md b/supported-tools.md index 985f0f4617..c6a99de02c 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -738,6 +738,7 @@ formatting. * [yamlfix](https://lyz-code.github.io/yamlfix) * [yamlfmt](https://github.com/google/yamlfmt) * [yamllint](https://yamllint.readthedocs.io/) + * [yq](https://github.com/mikefarah/yq) * YANG * [yang-lsp](https://github.com/theia-ide/yang-lsp) * Zeek diff --git a/test/handler/test_yq_handler.vader b/test/handler/test_yq_handler.vader new file mode 100644 index 0000000000..861bf9e401 --- /dev/null +++ b/test/handler/test_yq_handler.vader @@ -0,0 +1,19 @@ +Before: + runtime ale_linters/yaml/yq.vim + +After: + call ale#linter#Reset() + +Execute (Should parse error correctly): + AssertEqual + \ [ + \ { + \ 'lnum': 2, + \ 'text': "did not find expected ',' or ']'", + \ } + \ ], + \ ale_linters#yaml#yq#Handle(bufnr(''), [ + \ "Error: bad file '-': yaml: line 2: did not find expected ',' or ']'" + \ ]) + + diff --git a/test/linter/test_yq.vader b/test/linter/test_yq.vader new file mode 100644 index 0000000000..e2e7dcd100 --- /dev/null +++ b/test/linter/test_yq.vader @@ -0,0 +1,8 @@ +Before: + call ale#assert#SetUpLinterTest('yaml', 'yq') + +After: + call ale#assert#TearDownLinterTest() + +Execute(The default command should be correct): + AssertLinter 'yq', ale#Escape('yq')