Bug 468495 - KTextEditor (Kate, KWrite, KPart) new line on line start may eat text after tabs
Summary: KTextEditor (Kate, KWrite, KPart) new line on line start may eat text after tabs
Status: RESOLVED FIXED
Alias: None
Product: frameworks-ktexteditor
Classification: Frameworks and Libraries
Component: general (show other bugs)
Version: 5.105.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
: 469361 470006 471403 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-04-14 08:16 UTC by Mykola Krachkovsky
Modified: 2023-06-24 17:26 UTC (History)
5 users (show)

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


Attachments
Example file where bug manifests on 2nd&3rd lines (89 bytes, text/plain)
2023-04-14 08:16 UTC, Mykola Krachkovsky
Details
Patch without removing extra spaces (1003 bytes, patch)
2023-04-14 20:09 UTC, Mykola Krachkovsky
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mykola Krachkovsky 2023-04-14 08:16:00 UTC
Created attachment 158098 [details]
Example file where bug manifests on 2nd&3rd lines

SUMMARY
If line has tab indentation (work code styling) then pressing enter on line start (before tabs) may eat some characters from meaningful part of the line (after tabs).
I haven't discovered why it sometimes eats, sometimes keeps it fine.
How much characters would be eaten depends on tab count and tab width, loos like: (tab count) * (tab width - 1).

STEPS TO REPRODUCE
1. Create new file.
2. Add some text (2 lines is enough).
1-2. Or open file from attachment.
3. Place text cursor onto start of 2nd or 3rd line (1st isn't affected).
4. Press enter.

OBSERVED RESULT
New line without indentation is created and next line with indentation but meaningful part is corrupted — starting characters are dropped.

EXPECTED RESULT
New line without indentation and old line with indentation same as previous one. All meaningful characters preserved.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: openSUSE Tumbleweed 20230412
KDE Plasma Version: 5.27.4
KDE Frameworks Version: 5.105.0
Qt Version: 5.15.8+kde185

ADDITIONAL INFORMATION
All indentation modes are affected, but line has to start with tabs.
Seems doesn't depend on programming language (noticed in javascript file, but regular text files affected too): 'Tools' → 'Indentation' → 'Normal'/'C Style' — were checked, both affected.
Comment 1 Paul Worrall 2023-04-14 13:26:07 UTC
Can reproduce with Kate 22.12.3/FW 5.105.0; but not with Kate 22.08.2/FW 5.98.0
Comment 2 Waqar Ahmed 2023-04-14 18:40:44 UTC
can not reproduce with 5.104 but can reproduce with current qt6 master
Comment 3 Mykola Krachkovsky 2023-04-14 19:33:13 UTC
I think this commit made this behaviour:
(kf5) https://invent.kde.org/frameworks/ktexteditor/-/commit/f6e70dd237ff184097102c10be3a1dcde0c3ab76
(master) https://invent.kde.org/frameworks/ktexteditor/-/commit/431bdf7b40ffc385a6e52c884f8b2aa113901dde

This line looks suspicious:
`doc->editRemoveText(line, 0, indentDepth);`
before it was:
`doc->editRemoveText(line, 0, currentWhitespace.length());`

If indentDepth calculates tabs as tab width, then it removes all tabs and extra chars from line, explains why (tab count) * (tab width - 1), -1 — tab char itself.
Comment 4 Waqar Ahmed 2023-04-14 20:05:42 UTC
Git commit 89bf2451f4aeef3d787ff70eec531b9276d8b139 by Waqar Ahmed.
Committed on 14/04/2023 at 19:57.
Pushed by waqar into branch 'master'.

Fix indenting removes characters if line has tabs at start

M  +26   -0    autotests/src/katedocument_test.cpp
M  +1    -0    autotests/src/katedocument_test.h
M  +1    -1    src/utils/kateautoindent.cpp

https://invent.kde.org/frameworks/ktexteditor/commit/89bf2451f4aeef3d787ff70eec531b9276d8b139
Comment 5 Mykola Krachkovsky 2023-04-14 20:08:32 UTC
(In reply to Waqar Ahmed from comment #4)
> Git commit 89bf2451f4aeef3d787ff70eec531b9276d8b139 by Waqar Ahmed.
> Committed on 14/04/2023 at 19:57.
> Pushed by waqar into branch 'master'.
> 
> Fix indenting removes characters if line has tabs at start
> 
> M  +26   -0    autotests/src/katedocument_test.cpp
> M  +1    -0    autotests/src/katedocument_test.h
> M  +1    -1    src/utils/kateautoindent.cpp
> 
> https://invent.kde.org/frameworks/ktexteditor/commit/
> 89bf2451f4aeef3d787ff70eec531b9276d8b139

I've tried patch similar to that and it worked. But maybe it's better to not remove those spaces in the first place?
Comment 6 Mykola Krachkovsky 2023-04-14 20:09:10 UTC
Created attachment 158111 [details]
Patch without removing extra spaces

Like this patch?
Comment 7 Waqar Ahmed 2023-04-14 20:12:12 UTC
will work from a quick look. (patches not accepted here though)
Comment 8 Nate Graham 2023-04-15 02:46:44 UTC
Can the commit be backported to kf5?
Comment 9 Mykola Krachkovsky 2023-04-15 07:13:13 UTC
(In reply to Waqar Ahmed from comment #7)
> will work from a quick look. (patches not accepted here though)

I've created the merge request:
https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/541

I'm not insisting on merging it, change is subtle, though code looks cleaner to me.
Comment 10 Mykola Krachkovsky 2023-04-15 08:44:34 UTC
(In reply to Mykola Krachkovsky from comment #9)
> (In reply to Waqar Ahmed from comment #7)
> > will work from a quick look. (patches not accepted here though)
> 
> I've created the merge request:
> https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/541
> 
> I'm not insisting on merging it, change is subtle, though code looks cleaner
> to me.

revoked it.
Comment 11 Waqar Ahmed 2023-04-15 11:07:10 UTC
Git commit e767b59a91374be90a99e296aac406b6427c7932 by Waqar Ahmed.
Committed on 15/04/2023 at 11:06.
Pushed by waqar into branch 'kf5'.

Fix indenting removes characters if line has tabs at start
(cherry picked from commit 89bf2451f4aeef3d787ff70eec531b9276d8b139)

M  +24   -0    autotests/src/katedocument_test.cpp
M  +1    -0    autotests/src/katedocument_test.h
M  +1    -1    src/utils/kateautoindent.cpp

https://invent.kde.org/frameworks/ktexteditor/commit/e767b59a91374be90a99e296aac406b6427c7932
Comment 12 Waqar Ahmed 2023-04-15 11:08:52 UTC
picked to kf5 now
Comment 13 Waqar Ahmed 2023-05-05 01:46:53 UTC
*** Bug 469361 has been marked as a duplicate of this bug. ***
Comment 14 Paul Worrall 2023-05-19 17:50:11 UTC
*** Bug 470006 has been marked as a duplicate of this bug. ***
Comment 15 Paul Worrall 2023-06-24 14:37:29 UTC
*** Bug 471403 has been marked as a duplicate of this bug. ***