| 1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 |
// Distributed under an MIT license: http://codemirror.net/LICENSE |
| 3 |
|
| 4 |
(function(mod) { |
| 5 |
if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 6 |
mod(require("../../lib/codemirror")); |
| 7 |
else if (typeof define == "function" && define.amd) // AMD |
| 8 |
define(["../../lib/codemirror"], mod); |
| 9 |
else // Plain browser env |
| 10 |
mod(CodeMirror); |
| 11 |
})(function(CodeMirror) { |
| 12 |
function continueComment(cm) { |
| 13 |
if (cm.getOption("disableInput")) return CodeMirror.Pass; |
| 14 |
var ranges = cm.listSelections(), mode, inserts = []; |
| 15 |
for (var i = 0; i < ranges.length; i++) { |
| 16 |
var pos = ranges[i].head |
| 17 |
if (!/\bcomment\b/.test(cm.getTokenTypeAt(pos))) return CodeMirror.Pass; |
| 18 |
var modeHere = cm.getModeAt(pos) |
| 19 |
if (!mode) mode = modeHere; |
| 20 |
else if (mode != modeHere) return CodeMirror.Pass; |
| 21 |
|
| 22 |
var insert = null; |
| 23 |
if (mode.blockCommentStart && mode.blockCommentContinue) { |
| 24 |
var line = cm.getLine(pos.line).slice(0, pos.ch) |
| 25 |
var end = line.lastIndexOf(mode.blockCommentEnd), found |
| 26 |
if (end != -1 && end == pos.ch - mode.blockCommentEnd.length) { |
| 27 |
// Comment ended, don't continue it |
| 28 |
} else if ((found = line.lastIndexOf(mode.blockCommentStart)) > -1 && found > end) { |
| 29 |
insert = line.slice(0, found) |
| 30 |
if (/\S/.test(insert)) { |
| 31 |
insert = "" |
| 32 |
for (var j = 0; j < found; ++j) insert += " " |
| 33 |
} |
| 34 |
} else if ((found = line.indexOf(mode.blockCommentContinue)) > -1 && !/\S/.test(line.slice(0, found))) { |
| 35 |
insert = line.slice(0, found) |
| 36 |
} |
| 37 |
if (insert != null) insert += mode.blockCommentContinue |
| 38 |
} |
| 39 |
if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) { |
| 40 |
var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment); |
| 41 |
if (found > -1) { |
| 42 |
insert = line.slice(0, found); |
| 43 |
if (/\S/.test(insert)) insert = null; |
| 44 |
else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0]; |
| 45 |
} |
| 46 |
} |
| 47 |
if (insert == null) return CodeMirror.Pass; |
| 48 |
inserts[i] = "\n" + insert; |
| 49 |
} |
| 50 |
|
| 51 |
cm.operation(function() { |
| 52 |
for (var i = ranges.length - 1; i >= 0; i--) |
| 53 |
cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert"); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
function continueLineCommentEnabled(cm) { |
| 58 |
var opt = cm.getOption("continueComments"); |
| 59 |
if (opt && typeof opt == "object") |
| 60 |
return opt.continueLineComment !== false; |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
CodeMirror.defineOption("continueComments", null, function(cm, val, prev) { |
| 65 |
if (prev && prev != CodeMirror.Init) |
| 66 |
cm.removeKeyMap("continueComment"); |
| 67 |
if (val) { |
| 68 |
var key = "Enter"; |
| 69 |
if (typeof val == "string") |
| 70 |
key = val; |
| 71 |
else if (typeof val == "object" && val.key) |
| 72 |
key = val.key; |
| 73 |
var map = {name: "continueComment"}; |
| 74 |
map[key] = continueComment; |
| 75 |
cm.addKeyMap(map); |
| 76 |
} |
| 77 |
}); |
| 78 |
}); |
| 79 |
|