本站源代码
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.

79 lines
3.0KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. function continueComment(cm) {
  12. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  13. var ranges = cm.listSelections(), mode, inserts = [];
  14. for (var i = 0; i < ranges.length; i++) {
  15. var pos = ranges[i].head
  16. if (!/\bcomment\b/.test(cm.getTokenTypeAt(pos))) return CodeMirror.Pass;
  17. var modeHere = cm.getModeAt(pos)
  18. if (!mode) mode = modeHere;
  19. else if (mode != modeHere) return CodeMirror.Pass;
  20. var insert = null;
  21. if (mode.blockCommentStart && mode.blockCommentContinue) {
  22. var line = cm.getLine(pos.line).slice(0, pos.ch)
  23. var end = line.lastIndexOf(mode.blockCommentEnd), found
  24. if (end != -1 && end == pos.ch - mode.blockCommentEnd.length) {
  25. // Comment ended, don't continue it
  26. } else if ((found = line.lastIndexOf(mode.blockCommentStart)) > -1 && found > end) {
  27. insert = line.slice(0, found)
  28. if (/\S/.test(insert)) {
  29. insert = ""
  30. for (var j = 0; j < found; ++j) insert += " "
  31. }
  32. } else if ((found = line.indexOf(mode.blockCommentContinue)) > -1 && !/\S/.test(line.slice(0, found))) {
  33. insert = line.slice(0, found)
  34. }
  35. if (insert != null) insert += mode.blockCommentContinue
  36. }
  37. if (insert == null && mode.lineComment && continueLineCommentEnabled(cm)) {
  38. var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
  39. if (found > -1) {
  40. insert = line.slice(0, found);
  41. if (/\S/.test(insert)) insert = null;
  42. else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
  43. }
  44. }
  45. if (insert == null) return CodeMirror.Pass;
  46. inserts[i] = "\n" + insert;
  47. }
  48. cm.operation(function() {
  49. for (var i = ranges.length - 1; i >= 0; i--)
  50. cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
  51. });
  52. }
  53. function continueLineCommentEnabled(cm) {
  54. var opt = cm.getOption("continueComments");
  55. if (opt && typeof opt == "object")
  56. return opt.continueLineComment !== false;
  57. return true;
  58. }
  59. CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
  60. if (prev && prev != CodeMirror.Init)
  61. cm.removeKeyMap("continueComment");
  62. if (val) {
  63. var key = "Enter";
  64. if (typeof val == "string")
  65. key = val;
  66. else if (typeof val == "object" && val.key)
  67. key = val.key;
  68. var map = {name: "continueComment"};
  69. map[key] = continueComment;
  70. cm.addKeyMap(map);
  71. }
  72. });
  73. });
上海开阖软件有限公司 沪ICP备12045867号-1