Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / comment / continuecomment.js
1 (function(mod) {
2 if (typeof exports == "object" && typeof module == "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define == "function" && define.amd) // AMD
5 define(["../../lib/codemirror"], mod);
6 else // Plain browser env
7 mod(CodeMirror);
8 })(function(CodeMirror) {
9 var modes = ["clike", "css", "javascript"];
10
11 for (var i = 0; i < modes.length; ++i)
12 CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "});
13
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, token = cm.getTokenAt(pos);
19 if (token.type != "comment") return CodeMirror.Pass;
20 var modeHere = CodeMirror.innerMode(cm.getMode(), token.state).mode;
21 if (!mode) mode = modeHere;
22 else if (mode != modeHere) return CodeMirror.Pass;
23
24 var insert = null;
25 if (mode.blockCommentStart && mode.blockCommentContinue) {
26 var end = token.string.indexOf(mode.blockCommentEnd);
27 var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
28 if (end != -1 && end == token.string.length - mode.blockCommentEnd.length && pos.ch >= end) {
29 // Comment ended, don't continue it
30 } else if (token.string.indexOf(mode.blockCommentStart) == 0) {
31 insert = full.slice(0, token.start);
32 if (!/^\s*$/.test(insert)) {
33 insert = "";
34 for (var j = 0; j < token.start; ++j) insert += " ";
35 }
36 } else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
37 found + mode.blockCommentContinue.length > token.start &&
38 /^\s*$/.test(full.slice(0, found))) {
39 insert = full.slice(0, found);
40 }
41 if (insert != null) insert += mode.blockCommentContinue;
42 }
43 if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) {
44 var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
45 if (found > -1) {
46 insert = line.slice(0, found);
47 if (/\S/.test(insert)) insert = null;
48 else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
49 }
50 }
51 if (insert == null) return CodeMirror.Pass;
52 inserts[i] = "\n" + insert;
53 }
54
55 cm.operation(function() {
56 for (var i = ranges.length - 1; i >= 0; i--)
57 cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
58 });
59 }
60
61 function continueLineCommentEnabled(cm) {
62 var opt = cm.getOption("continueComments");
63 if (opt && typeof opt == "object")
64 return opt.continueLineComment !== false;
65 return true;
66 }
67
68 CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
69 if (prev && prev != CodeMirror.Init)
70 cm.removeKeyMap("continueComment");
71 if (val) {
72 var key = "Enter";
73 if (typeof val == "string")
74 key = val;
75 else if (typeof val == "object" && val.key)
76 key = val.key;
77 var map = {name: "continueComment"};
78 map[key] = continueComment;
79 cm.addKeyMap(map);
80 }
81 });
82 });