browser/devtools/sourceeditor/codemirror/comment.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(mod) {
michael@0 2 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 3 mod(require("../../lib/codemirror"));
michael@0 4 else if (typeof define == "function" && define.amd) // AMD
michael@0 5 define(["../../lib/codemirror"], mod);
michael@0 6 else // Plain browser env
michael@0 7 mod(CodeMirror);
michael@0 8 })(function(CodeMirror) {
michael@0 9 "use strict";
michael@0 10
michael@0 11 var noOptions = {};
michael@0 12 var nonWS = /[^\s\u00a0]/;
michael@0 13 var Pos = CodeMirror.Pos;
michael@0 14
michael@0 15 function firstNonWS(str) {
michael@0 16 var found = str.search(nonWS);
michael@0 17 return found == -1 ? 0 : found;
michael@0 18 }
michael@0 19
michael@0 20 CodeMirror.commands.toggleComment = function(cm) {
michael@0 21 var minLine = Infinity, ranges = cm.listSelections(), mode = null;
michael@0 22 for (var i = ranges.length - 1; i >= 0; i--) {
michael@0 23 var from = ranges[i].from(), to = ranges[i].to();
michael@0 24 if (from.line >= minLine) continue;
michael@0 25 if (to.line >= minLine) to = Pos(minLine, 0);
michael@0 26 minLine = from.line;
michael@0 27 if (mode == null) {
michael@0 28 if (cm.uncomment(from, to)) mode = "un";
michael@0 29 else { cm.lineComment(from, to); mode = "line"; }
michael@0 30 } else if (mode == "un") {
michael@0 31 cm.uncomment(from, to);
michael@0 32 } else {
michael@0 33 cm.lineComment(from, to);
michael@0 34 }
michael@0 35 }
michael@0 36 };
michael@0 37
michael@0 38 CodeMirror.defineExtension("lineComment", function(from, to, options) {
michael@0 39 if (!options) options = noOptions;
michael@0 40 var self = this, mode = self.getModeAt(from);
michael@0 41 var commentString = options.lineComment || mode.lineComment;
michael@0 42 if (!commentString) {
michael@0 43 if (options.blockCommentStart || mode.blockCommentStart) {
michael@0 44 options.fullLines = true;
michael@0 45 self.blockComment(from, to, options);
michael@0 46 }
michael@0 47 return;
michael@0 48 }
michael@0 49 var firstLine = self.getLine(from.line);
michael@0 50 if (firstLine == null) return;
michael@0 51 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
michael@0 52 var pad = options.padding == null ? " " : options.padding;
michael@0 53 var blankLines = options.commentBlankLines || from.line == to.line;
michael@0 54
michael@0 55 self.operation(function() {
michael@0 56 if (options.indent) {
michael@0 57 var baseString = firstLine.slice(0, firstNonWS(firstLine));
michael@0 58 for (var i = from.line; i < end; ++i) {
michael@0 59 var line = self.getLine(i), cut = baseString.length;
michael@0 60 if (!blankLines && !nonWS.test(line)) continue;
michael@0 61 if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
michael@0 62 self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
michael@0 63 }
michael@0 64 } else {
michael@0 65 for (var i = from.line; i < end; ++i) {
michael@0 66 if (blankLines || nonWS.test(self.getLine(i)))
michael@0 67 self.replaceRange(commentString + pad, Pos(i, 0));
michael@0 68 }
michael@0 69 }
michael@0 70 });
michael@0 71 });
michael@0 72
michael@0 73 CodeMirror.defineExtension("blockComment", function(from, to, options) {
michael@0 74 if (!options) options = noOptions;
michael@0 75 var self = this, mode = self.getModeAt(from);
michael@0 76 var startString = options.blockCommentStart || mode.blockCommentStart;
michael@0 77 var endString = options.blockCommentEnd || mode.blockCommentEnd;
michael@0 78 if (!startString || !endString) {
michael@0 79 if ((options.lineComment || mode.lineComment) && options.fullLines != false)
michael@0 80 self.lineComment(from, to, options);
michael@0 81 return;
michael@0 82 }
michael@0 83
michael@0 84 var end = Math.min(to.line, self.lastLine());
michael@0 85 if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
michael@0 86
michael@0 87 var pad = options.padding == null ? " " : options.padding;
michael@0 88 if (from.line > end) return;
michael@0 89
michael@0 90 self.operation(function() {
michael@0 91 if (options.fullLines != false) {
michael@0 92 var lastLineHasText = nonWS.test(self.getLine(end));
michael@0 93 self.replaceRange(pad + endString, Pos(end));
michael@0 94 self.replaceRange(startString + pad, Pos(from.line, 0));
michael@0 95 var lead = options.blockCommentLead || mode.blockCommentLead;
michael@0 96 if (lead != null) for (var i = from.line + 1; i <= end; ++i)
michael@0 97 if (i != end || lastLineHasText)
michael@0 98 self.replaceRange(lead + pad, Pos(i, 0));
michael@0 99 } else {
michael@0 100 self.replaceRange(endString, to);
michael@0 101 self.replaceRange(startString, from);
michael@0 102 }
michael@0 103 });
michael@0 104 });
michael@0 105
michael@0 106 CodeMirror.defineExtension("uncomment", function(from, to, options) {
michael@0 107 if (!options) options = noOptions;
michael@0 108 var self = this, mode = self.getModeAt(from);
michael@0 109 var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end);
michael@0 110
michael@0 111 // Try finding line comments
michael@0 112 var lineString = options.lineComment || mode.lineComment, lines = [];
michael@0 113 var pad = options.padding == null ? " " : options.padding, didSomething;
michael@0 114 lineComment: {
michael@0 115 if (!lineString) break lineComment;
michael@0 116 for (var i = start; i <= end; ++i) {
michael@0 117 var line = self.getLine(i);
michael@0 118 var found = line.indexOf(lineString);
michael@0 119 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;
michael@0 120 if (found == -1 && (i != end || i == start) && nonWS.test(line)) break lineComment;
michael@0 121 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
michael@0 122 lines.push(line);
michael@0 123 }
michael@0 124 self.operation(function() {
michael@0 125 for (var i = start; i <= end; ++i) {
michael@0 126 var line = lines[i - start];
michael@0 127 var pos = line.indexOf(lineString), endPos = pos + lineString.length;
michael@0 128 if (pos < 0) continue;
michael@0 129 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
michael@0 130 didSomething = true;
michael@0 131 self.replaceRange("", Pos(i, pos), Pos(i, endPos));
michael@0 132 }
michael@0 133 });
michael@0 134 if (didSomething) return true;
michael@0 135 }
michael@0 136
michael@0 137 // Try block comments
michael@0 138 var startString = options.blockCommentStart || mode.blockCommentStart;
michael@0 139 var endString = options.blockCommentEnd || mode.blockCommentEnd;
michael@0 140 if (!startString || !endString) return false;
michael@0 141 var lead = options.blockCommentLead || mode.blockCommentLead;
michael@0 142 var startLine = self.getLine(start), endLine = end == start ? startLine : self.getLine(end);
michael@0 143 var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endString);
michael@0 144 if (close == -1 && start != end) {
michael@0 145 endLine = self.getLine(--end);
michael@0 146 close = endLine.lastIndexOf(endString);
michael@0 147 }
michael@0 148 if (open == -1 || close == -1 ||
michael@0 149 !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) ||
michael@0 150 !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1))))
michael@0 151 return false;
michael@0 152
michael@0 153 self.operation(function() {
michael@0 154 self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
michael@0 155 Pos(end, close + endString.length));
michael@0 156 var openEnd = open + startString.length;
michael@0 157 if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
michael@0 158 self.replaceRange("", Pos(start, open), Pos(start, openEnd));
michael@0 159 if (lead) for (var i = start + 1; i <= end; ++i) {
michael@0 160 var line = self.getLine(i), found = line.indexOf(lead);
michael@0 161 if (found == -1 || nonWS.test(line.slice(0, found))) continue;
michael@0 162 var foundEnd = found + lead.length;
michael@0 163 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
michael@0 164 self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
michael@0 165 }
michael@0 166 });
michael@0 167 return true;
michael@0 168 });
michael@0 169 });

mercurial