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
8 })(function(CodeMirror
) {
9 var modes
= ["clike", "css", "javascript"];
11 for (var i
= 0; i
< modes
.length
; ++i
)
12 CodeMirror
.extendMode(modes
[i
], {blockCommentContinue
: " * "});
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
;
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
)) {
34 for (var j
= 0; j
< token
.start
; ++j
) insert
+= " ";
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
);
41 if (insert
!= null) insert
+= mode
.blockCommentContinue
;
43 if (insert
== null && mode
.lineComment
&& continueLineCommentEnabled(cm
)) {
44 var line
= cm
.getLine(pos
.line
), found
= line
.indexOf(mode
.lineComment
);
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];
51 if (insert
== null) return CodeMirror
.Pass
;
52 inserts
[i
] = "\n" + insert
;
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");
61 function continueLineCommentEnabled(cm
) {
62 var opt
= cm
.getOption("continueComments");
63 if (opt
&& typeof opt
== "object")
64 return opt
.continueLineComment
!== false;
68 CodeMirror
.defineOption("continueComments", null, function(cm
, val
, prev
) {
69 if (prev
&& prev
!= CodeMirror
.Init
)
70 cm
.removeKeyMap("continueComment");
73 if (typeof val
== "string")
75 else if (typeof val
== "object" && val
.key
)
77 var map
= {name
: "continueComment"};
78 map
[key
] = continueComment
;