github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/addon/comment/comment.js (about)

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