]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/isort.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / fixers / isort.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Fixing Python imports with isort.
3
4 call ale#Set('python_isort_executable', 'isort')
5 call ale#Set('python_isort_use_global', get(g:, 'ale_use_global_executables', 0))
6 call ale#Set('python_isort_options', '')
7 call ale#Set('python_isort_auto_pipenv', 0)
8 call ale#Set('python_isort_auto_poetry', 0)
9 call ale#Set('python_isort_auto_uv', 0)
10
11 function! ale#fixers#isort#GetExecutable(buffer) abort
12     if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_isort_auto_pipenv'))
13     \ && ale#python#PipenvPresent(a:buffer)
14         return 'pipenv'
15     endif
16
17     if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_isort_auto_poetry'))
18     \ && ale#python#PoetryPresent(a:buffer)
19         return 'poetry'
20     endif
21
22     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_isort_auto_uv'))
23     \ && ale#python#UvPresent(a:buffer)
24         return 'uv'
25     endif
26
27     return ale#python#FindExecutable(a:buffer, 'python_isort', ['isort'])
28 endfunction
29
30 function! ale#fixers#isort#GetCmd(buffer) abort
31     let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
32     let l:cmd = [ale#Escape(l:executable)]
33
34     if l:executable =~? '\(pipenv\|poetry\|uv\)$'
35         call extend(l:cmd, ['run', 'isort'])
36     endif
37
38     return join(l:cmd, ' ')
39 endfunction
40
41 function! ale#fixers#isort#FixForVersion(buffer, version) abort
42     let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
43     let l:cmd = [ale#Escape(l:executable)]
44
45     if l:executable =~? '\(pipenv\|poetry\|uv\)$'
46         call extend(l:cmd, ['run', 'isort'])
47     endif
48
49     if ale#semver#GTE(a:version, [5, 7, 0])
50         call add(l:cmd, '--filename %s')
51     endif
52
53     let l:options = ale#Var(a:buffer, 'python_isort_options')
54
55     if !empty(l:options)
56         call add(l:cmd, l:options)
57     endif
58
59     call add(l:cmd, '-')
60
61     return {
62     \   'cwd': '%s:h',
63     \   'command': join(l:cmd, ' '),
64     \}
65 endfunction
66
67 function! ale#fixers#isort#Fix(buffer) abort
68     let l:executable = ale#fixers#isort#GetExecutable(a:buffer)
69     let l:command = ale#fixers#isort#GetCmd(a:buffer) . ale#Pad('--version')
70
71     return ale#semver#RunWithVersionCheck(
72     \     a:buffer,
73     \     l:executable,
74     \     l:command,
75     \     function('ale#fixers#isort#FixForVersion'),
76     \)
77 endfunction