continuecomment.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. (function() {
  2. var modes = ["clike", "css", "javascript"];
  3. for (var i = 0; i < modes.length; ++i)
  4. CodeMirror.extendMode(modes[i], {blockCommentStart: "/*",
  5. blockCommentEnd: "*/",
  6. blockCommentContinue: " * "});
  7. CodeMirror.commands.newlineAndIndentContinueComment = function(cm) {
  8. var pos = cm.getCursor(), token = cm.getTokenAt(pos);
  9. var mode = CodeMirror.innerMode(cm.getMode(), token.state).mode;
  10. var space;
  11. if (token.type == "comment" && mode.blockCommentStart) {
  12. var end = token.string.indexOf(mode.blockCommentEnd);
  13. var full = cm.getRange({line: pos.line, ch: 0}, {line: pos.line, ch: token.end}), found;
  14. if (end != -1 && end == token.string.length - mode.blockCommentEnd.length) {
  15. // Comment ended, don't continue it
  16. } else if (token.string.indexOf(mode.blockCommentStart) == 0) {
  17. space = full.slice(0, token.start);
  18. if (!/^\s*$/.test(space)) {
  19. space = "";
  20. for (var i = 0; i < token.start; ++i) space += " ";
  21. }
  22. } else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
  23. found + mode.blockCommentContinue.length > token.start &&
  24. /^\s*$/.test(full.slice(0, found))) {
  25. space = full.slice(0, found);
  26. }
  27. }
  28. if (space != null)
  29. cm.replaceSelection("\n" + space + mode.blockCommentContinue, "end");
  30. else
  31. cm.execCommand("newlineAndIndent");
  32. };
  33. })();