| OLD | NEW |
| 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 // Distributed under an MIT license: http://codemirror.net/LICENSE | 2 // Distributed under an MIT license: http://codemirror.net/LICENSE |
| 3 | 3 |
| 4 (function(mod) { | 4 (function(mod) { |
| 5 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 6 mod(require("../../lib/codemirror")); | 6 mod(require("../../lib/codemirror")); |
| 7 else if (typeof define == "function" && define.amd) // AMD | 7 else if (typeof define == "function" && define.amd) // AMD |
| 8 define(["../../lib/codemirror"], mod); | 8 define(["../../lib/codemirror"], mod); |
| 9 else // Plain browser env | 9 else // Plain browser env |
| 10 mod(CodeMirror); | 10 mod(CodeMirror); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 } else if (mode == "un") { | 39 } else if (mode == "un") { |
| 40 cm.uncomment(from, to, options); | 40 cm.uncomment(from, to, options); |
| 41 } else { | 41 } else { |
| 42 cm.lineComment(from, to, options); | 42 cm.lineComment(from, to, options); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 // Rough heuristic to try and detect lines that are part of multi-line string | 47 // Rough heuristic to try and detect lines that are part of multi-line string |
| 48 function probablyInsideString(cm, pos, line) { | 48 function probablyInsideString(cm, pos, line) { |
| 49 return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"`]/
.test(line) | 49 return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"\`]
/.test(line) |
| 50 } |
| 51 |
| 52 function getMode(cm, pos) { |
| 53 var mode = cm.getMode() |
| 54 return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getMod
eAt(pos) |
| 50 } | 55 } |
| 51 | 56 |
| 52 CodeMirror.defineExtension("lineComment", function(from, to, options) { | 57 CodeMirror.defineExtension("lineComment", function(from, to, options) { |
| 53 if (!options) options = noOptions; | 58 if (!options) options = noOptions; |
| 54 var self = this, mode = self.getModeAt(from); | 59 var self = this, mode = getMode(self, from); |
| 55 var firstLine = self.getLine(from.line); | 60 var firstLine = self.getLine(from.line); |
| 56 if (firstLine == null || probablyInsideString(self, from, firstLine)) return
; | 61 if (firstLine == null || probablyInsideString(self, from, firstLine)) return
; |
| 57 | 62 |
| 58 var commentString = options.lineComment || mode.lineComment; | 63 var commentString = options.lineComment || mode.lineComment; |
| 59 if (!commentString) { | 64 if (!commentString) { |
| 60 if (options.blockCommentStart || mode.blockCommentStart) { | 65 if (options.blockCommentStart || mode.blockCommentStart) { |
| 61 options.fullLines = true; | 66 options.fullLines = true; |
| 62 self.blockComment(from, to, options); | 67 self.blockComment(from, to, options); |
| 63 } | 68 } |
| 64 return; | 69 return; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 88 for (var i = from.line; i < end; ++i) { | 93 for (var i = from.line; i < end; ++i) { |
| 89 if (blankLines || nonWS.test(self.getLine(i))) | 94 if (blankLines || nonWS.test(self.getLine(i))) |
| 90 self.replaceRange(commentString + pad, Pos(i, 0)); | 95 self.replaceRange(commentString + pad, Pos(i, 0)); |
| 91 } | 96 } |
| 92 } | 97 } |
| 93 }); | 98 }); |
| 94 }); | 99 }); |
| 95 | 100 |
| 96 CodeMirror.defineExtension("blockComment", function(from, to, options) { | 101 CodeMirror.defineExtension("blockComment", function(from, to, options) { |
| 97 if (!options) options = noOptions; | 102 if (!options) options = noOptions; |
| 98 var self = this, mode = self.getModeAt(from); | 103 var self = this, mode = getMode(self, from); |
| 99 var startString = options.blockCommentStart || mode.blockCommentStart; | 104 var startString = options.blockCommentStart || mode.blockCommentStart; |
| 100 var endString = options.blockCommentEnd || mode.blockCommentEnd; | 105 var endString = options.blockCommentEnd || mode.blockCommentEnd; |
| 101 if (!startString || !endString) { | 106 if (!startString || !endString) { |
| 102 if ((options.lineComment || mode.lineComment) && options.fullLines != fals
e) | 107 if ((options.lineComment || mode.lineComment) && options.fullLines != fals
e) |
| 103 self.lineComment(from, to, options); | 108 self.lineComment(from, to, options); |
| 104 return; | 109 return; |
| 105 } | 110 } |
| 111 if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return |
| 106 | 112 |
| 107 var end = Math.min(to.line, self.lastLine()); | 113 var end = Math.min(to.line, self.lastLine()); |
| 108 if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end; | 114 if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end; |
| 109 | 115 |
| 110 var pad = options.padding == null ? " " : options.padding; | 116 var pad = options.padding == null ? " " : options.padding; |
| 111 if (from.line > end) return; | 117 if (from.line > end) return; |
| 112 | 118 |
| 113 self.operation(function() { | 119 self.operation(function() { |
| 114 if (options.fullLines != false) { | 120 if (options.fullLines != false) { |
| 115 var lastLineHasText = nonWS.test(self.getLine(end)); | 121 var lastLineHasText = nonWS.test(self.getLine(end)); |
| 116 self.replaceRange(pad + endString, Pos(end)); | 122 self.replaceRange(pad + endString, Pos(end)); |
| 117 self.replaceRange(startString + pad, Pos(from.line, 0)); | 123 self.replaceRange(startString + pad, Pos(from.line, 0)); |
| 118 var lead = options.blockCommentLead || mode.blockCommentLead; | 124 var lead = options.blockCommentLead || mode.blockCommentLead; |
| 119 if (lead != null) for (var i = from.line + 1; i <= end; ++i) | 125 if (lead != null) for (var i = from.line + 1; i <= end; ++i) |
| 120 if (i != end || lastLineHasText) | 126 if (i != end || lastLineHasText) |
| 121 self.replaceRange(lead + pad, Pos(i, 0)); | 127 self.replaceRange(lead + pad, Pos(i, 0)); |
| 122 } else { | 128 } else { |
| 123 self.replaceRange(endString, to); | 129 self.replaceRange(endString, to); |
| 124 self.replaceRange(startString, from); | 130 self.replaceRange(startString, from); |
| 125 } | 131 } |
| 126 }); | 132 }); |
| 127 }); | 133 }); |
| 128 | 134 |
| 129 CodeMirror.defineExtension("uncomment", function(from, to, options) { | 135 CodeMirror.defineExtension("uncomment", function(from, to, options) { |
| 130 if (!options) options = noOptions; | 136 if (!options) options = noOptions; |
| 131 var self = this, mode = self.getModeAt(from); | 137 var self = this, mode = getMode(self, from); |
| 132 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line -
1, self.lastLine()), start = Math.min(from.line, end); | 138 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line -
1, self.lastLine()), start = Math.min(from.line, end); |
| 133 | 139 |
| 134 // Try finding line comments | 140 // Try finding line comments |
| 135 var lineString = options.lineComment || mode.lineComment, lines = []; | 141 var lineString = options.lineComment || mode.lineComment, lines = []; |
| 136 var pad = options.padding == null ? " " : options.padding, didSomething; | 142 var pad = options.padding == null ? " " : options.padding, didSomething; |
| 137 lineComment: { | 143 lineComment: { |
| 138 if (!lineString) break lineComment; | 144 if (!lineString) break lineComment; |
| 139 for (var i = start; i <= end; ++i) { | 145 for (var i = start; i <= end; ++i) { |
| 140 var line = self.getLine(i); | 146 var line = self.getLine(i); |
| 141 var found = line.indexOf(lineString); | 147 var found = line.indexOf(lineString); |
| 142 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1))
)) found = -1; | 148 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1))
)) found = -1; |
| 143 if (found == -1 && (i != end || i == start) && nonWS.test(line)) break l
ineComment; | 149 if (found == -1 && nonWS.test(line)) break lineComment; |
| 144 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment; | 150 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment; |
| 145 lines.push(line); | 151 lines.push(line); |
| 146 } | 152 } |
| 147 self.operation(function() { | 153 self.operation(function() { |
| 148 for (var i = start; i <= end; ++i) { | 154 for (var i = start; i <= end; ++i) { |
| 149 var line = lines[i - start]; | 155 var line = lines[i - start]; |
| 150 var pos = line.indexOf(lineString), endPos = pos + lineString.length; | 156 var pos = line.indexOf(lineString), endPos = pos + lineString.length; |
| 151 if (pos < 0) continue; | 157 if (pos < 0) continue; |
| 152 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng
th; | 158 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng
th; |
| 153 didSomething = true; | 159 didSomething = true; |
| 154 self.replaceRange("", Pos(i, pos), Pos(i, endPos)); | 160 self.replaceRange("", Pos(i, pos), Pos(i, endPos)); |
| 155 } | 161 } |
| 156 }); | 162 }); |
| 157 if (didSomething) return true; | 163 if (didSomething) return true; |
| 158 } | 164 } |
| 159 | 165 |
| 160 // Try block comments | 166 // Try block comments |
| 161 var startString = options.blockCommentStart || mode.blockCommentStart; | 167 var startString = options.blockCommentStart || mode.blockCommentStart; |
| 162 var endString = options.blockCommentEnd || mode.blockCommentEnd; | 168 var endString = options.blockCommentEnd || mode.blockCommentEnd; |
| 163 if (!startString || !endString) return false; | 169 if (!startString || !endString) return false; |
| 164 var lead = options.blockCommentLead || mode.blockCommentLead; | 170 var lead = options.blockCommentLead || mode.blockCommentLead; |
| 165 var startLine = self.getLine(start), endLine = end == start ? startLine : se
lf.getLine(end); | 171 var startLine = self.getLine(start), open = startLine.indexOf(startString) |
| 166 var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endSt
ring); | 172 if (open == -1) return false |
| 173 var endLine = end == start ? startLine : self.getLine(end) |
| 174 var close = endLine.indexOf(endString, end == start ? open + startString.len
gth : 0); |
| 167 if (close == -1 && start != end) { | 175 if (close == -1 && start != end) { |
| 168 endLine = self.getLine(--end); | 176 endLine = self.getLine(--end); |
| 169 close = endLine.lastIndexOf(endString); | 177 close = endLine.indexOf(endString); |
| 170 } | 178 } |
| 171 if (open == -1 || close == -1 || | 179 var insideStart = Pos(start, open + 1), insideEnd = Pos(end, close + 1) |
| 172 !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) || | 180 if (close == -1 || |
| 173 !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1)))) | 181 !/comment/.test(self.getTokenTypeAt(insideStart)) || |
| 182 !/comment/.test(self.getTokenTypeAt(insideEnd)) || |
| 183 self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1) |
| 174 return false; | 184 return false; |
| 175 | 185 |
| 176 // Avoid killing block comments completely outside the selection. | 186 // Avoid killing block comments completely outside the selection. |
| 177 // Positions of the last startString before the start of the selection, and
the first endString after it. | 187 // Positions of the last startString before the start of the selection, and
the first endString after it. |
| 178 var lastStart = startLine.lastIndexOf(startString, from.ch); | 188 var lastStart = startLine.lastIndexOf(startString, from.ch); |
| 179 var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(en
dString, lastStart + startString.length); | 189 var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(en
dString, lastStart + startString.length); |
| 180 if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from
.ch) return false; | 190 if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from
.ch) return false; |
| 181 // Positions of the first endString after the end of the selection, and the
last startString before it. | 191 // Positions of the first endString after the end of the selection, and the
last startString before it. |
| 182 firstEnd = endLine.indexOf(endString, to.ch); | 192 firstEnd = endLine.indexOf(endString, to.ch); |
| 183 var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd
- to.ch); | 193 var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd
- to.ch); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 194 var line = self.getLine(i), found = line.indexOf(lead); | 204 var line = self.getLine(i), found = line.indexOf(lead); |
| 195 if (found == -1 || nonWS.test(line.slice(0, found))) continue; | 205 if (found == -1 || nonWS.test(line.slice(0, found))) continue; |
| 196 var foundEnd = found + lead.length; | 206 var foundEnd = found + lead.length; |
| 197 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; | 207 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; |
| 198 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); | 208 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); |
| 199 } | 209 } |
| 200 }); | 210 }); |
| 201 return true; | 211 return true; |
| 202 }); | 212 }); |
| 203 }); | 213 }); |
| OLD | NEW |