mirror of
https://github.com/dense-analysis/ale
synced 2024-12-30 18:12:08 +00:00
Add a buildifier fixer for Bazel files (#3499)
This fixer enables buildifier's formatting and "lint fix" modes. Additional options can be provided via `bazel_buildifier_options`. It also implements some basic logic for guessing the file's type. buildifier itself usually does this based on the filenames provided on the command line, but because we're piping our buffer via stdin, we need to do this manually.
This commit is contained in:
parent
2550f5d952
commit
90b9597d50
@ -32,6 +32,11 @@ let s:default_registry = {
|
||||
\ 'suggested_filetypes': ['python'],
|
||||
\ 'description': 'Fix PEP8 issues with black.',
|
||||
\ },
|
||||
\ 'buildifier': {
|
||||
\ 'function': 'ale#fixers#buildifier#Fix',
|
||||
\ 'suggested_filetypes': ['bzl'],
|
||||
\ 'description': 'Format BUILD and .bzl files with buildifier.',
|
||||
\ },
|
||||
\ 'deno': {
|
||||
\ 'function': 'ale#fixers#deno#Fix',
|
||||
\ 'suggested_filetypes': ['typescript'],
|
||||
|
37
autoload/ale/fixers/buildifier.vim
Normal file
37
autoload/ale/fixers/buildifier.vim
Normal file
@ -0,0 +1,37 @@
|
||||
" Author: Jon Parise <jon@indelible.org>
|
||||
" Description: Format Bazel BUILD and .bzl files with buildifier.
|
||||
"
|
||||
call ale#Set('bazel_buildifier_executable', 'buildifier')
|
||||
call ale#Set('bazel_buildifier_use_global', get(g:, 'ale_use_global_executables', 0))
|
||||
call ale#Set('bazel_buildifier_options', '')
|
||||
|
||||
function! ale#fixers#buildifier#GetExecutable(buffer) abort
|
||||
return ale#node#FindExecutable(a:buffer, 'bazel_buildifier', [
|
||||
\ 'buildifier',
|
||||
\])
|
||||
endfunction
|
||||
|
||||
function! ale#fixers#buildifier#Fix(buffer) abort
|
||||
let l:executable = ale#Escape(ale#fixers#buildifier#GetExecutable(a:buffer))
|
||||
let l:options = ale#Var(a:buffer, 'bazel_buildifier_options')
|
||||
let l:filename = fnamemodify(bufname(a:buffer), ':t')
|
||||
|
||||
let l:command = l:executable . ' -mode fix -lint fix'
|
||||
|
||||
" Attempt to guess the file type based on the filename. buildifier itself
|
||||
" usually does this based on the filenames provided on the command line,
|
||||
" but because we're piping our buffer via stdin, we do this manually.
|
||||
if l:filename =~? 'WORKSPACE'
|
||||
let l:command .= ' -type workspace'
|
||||
elseif l:filename =~? 'BUILD'
|
||||
let l:command .= ' -type build'
|
||||
elseif l:filename =~? '.bzl$'
|
||||
let l:command .= ' -type bzl'
|
||||
endif
|
||||
|
||||
if l:options isnot# ''
|
||||
let l:command .= ' ' . l:options
|
||||
endif
|
||||
|
||||
return {'command': l:command . ' -'}
|
||||
endfunction
|
28
doc/ale-bazel.txt
Normal file
28
doc/ale-bazel.txt
Normal file
@ -0,0 +1,28 @@
|
||||
===============================================================================
|
||||
ALE Bazel Integration *ale-bazel-options*
|
||||
|
||||
===============================================================================
|
||||
buildifier *ale-bazel-buildifier*
|
||||
|
||||
g:ale_bazel_buildifier_executable *g:ale_bazel_buildifier_executable*
|
||||
*b:ale_bazel_buildifier_executable*
|
||||
Type: |String|
|
||||
Default: `'buildifier'`
|
||||
|
||||
See |ale-integrations-local-executables|
|
||||
|
||||
|
||||
g:ale_bazel_buildifier_options *g:ale_bazel_buildifier_options*
|
||||
*b:ale_bazel_buildifier_options*
|
||||
Type: |String|
|
||||
Default: `''`
|
||||
|
||||
This variable can be set to pass extra options to buildifier.
|
||||
|
||||
|
||||
g:ale_bazel_buildifier_use_global *g:ale_bazel_buildifier_use_global*
|
||||
*b:ale_bazel_buildifier_use_global*
|
||||
Type: |Number|
|
||||
Default: `get(g:, 'ale_use_global_executables', 0)`
|
||||
|
||||
See |ale-integrations-local-executables|
|
@ -43,6 +43,8 @@ Notes:
|
||||
* `shfmt`
|
||||
* Bats
|
||||
* `shellcheck`
|
||||
* Bazel
|
||||
* `buildifier`
|
||||
* BibTeX
|
||||
* `bibclean`
|
||||
* Bourne Shell
|
||||
|
@ -2614,6 +2614,8 @@ documented in additional help files.
|
||||
gawk..................................|ale-awk-gawk|
|
||||
bats....................................|ale-bats-options|
|
||||
shellcheck............................|ale-bats-shellcheck|
|
||||
bazel...................................|ale-bazel-options|
|
||||
buildifier............................|ale-bazel-buildifier|
|
||||
bib.....................................|ale-bib-options|
|
||||
bibclean..............................|ale-bib-bibclean|
|
||||
c.......................................|ale-c-options|
|
||||
|
@ -52,6 +52,8 @@ formatting.
|
||||
* [shfmt](https://github.com/mvdan/sh)
|
||||
* Bats
|
||||
* [shellcheck](https://www.shellcheck.net/)
|
||||
* Bazel
|
||||
* [buildifier](https://github.com/bazelbuild/buildtools)
|
||||
* BibTeX
|
||||
* [bibclean](http://ftp.math.utah.edu/pub/bibclean/)
|
||||
* Bourne Shell
|
||||
|
0
test/command_callback/bazel_paths/BUILD
Normal file
0
test/command_callback/bazel_paths/BUILD
Normal file
0
test/command_callback/bazel_paths/WORKSPACE
Normal file
0
test/command_callback/bazel_paths/WORKSPACE
Normal file
0
test/command_callback/bazel_paths/defs.bzl
Normal file
0
test/command_callback/bazel_paths/defs.bzl
Normal file
43
test/fixers/test_buildifier_fixer_callback.vader
Normal file
43
test/fixers/test_buildifier_fixer_callback.vader
Normal file
@ -0,0 +1,43 @@
|
||||
Before:
|
||||
let g:ale_bazel_buildifier_options = ''
|
||||
call ale#assert#SetUpFixerTest('bzl', 'buildifier')
|
||||
|
||||
After:
|
||||
call ale#assert#TearDownFixerTest()
|
||||
|
||||
Execute(The buildifier callback should return the correct default values):
|
||||
call ale#test#SetFilename('bazel_paths/WORKSPACE')
|
||||
|
||||
AssertFixer
|
||||
\ {
|
||||
\ 'command': ale#Escape(g:ale_bazel_buildifier_executable)
|
||||
\ . ' -mode fix -lint fix -type workspace -'
|
||||
\ }
|
||||
|
||||
Execute(The buildifier callback should include any additional options):
|
||||
call ale#test#SetFilename('bazel_paths/WORKSPACE')
|
||||
let g:ale_bazel_buildifier_options = '--some-option'
|
||||
|
||||
AssertFixer
|
||||
\ {
|
||||
\ 'command': ale#Escape(g:ale_bazel_buildifier_executable)
|
||||
\ . ' -mode fix -lint fix -type workspace --some-option -',
|
||||
\ }
|
||||
|
||||
Execute(The buildifier callback should recognize BUILD files):
|
||||
call ale#test#SetFilename('bazel_paths/BUILD')
|
||||
|
||||
AssertFixer
|
||||
\ {
|
||||
\ 'command': ale#Escape(g:ale_bazel_buildifier_executable)
|
||||
\ . ' -mode fix -lint fix -type build -'
|
||||
\ }
|
||||
|
||||
Execute(The buildifier callback should recognize .bzl files):
|
||||
call ale#test#SetFilename('bazel_paths/defs.bzl')
|
||||
|
||||
AssertFixer
|
||||
\ {
|
||||
\ 'command': ale#Escape(g:ale_bazel_buildifier_executable)
|
||||
\ . ' -mode fix -lint fix -type bzl -'
|
||||
\ }
|
Loading…
Reference in New Issue
Block a user