Skip to content

Commit

Permalink
Addresses issue #30 where code folding is broken after using Ctrl-Alt…
Browse files Browse the repository at this point in the history
…-- to fold current code region.

Fixed minor issue where code range opened and closed on the same line is still marked as folded when using shortcuts
  • Loading branch information
thehogfather committed Nov 4, 2013
1 parent f3ce60c commit 8425ee2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
A code folding extension for [Brackets](https://github.com/adobe/brackets/) based on [CodeMirror's folding addon](http://codemirror.net/demo/folding.html).
Peforms code folding based on brace ({}, []) matching and multiline comments for javascript, json, css, php and less files. Also has some support for html and xml files based on tag matching.

Supports quickly collapsing and expanding all code regions for .css and .less files through context menu items.

Alt-Clicking on code regions collapses or expands all foldable child regions (as found in OSX Finder).
Holding down the Alt-key while clicking on code regions collapses or expands all foldable child regions (as found in OSX Finder).

### How to install
Navigate to **Brackets > File > Install Extension** and paste url https://github.com/thehogfather/brackets-code-folding
Expand All @@ -15,11 +13,11 @@ Navigate to **Brackets > File > Install Extension** and paste url https://github
3. Restart or Reload Brackets
4. Toggle the extension with **Brackets > View > Enable Code Folding**

### Shortcuts
### Keyboard shortcuts
Ctrl-Alt-- Collapse code region at current cursor position
Ctrl-Alt-+ Expand code region at current cursor position
Alt-1 Collapse all code region in current editor
Shift-Alt-1 Expand all code region in current editor
Alt-1 Collapse all code regions in current editor
Shift-Alt-1 Expand all code regions in current editor

### License
MIT-licensed.
Expand Down
2 changes: 1 addition & 1 deletion foldhelpers/foldcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ define(function (require, exports, module) {
range = getRange(false);
}
}
if (!range || range.cleared || force === "unfold") { return; }
if (!range || range.cleared || force === "unfold" || range.to.line - range.from.line < minSize) { return; }

var myWidget = makeWidget(options);
var myRange = cm.markText(range.from, range.to, {
Expand Down
38 changes: 26 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ define(function (require, exports, module) {
COLLAPSE = "codefolding.collapse",
EXPAND = "codefolding.expand",
EXPAND_ALL = "codefolding.expand.all",
_lineFolds = {};
_lineFolds = {};
ExtensionUtils.loadStyleSheet(module, "main.less");
///load cm folding code
require("foldhelpers/foldcode")();
Expand All @@ -42,11 +42,13 @@ define(function (require, exports, module) {
function _foldLine(cm, line) {
if (!cm.isFolded(line)) { cm.foldCode(line); }
}

//expands a line if not already expanded
function _expandLine(cm, line) {
if (cm.isFolded(line)) { cm.unfoldCode(line); }
}

//gets the linefolds saved for the current document in the preference store
function getLineFolds(path) {
if (!_prefs.getValue(path)) { _prefs.setValue(path, []); }
return _prefs.getValue(path);
Expand Down Expand Up @@ -80,6 +82,7 @@ define(function (require, exports, module) {

function onGutterClick(cm, line, gutter, event) {
var opts = cm.state.foldGutter.options;
if (opts && opts.range && opts.range.from.line !== line) { opts.range = undefined; }
if (gutter !== opts.gutter) { return; }
var editor = EditorManager.getActiveEditor(), range, i;
if (cm.isFolded(line)) {
Expand All @@ -93,7 +96,7 @@ define(function (require, exports, module) {
}
} else {
if (event.altKey) {
range = _lineFolds[editor.document.file.fullPath][line] || opts.rangeFinder(cm, CodeMirror.Pos(line));
range = opts.rangeFinder(cm, CodeMirror.Pos(line));
if (range) {
for (i = range.to.line; i >= range.from.line; i--) {
cm.foldCode(i, opts);
Expand All @@ -104,16 +107,19 @@ define(function (require, exports, module) {
}
}
}

/**
Collapses the code region nearest the current cursor position. Nearest is found by searching from the current line and moving up the document until an
opening code-folding region is found.
*/
function collapseCurrent() {
var editor = EditorManager.getFocusedEditor(), _editorLineFolds = editor && editor.document ? _lineFolds[editor.document.file.fullPath] : undefined;
var editor = EditorManager.getFocusedEditor();
if (editor) {
var cm = editor._codeMirror, opts = cm.state.foldGutter.options;
var cursor = editor.getCursorPos(), i;
if (opts.rangeFinder) {
//move cursor up until a collapsible line is found
for (i = cursor.line; i >= 0; i--) {
opts.range = _editorLineFolds && _editorLineFolds[i] ? _editorLineFolds[i] : opts.rangeFinder(cm, CodeMirror.Pos(i));
opts.range = opts.rangeFinder(cm, CodeMirror.Pos(i));
if (opts.range) {
cm.foldCode(i, opts);
editor.setCursorPos(i);
Expand All @@ -123,7 +129,9 @@ define(function (require, exports, module) {
}
}
}

/**
expands the code region at the current cursor position.
*/
function expandCurrent() {
var editor = EditorManager.getFocusedEditor();
if (editor) {
Expand Down Expand Up @@ -160,10 +168,11 @@ define(function (require, exports, module) {
}
}
}

function onActiveEditorChanged(event, current, previous) {
if (current && current._codeMirror.getOption("gutters").indexOf("CodeMirror-foldgutter") === -1) {
var cm = current._codeMirror, path = current.document.file.fullPath;

function registerHandlers(editor) {
var cm = editor._codeMirror;
if (cm) {
var path = editor.document.file.fullPath;
_lineFolds[path] = _lineFolds[path] || {};
cm.setOption("gutters", ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]);
cm.setOption("foldGutter", {
Expand All @@ -176,7 +185,12 @@ define(function (require, exports, module) {
cm.on("unfold", function (cm, from, to) {
delete _lineFolds[path][from.line];
});

}
}

function onActiveEditorChanged(event, current, previous) {
if (current && current._codeMirror.getOption("gutters").indexOf("CodeMirror-foldgutter") === -1) {
registerHandlers(current);
restoreLineFolds(current);
}

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.0",
"version": "0.1.1",
"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 8425ee2

Please sign in to comment.