2014-10-31 17:30:24 -04:00
|
|
|
" Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
" Use of this source code is governed by a BSD-style
|
|
|
|
" license that can be found in the LICENSE file.
|
|
|
|
"
|
|
|
|
" fmt.vim: Vim command to format Go files with gofmt.
|
|
|
|
"
|
|
|
|
" This filetype plugin add a new commands for go buffers:
|
|
|
|
"
|
|
|
|
" :Fmt
|
|
|
|
"
|
|
|
|
" Filter the current Go buffer through gofmt.
|
|
|
|
" It tries to preserve cursor position and avoids
|
|
|
|
" replacing the buffer with stderr output.
|
|
|
|
"
|
|
|
|
" Options:
|
|
|
|
"
|
|
|
|
" g:go_fmt_command [default="gofmt"]
|
|
|
|
"
|
|
|
|
" Flag naming the gofmt executable to use.
|
|
|
|
"
|
|
|
|
" g:go_fmt_autosave [default=1]
|
|
|
|
"
|
|
|
|
" Flag to auto call :Fmt when saved file
|
|
|
|
"
|
|
|
|
|
|
|
|
if !exists("g:go_fmt_command")
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_fmt_command = "gofmt"
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists("g:go_goimports_bin")
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_goimports_bin = "goimports"
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists('g:go_fmt_fail_silently')
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_fmt_fail_silently = 0
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists('g:go_fmt_options')
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_fmt_options = ''
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
2015-01-18 07:58:28 -05:00
|
|
|
if !exists("g:go_fmt_experimental")
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_fmt_experimental = 0
|
2015-01-18 07:58:28 -05:00
|
|
|
endif
|
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
" we have those problems :
|
2014-10-31 17:30:24 -04:00
|
|
|
" 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
|
|
|
|
"
|
|
|
|
" The below function is an improved version that aims to fix all problems.
|
|
|
|
" it doesn't undo changes and break undo history. If you are here reading
|
|
|
|
" this and have VimL experience, please look at the function for
|
|
|
|
" improvements, patches are welcome :)
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#fmt#Format(withGoimport) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if g:go_fmt_experimental == 1
|
|
|
|
" Using winsaveview to save/restore cursor state has the problem of
|
|
|
|
" closing folds on save:
|
|
|
|
" https://github.com/fatih/vim-go/issues/502
|
|
|
|
" One fix is to use mkview instead. Unfortunately, this sometimes causes
|
|
|
|
" other bad side effects:
|
|
|
|
" https://github.com/fatih/vim-go/issues/728
|
|
|
|
" and still closes all folds if foldlevel>0:
|
|
|
|
" https://github.com/fatih/vim-go/issues/732
|
|
|
|
let l:curw = {}
|
|
|
|
try
|
|
|
|
mkview!
|
|
|
|
catch
|
|
|
|
let l:curw = winsaveview()
|
|
|
|
endtry
|
|
|
|
else
|
|
|
|
" Save cursor position and many other things.
|
|
|
|
let l:curw = winsaveview()
|
|
|
|
endif
|
|
|
|
|
|
|
|
" 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
|
|
|
|
" prevent an additional undo jump due to BufWritePre auto command and also
|
|
|
|
" restore 'redo' history because it's getting being destroyed every
|
|
|
|
" BufWritePre
|
|
|
|
let tmpundofile = tempname()
|
|
|
|
exe 'wundo! ' . tmpundofile
|
|
|
|
endif
|
|
|
|
|
|
|
|
" get the command first so we can test it
|
|
|
|
let bin_name = g:go_fmt_command
|
|
|
|
if a:withGoimport == 1
|
|
|
|
let bin_name = g:go_goimports_bin
|
|
|
|
endif
|
|
|
|
|
|
|
|
" 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()
|
|
|
|
let bin_path = go#path#CheckBinPath(bin_name)
|
|
|
|
if empty(bin_path)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if bin_name != "gofmt"
|
|
|
|
" change GOPATH too, so goimports can pick up the correct library
|
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
|
|
|
endif
|
|
|
|
|
|
|
|
" populate the final command with user based fmt options
|
|
|
|
let command = bin_path . ' -w '
|
|
|
|
if a:withGoimport != 1
|
|
|
|
let command = command . g:go_fmt_options
|
|
|
|
endif
|
|
|
|
|
|
|
|
if bin_name == "goimports"
|
|
|
|
if !exists('b:goimports_vendor_compatible')
|
|
|
|
let out = go#util#System(bin_path . " --help")
|
|
|
|
if out !~ "-srcdir"
|
2016-10-02 07:37:21 -04:00
|
|
|
call go#util#EchoWarning(printf("vim-go: goimports (%s) does not support srcdir. Update with: :GoUpdateBinaries", bin_path))
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
|
|
|
let b:goimports_vendor_compatible = 1
|
|
|
|
endif
|
2016-03-14 06:04:57 -04:00
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if exists('b:goimports_vendor_compatible') && b:goimports_vendor_compatible
|
|
|
|
let ssl_save = &shellslash
|
|
|
|
set noshellslash
|
2016-08-02 08:48:32 -04:00
|
|
|
let command = command . '-srcdir ' . shellescape(expand("%:p"))
|
2016-06-26 07:12:36 -04:00
|
|
|
let &shellslash = ssl_save
|
2015-01-18 07:58:28 -05:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" execute our command...
|
|
|
|
if go#util#IsWin()
|
|
|
|
let l:tmpname = tr(l:tmpname, '\', '/')
|
|
|
|
endif
|
|
|
|
let out = go#util#System(command . " " . l:tmpname)
|
|
|
|
|
|
|
|
if bin_name != "gofmt"
|
|
|
|
let $GOPATH = old_gopath
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:listtype = "locationlist"
|
|
|
|
"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\+')
|
|
|
|
if go#util#ShellError() == 0
|
|
|
|
" remove undo point caused via BufWritePre
|
|
|
|
try | silent undojoin | catch | endtry
|
|
|
|
|
|
|
|
" Replace current file with temp file, then reload buffer
|
|
|
|
let old_fileformat = &fileformat
|
2016-08-02 08:48:32 -04:00
|
|
|
if exists("*getfperm")
|
|
|
|
" save old file permissions
|
|
|
|
let original_fperm = getfperm(expand('%'))
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
call rename(l:tmpname, expand('%'))
|
2016-08-02 08:48:32 -04:00
|
|
|
" restore old file permissions
|
|
|
|
if exists("*setfperm") && original_fperm != ''
|
|
|
|
call setfperm(expand('%'), original_fperm)
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
silent edit!
|
|
|
|
let &fileformat = old_fileformat
|
|
|
|
let &syntax = &syntax
|
|
|
|
|
|
|
|
" clean up previous location list, but only if it's due to fmt
|
|
|
|
if exists('b:got_fmt_error') && b:got_fmt_error
|
|
|
|
let b:got_fmt_error = 0
|
|
|
|
call go#list#Clean(l:listtype)
|
|
|
|
call go#list#Window(l:listtype)
|
2015-02-04 05:43:54 -05:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
elseif g:go_fmt_fail_silently == 0
|
|
|
|
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*\(.*\)')
|
|
|
|
if !empty(tokens)
|
|
|
|
call add(errors, {"filename": @%,
|
|
|
|
\"lnum": tokens[2],
|
|
|
|
\"col": tokens[3],
|
|
|
|
\"text": tokens[4]})
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
if empty(errors)
|
|
|
|
% | " Couldn't detect gofmt error format, output errors
|
2015-12-08 08:20:04 -05:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
if !empty(errors)
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#list#Populate(l:listtype, errors, 'Format')
|
2016-06-26 07:12:36 -04:00
|
|
|
echohl Error | echomsg "Gofmt returned error" | echohl None
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let b:got_fmt_error = 1
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" We didn't use the temp file, so clean up
|
|
|
|
call delete(l:tmpname)
|
|
|
|
endif
|
2016-03-14 06:04:57 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if g:go_fmt_experimental == 1
|
|
|
|
" restore our undo history
|
|
|
|
silent! exe 'rundo ' . tmpundofile
|
|
|
|
call delete(tmpundofile)
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if g:go_fmt_experimental == 1
|
|
|
|
" Restore our cursor/windows positions, folds, etc.
|
|
|
|
if empty(l:curw)
|
|
|
|
silent! loadview
|
2016-02-20 08:13:10 -05:00
|
|
|
else
|
2016-06-26 07:12:36 -04:00
|
|
|
call winrestview(l:curw)
|
2016-02-20 08:13:10 -05:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
|
|
|
" Restore our cursor/windows positions.
|
|
|
|
call winrestview(l:curw)
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#fmt#ToggleFmtAutoSave() abort
|
2016-08-02 08:48:32 -04:00
|
|
|
if get(g:, "go_fmt_autosave", 1)
|
|
|
|
let g:go_fmt_autosave = 0
|
|
|
|
call go#util#EchoProgress("auto fmt disabled")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
let g:go_fmt_autosave = 1
|
|
|
|
call go#util#EchoProgress("auto fmt enabled")
|
|
|
|
endfunction
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|