comment.js 6.8 KB

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