The attached images show two kinds of selections and the result of indenting them once and twice with tab. For the selection that starts and ends in text, the involved lines are indented and the selected text stays the same (selection start and end positions are adjusted with the indent). This is predictable and convenient. For the selection that starts or ends in leading whitespace, after one tab the involved lines are indented but the selection is changed. Further indents do not affect the last line which has been shifted once. This behaviour is awkward, when selecting a block of code to indent, one must make sure to start AND end the selection in text, otherwise the last line is affected inconsistently. Eg. in Python code this is very significant. Reproducible: Always KDE version is 4.12.0, Kate version is 3.12.0, Kate Part version is 3.7. The issue is always reproducible with both kwrite and kate. Old bug https://bugs.kde.org/show_bug.cgi?id=279910 sounds vaguely similar.
Created attachment 84272 [details] selection anchors in text, 0 tabs
Created attachment 84273 [details] selection anchors in text, 1 tab
Created attachment 84274 [details] selection anchors in text, 2 tabs
Created attachment 84275 [details] selection anchors in leading whitespace, 0 tabs
Created attachment 84276 [details] selection anchors in leading whitespace, 1 tab
Created attachment 84277 [details] selection anchors in leading whitespace, 2 tabs
Created attachment 85443 [details] Proposed fix This patch fixes the bug for me. When the selection end is in leading whitespace, it moves to the end of the leading whitespace and not the beginning of the line. For this bug both insertBehavior == ExpandRight and ExpandLeft | ExpandRight work fine, but the second is used, to not regress https://bugs.kde.org/show_bug.cgi?id=279910 .
HELLO !?
Hi, sorry we didn't take a look earlier, thought I am not sure if that change would not cause other regressions.
I have been able to reproduce this bug in a unit test. If I can, I'll propose a fix that only involves the indent/unindent part of the code.
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/623
Git commit 02d2aa2984a54fb63b586448b8d859a514ad285b by Christoph Cullmann, on behalf of Rémi Peuchot. Committed on 31/10/2023 at 16:30. Pushed by cullmann into branch 'kf5'. Fix selection shrink when indenting 'editRemoveText' indirectly triggers 'TextRange::checkValidity' which silently shrink the selection if the removed text is at the start or end of the given range. 'editInsertText' will insert text without expanding the selection. The fix consists of doing insertion before deletion so that the selection is not shrinked. (more details in the Merge Request) FIXED-IN:5.112 (cherry picked from commit 00f218cdf0adce8b0e0b96fd0107803946848e07) M +5 -2 src/utils/kateautoindent.cpp https://invent.kde.org/frameworks/ktexteditor/-/commit/02d2aa2984a54fb63b586448b8d859a514ad285b