Skip to content
64 changes: 64 additions & 0 deletions autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
" The current list of defaults by filetype is:
" * cpp, proto, javascript: clang-format
" * go: gofmt
" * python: autopep8


call maktaba#library#Require('codefmtlib')
Expand Down Expand Up @@ -156,6 +157,69 @@ if !exists('s:gofmt')
call codefmtlib#AddDefaultFormatter(s:gofmt)
endif

" Formatter: autopep8
if !exists('s:autopep8')
let s:autopep8 = {
\ 'name': 'autopep8',
\ 'setup_instructions': 'Install autopep8 ' .
\ '(https://pypi.python.org/pypi/autopep8/).'}

function s:autopep8.IsAvailable() abort
return executable(s:plugin.Flag('autopep8_executable'))
endfunction

function s:autopep8.AppliesToBuffer() abort
return &filetype is# 'python'
endfunction

""
" Reformat the current buffer with autopep8 or the binary named in
" @flag(autopep8_executable), only targeting the range between {startline} and
" {endline}.
" @throws ShellError
function s:autopep8.FormatRange(startline, endline) abort
let l:executable = s:plugin.Flag('autopep8_executable')
if !exists('s:autopep8_supports_range')
let l:version_call =
\ maktaba#syscall#Create([l:executable, '--version']).Call()
" In some cases version is written to stderr, in some to stdout
let l:version_output =
\ version_call.stderr ? version_call.stderr : version_call.stdout
let s:autopep8_supports_range =
\ matchlist(l:version_output, '\m\Cautopep8 \(\d\+\)\.')[1] >= 1
endif

call maktaba#ensure#IsNumber(a:startline)
call maktaba#ensure#IsNumber(a:endline)
let l:lines = getline(1, line('$'))

if s:autopep8_supports_range
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that the '-' argument means stdin and put the '-' at the end of the list of args (I got a bit confused by the '-', '--range' list)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find it too confusing, but it could be more obvious at a glance if the lines were broken up differently:

      let l:cmd = [
          \ l:executable,
          \ '--range', ''.a:startline, ''.a:endline,
          \ '-' ]

Matt, you still think a comment would be necessary like that? Was it more that you didn't notice the '-' or just didn't know what to make of it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know what to make of it. I thought it was similar to the bash things
which say "all arguments after this one are files" which confused me and
sent me off to read the docs.

Matt
On Jan 26, 2015 11:51 AM, "David Barnett" notifications@github.com wrote:

In autoload/codefmt.vim
#4 (comment):

  • " @throws ShellError
  • function s:autopep8.FormatRange(startline, endline) abort
  • " Hack range formatting by formatting range individually, ignoring context.
  • let l:executable = s:plugin.Flag('autopep8_executable')
  • if !exists('s:autopep8_supports_range')
  •  let l:version_output =
    
  •      \ maktaba#syscall#Create([l:executable, '--version']).Call().stderr
    
  •  let s:autopep8_supports_range =
    
  •      \ matchlist(l:version_output, '\m\Cautopep8 (\d+).')[1] >= 1
    
  • endif
  • call maktaba#ensure#IsNumber(a:startline)
  • call maktaba#ensure#IsNumber(a:endline)
  • let l:lines = getline(1, line('$'))
  • if s:autopep8_supports_range

I didn't find it too confusing, but it could be more obvious at a glance
if the lines were broken up differently:

  let l:cmd = [
      \ l:executable,
      \ '--range', ''.a:startline, ''.a:endline,
      \ '-' ]

Matt, you still think a comment would be necessary like that? Was it more
that you didn't notice the '-' or just didn't know what to make of it?


Reply to this email directly or view it on GitHub
https://github.com/google/vim-codefmt/pull/4/files#r23544410.

let l:cmd = [ l:executable,
\ '--range', ''.a:startline, ''.a:endline,
\ '-' ]
let l:input = join(l:lines, "\n")
else
let l:cmd = [ l:executable, '-' ]
" Hack range formatting by formatting range individually, ignoring context.
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
endif

let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call()
let l:formatted = split(l:result.stdout, "\n")

if s:autopep8_supports_range
let l:full_formatted = l:formatted
else
" Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right.
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : []
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, just noticed what autopep8 don't output errors like that.

actually i am not even sure if it can return unsuccessful result when using stdin at all (when it faces wrong syntax or smth it is just ignoring it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's fine to get rid of the try/catch and just let the ShellError bubble up. It's caught and handled in codefmt. You can add a @throws ShellError line into the vimdoc for good measure.

endif

call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
endfunction

call codefmtlib#AddDefaultFormatter(s:autopep8)
endif

""
" Detects whether a formatter has been defined for the current buffer/filetype.
Expand Down
15 changes: 10 additions & 5 deletions doc/codefmt.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*codefmt.txt* Syntax-aware code formatting
*codefmt*
*codefmt.txt* Syntax-aware code formatting for a variety of languages
Google *codefmt*

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

*codefmt:autopep8_executable*
The path to the autopep8 executable.
Default: 'autopep8' `

*codefmt:clang_format_executable*
The path to the clang-format executable.
Default: 'clang-format' `
Expand All @@ -34,9 +38,9 @@ http://clang.llvm.org/docs/ClangFormatStyleOptions.html for details.
Default: 'file' `

*codefmt:gofmt_executable*
The path to the gofmt executable. For example, this can be changed to
"goimports" (http://go/goimports) to additionally adjust imports when
formatting.
The path to the gofmt executable. For example, this can be changed to
"goimports" (https://godoc.org/golang.org/x/tools/cmd/goimports) to
additionally adjust imports when formatting.
Default: 'gofmt' `

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

==============================================================================
COMMANDS *codefmt-commands*
Expand Down
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ if !s:enter
endif


""
" The path to the autopep8 executable.
call s:plugin.Flag('autopep8_executable', 'autopep8')

""
" The path to the clang-format executable.
call s:plugin.Flag('clang_format_executable', 'clang-format')
Expand Down