From e94a594e2ef65b27a79c406ce65c76a55fa39ead Mon Sep 17 00:00:00 2001 From: Derek P Sifford Date: Tue, 23 Oct 2018 20:44:31 -0400 Subject: [PATCH] add terraform fmt fixer for terraform and hcl filetypes See #1718 --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/terraform.vim | 17 ++++++++++ doc/ale-hcl.txt | 11 ++++++ doc/ale-terraform.txt | 18 ++++++++++ doc/ale.txt | 3 ++ .../test_terraform_fmt_fixer_callback.vader | 34 +++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 autoload/ale/fixers/terraform.vim create mode 100644 doc/ale-hcl.txt create mode 100644 test/fixers/test_terraform_fmt_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 76cce87f..98f52232 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -250,6 +250,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['c', 'cpp', 'cs', 'objc', 'objcpp', 'd', 'java', 'p', 'vala' ], \ 'description': 'Fix C, C++, C#, ObjectiveC, ObjectiveC++, D, Java, Pawn, and VALA files with uncrustify.', \ }, +\ 'terraform': { +\ 'function': 'ale#fixers#terraform#Fix', +\ 'suggested_filetypes': ['hcl', 'terraform'], +\ 'description': 'Fix tf and hcl files with terraform fmt.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/terraform.vim b/autoload/ale/fixers/terraform.vim new file mode 100644 index 00000000..bc05380a --- /dev/null +++ b/autoload/ale/fixers/terraform.vim @@ -0,0 +1,17 @@ +" Author: dsifford +" Description: Fixer for terraform and .hcl files + +call ale#Set('terraform_fmt_executable', 'terraform') +call ale#Set('terraform_fmt_options', '') + +function! ale#fixers#terraform#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'terraform_fmt_executable') + let l:options = ale#Var(a:buffer, 'terraform_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 new file mode 100644 index 00000000..8060ac44 --- /dev/null +++ b/doc/ale-hcl.txt @@ -0,0 +1,11 @@ +=============================================================================== +ALE HCL Integration *ale-hcl-options* + + +=============================================================================== +terraform-fmt *ale-hcl-terraform-fmt* + +See |ale-terraform-fmt| for information about the available options. + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-terraform.txt b/doc/ale-terraform.txt index ec86e9a0..49a55028 100644 --- a/doc/ale-terraform.txt +++ b/doc/ale-terraform.txt @@ -2,6 +2,24 @@ ALE Terraform Integration *ale-terraform-options* +=============================================================================== +fmt *ale-terraform-fmt* + +g:ale_terraform_fmt_executable *g:ale_terraform_fmt_executable* + *b:ale_terraform_fmt_executable* + + Type: |String| + Default: `'terraform'` + + This variable can be changed to use a different executable for terraform. + + +g:ale_terraform_fmt_options *g:ale_terraform_fmt_options* + *b:ale_terraform_fmt_options* + Type: |String| + Default: `''` + + =============================================================================== tflint *ale-terraform-tflint* diff --git a/doc/ale.txt b/doc/ale.txt index 3aab94b4..d6b280b0 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -129,6 +129,8 @@ CONTENTS *ale-contents* stack-build.........................|ale-haskell-stack-build| stylish-haskell.....................|ale-haskell-stylish-haskell| hie.................................|ale-haskell-hie| + hcl...................................|ale-hcl-options| + terraform-fmt.......................|ale-hcl-terraform-fmt| html..................................|ale-html-options| htmlhint............................|ale-html-htmlhint| tidy................................|ale-html-tidy| @@ -297,6 +299,7 @@ CONTENTS *ale-contents* tcl...................................|ale-tcl-options| nagelfar............................|ale-tcl-nagelfar| terraform.............................|ale-terraform-options| + fmt.................................|ale-terraform-fmt| tflint..............................|ale-terraform-tflint| tex...................................|ale-tex-options| chktex..............................|ale-tex-chktex| diff --git a/test/fixers/test_terraform_fmt_fixer_callback.vader b/test/fixers/test_terraform_fmt_fixer_callback.vader new file mode 100644 index 00000000..15377a7e --- /dev/null +++ b/test/fixers/test_terraform_fmt_fixer_callback.vader @@ -0,0 +1,34 @@ +Before: + Save g:ale_terraform_fmt_executable + Save g:ale_terraform_fmt_options + + " Use an invalid global executable, so we don't match it. + let g:ale_terraform_fmt_executable = 'xxxinvalid' + let g:ale_terraform_fmt_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The terraform fmt callback should return the correct default values): + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') . ' fmt -', + \ }, + \ ale#fixers#terraform#Fix(bufnr('')) + +Execute(The terraform fmt callback should include custom options): + let g:ale_terraform_fmt_options = "-list=true" + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' fmt' + \ . ' ' . g:ale_terraform_fmt_options + \ . ' -', + \ }, + \ ale#fixers#terraform#Fix(bufnr(''))