Skip to content

Commit

Permalink
Merge pull request ipython-contrib#908 from juhasch/fix/codefolding-i…
Browse files Browse the repository at this point in the history
…ndent

Update codefolding extension
  • Loading branch information
juhasch committed Mar 11, 2017
2 parents 57859ec + f820437 commit 9c1e5c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
65 changes: 47 additions & 18 deletions src/jupyter_contrib_nbextensions/nbextensions/codefolding/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,45 @@ define([
], function(IPython, $, require, events, configmod, utils, codecell, codemirror) {
"use strict";

var HOTKEY = 'Alt-F';
var foldingKey = { HOTKEY : toggleFolding };

var base_url = utils.get_body_data("baseUrl");
var config = new configmod.ConfigSection('notebook', {base_url: base_url});
config.load();

config.loaded.then(function() {
if (config.data.hasOwnProperty('codefolding_hotkey') ){
HOTKEY = config.data.codefolding_hotkey;
foldingKey = {};
foldingKey[HOTKEY] = toggleFolding;
// define default config parameter values
var params = {
codefolding_hotkey : 'Ctrl-x',
};

// updates default params with any specified in the server's config
var update_params = function() {
for (var key in params){
if (config.data.hasOwnProperty(key) ){
params[key] = config.data[key];
}
}
};

config.loaded.then(function() {
update_params();

// register actions with ActionHandler instance
var prefix = 'auto';
var name = 'toggle-codefolding';
var action = {
icon: 'fa-comment-o',
help : 'Toggle codefolding',
help_index : 'ec',
id : 'toggle_codefolding',
handler : toggleFolding
};
var action_full_name = IPython.keyboard_manager.actions.register(action, name, prefix);

// define keyboard shortcuts
var edit_mode_shortcuts = {};
edit_mode_shortcuts[params.codefolding_hotkey] = action_full_name;

// register keyboard shortcuts with keyboard_manager
IPython.notebook.keyboard_manager.edit_shortcuts.add_shortcuts(edit_mode_shortcuts);
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcuts(edit_mode_shortcuts);
});

/*
Expand All @@ -46,8 +72,12 @@ define([
* @param cm CodeMirror instance
*
*/
function toggleFolding(cm) {
var pos = cm.getCursor();
function toggleFolding() {
var cm = IPython.notebook.get_selected_cell().code_mirror;
var pos = {line: 0, ch: 0, xRel: 0};
if (IPython.notebook.mode === 'edit') {
pos = cm.getCursor();
}
var opts = cm.state.foldGutter.options;
cm.foldCode(pos, opts.rangeFinder);
}
Expand Down Expand Up @@ -105,14 +135,12 @@ define([
*/
function cellFolding(cell) {
if (CodeMirror.fold != undefined) {
var keys = cell.code_mirror.getOption('extraKeys');
cell.code_mirror.setOption('extraKeys', collect(keys, foldingKey ));
var mode = cell.code_mirror.getOption('mode');
/* use indent folding in Python */
if (mode.name == 'ipython' ) {
cell.code_mirror.setOption('foldGutter',{rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.firstline, CodeMirror.fold.magic, CodeMirror.fold.indent) });
var mode = cell.code_mirror.getMode();
/* set indent or brace folding */
if (mode.fold === 'indent' ) {
cell.code_mirror.setOption('foldGutter',{rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.firstline, CodeMirror.fold.magic, CodeMirror.fold.indent) });
} else {
cell.code_mirror.setOption('foldGutter',{rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.firstline, CodeMirror.fold.magic, CodeMirror.fold.brace) });
cell.code_mirror.setOption('foldGutter',{rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.firstline, CodeMirror.fold.magic, CodeMirror.fold.brace) });
}
var gutters = cell.code_mirror.getOption('gutters');
var found = jQuery.inArray("CodeMirror-foldgutter", gutters);
Expand Down Expand Up @@ -201,6 +229,7 @@ define([
require(['./firstline-fold', './magic-fold'], initGutter);
})
}
config.load()
};

return {load_ipython_extension : load_extension};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ Codefolding

This extension adds codefolding functionality from CodeMirror to a codecell.

After clicking on the gutter (left margin of codecell) or typing `Alt+F`, the code gets folded. See the examples below. The folding status is saved in the cell metadata of the notebook, so reloading of a notebook will restore the folding view.
In edit mode, clicking on the triangle in the gutter (left margin of codecell) or typing the codefolding hotkey
(default is `Alt+F`), folds the code.
In command mode, the folding hotkey relates to the first line of the codecell.

See the examples below. The folding status is saved in the cell metadata of the notebook, so reloading of a notebook will restore the folding view.

Supported modes
---------------
Expand Down Expand Up @@ -52,6 +56,7 @@ Folded:
![](magic-folded.png)



Internals
---------

Expand All @@ -65,6 +70,9 @@ cell.metadata.code_folding = [ 3, 20, 33 ]

When reloading the notebook, the folding status is restored.

The codefolding hotkey can be customized using the notebook extensions configurator.
The settings are stored as `"codefolding_hotkey": "alt-f"` in `the notebook.json` configuration file.


Exporting
---------
Expand Down

0 comments on commit 9c1e5c9

Please sign in to comment.