diff --git a/autoload/go/template.vim b/autoload/go/template.vim new file mode 100644 index 0000000000..73b37e8622 --- /dev/null +++ b/autoload/go/template.vim @@ -0,0 +1,31 @@ +let s:current_file = expand("") + +function! go#template#create() + let l:root_dir = fnamemodify(s:current_file, ':h:h:h') + + let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd ' + let dir = getcwd() + execute cd . fnameescape(expand("%:p:h")) + + let l:package_name = go#tool#PackageName() + + " if we can't figure out any package name(no Go files or non Go package + " files) from the directory create the template + if l:package_name == -1 + let l:template_file = get(g:, 'go_template_file', "hello_world.go") + let l:template_path = go#util#Join(l:root_dir, "templates", l:template_file) + exe '0r ' . l:template_path + $delete _ + else + let l:content = printf("package %s", l:package_name) + call append(0, l:content) + $delete _ + endif + + " Remove the '... [New File]' message line from the command line + echon + + execute cd . fnameescape(dir) +endfunction + +" vim: sw=2 ts=2 et diff --git a/autoload/go/tool.vim b/autoload/go/tool.vim index 276e5d02d6..487b933d75 100644 --- a/autoload/go/tool.vim +++ b/autoload/go/tool.vim @@ -43,6 +43,16 @@ function! go#tool#Imports() return imports endfunction +function! go#tool#PackageName() + let command = "go list -f '{{.Name}}'" + let out = go#tool#ExecuteInDir(command) + if go#util#ShellError() != 0 + return -1 + endif + + return split(out, '\n')[0] +endfunction + function! go#tool#ParseErrors(lines) let errors = [] diff --git a/autoload/go/util.vim b/autoload/go/util.vim index 3553b50429..321ce3a48a 100644 --- a/autoload/go/util.vim +++ b/autoload/go/util.vim @@ -25,6 +25,12 @@ function! go#util#LineEnding() return "\n" endfunction +" Join joins any number of path elements into a single path, adding a +" Separator if necessary and returns the result +function! go#util#Join(...) + return join(a:000, go#util#PathSep()) +endfunction + " IsWin returns 1 if current OS is Windows or 0 otherwise function! go#util#IsWin() let win = ['win16', 'win32', 'win64', 'win95'] diff --git a/doc/vim-go.txt b/doc/vim-go.txt index a81e8cc472..c72d53c306 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -1285,8 +1285,30 @@ Specifies whether `gocode` should add built-in types, functions and constants to an autocompletion proposals. By default it is enabled. > let g:go_gocode_propose_builtins = 1 +< + *g:go_template_enabled* + +When a new Go file is created, vim-go automatically fills the buffer content +with a Go code template. By default the template under +`templates/hello_world.go` is used. This can be changed with the +|g:go_template_file| setting. +If the new file is created in an already prepopulated package (with other Go +files), in this case a Go code template with only the Go package declaration +(which is automatically determined according to the current package) is added. +By default it is enabled. +> + let g:go_template_enabled = 1 +< + *g:go_template_file* + +Specifies the file under the `templates` folder that is used if a new Go file +is created. Checkout |g:go_template_enabled| for more info. By default the +`hello_world.go` file is used. +> + let g:go_template_file = "hello_world.go" +< =============================================================================== TROUBLESHOOTING *go-troubleshooting* diff --git a/plugin/go.vim b/plugin/go.vim index f0d6c8d3c8..fa7e866374 100644 --- a/plugin/go.vim +++ b/plugin/go.vim @@ -171,6 +171,11 @@ augroup vim-go if get(g:, "go_metalinter_autosave", 0) autocmd BufWritePost *.go call go#lint#Gometa(1) endif + + " create new template from scratch + if get(g:, "go_template_enabled", 1) + autocmd BufNewFile *.go call go#template#create() + endif augroup END " vim: sw=2 ts=2 et diff --git a/templates/hello_world.go b/templates/hello_world.go new file mode 100644 index 0000000000..50e8d8d393 --- /dev/null +++ b/templates/hello_world.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("vim-go") +}