mirror of
https://github.com/dense-analysis/ale.git
synced 2025-12-06 20:54:26 +08:00
add unimport fixer (#5068)
This commit is contained in:
@@ -192,6 +192,11 @@ let s:default_registry = {
|
|||||||
\ 'suggested_filetypes': ['python'],
|
\ 'suggested_filetypes': ['python'],
|
||||||
\ 'description': 'Tidy Python imports with pyflyby.',
|
\ 'description': 'Tidy Python imports with pyflyby.',
|
||||||
\ },
|
\ },
|
||||||
|
\ 'unimport': {
|
||||||
|
\ 'function': 'ale#fixers#unimport#Fix',
|
||||||
|
\ 'suggested_filetypes': ['python'],
|
||||||
|
\ 'description': 'unimport fixer',
|
||||||
|
\ },
|
||||||
\ 'importjs': {
|
\ 'importjs': {
|
||||||
\ 'function': 'ale#fixers#importjs#Fix',
|
\ 'function': 'ale#fixers#importjs#Fix',
|
||||||
\ 'suggested_filetypes': ['javascript'],
|
\ 'suggested_filetypes': ['javascript'],
|
||||||
|
|||||||
42
autoload/ale/fixers/unimport.vim
Normal file
42
autoload/ale/fixers/unimport.vim
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
call ale#Set('python_unimport_executable', 'unimport')
|
||||||
|
call ale#Set('python_unimport_use_global', get(g:, 'ale_use_global_executables', 0))
|
||||||
|
call ale#Set('python_unimport_options', '')
|
||||||
|
call ale#Set('python_unimport_auto_pipenv', 0)
|
||||||
|
call ale#Set('python_unimport_auto_poetry', 0)
|
||||||
|
call ale#Set('python_unimport_auto_uv', 0)
|
||||||
|
|
||||||
|
function! ale#fixers#unimport#GetExecutable(buffer) abort
|
||||||
|
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_unimport_auto_pipenv'))
|
||||||
|
\ && ale#python#PipenvPresent(a:buffer)
|
||||||
|
return 'pipenv'
|
||||||
|
endif
|
||||||
|
|
||||||
|
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_unimport_auto_poetry'))
|
||||||
|
\ && ale#python#PoetryPresent(a:buffer)
|
||||||
|
return 'poetry'
|
||||||
|
endif
|
||||||
|
|
||||||
|
if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_unimport_auto_uv'))
|
||||||
|
\ && ale#python#UvPresent(a:buffer)
|
||||||
|
return 'uv'
|
||||||
|
endif
|
||||||
|
|
||||||
|
return ale#python#FindExecutable(a:buffer, 'python_unimport', ['unimport'])
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! ale#fixers#unimport#Fix(buffer) abort
|
||||||
|
let l:executable = ale#fixers#unimport#GetExecutable(a:buffer)
|
||||||
|
let l:cmd = [ale#Escape(l:executable)]
|
||||||
|
|
||||||
|
if l:executable =~? '\(pipenv\|poetry\|uv\)$'
|
||||||
|
call extend(l:cmd, ['run', 'unimport'])
|
||||||
|
endif
|
||||||
|
|
||||||
|
let l:options = ale#Var(a:buffer, 'python_unimport_options')
|
||||||
|
|
||||||
|
if !empty(l:options)
|
||||||
|
call add(l:cmd, l:options)
|
||||||
|
endif
|
||||||
|
|
||||||
|
return {'command': join(l:cmd, ' ')}
|
||||||
|
endfunction
|
||||||
47
test/fixers/test_unimport_fixer_callback.vader
Normal file
47
test/fixers/test_unimport_fixer_callback.vader
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
Before:
|
||||||
|
call ale#assert#SetUpFixerTest('python', 'unimport')
|
||||||
|
|
||||||
|
let b:bin_dir = has('win32') ? 'Scripts' : 'bin'
|
||||||
|
|
||||||
|
After:
|
||||||
|
call ale#assert#TearDownFixerTest()
|
||||||
|
|
||||||
|
unlet! b:bin_dir
|
||||||
|
|
||||||
|
Execute(The unimport callback should return the correct default values):
|
||||||
|
call ale#test#SetFilename('../test-files/python/with_virtualenv/subdir/foo/bar.py')
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape(ale#path#Simplify(g:dir . '/../test-files/python/with_virtualenv/env/' . b:bin_dir . '/unimport')),
|
||||||
|
\ }
|
||||||
|
|
||||||
|
Execute(Pipenv is detected when python_unimport_auto_pipenv is set):
|
||||||
|
let g:ale_python_unimport_auto_pipenv = 1
|
||||||
|
|
||||||
|
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('pipenv') . ' run unimport'
|
||||||
|
\ }
|
||||||
|
|
||||||
|
Execute(Poetry is detected when python_unimport_auto_poetry is set):
|
||||||
|
let g:ale_python_unimport_auto_poetry = 1
|
||||||
|
|
||||||
|
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('poetry') . ' run unimport'
|
||||||
|
\ }
|
||||||
|
|
||||||
|
Execute(uv is detected when python_unimport_auto_uv is set):
|
||||||
|
let g:ale_python_unimport_auto_uv = 1
|
||||||
|
|
||||||
|
call ale#test#SetFilename('../test-files/python/uv/whatever.py')
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('uv') . ' run unimport'
|
||||||
|
\ }
|
||||||
Reference in New Issue
Block a user