Bug 450919 - Kate adds newline at the end of file, and doesn't even show it
Summary: Kate adds newline at the end of file, and doesn't even show it
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: general (show other bugs)
Version: 21.12.2
Platform: Other Linux
: VLO wishlist
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-02-27 12:07 UTC by php4fan
Modified: 2022-03-05 21:07 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
test file (27.01 KB, text/plain)
2022-02-27 12:07 UTC, php4fan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description php4fan 2022-02-27 12:07:39 UTC
Created attachment 147168 [details]
test file

STEPS TO REPRODUCE
1.  Open the attached txt file in Kate. This file has 4608 rows and 4607 newlines (not sure of which kind). It has NO newline at the end of the file
2. Without editing the file AT ALL, save it
3. close it and open it again

OBSERVED RESULT
When you save the file, a newline character is added for no reason at the end of the file, without warning.
Worse than that, you can't even see it. If you move the cursor at the end of the file, there is no empty line at the end (just like there wasn't before saving). Kate has modified the file but you can't see it.
But when you reopen it, you will see the empty line at the end in Kate. 
So not only Kate is modifying a file upon saving despite the fact that I have not touched it; and not only is it doing so silently. It is also inconsistent in how it shows the contents of the same file.
After step 2 the newline at the end has already been added but can't be seen. After step 3 nothing has changed wrt after step 2, but now the empty line can be seen.

EXPECTED RESULT
1. No change should be made to a file when saving it if I have done absolutely nothing in the editor. If I open a file and save it, it shouldn't be changed the slightest bit
2. If you have a good reason for changing a file that I haven't touched when I save it, you should warn me one way or another. You don't do that silently
3. If I close a file that I already saved and reopen it (meaning I am opening a file that is identical to how it was when I closed it), it should look the same, in every aspect, no matter what. If I don't see an empty line at the end before closing (and I repeat: after saving), than I shouldn't see an empty line at the end after reopening, given that the file hasn't been modified in the meantime. No matter how you choose to visually represent the presence or absence of a newline at the end of file (I don't think there's much ambiguity there actually, if there is a newline at the end I should see an empty line, I think that's all, but even if I'm missing something here), you must be consistent, and never ever should a file that hasn't changed look different.


SOFTWARE/OS VERSIONS
Operating System: openSUSE Tumbleweed 20220207
KDE Plasma Version: 5.24.0
KDE Frameworks Version: 5.90.0
Qt Version: 5.15.2
Kernel Version: 5.16.5-1-default (64-bit)
Graphics Platform: X11
Processors: 8 × Intel® Core™ i7-1065G7 CPU @ 1.30GHz
Memory: 7.3 GiB of RAM
Graphics Processor: Mesa Intel® Iris® Plus Graphics
Comment 1 Waqar Ahmed 2022-03-02 06:11:16 UTC
> When you save the file, a newline character is added for no reason at the end of the file

There is a reason.

You can disable this setting here: Configure Kate -> Open/Save -> Auto append new line on save
Comment 2 php4fan 2022-03-02 10:13:06 UTC
Ok, everything else remains valid.
Especially the part where the appended new line is not even shown.
Comment 3 Waqar Ahmed 2022-03-02 10:58:39 UTC
Yeah, but nothing critcial.
Comment 4 php4fan 2022-03-02 14:40:22 UTC
I strongly disagree with classifying this as "wishlist".

There are two things here:

1. The fact that when you save and the contents are changed (whatever the changes are and even if it is as per the settings), the first time that happens a warning should be shown with a "don't show again" option. That I can understand classifying as wishlist. It is standard behavior that one expects from any decent software, but it can be considered a feature request.

2. The fact that when you save the file and add a newline at the end, that newline is not shown. It is not shown in the same way it is when you close and reopen the file. This is a bug, and I don't know whether you call it "critical" or not, but it's pretty bad: you are not showing the current content of the file as-is. That's definitely way above wishlist. What will happen if I go to the end of the file and add something at the very end? Will that be added after or before the now-already-existing newline? What if I manually add a newline (which will look like it's the only newline at the end of the last line, while in fact Kate had already added another one)? Will there be one or two newline when saving?
 How can I trust an editor that does not show me the contents of the thing I'm editing reliably? One thing is that it doesn't warn me when modifying upon saving because that obeys a setting, but not showing the current contents reliably is a huge bug. It is not just a feature that is missing.
Comment 5 Waqar Ahmed 2022-03-02 14:57:44 UTC
> What will happen if I go to the end of the file and add something at the very end? Will that be added after or before the now-already-existing newline?

It will be added where you add it.

> What if I manually add a newline (which will look like it's the only newline at the end of the last line, while in fact Kate had already added another one)? Will there be one or two newline when saving?

one.

You see, this is something that the author of the file doesn't need to worry about. If the setting is enabled, the new line will be added at the end if it doesn't exist. Probably working as designed. If you don't like the feature, disable it and add newlines whenever you want.

> How can I trust an editor that does not show me the contents of the thing I'm editing reliably?

I don't know. Maybe use another editor to see if it's 1:1, it works reliably for me during the 8+ hours I spend using it everyday.
Comment 6 Christoph Cullmann 2022-03-02 15:00:48 UTC
e.g. vim doesn't show trailing or no-trailing newline at the end of a file at all.
Even if it adds one.

But yes, it would be nicer to show it.

But no, in my eyes this is nothing urgent either.

You can disable that behavior.
It is normal to add the newline for most editors (beside mcedit), therefore the default seems OK, too.
Comment 7 Bug Janitor Service 2022-03-05 18:18:16 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/310
Comment 8 Christoph Cullmann 2022-03-05 21:07:48 UTC
Git commit 501f0b04178962e35248675882206449d7a1f8ea by Christoph Cullmann.
Committed on 05/03/2022 at 18:17.
Pushed by cullmann into branch 'master'.

add new line on save inside the buffer

this works like for stripping trailing spaces
one now has the correct content in the buffer after save

M  +0    -11   src/buffer/katetextbuffer.cpp
M  +0    -14   src/buffer/katetextbuffer.h
M  +0    -3    src/document/katebuffer.cpp
M  +31   -15   src/document/katedocument.cpp
M  +3    -2    src/document/katedocument.h

https://invent.kde.org/frameworks/ktexteditor/commit/501f0b04178962e35248675882206449d7a1f8ea