2018-12-17 11:28:27 +00:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2016-07-03 11:53:59 +00:00
|
|
|
let s:current_file = expand("<sfile>")
|
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! go#template#create() abort
|
2018-06-14 10:31:12 +00:00
|
|
|
let l:go_template_use_pkg = go#config#TemplateUsePkg()
|
2016-07-03 11:53:59 +00:00
|
|
|
let l:root_dir = fnamemodify(s:current_file, ':h:h:h')
|
|
|
|
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
2018-02-04 11:35:08 +00:00
|
|
|
let l:package_name = -1
|
2016-07-03 11:53:59 +00:00
|
|
|
|
2018-02-04 11:35:08 +00:00
|
|
|
if isdirectory(expand('%:p:h'))
|
|
|
|
execute cd . fnameescape(expand('%:p:h'))
|
|
|
|
let l:package_name = go#tool#PackageName()
|
|
|
|
endif
|
2016-07-03 11:53:59 +00:00
|
|
|
|
|
|
|
" if we can't figure out any package name(no Go files or non Go package
|
2016-12-27 14:46:49 +00:00
|
|
|
" files) from the directory create the template or use the cwd
|
|
|
|
" as the name
|
|
|
|
if l:package_name == -1 && l:go_template_use_pkg != 1
|
2017-07-06 12:57:35 +00:00
|
|
|
let l:filename = fnamemodify(expand("%"), ':t')
|
|
|
|
if l:filename =~ "_test.go$"
|
2018-06-14 10:31:12 +00:00
|
|
|
let l:template_file = go#config#TemplateTestFile()
|
2017-07-06 12:57:35 +00:00
|
|
|
else
|
2018-06-14 10:31:12 +00:00
|
|
|
let l:template_file = go#config#TemplateFile()
|
2017-07-06 12:57:35 +00:00
|
|
|
endif
|
2016-07-03 11:53:59 +00:00
|
|
|
let l:template_path = go#util#Join(l:root_dir, "templates", l:template_file)
|
2018-03-31 14:56:26 +00:00
|
|
|
silent exe 'keepalt 0r ' . fnameescape(l:template_path)
|
2016-12-27 14:46:49 +00:00
|
|
|
elseif l:package_name == -1 && l:go_template_use_pkg == 1
|
|
|
|
" cwd is now the dir of the package
|
|
|
|
let l:path = fnamemodify(getcwd(), ':t')
|
|
|
|
let l:content = printf("package %s", l:path)
|
|
|
|
call append(0, l:content)
|
2016-08-02 12:48:32 +00:00
|
|
|
else
|
2016-07-03 11:53:59 +00:00
|
|
|
let l:content = printf("package %s", l:package_name)
|
|
|
|
call append(0, l:content)
|
|
|
|
endif
|
2017-07-06 12:57:35 +00:00
|
|
|
$delete _
|
2016-07-03 11:53:59 +00:00
|
|
|
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 14:46:49 +00:00
|
|
|
function! go#template#ToggleAutoCreate() abort
|
2018-06-14 10:31:12 +00:00
|
|
|
if go#config#TemplateAutocreate()
|
|
|
|
call go#config#SetTemplateAutocreate(0)
|
2016-08-02 12:48:32 +00:00
|
|
|
call go#util#EchoProgress("auto template create disabled")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-06-14 10:31:12 +00:00
|
|
|
call go#config#SetTemplateAutocreate(1)
|
2016-08-02 12:48:32 +00:00
|
|
|
call go#util#EchoProgress("auto template create enabled")
|
|
|
|
endfunction
|
|
|
|
|
2018-12-17 11:28:27 +00:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2016-07-03 11:53:59 +00:00
|
|
|
" vim: sw=2 ts=2 et
|