forked from codemirror/codemirror5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
826d5cf
commit 16541af
Showing
3 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters