Skip to content

Commit d667662

Browse files
authored
Merge pull request #142 from brentyi/add_black
Add support for Black (Python)
2 parents 6d69f93 + 976f4e4 commit d667662

File tree

7 files changed

+134
-3
lines changed

7 files changed

+134
-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, Black, or YAPF)
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, black, yapf
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
@@ -53,6 +53,10 @@ Default: 'dartfmt' `
5353
The path to the js-beautify executable.
5454
Default: 'js-beautify' `
5555

56+
*codefmt:black_executable*
57+
The path to the black executable.
58+
Default: 'black' `
59+
5660
*codefmt:yapf_executable*
5761
The path to the yapf executable.
5862
Default: 'yapf' `
@@ -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, black, yapf
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
@@ -29,6 +29,7 @@ call s:registry.AddExtension(codefmt#jsbeautify#GetFormatter())
2929
call s:registry.AddExtension(codefmt#clangformat#GetFormatter())
3030
call s:registry.AddExtension(codefmt#gofmt#GetFormatter())
3131
call s:registry.AddExtension(codefmt#dartfmt#GetFormatter())
32+
call s:registry.AddExtension(codefmt#black#GetFormatter())
3233
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
3334
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
3435
call s:registry.AddExtension(codefmt#gn#GetFormatter())

vroom/black.vroom

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
The built-in black formatter knows how to format python code.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
The black formatter expects the black executable to be installed on your
19+
system.
20+
21+
% f()
22+
:FormatCode black
23+
! black .*
24+
$ f()
25+
26+
The name or path of the black executable can be configured via the
27+
black_executable flag if the default of "black" doesn't work.
28+
29+
:Glaive codefmt black_executable='blackpy3'
30+
:FormatCode black
31+
! blackpy3 .*
32+
$ f()
33+
:Glaive codefmt black_executable='black'
34+
35+
36+
You can format any buffer with black specifying the formatter explicitly.
37+
Here's an example:
38+
39+
@clear
40+
% if True: pass
41+
42+
:FormatCode black
43+
! black .*
44+
$ if True:
45+
$ pass
46+
if True:
47+
pass
48+
@end
49+
50+
Here's a second example:
51+
52+
@clear
53+
% some_tuple=( 1,2, 3,'a' );<CR>
54+
|if bar : bar+=1; bar=bar* bar<CR>
55+
|else: bar-=1;
56+
57+
:FormatCode black
58+
! black .*
59+
$ some_tuple = (1, 2, 3, "a");
60+
$ if bar:
61+
$ bar += 1
62+
$ bar = bar * bar
63+
$ else:
64+
$ bar -= 1
65+
some_tuple = (1, 2, 3, "a");
66+
if bar:
67+
bar += 1
68+
bar = bar * bar
69+
else:
70+
bar -= 1
71+
@end

0 commit comments

Comments
 (0)