continuecomment.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. var modes = ["clike", "css", "javascript"];
  10. for (var i = 0; i < modes.length; ++i)
  11. CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "});
  12. function continueComment(cm) {
  13. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  14. var ranges = cm.listSelections(), mode, inserts = [];
  15. for (var i = 0; i < ranges.length; i++) {
  16. var pos = ranges[i].head, token = cm.getTokenAt(pos);
  17. if (token.type != "comment") return CodeMirror.Pass;
  18. var modeHere = CodeMirror.innerMode(cm.getMode(), token.state).mode;
  19. if (!mode) mode = modeHere;
  20. else if (mode != modeHere) return CodeMirror.Pass;
  21. var insert = null;
  22. if (mode.blockCommentStart && mode.blockCommentContinue) {
  23. var end = token.string.indexOf(mode.blockCommentEnd);
  24. var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
  25. if (end != -1 && end == token.string.length - mode.blockCommentEnd.length && pos.ch >= end) {
  26. // Comment ended, don't continue it
  27. } else if (token.string.indexOf(mode.blockCommentStart) == 0) {
  28. insert = full.slice(0, token.start);
  29. if (!/^\s*$/.test(insert)) {
  30. insert = "";
  31. for (var j = 0; j < token.start; ++j) insert += " ";
  32. }
  33. } else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
  34. found + mode.blockCommentContinue.length > token.start &&
  35. /^\s*$/.test(full.slice(0, found))) {
  36. insert = full.slice(0, found);
  37. }
  38. if (insert != null) insert += mode.blockCommentContinue;
  39. }
  40. if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) {
  41. var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
  42. if (found > -1) {
  43. insert = line.slice(0, found);
  44. if (/\S/.test(insert)) insert = null;
  45. else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
  46. }
  47. }
  48. if (insert == null) return CodeMirror.Pass;
  49. inserts[i] = "\n" + insert;
  50. }
  51. cm.operation(function() {
  52. for (var i = ranges.length - 1; i >= 0; i--)
  53. cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
  54. });
  55. }
  56. function continueLineCommentEnabled(cm) {
  57. var opt = cm.getOption("continueComments");
  58. if (opt && typeof opt == "object")
  59. return opt.continueLineComment !== false;
  60. return true;
  61. }
  62. CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
  63. if (prev && prev != CodeMirror.Init)
  64. cm.removeKeyMap("continueComment");
  65. if (val) {
  66. var key = "Enter";
  67. if (typeof val == "string")
  68. key = val;
  69. else if (typeof val == "object" && val.key)
  70. key = val.key;
  71. var map = {name: "continueComment"};
  72. map[key] = continueComment;
  73. cm.addKeyMap(map);
  74. }
  75. });
  76. });