| 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); |
| 11 })(function(CodeMirror) { | 11 })(function(CodeMirror) { |
| 12 "use strict"; | 12 "use strict"; |
| 13 | 13 |
| 14 var noOptions = {}; | 14 var noOptions = {}; |
| 15 var nonWS = /[^\s\u00a0]/; | 15 var nonWS = /[^\s\u00a0]/; |
| 16 var Pos = CodeMirror.Pos; | 16 var Pos = CodeMirror.Pos; |
| 17 | 17 |
| 18 function firstNonWS(str) { | 18 function firstNonWS(str) { |
| 19 var found = str.search(nonWS); | 19 var found = str.search(nonWS); |
| 20 return found == -1 ? 0 : found; | 20 return found == -1 ? 0 : found; |
| 21 } | 21 } |
| 22 | 22 |
| 23 CodeMirror.commands.toggleComment = function(cm) { | 23 CodeMirror.commands.toggleComment = function(cm) { |
| 24 var minLine = Infinity, ranges = cm.listSelections(), mode = null; | 24 cm.toggleComment(); |
| 25 }; |
| 26 |
| 27 CodeMirror.defineExtension("toggleComment", function(options) { |
| 28 if (!options) options = noOptions; |
| 29 var cm = this; |
| 30 var minLine = Infinity, ranges = this.listSelections(), mode = null; |
| 25 for (var i = ranges.length - 1; i >= 0; i--) { | 31 for (var i = ranges.length - 1; i >= 0; i--) { |
| 26 var from = ranges[i].from(), to = ranges[i].to(); | 32 var from = ranges[i].from(), to = ranges[i].to(); |
| 27 if (from.line >= minLine) continue; | 33 if (from.line >= minLine) continue; |
| 28 if (to.line >= minLine) to = Pos(minLine, 0); | 34 if (to.line >= minLine) to = Pos(minLine, 0); |
| 29 minLine = from.line; | 35 minLine = from.line; |
| 30 if (mode == null) { | 36 if (mode == null) { |
| 31 if (cm.uncomment(from, to)) mode = "un"; | 37 if (cm.uncomment(from, to, options)) mode = "un"; |
| 32 else { cm.lineComment(from, to); mode = "line"; } | 38 else { cm.lineComment(from, to, options); mode = "line"; } |
| 33 } else if (mode == "un") { | 39 } else if (mode == "un") { |
| 34 cm.uncomment(from, to); | 40 cm.uncomment(from, to, options); |
| 35 } else { | 41 } else { |
| 36 cm.lineComment(from, to); | 42 cm.lineComment(from, to, options); |
| 37 } | 43 } |
| 38 } | 44 } |
| 39 }; | 45 }); |
| 46 |
| 47 // Rough heuristic to try and detect lines that are part of multi-line string |
| 48 function probablyInsideString(cm, pos, line) { |
| 49 return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"`]/
.test(line) |
| 50 } |
| 40 | 51 |
| 41 CodeMirror.defineExtension("lineComment", function(from, to, options) { | 52 CodeMirror.defineExtension("lineComment", function(from, to, options) { |
| 42 if (!options) options = noOptions; | 53 if (!options) options = noOptions; |
| 43 var self = this, mode = self.getModeAt(from); | 54 var self = this, mode = self.getModeAt(from); |
| 55 var firstLine = self.getLine(from.line); |
| 56 if (firstLine == null || probablyInsideString(self, from, firstLine)) return
; |
| 57 |
| 44 var commentString = options.lineComment || mode.lineComment; | 58 var commentString = options.lineComment || mode.lineComment; |
| 45 if (!commentString) { | 59 if (!commentString) { |
| 46 if (options.blockCommentStart || mode.blockCommentStart) { | 60 if (options.blockCommentStart || mode.blockCommentStart) { |
| 47 options.fullLines = true; | 61 options.fullLines = true; |
| 48 self.blockComment(from, to, options); | 62 self.blockComment(from, to, options); |
| 49 } | 63 } |
| 50 return; | 64 return; |
| 51 } | 65 } |
| 52 var firstLine = self.getLine(from.line); | 66 |
| 53 if (firstLine == null) return; | |
| 54 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.lin
e, self.lastLine() + 1); | 67 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.lin
e, self.lastLine() + 1); |
| 55 var pad = options.padding == null ? " " : options.padding; | 68 var pad = options.padding == null ? " " : options.padding; |
| 56 var blankLines = options.commentBlankLines || from.line == to.line; | 69 var blankLines = options.commentBlankLines || from.line == to.line; |
| 57 | 70 |
| 58 self.operation(function() { | 71 self.operation(function() { |
| 59 if (options.indent) { | 72 if (options.indent) { |
| 60 var baseString = firstLine.slice(0, firstNonWS(firstLine)); | 73 var baseString = null; |
| 74 for (var i = from.line; i < end; ++i) { |
| 75 var line = self.getLine(i); |
| 76 var whitespace = line.slice(0, firstNonWS(line)); |
| 77 if (baseString == null || baseString.length > whitespace.length) { |
| 78 baseString = whitespace; |
| 79 } |
| 80 } |
| 61 for (var i = from.line; i < end; ++i) { | 81 for (var i = from.line; i < end; ++i) { |
| 62 var line = self.getLine(i), cut = baseString.length; | 82 var line = self.getLine(i), cut = baseString.length; |
| 63 if (!blankLines && !nonWS.test(line)) continue; | 83 if (!blankLines && !nonWS.test(line)) continue; |
| 64 if (line.slice(0, cut) != baseString) cut = firstNonWS(line); | 84 if (line.slice(0, cut) != baseString) cut = firstNonWS(line); |
| 65 self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i,
cut)); | 85 self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i,
cut)); |
| 66 } | 86 } |
| 67 } else { | 87 } else { |
| 68 for (var i = from.line; i < end; ++i) { | 88 for (var i = from.line; i < end; ++i) { |
| 69 if (blankLines || nonWS.test(self.getLine(i))) | 89 if (blankLines || nonWS.test(self.getLine(i))) |
| 70 self.replaceRange(commentString + pad, Pos(i, 0)); | 90 self.replaceRange(commentString + pad, Pos(i, 0)); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 var line = self.getLine(i), found = line.indexOf(lead); | 194 var line = self.getLine(i), found = line.indexOf(lead); |
| 175 if (found == -1 || nonWS.test(line.slice(0, found))) continue; | 195 if (found == -1 || nonWS.test(line.slice(0, found))) continue; |
| 176 var foundEnd = found + lead.length; | 196 var foundEnd = found + lead.length; |
| 177 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; | 197 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd
+= pad.length; |
| 178 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); | 198 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); |
| 179 } | 199 } |
| 180 }); | 200 }); |
| 181 return true; | 201 return true; |
| 182 }); | 202 }); |
| 183 }); | 203 }); |
| OLD | NEW |