Index: third_party/WebKit/Source/devtools/front_end/cm/comment.js |
diff --git a/third_party/WebKit/Source/devtools/front_end/cm/comment.js b/third_party/WebKit/Source/devtools/front_end/cm/comment.js |
index 2dd114d332dc9b1e0b19cfff3fc60061c7cb9fbe..2c4f975d08f59cc3f2e5d4d5a9933ef2c1ec5cc8 100644 |
--- a/third_party/WebKit/Source/devtools/front_end/cm/comment.js |
+++ b/third_party/WebKit/Source/devtools/front_end/cm/comment.js |
@@ -21,26 +21,40 @@ |
} |
CodeMirror.commands.toggleComment = function(cm) { |
- var minLine = Infinity, ranges = cm.listSelections(), mode = null; |
+ cm.toggleComment(); |
+ }; |
+ |
+ CodeMirror.defineExtension("toggleComment", function(options) { |
+ if (!options) options = noOptions; |
+ var cm = this; |
+ var minLine = Infinity, ranges = this.listSelections(), mode = null; |
for (var i = ranges.length - 1; i >= 0; i--) { |
var from = ranges[i].from(), to = ranges[i].to(); |
if (from.line >= minLine) continue; |
if (to.line >= minLine) to = Pos(minLine, 0); |
minLine = from.line; |
if (mode == null) { |
- if (cm.uncomment(from, to)) mode = "un"; |
- else { cm.lineComment(from, to); mode = "line"; } |
+ if (cm.uncomment(from, to, options)) mode = "un"; |
+ else { cm.lineComment(from, to, options); mode = "line"; } |
} else if (mode == "un") { |
- cm.uncomment(from, to); |
+ cm.uncomment(from, to, options); |
} else { |
- cm.lineComment(from, to); |
+ cm.lineComment(from, to, options); |
} |
} |
- }; |
+ }); |
+ |
+ // Rough heuristic to try and detect lines that are part of multi-line string |
+ function probablyInsideString(cm, pos, line) { |
+ return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"`]/.test(line) |
+ } |
CodeMirror.defineExtension("lineComment", function(from, to, options) { |
if (!options) options = noOptions; |
var self = this, mode = self.getModeAt(from); |
+ var firstLine = self.getLine(from.line); |
+ if (firstLine == null || probablyInsideString(self, from, firstLine)) return; |
+ |
var commentString = options.lineComment || mode.lineComment; |
if (!commentString) { |
if (options.blockCommentStart || mode.blockCommentStart) { |
@@ -49,15 +63,21 @@ |
} |
return; |
} |
- var firstLine = self.getLine(from.line); |
- if (firstLine == null) return; |
+ |
var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1); |
var pad = options.padding == null ? " " : options.padding; |
var blankLines = options.commentBlankLines || from.line == to.line; |
self.operation(function() { |
if (options.indent) { |
- var baseString = firstLine.slice(0, firstNonWS(firstLine)); |
+ var baseString = null; |
+ for (var i = from.line; i < end; ++i) { |
+ var line = self.getLine(i); |
+ var whitespace = line.slice(0, firstNonWS(line)); |
+ if (baseString == null || baseString.length > whitespace.length) { |
+ baseString = whitespace; |
+ } |
+ } |
for (var i = from.line; i < end; ++i) { |
var line = self.getLine(i), cut = baseString.length; |
if (!blankLines && !nonWS.test(line)) continue; |