Skip to content

Commit

Permalink
documented and refactored restoreLineFolds function
Browse files Browse the repository at this point in the history
  • Loading branch information
thehogfather committed Aug 9, 2015
1 parent 0f9ffba commit eb419aa
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/extensions/default/CodeFolding/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,33 @@ define(function (require, exports, module) {
* @param {Editor} editor the editor whose saved line folds should be restored
*/
function restoreLineFolds(editor) {
function isInViewStateSelection(range, selection) {
/**
* Checks if the range from and to Pos is the same as the selection start and end Pos
* @param {Object} range {from, to} where from and to are CodeMirror.Pos objects
* @param {Object} selection {start, end} where start and end are CodeMirror.Pos objects
* @returns {Boolean} true if the range and selection span the same region and false otherwise
*/
function rangeEqualsSelection(range, selection) {
return range.from.line === selection.start.line && range.from.ch === selection.start.ch &&
range.to.line === selection.end.line && range.to.ch === selection.end.ch;
}

/**
* Checks if the range is equal to one of the selections in the viewState
* @param {Object} range {from, to} where from and to are CodeMirror.Pos objects.
* @param {Object} viewState The current editor's ViewState object
* @returns {Boolean} true if the range is found in the list of selections or false if not.
*/
function isInViewStateSelection(range, viewState) {
if (!viewState || !viewState.selections) {
return false;
}

return viewState.selections.some(function (selection) {
return rangeEqualsSelection(range, selection);
});
}

var saveFolds = prefs.getSetting("saveFoldStates");
if (!editor || !saveFolds) {
return;
Expand All @@ -103,9 +125,7 @@ define(function (require, exports, module) {
var nonSelectionFolds = {}, selectionFolds = {}, range;
Object.keys(folds).forEach(function (line) {
range = folds[line];
if (viewState.selections && viewState.selections.some(function (selection) {
return isInViewStateSelection(range, selection);
})) {
if (isInViewStateSelection(range, viewState)) {
selectionFolds[line] = range;
} else {
nonSelectionFolds[line] = range;
Expand Down

0 comments on commit eb419aa

Please sign in to comment.