From e275fac1c9bd455a152e24d6e0e3635400eee6aa Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sat, 21 Apr 2018 12:11:40 +0200 Subject: [PATCH] + enh: "Toggle Folds": Selection range (really) toggles folds. --- src/Edit.c | 81 ++++++++++++++++++++++++++++-------------------------- src/Edit.h | 8 +++--- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index b9615e2ab..383a2cee8 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -7975,16 +7975,10 @@ int FileVars_GetEncoding(LPFILEVARS lpfv) { #define FOLD_CHILDREN SCMOD_CTRL #define FOLD_SIBLINGS SCMOD_SHIFT -bool __stdcall FoldToggleNode(DocLn ln, FOLD_ACTION action) +bool __forceinline _FoldToggleNode(DocLn ln, FOLD_ACTION action) { - const bool fExpanded = SciCall_GetFoldExpanded(ln); - - if ((action == FOLD && fExpanded) || (action == EXPAND && !fExpanded)) - { - SciCall_ToggleFold(ln); - return true; - } - else if (action == SNIFF) + bool const fExpanded = SciCall_GetFoldExpanded(ln); + if ((action == SNIFF) || ((action == FOLD) && fExpanded) || ((action == EXPAND) && !fExpanded)) { SciCall_ToggleFold(ln); return true; @@ -8023,23 +8017,24 @@ void __stdcall EditFoldPerformAction(DocLn ln, int mode, FOLD_ACTION action) if (lv < lvStop || (lv == lvStop && fHeader && ln != lnNode)) return; else if (fHeader && (lv == lvNode || (lv > lvNode && mode & FOLD_CHILDREN))) - FoldToggleNode(ln, action); + _FoldToggleNode(ln, action); } } else { - FoldToggleNode(ln, action); + _FoldToggleNode(ln, action); } } void EditToggleFolds(FOLD_ACTION action, bool bForceAll) { - static FOLD_ACTION sLastAction = EXPAND; + static FOLD_ACTION sbLastSniffAllAction = EXPAND; + bool bAllLines = true; DocLn iStartLine = 0; DocLn iEndLine = SciCall_GetLineCount() - 1; - if (!bForceAll && !SciCall_IsSelectionEmpty()) + if (!bForceAll && !SciCall_IsSelectionEmpty()) { DocLn const iBegLn = SciCall_LineFromPosition(SciCall_GetSelectionStart()); DocLn const iEndLn = SciCall_LineFromPosition(SciCall_GetSelectionEnd()); @@ -8047,42 +8042,50 @@ void EditToggleFolds(FOLD_ACTION action, bool bForceAll) if (iBegLn != iEndLn) { iStartLine = iBegLn; iEndLine = iEndLn; + bAllLines = false; } } - if (action == SNIFF) - { - int cntFolded = 0; - int cntExpanded = 0; - for (DocLn ln = iStartLine; ln <= iEndLine; ++ln) - { - if (SciCall_GetFoldLevel(ln) & SC_FOLDLEVELHEADERFLAG) + bool fToggled = false; + + if (bAllLines) { + if (action == SNIFF) { + // determine majority to find action for all + int cntFolded = 0; + int cntExpanded = 0; + for (DocLn ln = iStartLine; ln <= iEndLine; ++ln) { - if (SciCall_GetLastChild(ln,-1) != ln) + if (SciCall_GetFoldLevel(ln) & SC_FOLDLEVELHEADERFLAG) { - if (SciCall_GetFoldExpanded(ln)) - ++cntExpanded; - else - ++cntFolded; + if (SciCall_GetLastChild(ln, -1) != ln) + { + if (SciCall_GetFoldExpanded(ln)) + ++cntExpanded; + else + ++cntFolded; + } } } + if (cntFolded == cntExpanded) { + action = (sbLastSniffAllAction == FOLD) ? EXPAND : FOLD; + } + else { + action = (cntFolded < cntExpanded) ? FOLD : EXPAND; + } + sbLastSniffAllAction = action; } - if (cntFolded == cntExpanded) { - action = (sLastAction == FOLD) ? EXPAND : FOLD; - } - else { - action = (cntFolded < cntExpanded) ? FOLD : EXPAND; - } - sLastAction = action; - } - bool fToggled = false; - - for (DocLn ln = iStartLine; ln <= iEndLine; ++ln) + SciCall_FoldAll(action); + fToggled = true; + } + else // in selection { - if (SciCall_GetFoldLevel(ln) & SC_FOLDLEVELHEADERFLAG) + for (DocLn ln = iStartLine; ln <= iEndLine; ++ln) { - if (FoldToggleNode(ln, action)) { fToggled = true; } + if (SciCall_GetFoldLevel(ln) & SC_FOLDLEVELHEADERFLAG) + { + if (_FoldToggleNode(ln, action)) { fToggled = !fToggled ? true : false; } + } } } if (fToggled) { SciCall_ScrollCaret(); } @@ -8167,7 +8170,7 @@ void EditFoldAltArrow(FOLD_MOVE move, FOLD_ACTION action) if (SciCall_GetFoldLevel(ln) & SC_FOLDLEVELHEADERFLAG) { if (action != SNIFF) { - FoldToggleNode(ln, action); + _FoldToggleNode(ln, action); } } } diff --git a/src/Edit.h b/src/Edit.h index 063a3bc0c..15eb5b0af 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -157,9 +157,9 @@ int FileVars_GetEncoding(LPFILEVARS); // Folding Functions // typedef enum { - EXPAND = 1, - SNIFF = 0, - FOLD = -1 + FOLD = SC_FOLDACTION_CONTRACT, + EXPAND = SC_FOLDACTION_EXPAND, + SNIFF = SC_FOLDACTION_TOGGLE } FOLD_ACTION; typedef enum { @@ -168,7 +168,7 @@ typedef enum { DOWN = 1 } FOLD_MOVE; -void EditToggleFolds(FOLD_ACTION, bool); +void EditToggleFolds(FOLD_ACTION,bool); void EditFoldClick(DocLn, int); void EditFoldAltArrow(FOLD_MOVE, FOLD_ACTION);