browser/devtools/sourceeditor/codemirror/comment.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/codemirror/comment.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,169 @@
     1.4 +(function(mod) {
     1.5 +  if (typeof exports == "object" && typeof module == "object") // CommonJS
     1.6 +    mod(require("../../lib/codemirror"));
     1.7 +  else if (typeof define == "function" && define.amd) // AMD
     1.8 +    define(["../../lib/codemirror"], mod);
     1.9 +  else // Plain browser env
    1.10 +    mod(CodeMirror);
    1.11 +})(function(CodeMirror) {
    1.12 +  "use strict";
    1.13 +
    1.14 +  var noOptions = {};
    1.15 +  var nonWS = /[^\s\u00a0]/;
    1.16 +  var Pos = CodeMirror.Pos;
    1.17 +
    1.18 +  function firstNonWS(str) {
    1.19 +    var found = str.search(nonWS);
    1.20 +    return found == -1 ? 0 : found;
    1.21 +  }
    1.22 +
    1.23 +  CodeMirror.commands.toggleComment = function(cm) {
    1.24 +    var minLine = Infinity, ranges = cm.listSelections(), mode = null;
    1.25 +    for (var i = ranges.length - 1; i >= 0; i--) {
    1.26 +      var from = ranges[i].from(), to = ranges[i].to();
    1.27 +      if (from.line >= minLine) continue;
    1.28 +      if (to.line >= minLine) to = Pos(minLine, 0);
    1.29 +      minLine = from.line;
    1.30 +      if (mode == null) {
    1.31 +        if (cm.uncomment(from, to)) mode = "un";
    1.32 +        else { cm.lineComment(from, to); mode = "line"; }
    1.33 +      } else if (mode == "un") {
    1.34 +        cm.uncomment(from, to);
    1.35 +      } else {
    1.36 +        cm.lineComment(from, to);
    1.37 +      }
    1.38 +    }
    1.39 +  };
    1.40 +
    1.41 +  CodeMirror.defineExtension("lineComment", function(from, to, options) {
    1.42 +    if (!options) options = noOptions;
    1.43 +    var self = this, mode = self.getModeAt(from);
    1.44 +    var commentString = options.lineComment || mode.lineComment;
    1.45 +    if (!commentString) {
    1.46 +      if (options.blockCommentStart || mode.blockCommentStart) {
    1.47 +        options.fullLines = true;
    1.48 +        self.blockComment(from, to, options);
    1.49 +      }
    1.50 +      return;
    1.51 +    }
    1.52 +    var firstLine = self.getLine(from.line);
    1.53 +    if (firstLine == null) return;
    1.54 +    var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
    1.55 +    var pad = options.padding == null ? " " : options.padding;
    1.56 +    var blankLines = options.commentBlankLines || from.line == to.line;
    1.57 +
    1.58 +    self.operation(function() {
    1.59 +      if (options.indent) {
    1.60 +        var baseString = firstLine.slice(0, firstNonWS(firstLine));
    1.61 +        for (var i = from.line; i < end; ++i) {
    1.62 +          var line = self.getLine(i), cut = baseString.length;
    1.63 +          if (!blankLines && !nonWS.test(line)) continue;
    1.64 +          if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
    1.65 +          self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
    1.66 +        }
    1.67 +      } else {
    1.68 +        for (var i = from.line; i < end; ++i) {
    1.69 +          if (blankLines || nonWS.test(self.getLine(i)))
    1.70 +            self.replaceRange(commentString + pad, Pos(i, 0));
    1.71 +        }
    1.72 +      }
    1.73 +    });
    1.74 +  });
    1.75 +
    1.76 +  CodeMirror.defineExtension("blockComment", function(from, to, options) {
    1.77 +    if (!options) options = noOptions;
    1.78 +    var self = this, mode = self.getModeAt(from);
    1.79 +    var startString = options.blockCommentStart || mode.blockCommentStart;
    1.80 +    var endString = options.blockCommentEnd || mode.blockCommentEnd;
    1.81 +    if (!startString || !endString) {
    1.82 +      if ((options.lineComment || mode.lineComment) && options.fullLines != false)
    1.83 +        self.lineComment(from, to, options);
    1.84 +      return;
    1.85 +    }
    1.86 +
    1.87 +    var end = Math.min(to.line, self.lastLine());
    1.88 +    if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
    1.89 +
    1.90 +    var pad = options.padding == null ? " " : options.padding;
    1.91 +    if (from.line > end) return;
    1.92 +
    1.93 +    self.operation(function() {
    1.94 +      if (options.fullLines != false) {
    1.95 +        var lastLineHasText = nonWS.test(self.getLine(end));
    1.96 +        self.replaceRange(pad + endString, Pos(end));
    1.97 +        self.replaceRange(startString + pad, Pos(from.line, 0));
    1.98 +        var lead = options.blockCommentLead || mode.blockCommentLead;
    1.99 +        if (lead != null) for (var i = from.line + 1; i <= end; ++i)
   1.100 +          if (i != end || lastLineHasText)
   1.101 +            self.replaceRange(lead + pad, Pos(i, 0));
   1.102 +      } else {
   1.103 +        self.replaceRange(endString, to);
   1.104 +        self.replaceRange(startString, from);
   1.105 +      }
   1.106 +    });
   1.107 +  });
   1.108 +
   1.109 +  CodeMirror.defineExtension("uncomment", function(from, to, options) {
   1.110 +    if (!options) options = noOptions;
   1.111 +    var self = this, mode = self.getModeAt(from);
   1.112 +    var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end);
   1.113 +
   1.114 +    // Try finding line comments
   1.115 +    var lineString = options.lineComment || mode.lineComment, lines = [];
   1.116 +    var pad = options.padding == null ? " " : options.padding, didSomething;
   1.117 +    lineComment: {
   1.118 +      if (!lineString) break lineComment;
   1.119 +      for (var i = start; i <= end; ++i) {
   1.120 +        var line = self.getLine(i);
   1.121 +        var found = line.indexOf(lineString);
   1.122 +        if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;
   1.123 +        if (found == -1 && (i != end || i == start) && nonWS.test(line)) break lineComment;
   1.124 +        if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
   1.125 +        lines.push(line);
   1.126 +      }
   1.127 +      self.operation(function() {
   1.128 +        for (var i = start; i <= end; ++i) {
   1.129 +          var line = lines[i - start];
   1.130 +          var pos = line.indexOf(lineString), endPos = pos + lineString.length;
   1.131 +          if (pos < 0) continue;
   1.132 +          if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
   1.133 +          didSomething = true;
   1.134 +          self.replaceRange("", Pos(i, pos), Pos(i, endPos));
   1.135 +        }
   1.136 +      });
   1.137 +      if (didSomething) return true;
   1.138 +    }
   1.139 +
   1.140 +    // Try block comments
   1.141 +    var startString = options.blockCommentStart || mode.blockCommentStart;
   1.142 +    var endString = options.blockCommentEnd || mode.blockCommentEnd;
   1.143 +    if (!startString || !endString) return false;
   1.144 +    var lead = options.blockCommentLead || mode.blockCommentLead;
   1.145 +    var startLine = self.getLine(start), endLine = end == start ? startLine : self.getLine(end);
   1.146 +    var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endString);
   1.147 +    if (close == -1 && start != end) {
   1.148 +      endLine = self.getLine(--end);
   1.149 +      close = endLine.lastIndexOf(endString);
   1.150 +    }
   1.151 +    if (open == -1 || close == -1 ||
   1.152 +        !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) ||
   1.153 +        !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1))))
   1.154 +      return false;
   1.155 +
   1.156 +    self.operation(function() {
   1.157 +      self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
   1.158 +                        Pos(end, close + endString.length));
   1.159 +      var openEnd = open + startString.length;
   1.160 +      if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
   1.161 +      self.replaceRange("", Pos(start, open), Pos(start, openEnd));
   1.162 +      if (lead) for (var i = start + 1; i <= end; ++i) {
   1.163 +        var line = self.getLine(i), found = line.indexOf(lead);
   1.164 +        if (found == -1 || nonWS.test(line.slice(0, found))) continue;
   1.165 +        var foundEnd = found + lead.length;
   1.166 +        if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
   1.167 +        self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
   1.168 +      }
   1.169 +    });
   1.170 +    return true;
   1.171 +  });
   1.172 +});

mercurial