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.

comment.js 9.0KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "use strict";
  12. var noOptions = {};
  13. var nonWS = /[^\s\u00a0]/;
  14. var Pos = CodeMirror.Pos, cmp = CodeMirror.cmpPos;
  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. function getMode(cm, pos) {
  46. var mode = cm.getMode()
  47. return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getModeAt(pos)
  48. }
  49. CodeMirror.defineExtension("lineComment", function(from, to, options) {
  50. if (!options) options = noOptions;
  51. var self = this, mode = getMode(self, from);
  52. var firstLine = self.getLine(from.line);
  53. if (firstLine == null || probablyInsideString(self, from, firstLine)) return;
  54. var commentString = options.lineComment || mode.lineComment;
  55. if (!commentString) {
  56. if (options.blockCommentStart || mode.blockCommentStart) {
  57. options.fullLines = true;
  58. self.blockComment(from, to, options);
  59. }
  60. return;
  61. }
  62. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1);
  63. var pad = options.padding == null ? " " : options.padding;
  64. var blankLines = options.commentBlankLines || from.line == to.line;
  65. self.operation(function() {
  66. if (options.indent) {
  67. var baseString = null;
  68. for (var i = from.line; i < end; ++i) {
  69. var line = self.getLine(i);
  70. var whitespace = line.slice(0, firstNonWS(line));
  71. if (baseString == null || baseString.length > whitespace.length) {
  72. baseString = whitespace;
  73. }
  74. }
  75. for (var i = from.line; i < end; ++i) {
  76. var line = self.getLine(i), cut = baseString.length;
  77. if (!blankLines && !nonWS.test(line)) continue;
  78. if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
  79. self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut));
  80. }
  81. } else {
  82. for (var i = from.line; i < end; ++i) {
  83. if (blankLines || nonWS.test(self.getLine(i)))
  84. self.replaceRange(commentString + pad, Pos(i, 0));
  85. }
  86. }
  87. });
  88. });
  89. CodeMirror.defineExtension("blockComment", function(from, to, options) {
  90. if (!options) options = noOptions;
  91. var self = this, mode = getMode(self, from);
  92. var startString = options.blockCommentStart || mode.blockCommentStart;
  93. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  94. if (!startString || !endString) {
  95. if ((options.lineComment || mode.lineComment) && options.fullLines != false)
  96. self.lineComment(from, to, options);
  97. return;
  98. }
  99. if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return
  100. var end = Math.min(to.line, self.lastLine());
  101. if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
  102. var pad = options.padding == null ? " " : options.padding;
  103. if (from.line > end) return;
  104. self.operation(function() {
  105. if (options.fullLines != false) {
  106. var lastLineHasText = nonWS.test(self.getLine(end));
  107. self.replaceRange(pad + endString, Pos(end));
  108. self.replaceRange(startString + pad, Pos(from.line, 0));
  109. var lead = options.blockCommentLead || mode.blockCommentLead;
  110. if (lead != null) for (var i = from.line + 1; i <= end; ++i)
  111. if (i != end || lastLineHasText)
  112. self.replaceRange(lead + pad, Pos(i, 0));
  113. } else {
  114. var atCursor = cmp(self.getCursor("to"), to) == 0, empty = !self.somethingSelected()
  115. self.replaceRange(endString, to);
  116. if (atCursor) self.setSelection(empty ? to : self.getCursor("from"), to)
  117. self.replaceRange(startString, from);
  118. }
  119. });
  120. });
  121. CodeMirror.defineExtension("uncomment", function(from, to, options) {
  122. if (!options) options = noOptions;
  123. var self = this, mode = getMode(self, from);
  124. var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end);
  125. // Try finding line comments
  126. var lineString = options.lineComment || mode.lineComment, lines = [];
  127. var pad = options.padding == null ? " " : options.padding, didSomething;
  128. lineComment: {
  129. if (!lineString) break lineComment;
  130. for (var i = start; i <= end; ++i) {
  131. var line = self.getLine(i);
  132. var found = line.indexOf(lineString);
  133. if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1;
  134. if (found == -1 && nonWS.test(line)) break lineComment;
  135. if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
  136. lines.push(line);
  137. }
  138. self.operation(function() {
  139. for (var i = start; i <= end; ++i) {
  140. var line = lines[i - start];
  141. var pos = line.indexOf(lineString), endPos = pos + lineString.length;
  142. if (pos < 0) continue;
  143. if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length;
  144. didSomething = true;
  145. self.replaceRange("", Pos(i, pos), Pos(i, endPos));
  146. }
  147. });
  148. if (didSomething) return true;
  149. }
  150. // Try block comments
  151. var startString = options.blockCommentStart || mode.blockCommentStart;
  152. var endString = options.blockCommentEnd || mode.blockCommentEnd;
  153. if (!startString || !endString) return false;
  154. var lead = options.blockCommentLead || mode.blockCommentLead;
  155. var startLine = self.getLine(start), open = startLine.indexOf(startString)
  156. if (open == -1) return false
  157. var endLine = end == start ? startLine : self.getLine(end)
  158. var close = endLine.indexOf(endString, end == start ? open + startString.length : 0);
  159. var insideStart = Pos(start, open + 1), insideEnd = Pos(end, close + 1)
  160. if (close == -1 ||
  161. !/comment/.test(self.getTokenTypeAt(insideStart)) ||
  162. !/comment/.test(self.getTokenTypeAt(insideEnd)) ||
  163. self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1)
  164. return false;
  165. // Avoid killing block comments completely outside the selection.
  166. // Positions of the last startString before the start of the selection, and the first endString after it.
  167. var lastStart = startLine.lastIndexOf(startString, from.ch);
  168. var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(endString, lastStart + startString.length);
  169. if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from.ch) return false;
  170. // Positions of the first endString after the end of the selection, and the last startString before it.
  171. firstEnd = endLine.indexOf(endString, to.ch);
  172. var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch);
  173. lastStart = (firstEnd == -1 || almostLastStart == -1) ? -1 : to.ch + almostLastStart;
  174. if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false;
  175. self.operation(function() {
  176. self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)),
  177. Pos(end, close + endString.length));
  178. var openEnd = open + startString.length;
  179. if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length;
  180. self.replaceRange("", Pos(start, open), Pos(start, openEnd));
  181. if (lead) for (var i = start + 1; i <= end; ++i) {
  182. var line = self.getLine(i), found = line.indexOf(lead);
  183. if (found == -1 || nonWS.test(line.slice(0, found))) continue;
  184. var foundEnd = found + lead.length;
  185. if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
  186. self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
  187. }
  188. });
  189. return true;
  190. });
  191. });