Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
51008 views
1
(function() {
2
var modes = ["clike", "css", "javascript"];
3
4
for (var i = 0; i < modes.length; ++i)
5
CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "});
6
7
function continueComment(cm) {
8
var pos = cm.getCursor(), token = cm.getTokenAt(pos);
9
if (token.type != "comment" || cm.getOption("disableInput")) return CodeMirror.Pass;
10
var mode = CodeMirror.innerMode(cm.getMode(), token.state).mode;
11
12
var insert;
13
if (mode.blockCommentStart && mode.blockCommentContinue) {
14
var end = token.string.indexOf(mode.blockCommentEnd);
15
var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
16
if (end != -1 && end == token.string.length - mode.blockCommentEnd.length && pos.ch >= end) {
17
// Comment ended, don't continue it
18
} else if (token.string.indexOf(mode.blockCommentStart) == 0) {
19
insert = full.slice(0, token.start);
20
if (!/^\s*$/.test(insert)) {
21
insert = "";
22
for (var i = 0; i < token.start; ++i) insert += " ";
23
}
24
} else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
25
found + mode.blockCommentContinue.length > token.start &&
26
/^\s*$/.test(full.slice(0, found))) {
27
insert = full.slice(0, found);
28
}
29
if (insert != null) insert += mode.blockCommentContinue;
30
}
31
if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) {
32
var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
33
if (found > -1) {
34
insert = line.slice(0, found);
35
if (/\S/.test(insert)) insert = null;
36
else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
37
}
38
}
39
40
if (insert != null)
41
cm.replaceSelection("\n" + insert, "end");
42
else
43
return CodeMirror.Pass;
44
}
45
46
function continueLineCommentEnabled(cm) {
47
var opt = cm.getOption("continueComments");
48
if (opt && typeof opt == "object")
49
return opt.continueLineComment !== false;
50
return true;
51
}
52
53
CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
54
if (prev && prev != CodeMirror.Init)
55
cm.removeKeyMap("continueComment");
56
if (val) {
57
var key = "Enter";
58
if (typeof val == "string")
59
key = val;
60
else if (typeof val == "object" && val.key)
61
key = val.key;
62
var map = {name: "continueComment"};
63
map[key] = continueComment;
64
cm.addKeyMap(map);
65
}
66
});
67
})();
68
69