5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
13
var repeat = String.prototype.repeat || function (n) { return Array(n + 1).join(this); };
14
function continueComment(cm) {
15
if (cm.getOption("disableInput")) return CodeMirror.Pass;
16
var ranges = cm.listSelections(), mode, inserts = [];
17
for (var i = 0; i < ranges.length; i++) {
18
var pos = ranges[i].head
19
if (!/\bcomment\b/.test(cm.getTokenTypeAt(pos))) return CodeMirror.Pass;
20
var modeHere = cm.getModeAt(pos)
21
if (!mode) mode = modeHere;
22
else if (mode != modeHere) return CodeMirror.Pass;
24
var insert = null, line, found;
25
var blockStart = mode.blockCommentStart, lineCmt = mode.lineComment;
26
if (blockStart && mode.blockCommentContinue) {
27
line = cm.getLine(pos.line);
28
var end = line.lastIndexOf(mode.blockCommentEnd, pos.ch - mode.blockCommentEnd.length);
31
if (end != -1 && end == pos.ch - mode.blockCommentEnd.length ||
32
lineCmt && (found = line.lastIndexOf(lineCmt, pos.ch - 1)) > -1 &&
33
/\bcomment\b/.test(cm.getTokenTypeAt({line: pos.line, ch: found + 1}))) {
35
} else if (pos.ch >= blockStart.length &&
36
(found = line.lastIndexOf(blockStart, pos.ch - blockStart.length)) > -1 &&
40
if (nonspaceAfter(0, line) >= found) {
41
insert = line.slice(0, found);
43
var tabSize = cm.options.tabSize, numTabs;
44
found = CodeMirror.countColumn(line, found, tabSize);
45
insert = !cm.options.indentWithTabs ? repeat.call(" ", found) :
46
repeat.call("\t", (numTabs = Math.floor(found / tabSize))) +
47
repeat.call(" ", found - tabSize * numTabs);
49
} else if ((found = line.indexOf(mode.blockCommentContinue)) > -1 &&
51
found <= nonspaceAfter(0, line)) {
52
insert = line.slice(0, found);
54
if (insert != null) insert += mode.blockCommentContinue
56
if (insert == null && lineCmt && continueLineCommentEnabled(cm)) {
57
if (line == null) line = cm.getLine(pos.line);
58
found = line.indexOf(lineCmt);
60
if (!pos.ch && !found) insert = "";
62
else if (found > -1 && nonspaceAfter(0, line) >= found) {
64
insert = nonspaceAfter(pos.ch, line) > -1;
67
var next = cm.getLine(pos.line + 1) || '',
68
nextFound = next.indexOf(lineCmt);
69
insert = nextFound > -1 && nonspaceAfter(0, next) >= nextFound || null;
72
insert = line.slice(0, found) + lineCmt +
73
line.slice(found + lineCmt.length).match(/^\s*/)[0];
77
if (insert == null) return CodeMirror.Pass;
78
inserts[i] = "\n" + insert;
81
cm.operation(function() {
82
for (var i = ranges.length - 1; i >= 0; i--)
83
cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
87
function nonspaceAfter(ch, str) {
88
nonspace.lastIndex = ch;
89
var m = nonspace.exec(str);
90
return m ? m.index : -1;
93
function continueLineCommentEnabled(cm) {
94
var opt = cm.getOption("continueComments");
95
if (opt && typeof opt == "object")
96
return opt.continueLineComment !== false;
100
CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
101
if (prev && prev != CodeMirror.Init)
102
cm.removeKeyMap("continueComment");
105
if (typeof val == "string")
107
else if (typeof val == "object" && val.key)
109
var map = {name: "continueComment"};
110
map[key] = continueComment;