Bug 352038

Summary: Indentation does not work as specified in the source formatter
Product: [Applications] kdevelop Reporter: Fikret Skrgic <skrgic>
Component: All editorsAssignee: kdevelop-bugs-null
Status: CONFIRMED ---    
Severity: normal CC: aspotashev, igorkuo
Priority: NOR    
Version First Reported In: 4.7.1   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Screenshot
Screenshot
Screenshot
Screenshot
Screenshot
Screenshot

Description Fikret Skrgic 2015-08-30 22:20:49 UTC
If one edits a formatting style and in the list of offered possible indentations selects "Class", it is shown correctly in the sample preview, but does not work the same way when actually editing code. If "Class" is selected, when editing the formatting style, in the preview the access modifiers (private, protected, public) get correctly indented. When actually editing code, they are never indented by the editor regardless of options selected. Moreover, if I indent it myself, as soon as I finished typing the access modifier and add a colon at the end, the editor immediately unindents it.

Reproducible: Always

Steps to Reproduce:
1. Pick a formatting style, change it, if necessary, so that class is indented and observe correct behaviour in the sample preview.
2. Save the style and select it.
3. Edit some code with an access modifier and observe that the editor does not follow the selected formatting style.

Actual Results:  
Access modifiers get unindented. Also, if one copies correctly indented code and pastes it, it gets all unindented.

Expected Results:  
The editor follows the selected formatting style.
Comment 1 Justin Zobel 2022-10-21 23:59:44 UTC
Thank you for reporting this bug in KDE software. As it has been a while since this issue was reported, can we please ask you to see if you can reproduce the issue with a recent software version?

If you can reproduce the issue, please change the status to "CONFIRMED" when replying. Thank you!
Comment 2 Fikret Skrgic 2022-10-23 08:55:28 UTC
(In reply to Justin Zobel from comment #1)
> Thank you for reporting this bug in KDE software. As it has been a while
> since this issue was reported, can we please ask you to see if you can
> reproduce the issue with a recent software version?
> 
> If you can reproduce the issue, please change the status to "CONFIRMED" when
> replying. Thank you!

It is still there and has been there for decades now.
Comment 3 Igor Kushnir 2022-10-23 13:58:36 UTC
Have you tried checking the *Override Kate Indentation Mode* checkbox on the Source Formatter tab of KDevelop settings?
Comment 4 Fikret Skrgic 2022-10-23 14:39:56 UTC
(In reply to Igor Kushnir from comment #3)
> Have you tried checking the *Override Kate Indentation Mode* checkbox on the
> Source Formatter tab of KDevelop settings?

It makes no difference.
Comment 5 Fikret Skrgic 2022-10-23 14:43:36 UTC
Created attachment 153141 [details]
Screenshot
Comment 6 Fikret Skrgic 2022-10-23 14:44:03 UTC
Created attachment 153142 [details]
Screenshot
Comment 7 Fikret Skrgic 2022-10-23 14:44:17 UTC
Created attachment 153143 [details]
Screenshot
Comment 8 Fikret Skrgic 2022-10-23 14:44:34 UTC
Created attachment 153144 [details]
Screenshot
Comment 9 Fikret Skrgic 2022-10-23 14:44:49 UTC
Created attachment 153145 [details]
Screenshot
Comment 10 Fikret Skrgic 2022-10-23 14:45:09 UTC
Created attachment 153146 [details]
Screenshot
Comment 11 Fikret Skrgic 2022-10-23 14:50:00 UTC
Screenshots 1 and 2 show the difference between selecting and not selecting class in the indentation. When class is selected, access modifiers should be indented. In the third you can see my style. In the fourth there is some code I will try to modify. In the fifth I type "protected" (no colon yet). In the sixth I add a colon and the text immediately gets shifted to the left.
Comment 12 Igor Kushnir 2022-10-23 15:07:48 UTC
Select the class's code, right click on the selection, trigger the Reformat Source action. Does the result agree with your formatting configuration?
Comment 13 Igor Kushnir 2022-10-23 15:20:18 UTC
Is there a Kate Modeline (https://kate-editor.org/2006/02/09/kate-modelines/) in the file you are editing?
Comment 14 Fikret Skrgic 2022-10-23 15:55:50 UTC
(In reply to Igor Kushnir from comment #12)
> Select the class's code, right click on the selection, trigger the Reformat
> Source action. Does the result agree with your formatting configuration?

Yes, if I do that, the code gets fixed.
Comment 15 Fikret Skrgic 2022-10-23 15:56:14 UTC
(In reply to Igor Kushnir from comment #13)
> Is there a Kate Modeline
> (https://kate-editor.org/2006/02/09/kate-modelines/) in the file you are
> editing?

No, there are no modelines.
Comment 16 Fikret Skrgic 2022-10-23 16:30:14 UTC
Another problem, which is probably related, and I would also be really happy if it was fixed. If in Settings -> Editor -> Editing -> Indentation, one selects to "adjust indentation of code pasted from the clipboard", it does not follow the source formatting style at all. This problem is also decades old. I have been using KDevelop since not much later than it came into existence and both these problems have always been there. So, there must be some formatting code, which supersedes, overrides, or bypasses the selected source formatting.
Comment 17 Igor Kushnir 2022-10-24 05:51:56 UTC
I confirm the issue. The pasting issue is probably of the same origin, seeing as it comes from the KTextEditor configuration common to KDevelop, Kate and KWrite. The *Override Kate Indentation Mode* checkbox should ideally prevent this issue. But currently this checkbox only affects tabs/spaces and indentation width: https://invent.kde.org/kdevelop/kdevelop/-/blob/4ffbedc9e97bb84bb242ad5f7b76b6a7153b5a4d/kdevplatform/shell/sourceformattercontroller.cpp#L654. The thing is, the underlying KTextEditor framework has its own formatting, which KDevelop needs to override. The KTextEditor formatting configuration is not as sophisticated as KDevelop's one.

You can try to improve the situation by changing the default indentation mode on the Editing->Indentation tab or configuring formatting on the Open/Save->Modes & Filetypes tab: use the Variables line edit and the Indentation mode combobox to configure KTextEditor's formatting for each Filetype you use. See the documentation: https://kate-editor.org/2006/02/09/kate-modelines/ Unfortunately this configuration is system-wide, applies to all files and not only in KDevelop, but in Kate and KWrite too. Consider also https://kate-editor.org/2006/02/09/kateconfig-files/

If you manage to work around this bug by configuring KTextEditor, consider contributing improvements to the adaptEditorIndentationMode() function I have linked. This way KDevelop's per-session or per-project formatting configuration would automatically override more KTextEditor formatting options. Though I expect that making your workaround generally useful could prove to be a challenging and time-consuming task, because the current formatting configuration has to be extracted from various source formatters before it can be applied to KTextEditor.

Another potential fix in KDevelop is intercepting KTextEditor's formatting action (such as unindenting the protected: label) and reformatting the affected source code range with KDevelop's formatter instead (or reformat after each KTextEditor's formatting action).