Skip to content

Commit

Permalink
addresses issue #32 where line fold state is not being correctly pers…
Browse files Browse the repository at this point in the history
…isted after modifying document. Consequently restoring line folds results in wrong folded regions.
  • Loading branch information
thehogfather committed Nov 16, 2013
1 parent 8425ee2 commit b16a887
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
15 changes: 8 additions & 7 deletions foldhelpers/xml-fold.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
/*global define, d3, require, $, brackets, window, CodeMirror */
define(function (require, exports, module) {
"use strict";


var Pos = CodeMirror.Pos;
function cmp(a, b) { return a.line - b.line || a.ch - b.ch; }

Expand Down Expand Up @@ -37,7 +35,8 @@ define(function (require, exports, module) {
iter.text = iter.cm.getLine(++iter.line);
return true;
}
function prevLine(iter) {

function prevLine(iter) {
if (iter.line <= iter.min) { return; }
iter.text = iter.cm.getLine(--iter.line);
iter.ch = iter.text.length;
Expand All @@ -57,7 +56,8 @@ define(function (require, exports, module) {
return selfClose ? "selfClose" : "regular";
}
}
function toTagStart(iter) {

function toTagStart(iter) {
for (;;) {
var lt = iter.ch ? iter.text.lastIndexOf("<", iter.ch - 1) : -1;
if (lt == -1) {
Expand All @@ -83,7 +83,8 @@ define(function (require, exports, module) {
return found;
}
}
function toPrevTag(iter) {

function toPrevTag(iter) {
for (;;) {
var gt = iter.ch ? iter.text.lastIndexOf(">", iter.ch - 1) : -1;
if (gt == -1) { if (prevLine(iter)) { continue; } else { return; } }
Expand Down Expand Up @@ -120,7 +121,8 @@ define(function (require, exports, module) {
}
}
}
function findMatchingOpen(iter, tag) {

function findMatchingOpen(iter, tag) {
var stack = [];
for (;;) {
var prev = toPrevTag(iter);
Expand Down Expand Up @@ -179,7 +181,6 @@ define(function (require, exports, module) {
}
};


module.exports = function (cm, start) {
var iter = new Iter(cm, start.line, 0);
for (;;) {
Expand Down
29 changes: 22 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ define(function (require, exports, module) {
EXPAND = "codefolding.expand",
EXPAND_ALL = "codefolding.expand.all",
_lineFolds = {};

ExtensionUtils.loadStyleSheet(module, "main.less");
///load cm folding code
require("foldhelpers/foldcode")();
Expand Down Expand Up @@ -53,7 +52,24 @@ define(function (require, exports, module) {
if (!_prefs.getValue(path)) { _prefs.setValue(path, []); }
return _prefs.getValue(path);
}

/** gets the folded regions in the editor.
* @returns a map containing {linenumber: {from, to}}
*/
function getLineFoldsInEditor(editor) {
var cm = editor._codeMirror, i, folds = {};
if (cm) {
var marks = cm.getAllMarks();
marks.filter(function (m) {return m.__isFold; })
.forEach(function (mark) {
var range = mark.find();
if (range) {
folds[range.from.line] = range;
}
});
}
return folds;
}
/**Restores the linefolds in the editor using values fetched from the preference store*/
function restoreLineFolds(editor) {
var cm = editor._codeMirror, rangeFinder, foldFunc;
if (!cm) {return; }
Expand All @@ -68,16 +84,15 @@ define(function (require, exports, module) {
keys.forEach(function (lineNumber) {
cm.foldCode(+lineNumber, {range: folds[lineNumber]});
});
_lineFolds[path] = folds;
}
}

/**Saves the line folds in the editor using the preference storage**/
function saveLineFolds(editor) {
if (!editor) { return; }
var folds = getLineFoldsInEditor(editor);
var path = editor.document.file.fullPath;
var cm = editor._codeMirror;
if (cm && _lineFolds[path]) {
_prefs.setValue(path, _lineFolds[path]);
}
_prefs.setValue(path, folds);
}

function onGutterClick(cm, line, gutter, event) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brackets-code-folding",
"version": "0.1.1",
"version": "0.1.2",
"description": "Brackets extension that provides simple code folding for css, less, json, javascript, xml and html files.",
"keywords": [".js", ".json", "code folding", "code collapsing", ".html", ".xml", ".htm", ".php", ".css", ".less"],
"categories": ["formatting", "general", "editing", "visual"],
Expand Down

0 comments on commit b16a887

Please sign in to comment.