-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3409 from lyz-code/feat/add-autoimport-support
feat: add autoimport fixer
- Loading branch information
Showing
9 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
" Author: lyz-code | ||
" Description: Fixing Python imports with autoimport. | ||
|
||
call ale#Set('python_autoimport_executable', 'autoimport') | ||
call ale#Set('python_autoimport_options', '') | ||
call ale#Set('python_autoimport_use_global', get(g:, 'ale_use_global_executables', 0)) | ||
|
||
function! ale#fixers#autoimport#Fix(buffer) abort | ||
let l:options = ale#Var(a:buffer, 'python_autoimport_options') | ||
|
||
let l:executable = ale#python#FindExecutable( | ||
\ a:buffer, | ||
\ 'python_autoimport', | ||
\ ['autoimport'], | ||
\) | ||
|
||
if !executable(l:executable) | ||
return 0 | ||
endif | ||
|
||
return { | ||
\ 'command': ale#path#BufferCdString(a:buffer) | ||
\ . ale#Escape(l:executable) . (!empty(l:options) ? ' ' . l:options : '') . ' -', | ||
\} | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Before: | ||
Save g:ale_python_autoimport_executable | ||
Save g:ale_python_autoimport_options | ||
|
||
" Use an invalid global executable, so we don't match it. | ||
let g:ale_python_autoimport_executable = 'xxxinvalid' | ||
let g:ale_python_autoimport_options = '' | ||
|
||
call ale#test#SetDirectory('/testplugin/test/fixers') | ||
silent cd .. | ||
silent cd command_callback | ||
let g:dir = getcwd() | ||
|
||
let b:bin_dir = has('win32') ? 'Scripts' : 'bin' | ||
|
||
After: | ||
Restore | ||
|
||
unlet! b:bin_dir | ||
|
||
call ale#test#RestoreDirectory() | ||
|
||
Execute(The autoimport callback should return the correct default values): | ||
AssertEqual | ||
\ 0, | ||
\ ale#fixers#autoimport#Fix(bufnr('')) | ||
|
||
silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py') | ||
AssertEqual | ||
\ { | ||
\ 'command': ale#path#BufferCdString(bufnr('')) | ||
\ . ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/' . b:bin_dir . '/autoimport')) . ' -', | ||
\ }, | ||
\ ale#fixers#autoimport#Fix(bufnr('')) | ||
|
||
Execute(The autoimport callback should respect custom options): | ||
let g:ale_python_autoimport_options = '--multi-line=3 --trailing-comma' | ||
|
||
AssertEqual | ||
\ 0, | ||
\ ale#fixers#autoimport#Fix(bufnr('')) | ||
|
||
silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py') | ||
AssertEqual | ||
\ { | ||
\ 'command': ale#path#BufferCdString(bufnr('')) | ||
\ . ale#Escape(ale#path#Simplify(g:dir . '/python_paths/with_virtualenv/env/' . b:bin_dir . '/autoimport')) | ||
\ . ' --multi-line=3 --trailing-comma -', | ||
\ }, | ||
\ ale#fixers#autoimport#Fix(bufnr('')) |