From b16a887b6b948997a50b81bc32ab26c1e791ddbf Mon Sep 17 00:00:00 2001 From: Patrick Oladimeji Date: Sat, 16 Nov 2013 13:52:44 +0000 Subject: [PATCH] addresses issue #32 where line fold state is not being correctly persisted after modifying document. Consequently restoring line folds results in wrong folded regions. --- foldhelpers/xml-fold.js | 15 ++++++++------- main.js | 29 ++++++++++++++++++++++------- package.json | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/foldhelpers/xml-fold.js b/foldhelpers/xml-fold.js index ef7ef26..929d494 100644 --- a/foldhelpers/xml-fold.js +++ b/foldhelpers/xml-fold.js @@ -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; } @@ -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; @@ -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) { @@ -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; } } @@ -120,7 +121,8 @@ define(function (require, exports, module) { } } } - function findMatchingOpen(iter, tag) { + + function findMatchingOpen(iter, tag) { var stack = []; for (;;) { var prev = toPrevTag(iter); @@ -179,7 +181,6 @@ define(function (require, exports, module) { } }; - module.exports = function (cm, start) { var iter = new Iter(cm, start.line, 0); for (;;) { diff --git a/main.js b/main.js index 7dad563..d67f07e 100644 --- a/main.js +++ b/main.js @@ -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")(); @@ -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; } @@ -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) { diff --git a/package.json b/package.json index ce0b9ab..1ec9c70 100644 --- a/package.json +++ b/package.json @@ -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"],