netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

The blank line at end of file

Open Eugene-Melbourne opened this issue 3 years ago • 3 comments

Apache NetBeans version

Apache NetBeans 15

What happened

When programming on PHP, thanks to Netbeans Options->editor->formatting you can be in control of Blank Lines. Except for one critical parameter "at end of file".

  1. Because. In PHP we have 2 standards:

PSR-2 convention that states:

All PHP files MUST end with a single blank line.

PSR-12 #2.2 Files:

All PHP files MUST end with a non-blank line....

It would be good to give a user to decide - what to do with "at end of file", either put a blank line there, or not.

  1. Because. GIT/github giving a warning message "No newline at end of file"... So the current situation with NetBeans 15 is causing this warning to be shown. you can see stackoverflow people struggle because of it.

  2. Because in Netbeans file comparing window, the blank line "at end of file" is not shown like a change. It is just ignored colour-wise. And plus, it breaks the line to line relationship. It shows the last line of one file against the last line of the other. The differen lines are being hard-tied on the same level. Instead, the blank line should be shown on the extra line. Because of this mistake of hard-tied last lines the shift of wrong lines being put one against the other is keeping going and affects the whole file. The whole visible part of the files are being shifted unpleasantly. (It is especially easy to see on files longer than 30 lines, when your file comparing window can only display 20 lines)

How to reproduce

No response

Did this work correctly in an earlier version?

Apache NetBeans 13

Operating System

Windowns 10

JDK

Java: 10.0.1; Java HotSpot(TM) 64-Bit Server VM 10.0.1+10

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

No response

Are you willing to submit a pull request?

No

Code of Conduct

Yes

Eugene-Melbourne avatar Sep 16 '22 02:09 Eugene-Melbourne

Actually both PSR-2 and PSR-12 say the same thing, but phrased differently.

PSR-2

All PHP files MUST end with a single blank line.

PSR-12

All PHP files MUST end with a non-blank line, terminated with a single LF.

While I strongly agree, that this is something that IDE should allow us to control, there's actually a plugin by one of the PHP maintainers for netbeans that solves this isssue. https://github.com/junichi11/netbeans-nonewline-resolver

terax6669 avatar Sep 25 '22 15:09 terax6669

"a non-blank line, terminated with a single LF" looks like this:

"text(LF)(EOF)"

"files MUST end with a single blank line" would be:

"text(LF)(LF)(EOF)"

Thank you for the plugin. It is working

Eugene-Melbourne avatar Sep 27 '22 23:09 Eugene-Melbourne

@Eugene-Melbourne your quote was incomplete. PSR-12 says

All PHP files MUST end with a non-blank line, terminated with a single LF.

So there must not be a blank line at the end of the file, but the last line has to be terminated with a LF.

In fact this is a linux convention followed by common text editors. In Linux the LF character is not regarded as a line seperator, but as a line terminator, where valid lines should end with LF. You can read more about that distinction on wikipedia.

If you create an empty file on Linux in Netbeans, insert some characters (without pressing return key) and save, it won't add the line terminator LF. But this should be done on linux text files (see here or there).

The current behaviour leads to issues when you are using tools like cat. Here's an example:

# create an empty file in netbeans and enter "test" without pressing the return key. Then on the terminal do the following:

naitsirch@linux1:~$ cat file_created_by_netbeans.txt
testnaitsirch@linux1:~$

It would be nice if this could be changed for all text file types if the Unix(LF) formatting is used.

Also important

If a text file with Unix(LF) formatting is opened, where the last line is terminated with an LF (some text<LF><EOF>), Netbeans should not show it with an extra empty line. Currently this happens because the LF is interpreted as a line seperator instead of a line terminator.

stollr avatar Oct 12 '22 09:10 stollr

Could you write details?

e.g.

// example code

Steps to reproduce

Actual result

something

Expected result

something

junichi11 avatar Apr 07 '23 16:04 junichi11

Steps to reproduce:

  • On a linux system, create a new empty file (the file type is irrelevant, it is not just for PHP). Let's name it test.md.
  • Activate View -> Show Non-printable Characters
  • Type TEST and Save
  • optional: Add the new file to the Git index with git add test.md

Actual result without Return

Compare the screenshots of Netbeans (left) and the output of cat -e test.md in a linux terminal:

grafik

In the Netbeans editor it shows the ¶ at the end of the line. But when you print the file at the terminal you see, that there is no trailing LF character at the end of the line.

Running git diff --cached test.md will output:

grafik

This indicates that Git is missing the LF at the end of the file.

Actual result with Return

Now, press Return, save and compare the screenshots again:

grafik

Netbeans shows two lines now, although other linux tools interprete this as one line.

If you run git add test.md and git diff --cached test.md again, Git will also show only one line without the "No newline at end of file" notice.

Expected result

For files with Unix (LF) line terminators, I expect that every line is terminated with a LF character:

grafik

I hope this helps to understand the issue.

stollr avatar Apr 11 '23 08:04 stollr

@stollr Well... I asked the reporter(@Eugene-Melbourne ). It's another problem, I think. So, please report your problem as a new issue. (I don't know about it, sorry. At least, it's not a problem of the PHP area.)

junichi11 avatar Apr 11 '23 12:04 junichi11