You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.3 KiB

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