Bug 432573 - Code folding not working properly for Python
Summary: Code folding not working properly for Python
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: folding (other bugs)
Version First Reported In: 20.12.1
Platform: Manjaro Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-06 14:39 UTC by Rohit sardana
Modified: 2023-02-26 20:50 UTC (History)
2 users (show)

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


Attachments
Screenshot of part of the code Mentioned above (95.36 KB, image/png)
2021-02-06 14:39 UTC, Rohit sardana
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rohit sardana 2021-02-06 14:39:50 UTC
Created attachment 135464 [details]
Screenshot of part of the code Mentioned above

SUMMARY
Code folding for 'def' keyword in python, not working properly. Some instances of 'def' are left out of folding.

STEPS TO REPRODUCE
1. This part shows code folding,
def readable_bytes(n_bytes):
    symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n_bytes >= prefix[s]:
            value = int(float(n_bytes) / prefix[s])
            return '%s%s' % (value, s)
    return "%sB" % n_bytes
2. This part doesn't show code folding,
def get_processes_info(interval):

    global average_number
    global procs_cpu_average

    # Sleeps for "interval"
    time.sleep(interval)
    procs = []
    for p in psutil.process_iter():
        try:
            p.dict = p.as_dict([
                'pid',              # Unique ID
                'username',         # Owner
                'nice',             # Priority
                'memory_percent',   # Memory usage
                'cpu_percent',      # CPU usage
                'name',             # Description
                'status',           # For splitting processes according to their status
            ])
.....
....
def print_header(procs):

    global this_user
    global cpu_cores
    global header_message
    global average_number
    global procs_cpu_average
    global change_priority_status

    # Function that returns the no. of pipeline characters and space characters (max. of 40)
    def count_pipelines(perc):
        dashes = "|" * int((float(perc) / 10 * 4.1))
        empty_dashes = " " * (41 - len(dashes))
        return dashes + empty_dashes
...
....
...
3. Above code is from "bpytop" source code on 'Github.

OBSERVED RESULT
Code folding should recognise 'def' keyword in python.

EXPECTED RESULT
Other editors correctly show Code folding.

SOFTWARE/OS VERSIONS

 
Linux/KDE Plasma: KDE Plasma
(available in About System)
KDE Plasma Version: 5.20.5
KDE Frameworks Version: 5.78.0
Qt Version: 5.15.2


ADDITIONAL INFORMATION
Comment 1 pify 2021-02-20 17:45:16 UTC
The problem seems generic: as soon as there is an empty line between the line of code that defines a block (`def`, `if`...) and the code inside this block, the code fold is not proposed.


This code proposes 4 folds:

def test():
    code = ""

    if True:
        code = ""
    
    while False:
        code = ""

    test:
        something


This code does not offer any fold:

def test():

    code = ""

    if True:

        code = ""

    while False:

        code = ""

    test:

        something
Comment 2 Christoph Cullmann 2023-02-26 20:50:48 UTC
This is fixed now.

https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/451