Skip to content

Commit

Permalink
+ enh: "Toggle Folds": Selection range (really) toggles folds.
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiKoHoff committed Apr 21, 2018
1 parent d28d7d7 commit e275fac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
81 changes: 42 additions & 39 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -8023,66 +8017,75 @@ 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());
// selection range must span at least two lines
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(); }
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down

0 comments on commit e275fac

Please sign in to comment.