add rustfmt fixer

This commit is contained in:
Kelly Fox 2017-10-21 12:31:49 -05:00
parent 3878be9977
commit 35031a0b8a
7 changed files with 76 additions and 2 deletions

View File

@ -127,7 +127,7 @@ formatting.
| reStructuredText | [proselint](http://proselint.com/) |
| RPM spec | [rpmlint](https://github.com/rpm-software-management/rpmlint) (disabled by default; see `:help ale-integration-spec`) |
| Ruby | [brakeman](http://brakemanscanner.org/) !!, [rails_best_practices](https://github.com/flyerhzm/rails_best_practices) !!, [reek](https://github.com/troessner/reek), [rubocop](https://github.com/bbatsov/rubocop), [ruby](https://www.ruby-lang.org) |
| Rust | cargo !! (see `:help ale-integration-rust` for configuration instructions), [rls](https://github.com/rust-lang-nursery/rls), [rustc](https://www.rust-lang.org/) |
| 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 | [sass-lint](https://www.npmjs.com/package/sass-lint), [scss-lint](https://github.com/brigade/scss-lint), [stylelint](https://github.com/stylelint/stylelint), [prettier](https://github.com/prettier/prettier) |
| Scala | [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) |

View File

@ -107,6 +107,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['typescript'],
\ 'description': 'Fix typescript files with tslint --fix.',
\ },
\ 'rustfmt': {
\ 'function': 'ale#fixers#rustfmt#Fix',
\ 'suggested_filetypes': ['rust'],
\ 'description': 'Fix Rust files with Rustfmt.',
\ },
\}
" Reset the function registry to the default entries.

View File

@ -0,0 +1,17 @@
" Author: Kelly Fox <kelly@bumfuddled.com>
" Description: Integration of rustfmt with ALE.
call ale#Set('rust_rustfmt_executable', 'rustfmt')
call ale#Set('rust_rustfmt_options', '')
function! ale#fixers#rustfmt#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'rust_rustfmt_executable')
let l:options = ale#Var(a:buffer, 'rust_rustfmt_options')
return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' %t',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -22,6 +22,8 @@ Integration Information
over cargo. rls implements the Language Server Protocol for incremental
compilation of Rust code, and can check Rust files while you type. `rls`
requires Rust files to contained in Cargo projects.
4. rustfmt -- If you have `rustfmt` installed, you can use it as a fixer to
consistently reformat your Rust code.
Only cargo is enabled by default. To switch to using rustc instead of cargo,
configure |g:ale_linters| appropriately: >
@ -70,5 +72,16 @@ g:ale_rust_ignore_error_codes *g:ale_rust_ignore_error_codes*
let g:ale_rust_ignore_error_codes = ['E0432', 'E0433']
===============================================================================
rustfmt *ale-rust-rustfmt*
g:ale_rust_rustfmt_options *g:ale_rust_rustfmt_options*
*b:ale_rust_rustfmt_options*
Type: |String|
Default: `''`
This variable can be set to pass additional options to the rustfmt fixer.
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -142,6 +142,7 @@ CONTENTS *ale-contents*
cargo...............................|ale-rust-cargo|
rls.................................|ale-rust-rls|
rustc...............................|ale-rust-rustc|
rustfmt.............................|ale-rust-rustfmt|
sass..................................|ale-sass-options|
stylelint...........................|ale-sass-stylelint|
scala.................................|ale-scala-options|
@ -280,7 +281,7 @@ Notes:
* reStructuredText: `proselint`
* RPM spec: `rpmlint`
* Ruby: `brakeman`, `rails_best_practices`!!, `reek`, `rubocop`, `ruby`
* Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|)
* Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|), `rustfmt`
* SASS: `sass-lint`, `stylelint`
* SCSS: `sass-lint`, `scss-lint`, `stylelint`, `prettier`
* Scala: `scalac`, `scalastyle`

View File

@ -0,0 +1,38 @@
Before:
Save g:ale_rust_rustfmt_executable
Save g:ale_rust_rustfmt_options
" Use an invalid global executable, so we don't match it.
let g:ale_rust_rustfmt_executable = 'xxxinvalid'
let g:ale_rust_rustfmt_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The rustfmt callback should return the correct default values):
call ale#test#SetFilename('../rust_files/testfile.rs')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('xxxinvalid')
\ . ' %t',
\ },
\ ale#fixers#rustfmt#Fix(bufnr(''))
Execute(The rustfmt callback should include custom rustfmt options):
let g:ale_rust_rustfmt_options = "--skip-children"
call ale#test#SetFilename('../rust_files/testfile.rs')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('xxxinvalid')
\ . ' ' . g:ale_rust_rustfmt_options
\ . ' %t',
\ },
\ ale#fixers#rustfmt#Fix(bufnr(''))

View File