本站源代码
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.

192 lines
6.6KB

  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. var defaults = {
  12. pairs: "()[]{}''\"\"",
  13. closeBefore: ")]}'\":;>",
  14. triples: "",
  15. explode: "[]{}"
  16. };
  17. var Pos = CodeMirror.Pos;
  18. CodeMirror.defineOption("autoCloseBrackets", false, function(cm, val, old) {
  19. if (old && old != CodeMirror.Init) {
  20. cm.removeKeyMap(keyMap);
  21. cm.state.closeBrackets = null;
  22. }
  23. if (val) {
  24. ensureBound(getOption(val, "pairs"))
  25. cm.state.closeBrackets = val;
  26. cm.addKeyMap(keyMap);
  27. }
  28. });
  29. function getOption(conf, name) {
  30. if (name == "pairs" && typeof conf == "string") return conf;
  31. if (typeof conf == "object" && conf[name] != null) return conf[name];
  32. return defaults[name];
  33. }
  34. var keyMap = {Backspace: handleBackspace, Enter: handleEnter};
  35. function ensureBound(chars) {
  36. for (var i = 0; i < chars.length; i++) {
  37. var ch = chars.charAt(i), key = "'" + ch + "'"
  38. if (!keyMap[key]) keyMap[key] = handler(ch)
  39. }
  40. }
  41. ensureBound(defaults.pairs + "`")
  42. function handler(ch) {
  43. return function(cm) { return handleChar(cm, ch); };
  44. }
  45. function getConfig(cm) {
  46. var deflt = cm.state.closeBrackets;
  47. if (!deflt || deflt.override) return deflt;
  48. var mode = cm.getModeAt(cm.getCursor());
  49. return mode.closeBrackets || deflt;
  50. }
  51. function handleBackspace(cm) {
  52. var conf = getConfig(cm);
  53. if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
  54. var pairs = getOption(conf, "pairs");
  55. var ranges = cm.listSelections();
  56. for (var i = 0; i < ranges.length; i++) {
  57. if (!ranges[i].empty()) return CodeMirror.Pass;
  58. var around = charsAround(cm, ranges[i].head);
  59. if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass;
  60. }
  61. for (var i = ranges.length - 1; i >= 0; i--) {
  62. var cur = ranges[i].head;
  63. cm.replaceRange("", Pos(cur.line, cur.ch - 1), Pos(cur.line, cur.ch + 1), "+delete");
  64. }
  65. }
  66. function handleEnter(cm) {
  67. var conf = getConfig(cm);
  68. var explode = conf && getOption(conf, "explode");
  69. if (!explode || cm.getOption("disableInput")) return CodeMirror.Pass;
  70. var ranges = cm.listSelections();
  71. for (var i = 0; i < ranges.length; i++) {
  72. if (!ranges[i].empty()) return CodeMirror.Pass;
  73. var around = charsAround(cm, ranges[i].head);
  74. if (!around || explode.indexOf(around) % 2 != 0) return CodeMirror.Pass;
  75. }
  76. cm.operation(function() {
  77. var linesep = cm.lineSeparator() || "\n";
  78. cm.replaceSelection(linesep + linesep, null);
  79. cm.execCommand("goCharLeft");
  80. ranges = cm.listSelections();
  81. for (var i = 0; i < ranges.length; i++) {
  82. var line = ranges[i].head.line;
  83. cm.indentLine(line, null, true);
  84. cm.indentLine(line + 1, null, true);
  85. }
  86. });
  87. }
  88. function contractSelection(sel) {
  89. var inverted = CodeMirror.cmpPos(sel.anchor, sel.head) > 0;
  90. return {anchor: new Pos(sel.anchor.line, sel.anchor.ch + (inverted ? -1 : 1)),
  91. head: new Pos(sel.head.line, sel.head.ch + (inverted ? 1 : -1))};
  92. }
  93. function handleChar(cm, ch) {
  94. var conf = getConfig(cm);
  95. if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
  96. var pairs = getOption(conf, "pairs");
  97. var pos = pairs.indexOf(ch);
  98. if (pos == -1) return CodeMirror.Pass;
  99. var closeBefore = getOption(conf,"closeBefore");
  100. var triples = getOption(conf, "triples");
  101. var identical = pairs.charAt(pos + 1) == ch;
  102. var ranges = cm.listSelections();
  103. var opening = pos % 2 == 0;
  104. var type;
  105. for (var i = 0; i < ranges.length; i++) {
  106. var range = ranges[i], cur = range.head, curType;
  107. var next = cm.getRange(cur, Pos(cur.line, cur.ch + 1));
  108. if (opening && !range.empty()) {
  109. curType = "surround";
  110. } else if ((identical || !opening) && next == ch) {
  111. if (identical && stringStartsAfter(cm, cur))
  112. curType = "both";
  113. else if (triples.indexOf(ch) >= 0 && cm.getRange(cur, Pos(cur.line, cur.ch + 3)) == ch + ch + ch)
  114. curType = "skipThree";
  115. else
  116. curType = "skip";
  117. } else if (identical && cur.ch > 1 && triples.indexOf(ch) >= 0 &&
  118. cm.getRange(Pos(cur.line, cur.ch - 2), cur) == ch + ch) {
  119. if (cur.ch > 2 && /\bstring/.test(cm.getTokenTypeAt(Pos(cur.line, cur.ch - 2)))) return CodeMirror.Pass;
  120. curType = "addFour";
  121. } else if (identical) {
  122. var prev = cur.ch == 0 ? " " : cm.getRange(Pos(cur.line, cur.ch - 1), cur)
  123. if (!CodeMirror.isWordChar(next) && prev != ch && !CodeMirror.isWordChar(prev)) curType = "both";
  124. else return CodeMirror.Pass;
  125. } else if (opening && (next.length === 0 || /\s/.test(next) || closeBefore.indexOf(next) > -1)) {
  126. curType = "both";
  127. } else {
  128. return CodeMirror.Pass;
  129. }
  130. if (!type) type = curType;
  131. else if (type != curType) return CodeMirror.Pass;
  132. }
  133. var left = pos % 2 ? pairs.charAt(pos - 1) : ch;
  134. var right = pos % 2 ? ch : pairs.charAt(pos + 1);
  135. cm.operation(function() {
  136. if (type == "skip") {
  137. cm.execCommand("goCharRight");
  138. } else if (type == "skipThree") {
  139. for (var i = 0; i < 3; i++)
  140. cm.execCommand("goCharRight");
  141. } else if (type == "surround") {
  142. var sels = cm.getSelections();
  143. for (var i = 0; i < sels.length; i++)
  144. sels[i] = left + sels[i] + right;
  145. cm.replaceSelections(sels, "around");
  146. sels = cm.listSelections().slice();
  147. for (var i = 0; i < sels.length; i++)
  148. sels[i] = contractSelection(sels[i]);
  149. cm.setSelections(sels);
  150. } else if (type == "both") {
  151. cm.replaceSelection(left + right, null);
  152. cm.triggerElectric(left + right);
  153. cm.execCommand("goCharLeft");
  154. } else if (type == "addFour") {
  155. cm.replaceSelection(left + left + left + left, "before");
  156. cm.execCommand("goCharRight");
  157. }
  158. });
  159. }
  160. function charsAround(cm, pos) {
  161. var str = cm.getRange(Pos(pos.line, pos.ch - 1),
  162. Pos(pos.line, pos.ch + 1));
  163. return str.length == 2 ? str : null;
  164. }
  165. function stringStartsAfter(cm, pos) {
  166. var token = cm.getTokenAt(Pos(pos.line, pos.ch + 1))
  167. return /\bstring/.test(token.type) && token.start == pos.ch &&
  168. (pos.ch == 0 || !/\bstring/.test(cm.getTokenTypeAt(pos)))
  169. }
  170. });
上海开阖软件有限公司 沪ICP备12045867号-1