volgar1x / vim-gocode

A Go bundle for Vundle or Pathogen
238 stars 34 forks source link

Auto Fmt doesnt work for first file #4

Closed satyrius closed 10 years ago

satyrius commented 10 years ago

In you vim-golang plugin modification you have autoformating enabled for go filetype BufWritePre https://github.com/Blackrush/vim-gocode/blob/master/ftplugin/go/fmt.vim#L20.

But for me it works in very strange way. Open two files vim foo.go bar.go, modify both (ugly code to be formatted) then save. Fmt will be called be autocmd for second file opened, but not for first. Note that Fmt command exists for first file buffer, but does not called automatically. Any ideas why?

volgar1x commented 10 years ago

The :Fmt command only format your current buffer, you have to run it on every file you want to format. The auto-fmt works the same way, it only formats the current buffer on save. You have to save every file to force auto-fmt.

satyrius commented 10 years ago

Yes, I know how it works. But it looks like autocmd does not work on the first file. I do the following

# open vim editor
cd ~/devel/my_go_project && vim
# open first file using vim command mode
:e src/foo.go
# mangle formatting, save the file, nothing happens
# open another file
:e src/bar.go
# mangle formatting, save the file, Fmt works!
# them switch back to the first file
:b#
# try again to trigger autocmd by saving file, still not working

I fixed it by duplicating autocmd definition inside my .vimrc

autocmd FileType go autocmd BufWritePre <buffer> :keepjumps Fmt

But I cannot found the reason why it does not work from plugin.

volgar1x commented 10 years ago

That's weird, I never had this issue before.

satyrius commented 10 years ago

My further experiment...

# close first buffer
:bd
# open first file again
:e foo.go
# now it works

Yeah, that's weird.