2019-06-03 14:58:18 +00:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2018-05-06 15:45:27 +00:00
|
|
|
let s:current_file = expand("<sfile>")
|
|
|
|
|
|
|
|
function! go#template#create() abort
|
2019-06-03 14:58:18 +00:00
|
|
|
let l:go_template_use_pkg = go#config#TemplateUsePkg()
|
2018-05-06 15:45:27 +00:00
|
|
|
let l:root_dir = fnamemodify(s:current_file, ':h:h:h')
|
|
|
|
|
2019-06-03 14:58:18 +00:00
|
|
|
let l:package_name = go#tool#PackageName()
|
2018-05-06 15:45:27 +00:00
|
|
|
|
2019-06-03 14:58:18 +00:00
|
|
|
" if we can't figure out any package name (i.e. no Go files in the directory)
|
|
|
|
" from the directory create the template or use the directory as the name.
|
|
|
|
if l:package_name == -1
|
|
|
|
if l:go_template_use_pkg == 1
|
|
|
|
let l:path = fnamemodify(expand('%:p:h'), ':t')
|
|
|
|
let l:content = printf("package %s", l:path)
|
|
|
|
call append(0, l:content)
|
2018-05-06 15:45:27 +00:00
|
|
|
else
|
2019-06-03 14:58:18 +00:00
|
|
|
let l:filename = expand('%:t')
|
|
|
|
if l:filename =~ "_test.go$"
|
|
|
|
let l:template_file = go#config#TemplateTestFile()
|
|
|
|
else
|
|
|
|
let l:template_file = go#config#TemplateFile()
|
|
|
|
endif
|
|
|
|
let l:template_path = go#util#Join(l:root_dir, "templates", l:template_file)
|
|
|
|
silent exe 'keepalt 0r ' . fnameescape(l:template_path)
|
2018-05-06 15:45:27 +00:00
|
|
|
endif
|
|
|
|
else
|
|
|
|
let l:content = printf("package %s", l:package_name)
|
|
|
|
call append(0, l:content)
|
|
|
|
endif
|
2019-06-03 14:58:18 +00:00
|
|
|
" checking that the last line is empty shouldn't be necessary, but for some
|
|
|
|
" reason the last line isn't the expected empty line when run via tests.
|
|
|
|
if getline('$') is ''
|
|
|
|
$delete _
|
|
|
|
endif
|
2018-05-06 15:45:27 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#template#ToggleAutoCreate() abort
|
2019-06-03 14:58:18 +00:00
|
|
|
if go#config#TemplateAutocreate()
|
|
|
|
call go#config#SetTemplateAutocreate(0)
|
2018-05-06 15:45:27 +00:00
|
|
|
call go#util#EchoProgress("auto template create disabled")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-06-03 14:58:18 +00:00
|
|
|
call go#config#SetTemplateAutocreate(1)
|
2018-05-06 15:45:27 +00:00
|
|
|
call go#util#EchoProgress("auto template create enabled")
|
|
|
|
endfunction
|
|
|
|
|
2019-06-03 14:58:18 +00:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2018-05-06 15:45:27 +00:00
|
|
|
" vim: sw=2 ts=2 et
|