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.

183 lines
7.8 KiB

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