mirror of
https://github.com/amix/vimrc
synced 2025-06-16 01:25:00 +08:00
Updated plugins
This commit is contained in:
@ -43,8 +43,6 @@ if !exists("g:go_fmt_experimental")
|
||||
let g:go_fmt_experimental = 0
|
||||
endif
|
||||
|
||||
let s:got_fmt_error = 0
|
||||
|
||||
" we have those problems :
|
||||
" http://stackoverflow.com/questions/12741977/prevent-vim-from-updating-its-undo-tree
|
||||
" http://stackoverflow.com/questions/18532692/golang-formatter-and-vim-how-to-destroy-history-record?rq=1
|
||||
@ -57,10 +55,9 @@ function! go#fmt#Format(withGoimport)
|
||||
" save cursor position and many other things
|
||||
let l:curw=winsaveview()
|
||||
|
||||
" needed for testing if gofmt fails or not
|
||||
let l:tmpname=tempname()
|
||||
call writefile(getline(1,'$'), l:tmpname)
|
||||
|
||||
" Write current unsaved buffer to a temp file
|
||||
let l:tmpname = tempname()
|
||||
call writefile(getline(1, '$'), l:tmpname)
|
||||
|
||||
if g:go_fmt_experimental == 1
|
||||
" save our undo file to be restored after we are done. This is needed to
|
||||
@ -77,16 +74,15 @@ function! go#fmt#Format(withGoimport)
|
||||
let fmt_command = g:go_goimports_bin
|
||||
endif
|
||||
|
||||
" if it's something else than gofmt, we need to check the existing of that
|
||||
" binary. For example if it's goimports, let us check if it's installed,
|
||||
" check if the user has installed command binary.
|
||||
" For example if it's goimports, let us check if it's installed,
|
||||
" if not the user get's a warning via go#path#CheckBinPath()
|
||||
if fmt_command != "gofmt"
|
||||
" check if the user has installed goimports
|
||||
let bin_path = go#path#CheckBinPath(fmt_command)
|
||||
if empty(bin_path)
|
||||
return
|
||||
endif
|
||||
let bin_path = go#path#CheckBinPath(fmt_command)
|
||||
if empty(bin_path)
|
||||
return
|
||||
endif
|
||||
|
||||
if fmt_command != "gofmt"
|
||||
" change GOPATH too, so goimports can pick up the correct library
|
||||
let old_gopath = $GOPATH
|
||||
let $GOPATH = go#path#Detect()
|
||||
@ -95,17 +91,18 @@ function! go#fmt#Format(withGoimport)
|
||||
endif
|
||||
|
||||
" populate the final command with user based fmt options
|
||||
let command = fmt_command . ' ' . g:go_fmt_options
|
||||
let command = fmt_command . ' -w '
|
||||
if a:withGoimport != 1
|
||||
let command = command . g:go_fmt_options
|
||||
endif
|
||||
|
||||
" execute our command...
|
||||
let out = system(command . " " . l:tmpname)
|
||||
let splitted = split(out, '\n')
|
||||
|
||||
if fmt_command != "gofmt"
|
||||
let $GOPATH = old_gopath
|
||||
endif
|
||||
|
||||
|
||||
"if there is no error on the temp file replace the output with the current
|
||||
"file (if this fails, we can always check the outputs first line with:
|
||||
"splitted =~ 'package \w\+')
|
||||
@ -113,34 +110,19 @@ function! go#fmt#Format(withGoimport)
|
||||
" remove undo point caused via BufWritePre
|
||||
try | silent undojoin | catch | endtry
|
||||
|
||||
" do not include stderr to the buffer, this is due to goimports/gofmt
|
||||
" tha fails with a zero exit return value (sad yeah).
|
||||
let default_srr = &srr
|
||||
set srr=>%s
|
||||
" Replace current file with temp file, then reload buffer
|
||||
let old_fileformat = &fileformat
|
||||
call rename(l:tmpname, expand('%'))
|
||||
silent edit!
|
||||
let &fileformat = old_fileformat
|
||||
let &syntax = &syntax
|
||||
|
||||
" delete any leftover before we replace the whole file. Suppose the
|
||||
" file had 20 lines, but new output has 10 lines, only 1-10 are
|
||||
" replaced with setline, remaining lines 11-20 won't get touched. So
|
||||
" remove them.
|
||||
if line('$') > len(splitted)
|
||||
execute len(splitted) .',$delete'
|
||||
endif
|
||||
|
||||
" setline iterates over the list and replaces each line
|
||||
call setline(1, splitted)
|
||||
|
||||
" only clear quickfix if it was previously set, this prevents closing
|
||||
" other quickfixes
|
||||
if s:got_fmt_error
|
||||
let s:got_fmt_error = 0
|
||||
call setqflist([])
|
||||
cwindow
|
||||
endif
|
||||
|
||||
" put back the users srr setting
|
||||
let &srr = default_srr
|
||||
" clean up previous location list
|
||||
call go#list#Clean()
|
||||
call go#list#Window()
|
||||
elseif g:go_fmt_fail_silently == 0
|
||||
"otherwise get the errors and put them to quickfix window
|
||||
let splitted = split(out, '\n')
|
||||
"otherwise get the errors and put them to location list
|
||||
let errors = []
|
||||
for line in splitted
|
||||
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
|
||||
@ -155,11 +137,14 @@ function! go#fmt#Format(withGoimport)
|
||||
% | " Couldn't detect gofmt error format, output errors
|
||||
endif
|
||||
if !empty(errors)
|
||||
call setqflist(errors, 'r')
|
||||
call go#list#Populate(errors)
|
||||
echohl Error | echomsg "Gofmt returned error" | echohl None
|
||||
endif
|
||||
let s:got_fmt_error = 1
|
||||
cwindow
|
||||
|
||||
call go#list#Window(len(errors))
|
||||
|
||||
" We didn't use the temp file, so clean up
|
||||
call delete(l:tmpname)
|
||||
endif
|
||||
|
||||
if g:go_fmt_experimental == 1
|
||||
@ -169,7 +154,6 @@ function! go#fmt#Format(withGoimport)
|
||||
endif
|
||||
|
||||
" restore our cursor/windows positions
|
||||
call delete(l:tmpname)
|
||||
call winrestview(l:curw)
|
||||
endfunction
|
||||
|
||||
|
Reference in New Issue
Block a user