Fix issue #595 - fileformat changed by gofmt #615
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A simple fix for issue #595.
When :GoFmt is called from within Vim, it runs the gofmt command with the -w parameter, which modifies the source file (actually, it modifies a temp file, which is then swapped in, but you get the idea). One effect of this is that all line endings are changed to lf. When the current buffer is reloaded, vim detects the fileformat as 'unix' due to the lf line endings, even if the fileformat was previously 'dos'. This makes it impossible to maintain windows-style line endings (crlf), if you want them.
Prior to this recent commit: 01578fd, this did not occur. I believe it has to do with the fact that the lines output from gofmt were previously split and inserted line-by-line via Vim itself, which respected the existing fileformat variable. However, in the this commit, the behavior was changed so that gofmt takes care of modifying the file externally, outside of vim (via the -w flag). As a result, the fileformat variable is no longer respected, and is actually changed upon reloading the buffer.
I'm not sure if you'll want to merge this pull-request, or simply accept unix-style line endings as the only valid line-endings for a Go file (this seems to be the convention adopted by the authors of Go: golang/go#3961). In any case, I thought I'd bring it to your attention, because I, at least, wanted to keep using windows-style line endings on my windows machine (I let git do the conversion for me), and I expect that other people might want the same.
Thanks!