Skip to content

Commit

Permalink
Add support for syntax_tree fixer (dense-analysis#4268)
Browse files Browse the repository at this point in the history
This is the library that now powers prettier/plugin-ruby but is also
stands on its own: https://github.com/ruby-syntax-tree/syntax_tree
  • Loading branch information
molawson authored Aug 9, 2022
1 parent 5063804 commit 233b681
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions autoload/ale/fix/registry.vim
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['swift'],
\ 'description': 'Apply SwiftFormat to a file.',
\ },
\ 'syntax_tree': {
\ 'function': 'ale#fixers#syntax_tree#Fix',
\ 'suggested_filetypes': ['ruby'],
\ 'description': 'Fix ruby files with stree write',
\ },
\ 'apple-swift-format': {
\ 'function': 'ale#fixers#appleswiftformat#Fix',
\ 'suggested_filetypes': ['swift'],
Expand Down
19 changes: 19 additions & 0 deletions autoload/ale/fixers/syntax_tree.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
call ale#Set('ruby_syntax_tree_options', '')
call ale#Set('ruby_syntax_tree_executable', 'stree')

function! ale#fixers#syntax_tree#GetCommand(buffer) abort
let l:executable = ale#Var(a:buffer, 'ruby_syntax_tree_executable')
let l:options = ale#Var(a:buffer, 'ruby_syntax_tree_options')

return ale#ruby#EscapeExecutable(l:executable, 'stree')
\ . ' write'
\ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' %t'
endfunction

function! ale#fixers#syntax_tree#Fix(buffer) abort
return {
\ 'command': ale#fixers#syntax_tree#GetCommand(a:buffer),
\ 'read_temporary_file': 1,
\}
endfunction
20 changes: 20 additions & 0 deletions doc/ale-ruby.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,25 @@ g:ale_ruby_standardrb_options *g:ale_ruby_standardrb_options*
This variable can be changed to modify flags given to standardrb.


===============================================================================
syntax_tree *ale-ruby-syntax_tree*

g:ale_ruby_syntax_tree_executable *g:ale_ruby_syntax_tree_executable*
*b:ale_ruby_syntax_tree_executable*
Type: String
Default: `'stree'`

Override the invoked SyntaxTree binary. Set this to `'bundle'` to invoke
`'bundle` `exec` stree'.


g:ale_ruby_syntax_tree_options *g:ale_ruby_syntax_tree_options*
*b:ale_ruby_syntax_tree_options*
Type: |String|
Default: `''`

This variable can be changed to modify flags given to SyntaxTree.


===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ Notes:
* `solargraph`
* `sorbet`
* `standardrb`
* `syntax_tree`
* Rust
* `cargo`!!
* `cspell`
Expand Down
1 change: 1 addition & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,7 @@ documented in additional help files.
solargraph............................|ale-ruby-solargraph|
sorbet................................|ale-ruby-sorbet|
standardrb............................|ale-ruby-standardrb|
syntax_tree...........................|ale-ruby-syntax_tree|
rust....................................|ale-rust-options|
analyzer..............................|ale-rust-analyzer|
cargo.................................|ale-rust-cargo|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ formatting.
* [solargraph](https://solargraph.org)
* [sorbet](https://github.com/sorbet/sorbet)
* [standardrb](https://github.com/testdouble/standard)
* [syntax_tree](https://github.com/ruby-syntax-tree/syntax_tree)
* Rust
* [cargo](https://github.com/rust-lang/cargo) :floppy_disk: (see `:help ale-integration-rust` for configuration instructions)
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell)
Expand Down
37 changes: 37 additions & 0 deletions test/fixers/test_syntax_tree_fixer_callback.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Before:
Save g:ale_ruby_syntax_tree_executable
Save g:ale_ruby_syntax_tree_options

" Use an invalid global executable, so we don't match it.
let g:ale_ruby_syntax_tree_executable = 'xxxinvalid'
let g:ale_ruby_syntax_tree_options = ''

call ale#test#SetDirectory('/testplugin/test/fixers')

After:
Restore

call ale#test#RestoreDirectory()

Execute(The syntax_tree callback should return the correct default values):
call ale#test#SetFilename('../test-files/ruby/dummy.rb')

AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_ruby_syntax_tree_executable)
\ . ' write %t',
\ },
\ ale#fixers#syntax_tree#Fix(bufnr(''))

Execute(The syntax_tree callback should include custom options):
let g:ale_ruby_syntax_tree_options = '--print-width=100 --plugins=plugin/trailing_comma'
call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb')

AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_ruby_syntax_tree_executable)
\ . ' write --print-width=100 --plugins=plugin/trailing_comma %t',
\ },
\ ale#fixers#syntax_tree#Fix(bufnr(''))

0 comments on commit 233b681

Please sign in to comment.