1
0

continuecomment.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. function continueComment(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(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, 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. return CodeMirror.Pass;
  32. }
  33. CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
  34. if (prev && prev != CodeMirror.Init)
  35. cm.removeKeyMap("continueComment");
  36. var map = {name: "continueComment"};
  37. map[typeof val == "string" ? val : "Enter"] = continueComment;
  38. cm.addKeyMap(map);
  39. });
  40. })();