codeformatter icon indicating copy to clipboard operation
codeformatter copied to clipboard

Add code formatting rule to add newlines at the end of code files

Open dsplaisted opened this issue 9 years ago • 5 comments

@stephentoub has pointed out that text files should end with a newline. This PR adds a code formatting rule to do this for C# source code files.

dsplaisted avatar May 29 '15 22:05 dsplaisted

I guess I'll close #61 then. I never got to finishing it anyway. This closes #54.

khellang avatar May 31 '15 01:05 khellang

Thanks for the pointer to #61 @khellang. Based on reading the discussion there, I've fixed the rule for cases where there are comments or compiler directives at the end of the file.

For simplicity, this PR merely makes sure that the file ends with a newline (in contrast with #61). It doesn't try to prevent it from ending with multiple newlines.

dsplaisted avatar Jun 02 '15 18:06 dsplaisted

Ahh i just implemented this before seeing this PR :)

@dsplaisted: Can you rebase these commits, it's difficult to code review with context in different commits.

Priya91 avatar Jun 03 '15 07:06 Priya91

Won't this conflict with the standard formatter rule? The formatter by default will delete new lines at the end of the file.

jaredpar avatar Jun 04 '15 21:06 jaredpar

Note that this is only required in case of CRLF on Windows. If you are using LF in editor, extra linefeed will persist in diff, which means git doesn't require additional line-break.

I think git converts CRLF+CRLF as LF for end of file and considers single CRLF as invalid end of file terminator. One standard way to solve this problem is to have LF rule for all sources in .gitattributes file like this:

[*] 
end_of_line = lf

// or

[*.cs, *.vb, *.csproj] 
end_of_line = lf

(in corefx repository, .gitattributes is present, but almost never used: all rules are commented out)

Note, git only apply the .gitattributes rules on commit, meaning your local formatting will stay intact.

ghost avatar Jun 06 '15 22:06 ghost