Add 'scalafmt' fixer for Scala files

closes https://github.com/w0rp/ale/issues/1299
This commit is contained in:
Jeffrey Lau 2018-06-03 02:59:53 +08:00
parent 786fc0a62f
commit 77d0ac58ed
7 changed files with 134 additions and 2 deletions

View file

@ -164,7 +164,7 @@ formatting.
| Rust | cargo !! (see `:help ale-integration-rust` for configuration instructions), [rls](https://github.com/rust-lang-nursery/rls), [rustc](https://www.rust-lang.org/), [rustfmt](https://github.com/rust-lang-nursery/rustfmt) |
| SASS | [sass-lint](https://www.npmjs.com/package/sass-lint), [stylelint](https://github.com/stylelint/stylelint) |
| SCSS | [prettier](https://github.com/prettier/prettier), [sass-lint](https://www.npmjs.com/package/sass-lint), [scss-lint](https://github.com/brigade/scss-lint), [stylelint](https://github.com/stylelint/stylelint) |
| Scala | [fsc](https://www.scala-lang.org/old/sites/default/files/linuxsoft_archives/docu/files/tools/fsc.html), [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) |
| Scala | [fsc](https://www.scala-lang.org/old/sites/default/files/linuxsoft_archives/docu/files/tools/fsc.html), [scalac](http://scala-lang.org), [scalafmt](https://scalameta.org/scalafmt/), [scalastyle](http://www.scalastyle.org) |
| Slim | [slim-lint](https://github.com/sds/slim-lint) |
| SML | [smlnj](http://www.smlnj.org/) |
| Solidity | [solhint](https://github.com/protofire/solhint), [solium](https://github.com/duaraghav8/Solium) |

View file

@ -95,6 +95,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['ruby'],
\ 'description': 'Fix ruby files with rufo',
\ },
\ 'scalafmt': {
\ 'function': 'ale#fixers#scalafmt#Fix',
\ 'suggested_filetypes': ['scala'],
\ 'description': 'Fix Scala files using scalafmt',
\ },
\ 'standard': {
\ 'function': 'ale#fixers#standard#Fix',
\ 'suggested_filetypes': ['javascript'],

View file

@ -0,0 +1,26 @@
" Author: Jeffrey Lau https://github.com/zoonfafer
" Description: Integration of Scalafmt with ALE.
call ale#Set('scala_scalafmt_executable', 'scalafmt')
call ale#Set('scala_scalafmt_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('scala_scalafmt_options', '')
function! ale#fixers#scalafmt#GetCommand(buffer) abort
let l:executable = ale#Var(a:buffer, 'scala_scalafmt_executable')
let l:options = ale#Var(a:buffer, 'scala_scalafmt_options')
let l:exec_args = l:executable =~? 'ng$'
\ ? ' scalafmt'
\ : ''
return ale#Escape(l:executable) . l:exec_args
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' %t'
endfunction
function! ale#fixers#scalafmt#Fix(buffer) abort
return {
\ 'command': ale#fixers#scalafmt#GetCommand(a:buffer),
\ 'read_temporary_file': 1,
\}
endfunction

View file

@ -2,6 +2,37 @@
ALE Scala Integration *ale-scala-options*
===============================================================================
scalafmt *ale-scala-scalafmt*
If Nailgun is used, override `g:ale_scala_scalafmt_executable` like so: >
let g:ale_scala_scalafmt_executable = 'ng'
To disable `scalafmt` globally, use |g:ale_fixers| like so: >
let g:ale_fixers = {'scala': []}
<
See |g:ale_fixers| for more information on disabling fixers.
g:ale_scala_scalafmt_executable *g:ale_scala_scalafmt_executable*
*b:ale_scala_scalafmt_executable*
Type: |String|
Default: `'scalafmt'`
Override the invoked `scalafmt` binary. This is useful for running `scalafmt`
with Nailgun.
g:ale_scala_scalafmt_options *g:ale_scala_scalafmt_options*
*b:ale_scala_scalafmt_options*
Type: |String|
Default: `''`
A string containing additional options to pass to `'scalafmt'`, or
`'ng scalafmt'` if Nailgun is used.
===============================================================================
scalastyle *ale-scala-scalastyle*

View file

@ -218,6 +218,7 @@ CONTENTS *ale-contents*
sass..................................|ale-sass-options|
stylelint...........................|ale-sass-stylelint|
scala.................................|ale-scala-options|
scalafmt............................|ale-scala-scalafmt|
scalastyle..........................|ale-scala-scalastyle|
scss..................................|ale-scss-options|
prettier............................|ale-scss-prettier|
@ -388,7 +389,7 @@ Notes:
* Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|), `rustfmt`
* SASS: `sass-lint`, `stylelint`
* SCSS: `prettier`, `sass-lint`, `scss-lint`, `stylelint`
* Scala: `fsc`, `scalac`, `scalastyle`
* Scala: `fsc`, `scalac`, `scalafmt`, `scalastyle`
* Slim: `slim-lint`
* SML: `smlnj`
* Solidity: `solhint`, `solium`

View file

@ -0,0 +1,69 @@
Before:
Save g:ale_scala_scalafmt_executable
Save g:ale_scala_scalafmt_options
" Use an invalid global executable, so we don't match it.
let g:ale_scala_scalafmt_executable = 'xxxinvalid'
let g:ale_scala_scalafmt_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
silent cd ..
silent cd command_callback
let g:dir = getcwd()
After:
Restore
call ale#test#RestoreDirectory()
Execute(The scalafmt callback should return the correct default values):
call ale#test#SetFilename('scala_paths/dummy.scala')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_scala_scalafmt_executable)
\ . ' %t',
\ },
\ ale#fixers#scalafmt#Fix(bufnr(''))
Execute(The scalafmt callback should use ng with scalafmt automatically):
let g:ale_scala_scalafmt_executable = 'ng'
call ale#test#SetFilename('scala_paths/dummy.scala')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('ng')
\ . ' scalafmt'
\ . ' %t',
\ },
\ ale#fixers#scalafmt#Fix(bufnr(''))
Execute(The scalafmt callback should include custom scalafmt options):
let g:ale_scala_scalafmt_options = '--diff'
call ale#test#SetFilename('scala_paths/dummy.scala')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_scala_scalafmt_executable)
\ . ' --diff'
\ . ' %t',
\ },
\ ale#fixers#scalafmt#Fix(bufnr(''))
Execute(The scalafmt callback should include custom scalafmt options and use ng with scalafmt):
let g:ale_scala_scalafmt_options = '--diff'
let g:ale_scala_scalafmt_executable = 'ng'
call ale#test#SetFilename('scala_paths/dummy.scala')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('ng')
\ . ' scalafmt'
\ . ' --diff'
\ . ' %t',
\ },
\ ale#fixers#scalafmt#Fix(bufnr(''))