Skip to content

Commit 938b2e1

Browse files
maxjacobsonCarl Willman
authored andcommitted
Add --editor-mode flag when invoking rubocop (dense-analysis#5049)
* 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. * Make this a non-breaking change This will detect the current rubocop version and auto-enable --editor-mode for newer version of rubocop without affecting users of older versions of rubocop.
1 parent 3ef8af6 commit 938b2e1

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

autoload/ale/fixers/rubocop.vim

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,34 @@ function! ale#fixers#rubocop#PostProcess(buffer, output) abort
1919
return a:output[l:line :]
2020
endfunction
2121

22-
function! ale#fixers#rubocop#GetCommand(buffer) abort
22+
function! ale#fixers#rubocop#GetCommand(buffer, version) abort
2323
let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable')
2424
let l:options = ale#Var(a:buffer, 'ruby_rubocop_options')
2525
let l:auto_correct_all = ale#Var(a:buffer, 'ruby_rubocop_auto_correct_all')
26+
let l:editor_mode = ale#semver#GTE(a:version, [1, 61, 0])
2627

2728
return ale#ruby#EscapeExecutable(l:executable, 'rubocop')
2829
\ . (!empty(l:options) ? ' ' . l:options : '')
2930
\ . (l:auto_correct_all ? ' --auto-correct-all' : ' --auto-correct')
31+
\ . (l:editor_mode ? ' --editor-mode' : '')
3032
\ . ' --force-exclusion --stdin %s'
3133
endfunction
3234

33-
function! ale#fixers#rubocop#Fix(buffer) abort
35+
function! ale#fixers#rubocop#GetCommandForVersion(buffer, version) abort
3436
return {
35-
\ 'command': ale#fixers#rubocop#GetCommand(a:buffer),
36-
\ 'process_with': 'ale#fixers#rubocop#PostProcess'
37+
\ 'command': ale#fixers#rubocop#GetCommand(a:buffer, a:version),
38+
\ 'process_with': 'ale#fixers#rubocop#PostProcess'
3739
\}
3840
endfunction
41+
42+
function! ale#fixers#rubocop#Fix(buffer) abort
43+
let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable')
44+
let l:command = l:executable . ale#Pad('--version')
45+
46+
return ale#semver#RunWithVersionCheck(
47+
\ a:buffer,
48+
\ l:executable,
49+
\ l:command,
50+
\ function('ale#fixers#rubocop#GetCommandForVersion'),
51+
\)
52+
endfunction

test/fixers/test_rubocop_fixer_callback.vader

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,47 @@
11
Before:
2-
Save g:ale_ruby_rubocop_executable
3-
Save g:ale_ruby_rubocop_options
4-
5-
" Use an invalid global executable, so we don't match it.
6-
let g:ale_ruby_rubocop_executable = 'xxxinvalid'
7-
let g:ale_ruby_rubocop_options = ''
8-
9-
call ale#test#SetDirectory('/testplugin/test/fixers')
2+
call ale#assert#SetUpFixerTest('ruby', 'rubocop')
103

114
After:
12-
Restore
13-
14-
call ale#test#RestoreDirectory()
5+
call ale#assert#TearDownFixerTest()
156

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

19-
AssertEqual
10+
GivenCommandOutput ['1.61.0']
11+
12+
AssertFixer
2013
\ {
2114
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
2215
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
23-
\ . ' --auto-correct --force-exclusion --stdin %s',
24-
\ },
25-
\ ale#fixers#rubocop#Fix(bufnr(''))
16+
\ . ' --auto-correct --editor-mode --force-exclusion --stdin %s',
17+
\ }
2618

2719
Execute(The rubocop callback should include custom rubocop options):
2820
let g:ale_ruby_rubocop_options = '--except Lint/Debugger'
2921
call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb')
3022

31-
AssertEqual
23+
GivenCommandOutput ['1.61.0']
24+
25+
AssertFixer
3226
\ {
3327
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
3428
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
3529
\ . ' --except Lint/Debugger'
36-
\ . ' --auto-correct --force-exclusion --stdin %s',
37-
\ },
38-
\ ale#fixers#rubocop#Fix(bufnr(''))
30+
\ . ' --auto-correct --editor-mode --force-exclusion --stdin %s',
31+
\ }
3932

4033
Execute(The rubocop callback should use auto-correct-all option when set):
4134
let g:ale_ruby_rubocop_auto_correct_all = 1
4235
call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb')
4336

44-
AssertEqual
37+
GivenCommandOutput ['1.61.0']
38+
39+
AssertFixer
4540
\ {
4641
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
4742
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
48-
\ . ' --auto-correct-all --force-exclusion --stdin %s'
49-
\ },
50-
\ ale#fixers#rubocop#Fix(bufnr(''))
43+
\ . ' --auto-correct-all --editor-mode --force-exclusion --stdin %s'
44+
\ }
5145

5246
Execute(The rubocop post-processor should remove diagnostics content):
5347
AssertEqual
@@ -87,3 +81,16 @@ Execute(The rubocop post-processor should remove diagnostics content):
8781
\ ' ''forrest'',',
8882
\ ' ''run'']',
8983
\ ])
84+
85+
Execute(The rubocop callback should not use editor-mode option with older versions):
86+
call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb')
87+
88+
GivenCommandOutput ['1.59.0']
89+
90+
AssertFixer
91+
\ {
92+
\ 'process_with': 'ale#fixers#rubocop#PostProcess',
93+
\ 'command': ale#Escape(g:ale_ruby_rubocop_executable)
94+
\ . ' --auto-correct --force-exclusion --stdin %s'
95+
\ }
96+

0 commit comments

Comments
 (0)