Merge pull request #1209 from butlerx/java-fixer

add google-java-format fixer
This commit is contained in:
w0rp 2017-12-17 12:00:52 +00:00 committed by GitHub
commit a22def45b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 2 deletions

View File

@ -108,7 +108,7 @@ formatting.
| Haskell | [brittany](https://github.com/lspitzner/brittany), [ghc](https://www.haskell.org/ghc/), [stack-ghc](https://haskellstack.org/), [stack-build](https://haskellstack.org/) !!, [ghc-mod](https://github.com/DanielG/ghc-mod), [stack-ghc-mod](https://github.com/DanielG/ghc-mod), [hlint](https://hackage.haskell.org/package/hlint), [hdevtools](https://hackage.haskell.org/package/hdevtools), [hfmt](https://github.com/danstiner/hfmt) |
| HTML | [HTMLHint](http://htmlhint.com/), [proselint](http://proselint.com/), [tidy](http://www.html-tidy.org/), [write-good](https://github.com/btford/write-good) |
| Idris | [idris](http://www.idris-lang.org/) |
| Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) |
| Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html), [google-java-format](https://github.com/google/google-java-format) |
| JavaScript | [eslint](http://eslint.org/), [flow](https://flowtype.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [prettier](https://github.com/prettier/prettier), prettier-eslint >= 4.2.0, prettier-standard, [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo)
| JSON | [jsonlint](http://zaa.ch/jsonlint/), [prettier](https://github.com/prettier/prettier) |
| Kotlin | [kotlinc](https://kotlinlang.org) !!, [ktlint](https://ktlint.github.io) !! see `:help ale-integration-kotlin` for configuration instructions |

View File

@ -154,6 +154,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['sh'],
\ 'description': 'Fix sh files with shfmt.',
\ },
\ 'google_java_format': {
\ 'function': 'ale#fixers#google_java_format#Fix',
\ 'suggested_filetypes': ['java'],
\ 'description': 'Fix Java files with google-java-format.',
\ },
\}
" Reset the function registry to the default entries.

View File

@ -0,0 +1,23 @@
" Author: butlerx <butlerx@notthe,cloud>
" Description: Integration of Google-java-format with ALE.
call ale#Set('google_java_format_executable', 'google-java-format')
call ale#Set('google_java_format_use_global', 0)
call ale#Set('google_java_format_options', '')
function! ale#fixers#google_java_format#Fix(buffer) abort
let l:options = ale#Var(a:buffer, 'google_java_format_options')
let l:executable = ale#Var(a:buffer, 'google_java_format_executable')
if !executable(l:executable)
return 0
endif
return {
\ 'command': ale#Escape(l:executable)
\ . ' ' . (empty(l:options) ? '' : ' ' . l:options)
\ . ' --replace'
\ . ' %t',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -33,5 +33,24 @@ g:ale_java_javac_options *g:ale_java_javac_options*
This variable can be set to pass additional options to javac.
===============================================================================
google-java-format *ale-java-google-java-format*
g:ale_java_google_java_format_executable *g:ale_java_google_java_format_executable*
*b:ale_java_google_java_format_executable*
Type: |String|
Default: `'google-java-format'`
See |ale-integrations-local-executables|
g:ale_java_google_java_format_options *g:ale_java_google_java_format_options*
*b:ale_java_google_java_format_options*
Type: |String|
Default: `''`
This variable can be set to pass additional options
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -93,6 +93,7 @@ CONTENTS *ale-contents*
java..................................|ale-java-options|
checkstyle..........................|ale-java-checkstyle|
javac...............................|ale-java-javac|
google-java-format..................|ale-java-google-java-format|
javascript............................|ale-javascript-options|
eslint..............................|ale-javascript-eslint|
flow................................|ale-javascript-flow|
@ -307,7 +308,7 @@ Notes:
* Haskell: `brittany`, `ghc`, `stack-ghc`, `stack-build`!!, `ghc-mod`, `stack-ghc-mod`, `hlint`, `hdevtools`, `hfmt`
* HTML: `HTMLHint`, `proselint`, `tidy`, `write-good`
* Idris: `idris`
* Java: `checkstyle`, `javac`
* Java: `checkstyle`, `javac`, `google-java-format`
* JavaScript: `eslint`, `flow`, `jscs`, `jshint`, `prettier`, `prettier-eslint` >= 4.2.0, `prettier-standard`, `standard`, `xo`
* JSON: `jsonlint`, `prettier`
* Kotlin: `kotlinc`, `ktlint`

View File

@ -0,0 +1,27 @@
Before:
Save g:ale_google_java_format_executable
" Use an invalid global executable, so we don't match it.
let g:ale_google_java_format_executable = 'xxxinvalid'
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The google-java-format callback should return 0 when the executable isn't executable):
AssertEqual
\ 0,
\ ale#fixers#google_java_format#Fix(bufnr(''))
Execute(The google-java-format callback should run the command when the executable test passes):
let g:ale_google_java_format_executable = has('win32') ? 'cmd' : 'echo'
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(ale_google_java_format_executable) . ' --replace %t'
\ },
\ ale#fixers#google_java_format#Fix(bufnr(''))