classification
Title: Code folding in IDLE
Type: enhancement Stage: resolved
Components: IDLE Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: GeeVye, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2019-08-18 17:46 by GeeVye, last changed 2019-08-20 09:08 by terry.reedy. This issue is now closed.

Messages (3)
msg349922 - (view) Author: George Zhang (GeeVye) * Date: 2019-08-18 17:46
Congrats on adding line numbers to IDLE.

With this change, a change to add code folding could be done more easily as the folding can reference the line numbers. Many other IDEs have code folding but IDLE should also have it.

Code folding could be done with a +/- to the right of the line numbers and can show/hide the indented suite under the header (compound statements). It should use indentation as tool to figure out which parts can be folded. Blank lines don't count. Single line compound statements cannot be folded.

Something like this:

1  - | def spam(ham):
2  - |     if ham:
3    |         eggs()
4    | 
5  + | def frob():
8    | 
9  - | FOO = (
10   |     1, 2, 3, 4,
11   |     5, 6, 7, 8,
12   | )
13   | 
14   | BAR = (
17   | )
18   | 
19   | if True: print("True")
20   |
msg349925 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-08-18 21:02
I am familiar with the feature in Notepad++ on Windows.  But I never use its +- code folding.  IDLE already has two different and, to me, generally better code folding features.

To get an outline view of a file's classes and functions, use File => Module Browser or its shortcut.  One can selectively expand any item with nested class and function definitions.  Such multilevel outline views are not possible with Notepad++, as [+] mean expands completely, not just nested definitions.  I suspect that this is generally true of IDEs with marginal -+.

[Module browser can still be improved, such as by processing the code in an editor buffer instead of on disk, and updating automatically, but that is a different issue.]

One can instead get a dynamic view of nested compound statement headers with Options => Code Context. Look at any file longer than the editor window with nested compound statements, such as idlelib/editor.py.  This feature is impossible with all or nothing -+ collapse.

There may be other use cases, but then we have a major technical problem.  Tk Text widgets do not support code folding.  They have no method to hide lines in their text buffers.  Nor, as far as I know, is there any workaround with a 'Hide' tag.  Tk fonts have no 'visible' option.  Font size 1 or -1 is not 0, and size 0 is interpreted as a some default, around 15.

So hidden lines would have to be deleted.  The easiest option might be a dictionary mapping tk line numbers to lists of deleted lines and another function and data structure to map tk line numbers to the line numbers needed for display. Doing this without introducing bugs would be non-trivial.

I would not consider this unless there were a strong use case that would greatly benefit *and* be used by beginners.

Silent Ghost> This is an IDLE issue.  If this were added, which is extremely unlikely, it would be backported, just as line numbers were.  (See PEP 434.) It does not really matter which versions are marked here by the OP.
msg349935 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-19 05:33
Terry, you've made a good case that this feature request should be closed.
History
Date User Action Args
2019-08-20 09:08:20terry.reedysetstatus: open -> closed
resolution: rejected
stage: resolved
2019-08-19 05:33:35rhettingersetnosy: + rhettinger
messages: + msg349935
2019-08-18 21:02:37terry.reedysetmessages: + msg349925
versions: + Python 3.7, Python 3.8
2019-08-18 20:44:54SilentGhostsetversions: - Python 3.7, Python 3.8
2019-08-18 17:47:01GeeVyesettype: enhancement
2019-08-18 17:46:51GeeVyecreate