github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/addon/comment/continuecomment.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    var modes = ["clike", "css", "javascript"];
    13  
    14    for (var i = 0; i < modes.length; ++i)
    15      CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "});
    16  
    17    function continueComment(cm) {
    18      if (cm.getOption("disableInput")) return CodeMirror.Pass;
    19      var ranges = cm.listSelections(), mode, inserts = [];
    20      for (var i = 0; i < ranges.length; i++) {
    21        var pos = ranges[i].head, token = cm.getTokenAt(pos);
    22        if (token.type != "comment") return CodeMirror.Pass;
    23        var modeHere = CodeMirror.innerMode(cm.getMode(), token.state).mode;
    24        if (!mode) mode = modeHere;
    25        else if (mode != modeHere) return CodeMirror.Pass;
    26  
    27        var insert = null;
    28        if (mode.blockCommentStart && mode.blockCommentContinue) {
    29          var end = token.string.indexOf(mode.blockCommentEnd);
    30          var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
    31          if (end != -1 && end == token.string.length - mode.blockCommentEnd.length && pos.ch >= end) {
    32            // Comment ended, don't continue it
    33          } else if (token.string.indexOf(mode.blockCommentStart) == 0) {
    34            insert = full.slice(0, token.start);
    35            if (!/^\s*$/.test(insert)) {
    36              insert = "";
    37              for (var j = 0; j < token.start; ++j) insert += " ";
    38            }
    39          } else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
    40                     found + mode.blockCommentContinue.length > token.start &&
    41                     /^\s*$/.test(full.slice(0, found))) {
    42            insert = full.slice(0, found);
    43          }
    44          if (insert != null) insert += mode.blockCommentContinue;
    45        }
    46        if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) {
    47          var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
    48          if (found > -1) {
    49            insert = line.slice(0, found);
    50            if (/\S/.test(insert)) insert = null;
    51            else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
    52          }
    53        }
    54        if (insert == null) return CodeMirror.Pass;
    55        inserts[i] = "\n" + insert;
    56      }
    57  
    58      cm.operation(function() {
    59        for (var i = ranges.length - 1; i >= 0; i--)
    60          cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
    61      });
    62    }
    63  
    64    function continueLineCommentEnabled(cm) {
    65      var opt = cm.getOption("continueComments");
    66      if (opt && typeof opt == "object")
    67        return opt.continueLineComment !== false;
    68      return true;
    69    }
    70  
    71    CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
    72      if (prev && prev != CodeMirror.Init)
    73        cm.removeKeyMap("continueComment");
    74      if (val) {
    75        var key = "Enter";
    76        if (typeof val == "string")
    77          key = val;
    78        else if (typeof val == "object" && val.key)
    79          key = val.key;
    80        var map = {name: "continueComment"};
    81        map[key] = continueComment;
    82        cm.addKeyMap(map);
    83      }
    84    });
    85  });