Skip to content

Commit

Permalink
Merge pull request #1124 from jimmyfrasche/master
Browse files Browse the repository at this point in the history
Add option to use cwd as package name instead of template
  • Loading branch information
fatih authored Dec 17, 2016
2 parents ca4b443 + 66ce0c0 commit 9f9b937
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions autoload/go/template.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let s:current_file = expand("<sfile>")

function! go#template#create() abort
let l:go_template_use_pkg = get(g:, 'go_template_use_pkg', 0)
let l:root_dir = fnamemodify(s:current_file, ':h:h:h')

let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
Expand All @@ -10,12 +11,19 @@ function! go#template#create() abort
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
" 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
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 ' . fnameescape(l:template_path)
$delete _
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)
$delete _
else
let l:content = printf("package %s", l:package_name)
call append(0, l:content)
Expand Down
12 changes: 12 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,9 @@ 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.

To always use the package name instead of the template, enable the
|`g:go_template_use_pkg`| setting.

By default it is enabled.
>
let g:go_template_autocreate = 1
Expand All @@ -1453,6 +1456,15 @@ is created. Checkout |'g:go_template_autocreate'| for more info. By default
the `hello_world.go` file is used.
>
let g:go_template_file = "hello_world.go"
<
*'g:go_template_use_pkg'*

Specifies that, rather than using a template, the package name is used if a new
Go file is created. Checkout |'g:go_template_autocreate'| for more info. By
default the template file specified by |'g:go_template_file'| is used.

>
let g:go_template_use_pkg = 0
<
*'g:go_decls_includes'*

Expand Down

0 comments on commit 9f9b937

Please sign in to comment.