Skip to content

Commit f486fcd

Browse files
committed
[comment addon] Don't toggle comments inside multi-line strings
Issue codemirror#3941
1 parent 90ef4bd commit f486fcd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

addon/comment/comment.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@
4444
}
4545
});
4646

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+
}
51+
4752
CodeMirror.defineExtension("lineComment", function(from, to, options) {
4853
if (!options) options = noOptions;
4954
var self = this, mode = self.getModeAt(from);
55+
var firstLine = self.getLine(from.line);
56+
if (firstLine == null || probablyInsideString(self, from, firstLine)) return;
57+
5058
var commentString = options.lineComment || mode.lineComment;
5159
if (!commentString) {
5260
if (options.blockCommentStart || mode.blockCommentStart) {
@@ -55,8 +63,7 @@
5563
}
5664
return;
5765
}
58-
var firstLine = self.getLine(from.line);
59-
if (firstLine == null) return;
66+
6067
var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
6168
var pad = options.padding == null ? " " : options.padding;
6269
var blankLines = options.commentBlankLines || from.line == to.line;

0 commit comments

Comments
 (0)