Skip to content

Commit 605dc00

Browse files
authored
Add isort for python import sorting (#186)
1 parent f3a5dc7 commit 605dc00

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

autoload/codefmt/isort.vim

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
" Copyright 2020 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
""
20+
" @private
21+
" Formatter: isort
22+
function! codefmt#isort#GetFormatter() abort
23+
let l:formatter = {
24+
\ 'name': 'isort',
25+
\ 'setup_instructions': 'Install isort ' .
26+
\ '(https://pypi.python.org/pypi/isort/).'}
27+
28+
function l:formatter.IsAvailable() abort
29+
return executable(s:plugin.Flag('isort_executable'))
30+
endfunction
31+
32+
function l:formatter.AppliesToBuffer() abort
33+
return &filetype is# 'python'
34+
endfunction
35+
36+
""
37+
" Reformat the current buffer with isort or the binary named in
38+
" @flag(isort_executable)
39+
"
40+
" We implement Format(), and not FormatRange{,s}(), because isort doesn't
41+
" provide a hook for formatting a range
42+
function l:formatter.Format() abort
43+
let l:executable = s:plugin.Flag('isort_executable')
44+
45+
call codefmt#formatterhelpers#Format([
46+
\ l:executable,
47+
\ '-'])
48+
endfunction
49+
50+
return l:formatter
51+
endfunction

doc/codefmt.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ Default: 'yapf' `
9393
The path to the black executable.
9494
Default: 'black' `
9595

96+
*codefmt:isort_executable*
97+
The path to the isort executable.
98+
Default: 'isort' `
99+
96100
*codefmt:gn_executable*
97101
The path to the gn executable.
98102
Default: 'gn' `

instant/flags.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ call s:plugin.Flag('yapf_executable', 'yapf')
8888
" The path to the black executable.
8989
call s:plugin.Flag('black_executable', 'black')
9090

91+
""
92+
" The path to the isort executable.
93+
call s:plugin.Flag('isort_executable', 'isort')
94+
9195
""
9296
" The path to the gn executable.
9397
call s:plugin.Flag('gn_executable', 'gn')

plugin/register.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
7070
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
7171
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
7272
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
73+
call s:registry.AddExtension(codefmt#isort#GetFormatter())
7374
call s:registry.AddExtension(codefmt#black#GetFormatter())
7475
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
7576
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())

0 commit comments

Comments
 (0)