comment.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. "use strict";
  12. var noOptions = {};
  13. var nonWS = /[^\s\u00a0]/;
  14. var Pos = CodeMirror.Pos;
  15. function firstNonWS(str) {
  16. var found = str.search(nonWS);
  17. return found == -1 ? 0 : found;
  18. }
  19. CodeMirror.commands.toggleComment = function(cm) {
  20. cm.toggleComment();
  21. };
  22. CodeMirror.defineExtension("toggleComment", function(options) {
  23. if (!options) options = noOptions;
  24. var cm = this;
  25. var minLine = Infinity, ranges = this.listSelections(), mode = null;
  26. for (var i = ranges.length - 1; i >= 0; i--) {
  27. var from = ranges[i].from(), to = ranges[i].to();
  28. if (from.line >= minLine) continue;
  29. if (to.line >= minLine) to = Pos(minLine, 0);
  30. minLine = from.line;
  31. if (mode == null) {
  32. if (cm.uncomment(from, to, options)) mode = "un";
  33. else { cm.lineComment(from, to, options); mode = "line"; }
  34. } else if (mode == "un") {
  35. cm.uncomment(from, to, options);
  36. } else {
  37. cm.lineComment(from, to, options);
  38. }
  39. }
  40. });
  41. // Rough heuristic to try and detect lines that are part of multi-line string
  42. function probablyInsideString(cm, pos, line) {
  43. return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"`]/.test(line)
  44. }
  45. CodeMirror.defineExtension("lineComment", function(from, to, options) {
  46. if (!options) options = noOptions;
  47. var self = this, mode = self.getModeAt(from);
  48. var firstLine = self.getLine(from.line);
  49. if (firstLine == null || probablyInsideString(self, from, firstLine)) return;
  50. var commentString = options.lineComment || mode.lineComment;
  51. if (!commentString) {
  52. if (options.blockCommentStart || mode.blockCommentStart) {
  53. options.fullLines = true;
  54. self.blockComment(from, to, options);
  55. }
  56. return;
  57. }
  58. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
  59. var pad = options.padding == null ? " " : options.padding;
  60. var blankLines = options.commentBlankLines || from.line == to.line;
  61. self.operation(function() {
  62. if (options.indent) {
  63. var baseString = null;
  64. for (var i = from.line; i < end; ++i) {
  65. var line = self.getLine(i);
  66. var whitespace = line.slice(0, firstNonWS(line));
  67. if (baseString == null || baseString.length > whitespace.length) {
  68. baseString = whitespace;
  69. }
  70. }
  71. for (var i = from.line; i < end; ++i) {
  72. var line = self.getLine(i), cut = baseString.length;
  73. if (!blankLines && !nonWS.test(line)) continue;
  74. if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
  75. self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
  76. }
  77. } else {
  78. for (var i = from.line; i < end; ++i) {
  79. if (blankLines || nonWS.test(self.getLine(i)))
  80. self.replaceRange(commentString + pad, Pos(i, 0));
  81. }
  82. }
  83. });
  84. });
  85. CodeMirror.defineExtension("blockComment", function(from, to, options) {
  86. if (!options) options = noOptions;
  87. var self = this, mode = self.getModeAt(from);
  88. var startString = options.blockCommentStart || mode.blockCommentStart;
  89. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  90. if (!startString || !endString) {
  91. if ((options.lineComment || mode.lineComment) && options.fullLines != false)
  92. self.lineComment(from, to, options);
  93. return;
  94. }
  95. if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return
  96. var end = Math.min(to.line, self.lastLine());
  97. if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
  98. var pad = options.padding == null ? " " : options.padding;
  99. if (from.line > end) return;
  100. self.operation(function() {
  101. if (options.fullLines != false) {
  102. var lastLineHasText = nonWS.test(self.getLine(end));
  103. self.replaceRange(pad + endString, Pos(end));
  104. self.replaceRange(startString + pad, Pos(from.line, 0));
  105. var lead = options.blockCommentLead || mode.blockCommentLead;
  106. if (lead != null) for (var i = from.line + 1; i <= end; ++i)
  107. if (i != end || lastLineHasText)
  108. self.replaceRange(lead + pad, Pos(i, 0));
  109. } else {
  110. self.replaceRange(endString, to);
  111. self.replaceRange(startString, from);
  112. }
  113. });
  114. });
  115. CodeMirror.defineExtension("uncomment", function(from, to, options) {
  116. if (!options) options = noOptions;
  117. var self = this, mode = self.getModeAt(from);
  118. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end);
  119. // Try finding line comments
  120. var lineString = options.lineComment || mode.lineComment, lines = [];
  121. var pad = options.padding == null ? " " : options.padding, didSomething;
  122. lineComment: {
  123. if (!lineString) break lineComment;
  124. for (var i = start; i <= end; ++i) {
  125. var line = self.getLine(i);
  126. var found = line.indexOf(lineString);
  127. if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;
  128. if (found == -1 && nonWS.test(line)) break lineComment;
  129. if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
  130. lines.push(line);
  131. }
  132. self.operation(function() {
  133. for (var i = start; i <= end; ++i) {
  134. var line = lines[i - start];
  135. var pos = line.indexOf(lineString), endPos = pos + lineString.length;
  136. if (pos < 0) continue;
  137. if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
  138. didSomething = true;
  139. self.replaceRange("", Pos(i, pos), Pos(i, endPos));
  140. }
  141. });
  142. if (didSomething) return true;
  143. }
  144. // Try block comments
  145. var startString = options.blockCommentStart || mode.blockCommentStart;
  146. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  147. if (!startString || !endString) return false;
  148. var lead = options.blockCommentLead || mode.blockCommentLead;
  149. var startLine = self.getLine(start), open = startLine.indexOf(startString)
  150. if (open == -1) return false
  151. var endLine = end == start ? startLine : self.getLine(end)
  152. var close = endLine.indexOf(endString, end == start ? open + startString.length : 0);
  153. if (close == -1 && start != end) {
  154. endLine = self.getLine(--end);
  155. close = endLine.indexOf(endString);
  156. }
  157. if (close == -1 ||
  158. !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) ||
  159. !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1))))
  160. return false;
  161. // Avoid killing block comments completely outside the selection.
  162. // Positions of the last startString before the start of the selection, and the first endString after it.
  163. var lastStart = startLine.lastIndexOf(startString, from.ch);
  164. var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(endString, lastStart + startString.length);
  165. if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from.ch) return false;
  166. // Positions of the first endString after the end of the selection, and the last startString before it.
  167. firstEnd = endLine.indexOf(endString, to.ch);
  168. var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch);
  169. lastStart = (firstEnd == -1 || almostLastStart == -1) ? -1 : to.ch + almostLastStart;
  170. if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false;
  171. self.operation(function() {
  172. self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
  173. Pos(end, close + endString.length));
  174. var openEnd = open + startString.length;
  175. if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
  176. self.replaceRange("", Pos(start, open), Pos(start, openEnd));
  177. if (lead) for (var i = start + 1; i <= end; ++i) {
  178. var line = self.getLine(i), found = line.indexOf(lead);
  179. if (found == -1 || nonWS.test(line.slice(0, found))) continue;
  180. var foundEnd = found + lead.length;
  181. if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
  182. self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
  183. }
  184. });
  185. return true;
  186. });
  187. });