Skip to content

Commit 3b189b3

Browse files
committed
Add --editor-mode flag when invoking rubocop
Since RuboCop 1.61.0 (released in February 2024), RuboCop accepts an `--editor-mode` flag which improves editor integrations like ale. Some of RuboCop's auto-corrections can be surprising or annoying to run on save. When RuboCop is running via an LSP or when the `--editor-mode` flag is passed, it will understand that it is running in an editor, and it will hold off on making changes that might be surprising or annoying. For example, if I write ```ruby def call results = some_process end ``` This has an unused variable, and RuboCop will remove the unused variable when you run it. However, if you're in the middle of editing, you may not want it to remove that unused variable, because you may be about to add a usage of it. More context: - PR which introduced it: rubocop/rubocop#12682 - Release notes for 1.61: https://github.com/rubocop/rubocop/releases/tag/v1.61.0 - Docs: https://docs.rubocop.org/rubocop/1.80/configuration.html#contextual This will be a breaking change for anyone who is running an old version of RuboCop, because the flag will not exist for them. If they would like to opt out of this change, they can set an option to omit the flag. I think this ought to be enabled by default so that people will get this benefit out of the box. In the meantime, I am opting into this behavior by setting this option: ```vim let g:ale_ruby_rubocop_options = "--editor-mode" ``` So I appreciate that this seam was already introduced.
1 parent 4217461 commit 3b189b3

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

autoload/ale/fixers/rubocop.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
call ale#Set('ruby_rubocop_options', '')
22
call ale#Set('ruby_rubocop_auto_correct_all', 0)
33
call ale#Set('ruby_rubocop_executable', 'rubocop')
4+
call ale#Set('ruby_rubocop_editor_mode', '1')
45

56
" Rubocop fixer outputs diagnostics first and then the fixed
67
" output. These are delimited by a "=======" string that we
@@ -23,10 +24,12 @@ function! ale#fixers#rubocop#GetCommand(buffer) abort
2324
let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable')
2425
let l:options = ale#Var(a:buffer, 'ruby_rubocop_options')
2526
let l:auto_correct_all = ale#Var(a:buffer, 'ruby_rubocop_auto_correct_all')
27+
let l:editor_mode= ale#Var(a:buffer, 'ruby_rubocop_editor_mode')
2628

2729
return ale#ruby#EscapeExecutable(l:executable, 'rubocop')
2830
\ . (!empty(l:options) ? ' ' . l:options : '')
2931
\ . (l:auto_correct_all ? ' --auto-correct-all' : ' --auto-correct')
32+
\ . (l:editor_mode ? ' --editor-mode' : '')
3033
\ . ' --force-exclusion --stdin %s'
3134
endfunction
3235

doc/ale-ruby.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ g:ale_ruby_rubocop_auto_correct_all
187187
This variable can be changed to make rubocop to correct all offenses (unsafe).
188188

189189

190+
*ale-options.ruby_rubocop_editor_mode*
191+
*g:ale_ruby_rubocop_editor_mode*
192+
*b:ale_ruby_rubocop_editor_mode*
193+
ruby_rubocop_editor_mode
194+
g:ale_ruby_rubocop_editor_mode
195+
Type: |Number|
196+
Default: `1`
197+
198+
This variable can be changed to 0 to run rubocop without the `--editor-mode`
199+
flag (if using RuboCop < v1.61.0).
200+
201+
190202
===============================================================================
191203
ruby *ale-ruby-ruby*
192204

test/fixers/test_rubocop_fixer_callback.vader

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Execute(The rubocop callback should return the correct default values):
2020
\ {
2121
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
2222
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
23-
\ . ' --auto-correct --force-exclusion --stdin %s',
23+
\ . ' --auto-correct --editor-mode --force-exclusion --stdin %s',
2424
\ },
2525
\ ale#fixers#rubocop#Fix(bufnr(''))
2626

@@ -33,7 +33,7 @@ Execute(The rubocop callback should include custom rubocop options):
3333
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
3434
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
3535
\ . ' --except Lint/Debugger'
36-
\ . ' --auto-correct --force-exclusion --stdin %s',
36+
\ . ' --auto-correct --editor-mode --force-exclusion --stdin %s',
3737
\ },
3838
\ ale#fixers#rubocop#Fix(bufnr(''))
3939

@@ -45,7 +45,7 @@ Execute(The rubocop callback should use auto-correct-all option when set):
4545
\ {
4646
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
4747
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
48-
\ . ' --auto-correct-all --force-exclusion --stdin %s'
48+
\ . ' --auto-correct-all --editor-mode --force-exclusion --stdin %s'
4949
\ },
5050
\ ale#fixers#rubocop#Fix(bufnr(''))
5151

@@ -87,3 +87,15 @@ Execute(The rubocop post-processor should remove diagnostics content):
8787
\ ' ''forrest'',',
8888
\ ' ''run'']',
8989
\ ])
90+
91+
Execute(The rubocop callback should not use editor-mode option when configured not to):
92+
let g:ale_ruby_rubocop_editor_mode = 0
93+
call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb')
94+
95+
AssertEqual
96+
\ {
97+
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
98+
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
99+
\ . ' --auto-correct-all --force-exclusion --stdin %s'
100+
\ },
101+
\ ale#fixers#rubocop#Fix(bufnr(''))

0 commit comments

Comments
 (0)