From 9e88616c294509f82a27a0524dc25624f1089853 Mon Sep 17 00:00:00 2001 From: jimmyfrasche Date: Thu, 24 Nov 2016 12:53:00 -0800 Subject: [PATCH 1/2] Add option to use cwd as package name instead of template --- autoload/go/template.vim | 12 ++++++++++-- doc/vim-go.txt | 11 +++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/autoload/go/template.vim b/autoload/go/template.vim index 9cbfc0312e..c133b5fc0b 100644 --- a/autoload/go/template.vim +++ b/autoload/go/template.vim @@ -1,6 +1,7 @@ let s:current_file = expand("") function! go#template#create() abort + let l:go_template_no_file = get(g:, 'go_template_no_file', 0) let l:root_dir = fnamemodify(s:current_file, ':h:h:h') let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd ' @@ -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_no_file != 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_no_file == 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) diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 8060f28566..502d5a4b2f 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -1438,6 +1438,10 @@ 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. +To use the package's directory as the name of the package instead of a template +when new Go file is created in a directory with no Go files, enable the +|'g:go_template_no_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. @@ -1453,6 +1457,13 @@ 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_no_file'* + +Specifies that, rather than using a template, the directory name is used as the +package name if a new Go file is created. Checkout |'g:go_template_autocreate'| for more info. By default it is disabled. +> + let g:go_template_no_file = 0 < *'g:go_decls_includes'* From 66ce0c0d36349d0ba14c928956d149d9489560ee Mon Sep 17 00:00:00 2001 From: jimmyfrasche Date: Sun, 27 Nov 2016 14:22:53 -0800 Subject: [PATCH 2/2] make requested changes to documentation and setting name --- autoload/go/template.vim | 6 +++--- doc/vim-go.txt | 17 +++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/autoload/go/template.vim b/autoload/go/template.vim index c133b5fc0b..0a92988f75 100644 --- a/autoload/go/template.vim +++ b/autoload/go/template.vim @@ -1,7 +1,7 @@ let s:current_file = expand("") function! go#template#create() abort - let l:go_template_no_file = get(g:, 'go_template_no_file', 0) + 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 ' @@ -13,12 +13,12 @@ function! go#template#create() abort " if we can't figure out any package name(no Go files or non Go package " files) from the directory create the template or use the cwd " as the name - if l:package_name == -1 && l:go_template_no_file != 1 + 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_no_file == 1 + 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) diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 502d5a4b2f..180e768874 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -1438,14 +1438,13 @@ 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. -To use the package's directory as the name of the package instead of a template -when new Go file is created in a directory with no Go files, enable the -|'g:go_template_no_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. +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 @@ -1458,12 +1457,14 @@ the `hello_world.go` file is used. > let g:go_template_file = "hello_world.go" < - *'g:go_template_no_file'* + *'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. -Specifies that, rather than using a template, the directory name is used as the -package name if a new Go file is created. Checkout |'g:go_template_autocreate'| for more info. By default it is disabled. > - let g:go_template_no_file = 0 + let g:go_template_use_pkg = 0 < *'g:go_decls_includes'*