| 1 |
(function() { |
| 2 |
var modes = ["clike", "css", "javascript"]; |
| 3 |
for (var i = 0; i < modes.length; ++i) |
| 4 |
CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "}); |
| 5 |
|
| 6 |
function continueComment(cm) { |
| 7 |
var pos = cm.getCursor(), token = cm.getTokenAt(pos); |
| 8 |
if (token.type != "comment") return CodeMirror.Pass; |
| 9 |
var mode = CodeMirror.innerMode(cm.getMode(), token.state).mode; |
| 10 |
|
| 11 |
var insert; |
| 12 |
if (mode.blockCommentStart && mode.blockCommentContinue) { |
| 13 |
var end = token.string.indexOf(mode.blockCommentEnd); |
| 14 |
var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found; |
| 15 |
if (end != -1 && end == token.string.length - mode.blockCommentEnd.length) { |
| 16 |
// Comment ended, don't continue it |
| 17 |
} else if (token.string.indexOf(mode.blockCommentStart) == 0) { |
| 18 |
insert = full.slice(0, token.start); |
| 19 |
if (!/^\s*$/.test(insert)) { |
| 20 |
insert = ""; |
| 21 |
for (var i = 0; i < token.start; ++i) insert += " "; |
| 22 |
} |
| 23 |
} else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 && |
| 24 |
found + mode.blockCommentContinue.length > token.start && |
| 25 |
/^\s*$/.test(full.slice(0, found))) { |
| 26 |
insert = full.slice(0, found); |
| 27 |
} |
| 28 |
if (insert != null) insert += mode.blockCommentContinue; |
| 29 |
} |
| 30 |
if (insert == null && mode.lineComment) { |
| 31 |
var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment); |
| 32 |
if (found > -1) { |
| 33 |
insert = line.slice(0, found); |
| 34 |
if (/\S/.test(insert)) insert = null; |
| 35 |
else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0]; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if (insert != null) |
| 40 |
cm.replaceSelection("\n" + insert, "end"); |
| 41 |
else |
| 42 |
return CodeMirror.Pass; |
| 43 |
} |
| 44 |
|
| 45 |
CodeMirror.defineOption("continueComments", null, function(cm, val, prev) { |
| 46 |
if (prev && prev != CodeMirror.Init) |
| 47 |
cm.removeKeyMap("continueComment"); |
| 48 |
if (val) { |
| 49 |
var map = {name: "continueComment"}; |
| 50 |
map[typeof val == "string" ? val : "Enter"] = continueComment; |
| 51 |
cm.addKeyMap(map); |
| 52 |
} |
| 53 |
}); |
| 54 |
})(); |
| 55 |
|