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) {
15
var nonWS = /[^\s\u00a0]/;
16
var Pos = CodeMirror.Pos, cmp = CodeMirror.cmpPos;
18
function firstNonWS(str) {
19
var found = str.search(nonWS);
20
return found == -1 ? 0 : found;
23
CodeMirror.commands.toggleComment = function(cm) {
27
CodeMirror.defineExtension("toggleComment", function(options) {
28
if (!options) options = noOptions;
30
var minLine = Infinity, ranges = this.listSelections(), mode = null;
31
for (var i = ranges.length - 1; i >= 0; i--) {
32
var from = ranges[i].from(), to = ranges[i].to();
33
if (from.line >= minLine) continue;
34
if (to.line >= minLine) to = Pos(minLine, 0);
37
if (cm.uncomment(from, to, options)) mode = "un";
38
else { cm.lineComment(from, to, options); mode = "line"; }
39
} else if (mode == "un") {
40
cm.uncomment(from, to, options);
42
cm.lineComment(from, to, options);
48
function probablyInsideString(cm, pos, line) {
49
return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"\`]/.test(line)
52
function getMode(cm, pos) {
53
var mode = cm.getMode()
54
return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getModeAt(pos)
57
CodeMirror.defineExtension("lineComment", function(from, to, options) {
58
if (!options) options = noOptions;
59
var self = this, mode = getMode(self, from);
60
var firstLine = self.getLine(from.line);
61
if (firstLine == null || probablyInsideString(self, from, firstLine)) return;
63
var commentString = options.lineComment || mode.lineComment;
65
if (options.blockCommentStart || mode.blockCommentStart) {
66
options.fullLines = true;
67
self.blockComment(from, to, options);
72
var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
73
var pad = options.padding == null ? " " : options.padding;
74
var blankLines = options.commentBlankLines || from.line == to.line;
76
self.operation(function() {
78
var baseString = null;
79
for (var i = from.line; i < end; ++i) {
80
var line = self.getLine(i);
81
var whitespace = line.slice(0, firstNonWS(line));
82
if (baseString == null || baseString.length > whitespace.length) {
83
baseString = whitespace;
86
for (var i = from.line; i < end; ++i) {
87
var line = self.getLine(i), cut = baseString.length;
88
if (!blankLines && !nonWS.test(line)) continue;
89
if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
90
self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
93
for (var i = from.line; i < end; ++i) {
94
if (blankLines || nonWS.test(self.getLine(i)))
95
self.replaceRange(commentString + pad, Pos(i, 0));
101
CodeMirror.defineExtension("blockComment", function(from, to, options) {
102
if (!options) options = noOptions;
103
var self = this, mode = getMode(self, from);
104
var startString = options.blockCommentStart || mode.blockCommentStart;
105
var endString = options.blockCommentEnd || mode.blockCommentEnd;
106
if (!startString || !endString) {
107
if ((options.lineComment || mode.lineComment) && options.fullLines != false)
108
self.lineComment(from, to, options);
111
if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return
113
var end = Math.min(to.line, self.lastLine());
114
if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
116
var pad = options.padding == null ? " " : options.padding;
117
if (from.line > end) return;
119
self.operation(function() {
120
if (options.fullLines != false) {
121
var lastLineHasText = nonWS.test(self.getLine(end));
122
self.replaceRange(pad + endString, Pos(end));
123
self.replaceRange(startString + pad, Pos(from.line, 0));
124
var lead = options.blockCommentLead || mode.blockCommentLead;
125
if (lead != null) for (var i = from.line + 1; i <= end; ++i)
126
if (i != end || lastLineHasText)
127
self.replaceRange(lead + pad, Pos(i, 0));
129
var atCursor = cmp(self.getCursor("to"), to) == 0, empty = !self.somethingSelected()
130
self.replaceRange(endString, to);
131
if (atCursor) self.setSelection(empty ? to : self.getCursor("from"), to)
132
self.replaceRange(startString, from);
137
CodeMirror.defineExtension("uncomment", function(from, to, options) {
138
if (!options) options = noOptions;
139
var self = this, mode = getMode(self, from);
140
var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end);
143
var lineString = options.lineComment || mode.lineComment, lines = [];
144
var pad = options.padding == null ? " " : options.padding, didSomething;
146
if (!lineString) break lineComment;
147
for (var i = start; i <= end; ++i) {
148
var line = self.getLine(i);
149
var found = line.indexOf(lineString);
150
if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;
151
if (found == -1 && nonWS.test(line)) break lineComment;
152
if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
155
self.operation(function() {
156
for (var i = start; i <= end; ++i) {
157
var line = lines[i - start];
158
var pos = line.indexOf(lineString), endPos = pos + lineString.length;
159
if (pos < 0) continue;
160
if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
162
self.replaceRange("", Pos(i, pos), Pos(i, endPos));
165
if (didSomething) return true;
169
var startString = options.blockCommentStart || mode.blockCommentStart;
170
var endString = options.blockCommentEnd || mode.blockCommentEnd;
171
if (!startString || !endString) return false;
172
var lead = options.blockCommentLead || mode.blockCommentLead;
173
var startLine = self.getLine(start), open = startLine.indexOf(startString)
174
if (open == -1) return false
175
var endLine = end == start ? startLine : self.getLine(end)
176
var close = endLine.indexOf(endString, end == start ? open + startString.length : 0);
177
var insideStart = Pos(start, open + 1), insideEnd = Pos(end, close + 1)
179
!/comment/.test(self.getTokenTypeAt(insideStart)) ||
180
!/comment/.test(self.getTokenTypeAt(insideEnd)) ||
181
self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1)
186
var lastStart = startLine.lastIndexOf(startString, from.ch);
187
var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(endString, lastStart + startString.length);
188
if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from.ch) return false;
190
firstEnd = endLine.indexOf(endString, to.ch);
191
var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch);
192
lastStart = (firstEnd == -1 || almostLastStart == -1) ? -1 : to.ch + almostLastStart;
193
if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false;
195
self.operation(function() {
196
self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
197
Pos(end, close + endString.length));
198
var openEnd = open + startString.length;
199
if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
200
self.replaceRange("", Pos(start, open), Pos(start, openEnd));
201
if (lead) for (var i = start + 1; i <= end; ++i) {
202
var line = self.getLine(i), found = line.indexOf(lead);
203
if (found == -1 || nonWS.test(line.slice(0, found))) continue;
204
var foundEnd = found + lead.length;
205
if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
206
self.replaceRange("", Pos(i, found), Pos(i, foundEnd));