Bug 466351

Summary: Wrong auto indentation of C code
Product: [Applications] kate Reporter: Martinus <mhamant>
Component: indentationAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED INTENTIONAL    
Severity: normal CC: christoph, mhamant, waqar.17a
Priority: NOR    
Version First Reported In: 22.08.2   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Martinus 2023-02-24 12:58:11 UTC
SUMMARY
Wrong auto indentation of C code


STEPS TO REPRODUCE
Take this snippet (which pass compilation) and select C code style in the bottom-right corner drop-down

void test()
{int a, b; char c;
a = 0;b = 1;
c = 'f';}

void test2()
{int a, b;
a = 1;b = 3;}


void main() {
}

Highlight the text then use Selection > Format Indentation

OBSERVED RESULT

Oddly, indentations are introduced like if the closing function bracket was not taken into account:

void test()
{int a, b; char c;
    a = 0;b = 1;
    c = 'f';}
    
    void test2()
    {int a, b;
        a = 1;b = 3;}
        
        
        void main() {
        }

EXPECTED RESULT

No extra indentation introduced (ie, the original indentation is kept)

SOFTWARE/OS VERSIONS
Operating System: Kubuntu 22.10
KDE Plasma Version: 5.25.5
KDE Frameworks Version: 5.98.0
Qt Version: 5.15.6
Kernel Version: 5.19.0-31-generic (64-bit)
Graphics Platform: X11
Comment 1 Waqar Ahmed 2023-11-20 12:12:29 UTC
This is somewhat complex for the built-in indenter to handle correctly. Please use our Format plugin instead which uses clang-format under the hood.
Comment 2 Christoph Cullmann 2024-06-18 17:42:57 UTC
Yes, I am sorry that this will not work as intended, but the indenter is just some heuristics.
For complex reformatting better use the format plugin.
If you like to improve the indenter, the code is js, help is welcome

https://invent.kde.org/frameworks/ktexteditor/-/blob/master/src/script/data/indentation/cstyle.js?ref_type=heads