• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1" Copyright 2015 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" VIM Autoload script for YAPF support
16"
17" Place this script in your ~/.vim/autoload directory. You can add accessors to
18" ~/.vimrc, e.g.:
19"
20"    map <C-Y> :call yapf#YAPF()<cr>
21"    imap <C-Y> <c-o>:call yapf#YAPF()<cr>
22"
23function! yapf#YAPF() range
24  " Determine range to format.
25  let l:line_ranges = a:firstline . '-' . a:lastline
26  let l:cmd = 'yapf --lines=' . l:line_ranges
27
28  " Call YAPF with the current buffer
29  if exists('*systemlist')
30    let l:formatted_text = systemlist(l:cmd, join(getline(1, '$'), "\n") . "\n")
31  else
32    let l:formatted_text =
33        \ split(system(l:cmd, join(getline(1, '$'), "\n") . "\n"), "\n")
34  endif
35
36  if v:shell_error
37    echohl ErrorMsg
38    echomsg printf('"%s" returned error: %s', l:cmd, l:formatted_text[-1])
39    echohl None
40    return
41  endif
42
43  " Update the buffer.
44  execute '1,' . string(line('$')) . 'delete'
45  call setline(1, l:formatted_text)
46
47  " Reset cursor to first line of the formatted range.
48  call cursor(a:firstline, 1)
49endfunction
50