Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill the content of a new file automatically based on the context #918

Merged
merged 1 commit into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions autoload/go/template.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let s:current_file = expand("<sfile>")

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
10 changes: 10 additions & 0 deletions autoload/go/tool.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
6 changes: 6 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
22 changes: 22 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
5 changes: 5 additions & 0 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions templates/hello_world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("vim-go")
}