Bug 466351 - Wrong auto indentation of C code
Summary: Wrong auto indentation of C code
Status: RESOLVED INTENTIONAL
Alias: None
Product: kate
Classification: Applications
Component: indentation (other bugs)
Version First Reported In: 22.08.2
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-24 12:58 UTC by Martinus
Modified: 2024-06-18 17:42 UTC (History)
3 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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