Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65fc13a

Browse files
committed
Merge pull request #4 from actionless/add-autopep8
Add support for python via autopep8.
2 parents a69df96 + 35e4860 commit 65fc13a

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

‎autoload/codefmt.vim‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
" The current list of defaults by filetype is:
2828
" * cpp, proto, javascript: clang-format
2929
" * go: gofmt
30+
" * python: autopep8
3031

3132

3233
call maktaba#library#Require('codefmtlib')
@@ -156,6 +157,69 @@ if !exists('s:gofmt')
156157
call codefmtlib#AddDefaultFormatter(s:gofmt)
157158
endif
158159

160+
" Formatter: autopep8
161+
if !exists('s:autopep8')
162+
let s:autopep8 = {
163+
\ 'name': 'autopep8',
164+
\ 'setup_instructions': 'Install autopep8 ' .
165+
\ '(https://pypi.python.org/pypi/autopep8/).'}
166+
167+
function s:autopep8.IsAvailable() abort
168+
return executable(s:plugin.Flag('autopep8_executable'))
169+
endfunction
170+
171+
function s:autopep8.AppliesToBuffer() abort
172+
return &filetype is# 'python'
173+
endfunction
174+
175+
""
176+
" Reformat the current buffer with autopep8 or the binary named in
177+
" @flag(autopep8_executable), only targeting the range between {startline} and
178+
" {endline}.
179+
" @throws ShellError
180+
function s:autopep8.FormatRange(startline, endline) abort
181+
let l:executable = s:plugin.Flag('autopep8_executable')
182+
if !exists('s:autopep8_supports_range')
183+
let l:version_call =
184+
\ maktaba#syscall#Create([l:executable, '--version']).Call()
185+
" In some cases version is written to stderr, in some to stdout
186+
let l:version_output =
187+
\ version_call.stderr ? version_call.stderr : version_call.stdout
188+
let s:autopep8_supports_range =
189+
\ matchlist(l:version_output, '\m\Cautopep8 \(\d\+\)\.')[1] >= 1
190+
endif
191+
192+
call maktaba#ensure#IsNumber(a:startline)
193+
call maktaba#ensure#IsNumber(a:endline)
194+
let l:lines = getline(1, line('$'))
195+
196+
if s:autopep8_supports_range
197+
let l:cmd = [ l:executable,
198+
\ '--range', ''.a:startline, ''.a:endline,
199+
\ '-' ]
200+
let l:input = join(l:lines, "\n")
201+
else
202+
let l:cmd = [ l:executable, '-' ]
203+
" Hack range formatting by formatting range individually, ignoring context.
204+
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
205+
endif
206+
207+
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call()
208+
let l:formatted = split(l:result.stdout, "\n")
209+
210+
if s:autopep8_supports_range
211+
let l:full_formatted = l:formatted
212+
else
213+
" Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right.
214+
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : []
215+
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
216+
endif
217+
218+
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
219+
endfunction
220+
221+
call codefmtlib#AddDefaultFormatter(s:autopep8)
222+
endif
159223

160224
""
161225
" Checks whether {formatter} is available.

‎doc/codefmt.txt‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
*codefmt.txt* Syntax-aware code formatting
2-
*codefmt*
1+
*codefmt.txt* Syntax-aware code formatting for a variety of languages
2+
Google *codefmt*
33

44
==============================================================================
55
CONTENTS *codefmt-contents*
@@ -23,6 +23,10 @@ This plugin uses maktaba flags for configuration. Install Glaive
2323
(https://github.com/google/glaive) and use the |:Glaive| command to configure
2424
them.
2525

26+
*codefmt:autopep8_executable*
27+
The path to the autopep8 executable.
28+
Default: 'autopep8' `
29+
2630
*codefmt:clang_format_executable*
2731
The path to the clang-format executable.
2832
Default: 'clang-format' `
@@ -34,9 +38,9 @@ http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
3438
Default: 'file' `
3539

3640
*codefmt:gofmt_executable*
37-
The path to the gofmt executable. For example, this can be changed to
38-
"goimports" (http://go/goimports) to additionally adjust imports when
39-
formatting.
41+
The path to the gofmt executable. For example, this can be changed to
42+
"goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to
43+
additionally adjust imports when formatting.
4044
Default: 'gofmt' `
4145

4246
*codefmt:plugin[autocmds]*
@@ -75,6 +79,7 @@ plugins are enabled or what other software is installed on your system.
7579
The current list of defaults by filetype is:
7680
* cpp, proto, javascript: clang-format
7781
* go: gofmt
82+
* python: autopep8
7883

7984
==============================================================================
8085
COMMANDS *codefmt-commands*

‎instant/flags.vim‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ if !s:enter
3232
endif
3333

3434

35+
""
36+
" The path to the autopep8 executable.
37+
call s:plugin.Flag('autopep8_executable', 'autopep8')
38+
3539
""
3640
" The path to the clang-format executable.
3741
call s:plugin.Flag('clang_format_executable', 'clang-format')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /