2
0

comment.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. (function() {
  2. "use strict";
  3. var noOptions = {};
  4. var nonWS = /[^\s\u00a0]/;
  5. var Pos = CodeMirror.Pos;
  6. function firstNonWS(str) {
  7. var found = str.search(nonWS);
  8. return found == -1 ? 0 : found;
  9. }
  10. CodeMirror.commands.toggleComment = function(cm) {
  11. var from = cm.getCursor("start"), to = cm.getCursor("end");
  12. cm.uncomment(from, to) || cm.lineComment(from, to);
  13. };
  14. CodeMirror.defineExtension("lineComment", function(from, to, options) {
  15. if (!options) options = noOptions;
  16. var self = this, mode = CodeMirror.innerMode(self.getMode(), self.getTokenAt(from).state).mode;
  17. var commentString = options.lineComment || mode.lineComment;
  18. if (!commentString) {
  19. if (options.blockCommentStart || mode.blockCommentStart) {
  20. options.fullLines = true;
  21. self.blockComment(from, to, options);
  22. }
  23. return;
  24. }
  25. var firstLine = self.getLine(from.line);
  26. if (firstLine == null) return;
  27. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
  28. var pad = options.padding == null ? " " : options.padding;
  29. var blankLines = options.commentBlankLines;
  30. self.operation(function() {
  31. if (options.indent) {
  32. var baseString = firstLine.slice(0, firstNonWS(firstLine));
  33. for (var i = from.line; i < end; ++i) {
  34. var line = self.getLine(i), cut = baseString.length;
  35. if (!blankLines && !nonWS.test(line)) continue;
  36. if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
  37. self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
  38. }
  39. } else {
  40. for (var i = from.line; i < end; ++i) {
  41. if (blankLines || nonWS.test(self.getLine(i)))
  42. self.replaceRange(commentString + pad, Pos(i, 0));
  43. }
  44. }
  45. });
  46. });
  47. CodeMirror.defineExtension("blockComment", function(from, to, options) {
  48. if (!options) options = noOptions;
  49. var self = this, mode = CodeMirror.innerMode(self.getMode(), self.getTokenAt(from).state).mode;
  50. var startString = options.blockCommentStart || mode.blockCommentStart;
  51. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  52. if (!startString || !endString) {
  53. if ((options.lineComment || mode.lineComment) && options.fullLines != false)
  54. self.lineComment(from, to, options);
  55. return;
  56. }
  57. var end = Math.min(to.line, self.lastLine());
  58. if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
  59. var pad = options.padding == null ? " " : options.padding;
  60. if (from.line > end) return;
  61. self.operation(function() {
  62. if (options.fullLines != false) {
  63. var lastLineHasText = nonWS.test(self.getLine(end));
  64. self.replaceRange(pad + endString, Pos(end));
  65. self.replaceRange(startString + pad, Pos(from.line, 0));
  66. var lead = options.blockCommentLead || mode.blockCommentLead;
  67. if (lead != null) for (var i = from.line + 1; i <= end; ++i)
  68. if (i != end || lastLineHasText)
  69. self.replaceRange(lead + pad, Pos(i, 0));
  70. } else {
  71. self.replaceRange(endString, to);
  72. self.replaceRange(startString, from);
  73. }
  74. });
  75. });
  76. CodeMirror.defineExtension("uncomment", function(from, to, options) {
  77. if (!options) options = noOptions;
  78. var self = this, mode = CodeMirror.innerMode(self.getMode(), self.getTokenAt(from).state).mode;
  79. var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end);
  80. // Try finding line comments
  81. var lineString = options.lineComment || mode.lineComment, lines = [];
  82. var pad = options.padding == null ? " " : options.padding;
  83. lineComment: for(;;) {
  84. if (!lineString) break;
  85. for (var i = start; i <= end; ++i) {
  86. var line = self.getLine(i);
  87. var found = line.indexOf(lineString);
  88. if (found == -1 && (i != end || i == start) && nonWS.test(line)) break lineComment;
  89. if (i != start && nonWS.test(line.slice(0, found))) break lineComment;
  90. lines.push(line);
  91. }
  92. self.operation(function() {
  93. for (var i = start; i <= end; ++i) {
  94. var line = lines[i - start];
  95. var pos = line.indexOf(lineString), endPos = pos + lineString.length;
  96. if (pos < 0) continue;
  97. if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
  98. self.replaceRange("", Pos(i, pos), Pos(i, endPos));
  99. }
  100. });
  101. return true;
  102. }
  103. // Try block comments
  104. var startString = options.blockCommentStart || mode.blockCommentStart;
  105. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  106. if (!startString || !endString) return false;
  107. var lead = options.blockCommentLead || mode.blockCommentLead;
  108. var startLine = self.getLine(start), endLine = end == start ? startLine : self.getLine(end);
  109. var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endString);
  110. if (close == -1 && start != end) {
  111. endLine = self.getLine(--end);
  112. close = endLine.lastIndexOf(endString);
  113. }
  114. if (open == -1 || close == -1) return false;
  115. self.operation(function() {
  116. self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
  117. Pos(end, close + endString.length));
  118. var openEnd = open + startString.length;
  119. if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
  120. self.replaceRange("", Pos(start, open), Pos(start, openEnd));
  121. if (lead) for (var i = start + 1; i <= end; ++i) {
  122. var line = self.getLine(i), found = line.indexOf(lead);
  123. if (found == -1 || nonWS.test(line.slice(0, found))) continue;
  124. var foundEnd = found + lead.length;
  125. if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
  126. self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
  127. }
  128. });
  129. return true;
  130. });
  131. })();