Skip to content

Commit

Permalink
[comment-fold addon] Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr authored and marijnh committed Aug 19, 2013
1 parent 826d5cf commit 16541af
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
44 changes: 44 additions & 0 deletions addon/fold/comment-fold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CodeMirror.registerHelper("fold", "comment", function(cm, start) {
var mode = cm.getMode(), startToken = mode.blockCommentStart, endToken = mode.blockCommentEnd;
if (!startToken || !endToken) return;
var line = start.line, lineText = cm.getLine(line);
var startCh, tokenType;

function findOpening(openCh) {
for (var at = start.ch, pass = 0;;) {
var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
if (found == -1) {
if (pass == 1) break;
pass = 1;
at = lineText.length;
continue;
}
if (pass == 1 && found < start.ch) break;
tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
if (/^(comment)/.test(tokenType)) return found + 1;
at = found - 1;
}
}

var startCh = findOpening(startToken);
if (startCh == null) return;
var count = 1, lastLine = cm.lastLine(), end, endCh;
outer: for (var i = line; i <= lastLine; ++i) {
var text = cm.getLine(i), pos = i == line ? startCh : 0;
for (;;) {
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
if (nextOpen < 0) nextOpen = text.length;
if (nextClose < 0) nextClose = text.length;
pos = Math.min(nextOpen, nextClose);
if (pos == text.length) break;
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == tokenType) {
if (pos == nextOpen) ++count;
else if (!--count) { end = i; endCh = pos; break outer; }
}
++pos;
}
}
if (end == null || line == end && endCh == startCh) return;
return {from: CodeMirror.Pos(line, startCh),
to: CodeMirror.Pos(end, endCh)};
});
10 changes: 8 additions & 2 deletions demo/folding.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script src="../addon/fold/foldgutter.js"></script>
<script src="../addon/fold/brace-fold.js"></script>
<script src="../addon/fold/xml-fold.js"></script>
<script src="../addon/fold/comment-fold.js"></script>
<script src="../mode/javascript/javascript.js"></script>
<script src="../mode/xml/xml.js"></script>
<style type="text/css">
Expand Down Expand Up @@ -55,7 +56,10 @@ <h2>Code Folding Demo</h2>
<div style="max-width: 50em; margin-bottom: 1em">JavaScript:<br><textarea id="code" name="code"></textarea></div>
<div style="max-width: 50em">HTML:<br><textarea id="code-html" name="code-html"></textarea></div>
</form>
<script id="script">
<script id="script">
/*
* Demonstration of code folding
*/
window.onload = function() {
var te = document.getElementById("code");
var sc = document.getElementById("script");
Expand All @@ -69,7 +73,9 @@ <h2>Code Folding Demo</h2>
lineNumbers: true,
lineWrapping: true,
extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
foldGutter: true,
foldGutter: {
rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.brace, CodeMirror.fold.comment)
},
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
});
editor.foldCode(CodeMirror.Pos(8, 0));
Expand Down
6 changes: 5 additions & 1 deletion mode/xquery/xquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,11 @@ CodeMirror.defineMode("xquery", function() {
if (stream.eatSpace()) return null;
var style = state.tokenize(stream, state);
return style;
}
},

blockCommentStart: "(:",
blockCommentEnd: ":)"

};

});
Expand Down

0 comments on commit 16541af

Please sign in to comment.