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

Fix issue #595 - fileformat changed by gofmt #615

Merged
merged 2 commits into from
Nov 24, 2015

Conversation

cochran-at-niche
Copy link
Contributor

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!

@fatih
Copy link
Owner

fatih commented Nov 24, 2015

Hi @cochran-at-niche

This totally makes sense. I'm ok to merge it as I really don't want to change anythign. Just the order of saving and restoring the filetype seems to be wrong. Shouldn't we save before the renaming? silent edit! just reloads the file actually. Again not sure, can you test it?

@cochran-at-niche
Copy link
Contributor Author

I'm 99% sure that the fileformat variable is changed when the file/buffer is reloaded (silent edit!) - that's when vim attempts to detect the fileformat, and ends up setting it to 'unix'. I can confirm that it fixed the issue for me, at least.

In any case, I went ahead and changed it so that the fileformat is saved before the temp file is renamed, just to be safe.

fatih added a commit that referenced this pull request Nov 24, 2015
Fix issue #595 - fileformat changed by gofmt
@fatih fatih merged commit f2f4969 into fatih:master Nov 24, 2015
@fatih
Copy link
Owner

fatih commented Nov 24, 2015

Thanks @cochran-at-niche

@cochran-at-niche
Copy link
Contributor Author

You're welcome! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants