From 35031a0b8a384766277a8b38f84c28ae89a17430 Mon Sep 17 00:00:00 2001 From: Kelly Fox Date: Sat, 21 Oct 2017 12:31:49 -0500 Subject: [PATCH] add rustfmt fixer --- README.md | 2 +- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/rustfmt.vim | 17 +++++++++ doc/ale-rust.txt | 13 +++++++ doc/ale.txt | 3 +- test/fixers/test_rustfmt_fixer_callback.vader | 38 +++++++++++++++++++ test/rust_files/testfile.rs | 0 7 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 autoload/ale/fixers/rustfmt.vim create mode 100644 test/fixers/test_rustfmt_fixer_callback.vader create mode 100644 test/rust_files/testfile.rs diff --git a/README.md b/README.md index 120177e..df5ba22 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index d26c71a..07daa40 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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. diff --git a/autoload/ale/fixers/rustfmt.vim b/autoload/ale/fixers/rustfmt.vim new file mode 100644 index 0000000..fb5ac61 --- /dev/null +++ b/autoload/ale/fixers/rustfmt.vim @@ -0,0 +1,17 @@ +" Author: Kelly Fox +" 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 diff --git a/doc/ale-rust.txt b/doc/ale-rust.txt index 52dc3d6..e20aea2 100644 --- a/doc/ale-rust.txt +++ b/doc/ale-rust.txt @@ -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: diff --git a/doc/ale.txt b/doc/ale.txt index fb0b5a7..acf6428 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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` diff --git a/test/fixers/test_rustfmt_fixer_callback.vader b/test/fixers/test_rustfmt_fixer_callback.vader new file mode 100644 index 0000000..36dd58a --- /dev/null +++ b/test/fixers/test_rustfmt_fixer_callback.vader @@ -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('')) diff --git a/test/rust_files/testfile.rs b/test/rust_files/testfile.rs new file mode 100644 index 0000000..e69de29