Skip to content

Commit

Permalink
Add indentRangeFinder function to lib/util/foldcode.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Feb 13, 2012
1 parent 1aa4d6f commit 2b3fc13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions doc/manual.html
Expand Up @@ -712,8 +712,8 @@ <h2 id="addons">Add-ons</h2>
of <a href="#util_dialog"><code>openDialog</code></a> when
available to make prompting for search queries less ugly.</dd>
<dt id="util_foldcode"><a href="../lib/util/foldcode.js"><code>foldcode.js</code></a></dt>
<dd>Helps with code folding. See <a href="../demo/folding.html">the
demo</a> for an example.
<dd>Helps with code folding.
See <a href="../demo/folding.html">the demo</a> for an example.
Call <code>CodeMirror.newFoldFunction</code> with a range-finder
helper function to create a function that will, when applied to
a CodeMirror instance and a line number, attempt to fold or
Expand All @@ -722,7 +722,11 @@ <h2 id="addons">Add-ons</h2>
line number, and returns an end line for the block, or null if
no block is started on that line. This file
provides <code>CodeMirror.braceRangeFinder</code>, which finds
blocks in brace languages (JavaScript, C, Java, etc).</dd>
blocks in brace languages (JavaScript, C, Java,
etc), <code>CodeMirror.indentRangeFinder</code>, for languages
where indentation determines block structure (Python, Haskell),
and <code>CodeMirror.tagRangeFinder</code>, for XML-style
languages.</dd>
<dt id="util_runmode"><a href="../lib/util/runmode.js"><code>runmode.js</code></a></dt>
<dd>Can be used to run a CodeMirror mode over text without
actually opening an editor instance.
Expand Down
13 changes: 13 additions & 0 deletions lib/util/foldcode.js
Expand Up @@ -130,6 +130,19 @@ CodeMirror.braceRangeFinder = function(cm, line) {
return end;
};

CodeMirror.indentRangeFinder = function(cm, line) {
var tabSize = cm.getOption("tabSize");
var myIndent = cm.getLineHandle(line).indentation(tabSize), last;
for (var i = line + 1, end = cm.lineCount(); i < end; ++i) {
var handle = cm.getLineHandle(i);
if (!/^\s*$/.test(handle.text)) {
if (handle.indentation(tabSize) <= myIndent) break;
last = i;
}
}
if (!last) return null;
return last + 1;
};

CodeMirror.newFoldFunction = function(rangeFinder, markText) {
var folded = [];
Expand Down

0 comments on commit 2b3fc13

Please sign in to comment.