Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
75b3ace
Added feature to enable code folding for arbitrary selections in the …
thehogfather Jul 30, 2015
798c8d0
Basic unit tests for code-folding extension
thehogfather Jul 31, 2015
210f0ba
Merge branch 'master' of https://github.com/adobe/brackets into code-…
thehogfather Jul 31, 2015
411ee7c
Merge branch 'master' of https://github.com/adobe/brackets into fold-…
thehogfather Jul 31, 2015
3523389
added test cases including enabling and disabling extension, and chec…
thehogfather Aug 3, 2015
574279a
Merge branch 'master' of https://github.com/adobe/brackets into code-…
thehogfather Aug 3, 2015
f601475
Merge commit '1e09fb219224cd80137a8bd1f0cb8326c6814010' into fold-sel…
thehogfather Aug 3, 2015
1eba0ae
Added nls Strings description for code-folding feature for making sel…
thehogfather Aug 5, 2015
ab8039f
added test for clearing correctness of clearing fold marks in editor
thehogfather Aug 5, 2015
25ae796
updated logic for restoring line folds to account for selection folds
thehogfather Aug 8, 2015
2e0adde
Merge branch 'master' of https://github.com/adobe/brackets into fold-…
thehogfather Aug 8, 2015
8e9dbee
Merge branch 'master' of https://github.com/adobe/brackets into code-…
thehogfather Aug 8, 2015
853b5d1
fix for the creation of multiple fold range widgets for a single coll…
thehogfather Aug 9, 2015
0f9ffba
Merge branch 'fold-selected' into code-folding-unit-tests
thehogfather Aug 9, 2015
eb419aa
documented and refactored restoreLineFolds function
thehogfather Aug 9, 2015
dd3be9f
documented and refactored restoreLineFolds function
thehogfather Aug 9, 2015
632bff4
Merge branch 'code-folding-unit-tests' into fold-selected
thehogfather Aug 11, 2015
0545f9d
fixed refresh rendering bug with selection based fold
thehogfather Aug 12, 2015
f1e4472
Revert "documented and refactored restoreLineFolds function"
thehogfather Aug 12, 2015
3603849
removed unit test files
thehogfather Aug 12, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/extensions/default/CodeFolding/Prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/*global define, brackets*/
define(function (require, exports, module) {
"use strict";

var ProjectManager = brackets.getModule("project/ProjectManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Strings = brackets.getModule("strings"),
Expand All @@ -20,7 +20,8 @@ define(function (require, exports, module) {
SAVE_FOLD_STATES = "Save fold states",
ALWAYS_USE_INDENT_FOLD = "Always use indent fold",
HIDE_FOLD_BUTTONS = "Hide fold triangles",
MAX_FOLD_LEVEL = "Max fold level";
MAX_FOLD_LEVEL = "Max fold level",
MAKE_SELECTIONS_FOLDABLE = "Makes selections foldable";

//default preference values
prefs.definePreference("enabled", "boolean", true,
Expand All @@ -35,7 +36,9 @@ define(function (require, exports, module) {
{name: HIDE_FOLD_BUTTONS, description: Strings.DESCRIPTION_CODE_FOLDING_HIDE_UNTIL_MOUSEOVER});
prefs.definePreference("maxFoldLevel", "number", 2,
{name: MAX_FOLD_LEVEL, description: Strings.DESCRIPTION_CODE_FOLDING_MAX_FOLD_LEVEL});

prefs.definePreference("makeSelectionsFoldable", "boolean", true,
{name: MAKE_SELECTIONS_FOLDABLE, description: MAKE_SELECTIONS_FOLDABLE});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Strings.* instead for the description so it's translateable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcelgerber sure thing! I shall update.


PreferencesManager.stateManager.definePreference(FOLDS_PREF_KEY, "object", {});

/**
Expand Down Expand Up @@ -75,7 +78,7 @@ define(function (require, exports, module) {

return ranges;
}

/**
* Returns a 'context' object for getting/setting project-specific view state preferences.
* Similar to code in MultiRangeInlineEditor._getPrefsContext()...
Expand Down
30 changes: 30 additions & 0 deletions src/extensions/default/CodeFolding/foldhelpers/foldSelected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Selection range helper for code folding.
* @author Patrick Oladimeji
* @date 31/07/2015 00:11:53
*/
/*global define*/
define(function (require, exports, module) {
"use strict";

/**
* This helper returns the start and end range represeting the current selection in the editor.
* @param {Object} cm The Codemirror instance
* @param {Object} start A Codemirror.Pos object {line, ch} representing the current line we are
* checking for fold ranges
* @returns {Object} The fold range, {from, to} representing the current selection.
*/
function SelectionFold(cm, start) {
if (!cm.somethingSelected()) {
return;
}

var from = cm.getCursor("from"),
to = cm.getCursor("to");
if (from.line === start.line) {
return {from: from, to: to};
}
}

module.exports = SelectionFold;
});
4 changes: 3 additions & 1 deletion src/extensions/default/CodeFolding/foldhelpers/foldcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ define(function (require, exports, module) {
};

/**
* Helper to combine an array of fold range finders into one
* Helper to combine an array of fold range finders into one. This goes through the
* list of fold helpers in the parameter arguments and returns the first non-null
* range found from calling the fold helpers in order.
*/
CodeMirror.registerHelper("fold", "combine", function () {
var funcs = Array.prototype.slice.call(arguments, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/extensions/default/CodeFolding/foldhelpers/foldgutter.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ define(function (require, exports, module) {
cm.off("gutterClick", old.onGutterClick);
cm.off("change", onChange);
cm.off("viewportChange", onViewportChange);
cm.off("cursorActivity", onViewportChange);

cm.off("fold", onFold);
cm.off("unfold", onUnFold);
cm.off("swapDoc", updateInViewport);
Expand All @@ -366,6 +368,7 @@ define(function (require, exports, module) {
cm.on("gutterClick", val.onGutterClick);
cm.on("change", onChange);
cm.on("viewportChange", onViewportChange);
cm.on("cursorActivity", onViewportChange);
cm.on("fold", onFold);
cm.on("unfold", onUnFold);
cm.on("swapDoc", updateInViewport);
Expand Down
34 changes: 19 additions & 15 deletions src/extensions/default/CodeFolding/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/*global define, $, brackets*/
define(function (require, exports, module) {
"use strict";

var CodeMirror = brackets.getModule("thirdparty/CodeMirror/lib/codemirror"),
Strings = brackets.getModule("strings"),
AppInit = brackets.getModule("utils/AppInit"),
Expand All @@ -56,7 +56,7 @@ define(function (require, exports, module) {
expandAllKeyMac = "Cmd-Shift-1";

ExtensionUtils.loadStyleSheet(module, "main.less");

// Load CodeMirror addons
brackets.getModule(["thirdparty/CodeMirror/addon/fold/brace-fold"]);
brackets.getModule(["thirdparty/CodeMirror/addon/fold/comment-fold"]);
Expand All @@ -67,12 +67,13 @@ define(function (require, exports, module) {
// e.g. collapsing all children when 'alt' key is pressed
var foldGutter = require("foldhelpers/foldgutter"),
foldCode = require("foldhelpers/foldcode"),
indentFold = require("foldhelpers/indentFold");


indentFold = require("foldhelpers/indentFold"),
selectionFold = require("foldhelpers/foldSelected");


/** Set to true when init() has run; set back to false after deinit() has run */
var _isInitialized = false;

/**
* Restores the linefolds in the editor using values fetched from the preference store
* Checks the document to ensure that changes have not been made (e.g., in a different editor)
Expand Down Expand Up @@ -262,7 +263,7 @@ define(function (require, exports, module) {
cm.refresh(); // force recomputing gutter width - .folding-enabled class affected linenumbers gutter
CodeMirror.defineOption("foldGutter", false, null);
}

/** Add gutter and restore saved expand/collapse state */
function enableFoldingInEditor(editor) {
if (editor._codeMirror.getOption("gutters").indexOf(GUTTER_NAME) === -1) {
Expand Down Expand Up @@ -300,7 +301,7 @@ define(function (require, exports, module) {
*/
function deinit() {
_isInitialized = false;

KeyBindingManager.removeBinding(collapseKey);
KeyBindingManager.removeBinding(expandKey);
KeyBindingManager.removeBinding(collapseAllKey);
Expand All @@ -314,7 +315,7 @@ define(function (require, exports, module) {
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).removeMenuItem(EXPAND);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).removeMenuItem(COLLAPSE_ALL);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).removeMenuItem(EXPAND_ALL);

EditorManager.off(".CodeFolding");
DocumentManager.off(".CodeFolding");
ProjectManager.off(".CodeFolding");
Expand All @@ -325,19 +326,22 @@ define(function (require, exports, module) {
removeGutter(editor);
});
}

/**
* Enable code-folding functionality
*/
function init() {
_isInitialized = true;

foldCode.init();
foldGutter.init();

// Many CodeMirror modes specify which fold helper should be used for that language. For a few that
// don't, we register helpers explicitly here. We also register a global helper for generic indent-based
// folding, which cuts across all languages if enabled via preference.
CodeMirror.registerGlobalHelper("fold", "selectionFold", function (mode, cm) {
return prefs.getSetting("makeSelectionsFoldable");
}, selectionFold);
CodeMirror.registerGlobalHelper("fold", "indent", function (mode, cm) {
return prefs.getSetting("alwaysUseIndentFold");
}, indentFold);
Expand All @@ -358,7 +362,7 @@ define(function (require, exports, module) {
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(EXPAND_ALL);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(COLLAPSE);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(EXPAND);

//register keybindings
KeyBindingManager.addBinding(COLLAPSE_ALL, [ {key: collapseAllKey}, {key: collapseAllKeyMac, platform: "mac"} ]);
KeyBindingManager.addBinding(EXPAND_ALL, [ {key: expandAllKey}, {key: expandAllKeyMac, platform: "mac"} ]);
Expand All @@ -370,7 +374,7 @@ define(function (require, exports, module) {
enableFoldingInEditor(editor);
});
}

/**
* Register change listener for the preferences file.
*/
Expand All @@ -394,7 +398,7 @@ define(function (require, exports, module) {
CommandManager.register(Strings.EXPAND_ALL, EXPAND_ALL, expandAll);
CommandManager.register(Strings.COLLAPSE_CURRENT, COLLAPSE, collapseCurrent);
CommandManager.register(Strings.EXPAND_CURRENT, EXPAND, expandCurrent);

if (prefs.getSetting("enabled")) {
init();
}
Expand Down