ProgerXP / Notepad2e

Word highlighting, simultaneous editing, split views, math evaluation, un/grep, comment reformatting, UAC elevation, complete regexps (PCRE), Lua lexers, DPI awareness and more (XP+)
Other
372 stars 52 forks source link

New command: Show Outline #432

Closed ProgerXP closed 1 year ago

ProgerXP commented 1 year ago

New command and button (inserted by default after Print, see #431), titled Show Outline, hotkey F10. Shows a dialog similar to Encoding: titled Outline, with 2 buttons and no label text on top, but wider: default width equals width of the main window minus 32 from left and 32 from right, or 300 pixels (whatever is larger).

The list has no icons. The list is populated based on Scintilla's code folding data: given the folding tree, locate line with the caret and take all nodes starting from the immediate parent up to document root. Place every node in the F10 list as textual content of its line* (capped to 1000 characters per line), prefixed with line number, then 2 spaces, then Tab (spaces are necessary to ensure minimal spacing). The list is using regular dialog font, not monospace since it's short.

* Line to be included is subject to this algorithm: begin with the line of the region start, count its non-whitespace symbols, if >= 10 then stop and add this line to the list, else decrement line number, check if it equals line number of the preceding region or <0, if so then stop and add the original line to the list, else loop. In simpler terms, locate the first line before the beginning of the region with at least 10 non-whitespace symbols.

As a result, the list will show first lines of every folding region that is parent of the current line. If the current line itself is a beginning of some folding region, the list does not include it (since navigating to the same line is useless).

OK button navigates to the currently selected item by putting the caret after indentation (leading whitespace). On display, last item in the list is selected (take note of https://github.com/ProgerXP/Notepad2e/issues/240#issuecomment-605075050).

Since maintaining folding regions may cause lags whenever caret is moved and since F10 isn't used too often, it is suggested that automatic tracking of regions by Scintilla (on any document change) is disabled, at least on "large" documents. Rather, construction of folding regions happens only when the command is called. In this case, if no regions were found upon execution, the command should display standard Notepad2 message box saying This line is not part of any block.. Otherwise, if folding is maintained then the command should be disabled if calling it would produce the message.


For example, given this document:

https://github.com/ProgerXP/Notepad2e/blob/4cffb6cb16b7ef24855873be912b67bde7bafdbf/src/Edit.c#L581-L589

With caret on line 589, F10 would produce a list with two items:

581  int EditDetectEOLMode(HWND hwnd, char *lpData, DWORD cbData)
588    while (*cp && (*cp != '\x0D' && *cp != '\x0A'))

Note that the first entry is line 581, not 582 even though 582 is what Scintilla indicates as start of the region.

In case there were another line with sole { between lines 581 and 582, the list would include such a line:

int EditDetectEOLMode(HWND hwnd, char *lpData, DWORD cbData)
{
..{   // scan up, find "{", determine it's part of another region, give up and insert "..{" in the list

New companion command: Go Above (Shift+F10), no toolbar button. Works as if user has opened F10 and pressed OK (on the last list item). Disabled when F10 is disabled. Shows the same message as F10.

I see Shift+F10 currently invokes selection context menu. Is it a Notepad2 or Scintilla feature?

Add both commands to the end of View, after ---. Also, add new --- before File > Open and File > Save.

ProgerXP commented 1 year ago

I see Shift+F10 currently invokes selection context menu. Is it a Notepad2 or Scintilla feature?

This is actually a standard Windows hotkey.

Memo: multi-line function calls also create folding points, subject to Alt+[/] (https://github.com/ProgerXP/Notepad2e/issues/441#issuecomment-1457708997).

cshnik commented 12 months ago