user3871
user3871

Reputation: 12718

Atom / Github removing end-of-file newline

Using Atom 1.14.3, I have whitespace package that handles auto-inserting newlines at the end of files.

Even if I delete the end newline and hit save, it re-adds the newline. This is good.

enter image description here

Whitespace package configuration seems to be okay:

enter image description here

The problem is, when I commit to Github, it says the newline has been removed:

enter image description here

Why is this? Is it an Atom issue, or a potential local github setting issue?


EDIT: somehow, I needed to disable whitespace package, manually add two CRLF at the end of the file, and then commit for Github to pick up the single CRLF at the end of the file.

enter image description here

Upvotes: 3

Views: 6356

Answers (1)

Dan Lowe
Dan Lowe

Reputation: 56548

I think you might be misunderstanding where the newlines are.

Let's look at your two screenshots, and where the newlines are in each.

screenshot 1

233    return router;\n
234  };\n

Here we have 234 as the last line in the file. We have a line 235 displayed, but that is because the newline on 234 creates the next line for your editor cursor to be on. If you started typing on 235, you'd be creating more content. But right now, 235 is an empty line (including having no terminating newline).

screenshot 2

233    return router;\n
234  };\n
235  \n

This is similar except it also has an empty line 235 that ends with a newline. Now the newline-less empty input line has moved to 236.

When you saved with the whitespace package active, it removed extraneous newlines at the end of the file, leaving only one. As in the first screenshot. However, when you look at the Github diff, things are little different. Github is showing you the file contents, not in an editor. So there is no reason to have the phantom last line for your cursor. Instead, it shows you the simple truth of the matter: line 234 is the last line in the file. Line 235 is now gone.

Let's take a look at the settings for the whitespace package. Specifically the first setting:

Ensure Single Trailing Newline

If the buffer doesn't end with a newline character when it's saved, then append one. If it ends with more than one newline, remove all but one. To disable/enable for a certain language, use syntax-scoped properties in your config.cson.

Here are the first two sentences of the description again, with some emphasis added:

If the buffer doesn't end with a newline character when it's saved, then append one. If it ends with more than one newline, remove all but one.

Upvotes: 3

Related Questions