Skip to content

Commit 774e068

Browse files
committed
Add Black support for Python formatting
1 parent 6d69f93 commit 774e068

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
2323
* JavaScript (clang-format or [prettier](https://prettier.io))
2424
* JSON (js-beautify)
2525
* Proto (clang-format)
26-
* Python (Autopep8 or YAPF)
26+
* Python (Autopep8, YAPF, or Black)
2727
* Rust ([rustfmt](https://github.com/rust-lang/rustfmt))
2828
* TypeScript (clang-format)
2929
* Shell (shfmt)

autoload/codefmt.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
" * fish: fish_indent
3434
" * gn: gn
3535
" * go: gofmt
36-
" * python: autopep8, yapf
36+
" * python: autopep8, yapf, black
3737

3838

3939
let s:plugin = maktaba#plugin#Get('codefmt')

autoload/codefmt/black.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: black
22+
function! codefmt#black#GetFormatter() abort
23+
let l:formatter = {
24+
\ 'name': 'black',
25+
\ 'setup_instructions': 'Install black ' .
26+
\ '(https://pypi.python.org/pypi/black/).'}
27+
28+
function l:formatter.IsAvailable() abort
29+
return executable(s:plugin.Flag('black_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 black or the binary named in
38+
" @flag(black_executable)
39+
"
40+
" We implement Format(), and not FormatRange{,s}(), because black doesn't
41+
" provide a hook for formatting a range
42+
function l:formatter.Format() abort
43+
let l:executable = s:plugin.Flag('black_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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Default: 'js-beautify' `
5757
The path to the yapf executable.
5858
Default: 'yapf' `
5959

60+
*codefmt:black_executable*
61+
The path to the black executable.
62+
Default: 'black' `
63+
6064
*codefmt:gn_executable*
6165
The path to the gn executable.
6266
Default: 'gn' `
@@ -162,7 +166,7 @@ The current list of defaults by filetype is:
162166
* fish: fish_indent
163167
* gn: gn
164168
* go: gofmt
165-
* python: autopep8, yapf
169+
* python: autopep8, yapf, black
166170

167171
==============================================================================
168172
DICTIONARIES *codefmt-dicts*

instant/flags.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ call s:plugin.Flag('js_beautify_executable', 'js-beautify')
8383
" The path to the yapf executable.
8484
call s:plugin.Flag('yapf_executable', 'yapf')
8585

86+
""
87+
" The path to the black executable.
88+
call s:plugin.Flag('black_executable', 'black')
89+
8690
""
8791
" The path to the gn executable.
8892
call s:plugin.Flag('gn_executable', 'gn')

plugin/register.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
3030
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
3131
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())
3232
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
33+
call s:registry.AddExtension(codefmt#black#GetFormatter())
3334
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
3435
call s:registry.AddExtension(codefmt#gn#GetFormatter())
3536
call s:registry.AddExtension(codefmt#buildifier#GetFormatter())

0 commit comments

Comments
 (0)