From 429f5a14474c21392f5d5f863f4a98e04a7e02d0 Mon Sep 17 00:00:00 2001 From: Zhuoyun Wei Date: Mon, 16 May 2022 05:15:52 -0700 Subject: [PATCH] Add support for Packer (#4192) * Add support for HashiCorp Packer * Add test for packer fmt * Add doc for HCL/Packer * Add link to Packer doc * Also suggest packer fix for packer ft * Add more links to TOC --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/packer.vim | 17 ++++++++++ doc/ale-hcl.txt | 5 +++ doc/ale-packer.txt | 24 +++++++++++++ doc/ale-supported-languages-and-tools.txt | 3 ++ doc/ale.txt | 3 ++ supported-tools.md | 3 ++ .../test_packer_fmt_fixer_callback.vader | 34 +++++++++++++++++++ 8 files changed, 94 insertions(+) create mode 100644 autoload/ale/fixers/packer.vim create mode 100644 doc/ale-packer.txt create mode 100644 test/fixers/test_packer_fmt_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 6932ad9d..eae29000 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -436,6 +436,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['hcl', 'terraform'], \ 'description': 'Fix tf and hcl files with terraform fmt.', \ }, +\ 'packer': { +\ 'function': 'ale#fixers#packer#Fix', +\ 'suggested_filetypes': ['hcl', 'packer'], +\ 'description': 'Fix Packer HCL files with packer fmt.', +\ }, \ 'crystal': { \ 'function': 'ale#fixers#crystal#Fix', \ 'suggested_filetypes': ['cr'], diff --git a/autoload/ale/fixers/packer.vim b/autoload/ale/fixers/packer.vim new file mode 100644 index 00000000..8770550d --- /dev/null +++ b/autoload/ale/fixers/packer.vim @@ -0,0 +1,17 @@ +" Author: Zhuoyun Wei +" Description: Fixer for Packer HCL files + +call ale#Set('packer_fmt_executable', 'packer') +call ale#Set('packer_fmt_options', '') + +function! ale#fixers#packer#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'packer_fmt_executable') + let l:options = ale#Var(a:buffer, 'packer_fmt_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' fmt' + \ . (empty(l:options) ? '' : ' ' . l:options) + \ . ' -' + \} +endfunction diff --git a/doc/ale-hcl.txt b/doc/ale-hcl.txt index 59b0a9da..71e1114e 100644 --- a/doc/ale-hcl.txt +++ b/doc/ale-hcl.txt @@ -2,6 +2,11 @@ ALE HCL Integration *ale-hcl-options* +=============================================================================== +packer-fmt *ale-hcl-packer-fmt* + +See |ale-packer-fmt-fixer| for information about the available options. + =============================================================================== terraform-fmt *ale-hcl-terraform-fmt* diff --git a/doc/ale-packer.txt b/doc/ale-packer.txt new file mode 100644 index 00000000..11b7cc22 --- /dev/null +++ b/doc/ale-packer.txt @@ -0,0 +1,24 @@ +=============================================================================== +ALE Packer Integration *ale-packer-options* + + +=============================================================================== +packer-fmt-fixer *ale-packer-fmt-fixer* + +g:ale_packer_fmt_executable *g:ale_packer_fmt_executable* + *b:ale_packer_fmt_executable* + + Type: |String| + Default: `'packer'` + + This variable can be changed to use a different executable for packer. + + +g:ale_packer_fmt_options *g:ale_packer_fmt_options* + *b:ale_packer_fmt_options* + Type: |String| + Default: `''` + + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index e4c021c3..5e5e0124 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -239,6 +239,7 @@ Notes: * `stack-ghc` * `stylish-haskell` * HCL + * `packer-fmt` * `terraform-fmt` * HTML * `VSCode HTML language server` @@ -392,6 +393,8 @@ Notes: * `ibm_validator` * `prettier` * `yamllint` +* Packer + * `packer-fmt-fixer` * Pascal * `ptop` * Pawn diff --git a/doc/ale.txt b/doc/ale.txt index 5430affb..4f55258f 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2911,6 +2911,7 @@ documented in additional help files. hie...................................|ale-haskell-hie| ormolu................................|ale-haskell-ormolu| hcl.....................................|ale-hcl-options| + packer-fmt............................|ale-hcl-packer-fmt| terraform-fmt.........................|ale-hcl-terraform-fmt| help....................................|ale-help-options| cspell................................|ale-help-cspell| @@ -3041,6 +3042,8 @@ documented in additional help files. ibm_validator.........................|ale-openapi-ibm-validator| prettier..............................|ale-openapi-prettier| yamllint..............................|ale-openapi-yamllint| + packer..................................|ale-packer-options| + packer-fmt-fixer......................|ale-packer-fmt-fixer| pascal..................................|ale-pascal-options| ptop..................................|ale-pascal-ptop| pawn....................................|ale-pawn-options| diff --git a/supported-tools.md b/supported-tools.md index 1e7314a1..c4e26f23 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -248,6 +248,7 @@ formatting. * [stack-ghc](https://haskellstack.org/) * [stylish-haskell](https://github.com/jaspervdj/stylish-haskell) * HCL + * [packer-fmt](https://github.com/hashicorp/packer) * [terraform-fmt](https://github.com/hashicorp/terraform) * HTML * [VSCode HTML language server](https://github.com/hrsh7th/vscode-langservers-extracted) @@ -401,6 +402,8 @@ formatting. * [ibm_validator](https://github.com/IBM/openapi-validator) * [prettier](https://github.com/prettier/prettier) * [yamllint](https://yamllint.readthedocs.io/) +* Packer (HCL) + * [packer-fmt-fixer](https://github.com/hashicorp/packer) * Pascal * [ptop](https://www.freepascal.org/tools/ptop.var) * Pawn diff --git a/test/fixers/test_packer_fmt_fixer_callback.vader b/test/fixers/test_packer_fmt_fixer_callback.vader new file mode 100644 index 00000000..2eb07ed4 --- /dev/null +++ b/test/fixers/test_packer_fmt_fixer_callback.vader @@ -0,0 +1,34 @@ +Before: + Save g:ale_packer_fmt_executable + Save g:ale_packer_fmt_options + + " Use an invalid global executable, so we don't match it. + let g:ale_packer_fmt_executable = 'xxxinvalid' + let g:ale_packer_fmt_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The packer fmt callback should return the correct default values): + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') . ' fmt -', + \ }, + \ ale#fixers#packer#Fix(bufnr('')) + +Execute(The packer fmt callback should include custom options): + let g:ale_packer_fmt_options = "-list=true" + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' fmt' + \ . ' ' . g:ale_packer_fmt_options + \ . ' -', + \ }, + \ ale#fixers#packer#Fix(bufnr(''))