OLD | NEW |
1 (function() { | 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) { |
2 "use strict"; | 9 "use strict"; |
3 | 10 |
4 var noOptions = {}; | 11 var noOptions = {}; |
5 var nonWS = /[^\s\u00a0]/; | 12 var nonWS = /[^\s\u00a0]/; |
6 var Pos = CodeMirror.Pos; | 13 var Pos = CodeMirror.Pos; |
7 | 14 |
8 function firstNonWS(str) { | 15 function firstNonWS(str) { |
9 var found = str.search(nonWS); | 16 var found = str.search(nonWS); |
10 return found == -1 ? 0 : found; | 17 return found == -1 ? 0 : found; |
11 } | 18 } |
12 | 19 |
13 CodeMirror.commands.toggleComment = function(cm) { | 20 CodeMirror.commands.toggleComment = function(cm) { |
14 var from = cm.getCursor("start"), to = cm.getCursor("end"); | 21 var minLine = Infinity, ranges = cm.listSelections(), mode = null; |
15 cm.uncomment(from, to) || cm.lineComment(from, to); | 22 for (var i = ranges.length - 1; i >= 0; i--) { |
| 23 var from = ranges[i].from(), to = ranges[i].to(); |
| 24 if (from.line >= minLine) continue; |
| 25 if (to.line >= minLine) to = Pos(minLine, 0); |
| 26 minLine = from.line; |
| 27 if (mode == null) { |
| 28 if (cm.uncomment(from, to)) mode = "un"; |
| 29 else { cm.lineComment(from, to); mode = "line"; } |
| 30 } else if (mode == "un") { |
| 31 cm.uncomment(from, to); |
| 32 } else { |
| 33 cm.lineComment(from, to); |
| 34 } |
| 35 } |
16 }; | 36 }; |
17 | 37 |
18 CodeMirror.defineExtension("lineComment", function(from, to, options) { | 38 CodeMirror.defineExtension("lineComment", function(from, to, options) { |
19 if (!options) options = noOptions; | 39 if (!options) options = noOptions; |
20 var self = this, mode = self.getModeAt(from); | 40 var self = this, mode = self.getModeAt(from); |
21 var commentString = options.lineComment || mode.lineComment; | 41 var commentString = options.lineComment || mode.lineComment; |
22 if (!commentString) { | 42 if (!commentString) { |
23 if (options.blockCommentStart || mode.blockCommentStart) { | 43 if (options.blockCommentStart || mode.blockCommentStart) { |
24 options.fullLines = true; | 44 options.fullLines = true; |
25 self.blockComment(from, to, options); | 45 self.blockComment(from, to, options); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, en
d); | 109 var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, en
d); |
90 | 110 |
91 // Try finding line comments | 111 // Try finding line comments |
92 var lineString = options.lineComment || mode.lineComment, lines = []; | 112 var lineString = options.lineComment || mode.lineComment, lines = []; |
93 var pad = options.padding == null ? " " : options.padding, didSomething; | 113 var pad = options.padding == null ? " " : options.padding, didSomething; |
94 lineComment: { | 114 lineComment: { |
95 if (!lineString) break lineComment; | 115 if (!lineString) break lineComment; |
96 for (var i = start; i <= end; ++i) { | 116 for (var i = start; i <= end; ++i) { |
97 var line = self.getLine(i); | 117 var line = self.getLine(i); |
98 var found = line.indexOf(lineString); | 118 var found = line.indexOf(lineString); |
| 119 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1))
)) found = -1; |
99 if (found == -1 && (i != end || i == start) && nonWS.test(line)) break l
ineComment; | 120 if (found == -1 && (i != end || i == start) && nonWS.test(line)) break l
ineComment; |
100 if (i != start && found > -1 && nonWS.test(line.slice(0, found))) break
lineComment; | 121 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment; |
101 lines.push(line); | 122 lines.push(line); |
102 } | 123 } |
103 self.operation(function() { | 124 self.operation(function() { |
104 for (var i = start; i <= end; ++i) { | 125 for (var i = start; i <= end; ++i) { |
105 var line = lines[i - start]; | 126 var line = lines[i - start]; |
106 var pos = line.indexOf(lineString), endPos = pos + lineString.length; | 127 var pos = line.indexOf(lineString), endPos = pos + lineString.length; |
107 if (pos < 0) continue; | 128 if (pos < 0) continue; |
108 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng
th; | 129 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng
th; |
109 didSomething = true; | 130 didSomething = true; |
110 self.replaceRange("", Pos(i, pos), Pos(i, endPos)); | 131 self.replaceRange("", Pos(i, pos), Pos(i, endPos)); |
111 } | 132 } |
112 }); | 133 }); |
113 if (didSomething) return true; | 134 if (didSomething) return true; |
114 } | 135 } |
115 | 136 |
116 // Try block comments | 137 // Try block comments |
117 var startString = options.blockCommentStart || mode.blockCommentStart; | 138 var startString = options.blockCommentStart || mode.blockCommentStart; |
118 var endString = options.blockCommentEnd || mode.blockCommentEnd; | 139 var endString = options.blockCommentEnd || mode.blockCommentEnd; |
119 if (!startString || !endString) return false; | 140 if (!startString || !endString) return false; |
120 var lead = options.blockCommentLead || mode.blockCommentLead; | 141 var lead = options.blockCommentLead || mode.blockCommentLead; |
121 var startLine = self.getLine(start), endLine = end == start ? startLine : se
lf.getLine(end); | 142 var startLine = self.getLine(start), endLine = end == start ? startLine : se
lf.getLine(end); |
122 var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endSt
ring); | 143 var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endSt
ring); |
123 if (close == -1 && start != end) { | 144 if (close == -1 && start != end) { |
124 endLine = self.getLine(--end); | 145 endLine = self.getLine(--end); |
125 close = endLine.lastIndexOf(endString); | 146 close = endLine.lastIndexOf(endString); |
126 } | 147 } |
127 if (open == -1 || close == -1) return false; | 148 if (open == -1 || close == -1 || |
| 149 !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) || |
| 150 !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1)))) |
| 151 return false; |
128 | 152 |
129 self.operation(function() { | 153 self.operation(function() { |
130 self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.l
ength, close) == pad ? pad.length : 0)), | 154 self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.l
ength, close) == pad ? pad.length : 0)), |
131 Pos(end, close + endString.length)); | 155 Pos(end, close + endString.length)); |
132 var openEnd = open + startString.length; | 156 var openEnd = open + startString.length; |
133 if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd
+= pad.length; | 157 if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd
+= pad.length; |
134 self.replaceRange("", Pos(start, open), Pos(start, openEnd)); | 158 self.replaceRange("", Pos(start, open), Pos(start, openEnd)); |
135 if (lead) for (var i = start + 1; i <= end; ++i) { | 159 if (lead) for (var i = start + 1; i <= end; ++i) { |
136 var line = self.getLine(i), found = line.indexOf(lead); | 160 var line = self.getLine(i), found = line.indexOf(lead); |
137 if (found == -1 || nonWS.test(line.slice(0, found))) continue; | 161 if (found == -1 || nonWS.test(line.slice(0, found))) continue; |
138 var foundEnd = found + lead.length; | 162 var foundEnd = found + lead.length; |
139 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; | 163 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; |
140 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); | 164 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); |
141 } | 165 } |
142 }); | 166 }); |
143 return true; | 167 return true; |
144 }); | 168 }); |
145 })(); | 169 }); |
OLD | NEW |