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

151 lines
6.3KB

  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 ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
  12. (document.documentMode == null || document.documentMode < 8);
  13. var Pos = CodeMirror.Pos;
  14. var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"};
  15. function bracketRegex(config) {
  16. return config && config.bracketRegex || /[(){}[\]]/
  17. }
  18. function findMatchingBracket(cm, where, config) {
  19. var line = cm.getLineHandle(where.line), pos = where.ch - 1;
  20. var afterCursor = config && config.afterCursor
  21. if (afterCursor == null)
  22. afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className)
  23. var re = bracketRegex(config)
  24. // A cursor is defined as between two characters, but in in vim command mode
  25. // (i.e. not insert mode), the cursor is visually represented as a
  26. // highlighted box on top of the 2nd character. Otherwise, we allow matches
  27. // from before or after the cursor.
  28. var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) ||
  29. re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)];
  30. if (!match) return null;
  31. var dir = match.charAt(1) == ">" ? 1 : -1;
  32. if (config && config.strict && (dir > 0) != (pos == where.ch)) return null;
  33. var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
  34. var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config);
  35. if (found == null) return null;
  36. return {from: Pos(where.line, pos), to: found && found.pos,
  37. match: found && found.ch == match.charAt(0), forward: dir > 0};
  38. }
  39. // bracketRegex is used to specify which type of bracket to scan
  40. // should be a regexp, e.g. /[[\]]/
  41. //
  42. // Note: If "where" is on an open bracket, then this bracket is ignored.
  43. //
  44. // Returns false when no bracket was found, null when it reached
  45. // maxScanLines and gave up
  46. function scanForBracket(cm, where, dir, style, config) {
  47. var maxScanLen = (config && config.maxScanLineLength) || 10000;
  48. var maxScanLines = (config && config.maxScanLines) || 1000;
  49. var stack = [];
  50. var re = bracketRegex(config)
  51. var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
  52. : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
  53. for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
  54. var line = cm.getLine(lineNo);
  55. if (!line) continue;
  56. var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
  57. if (line.length > maxScanLen) continue;
  58. if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
  59. for (; pos != end; pos += dir) {
  60. var ch = line.charAt(pos);
  61. if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
  62. var match = matching[ch];
  63. if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
  64. else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
  65. else stack.pop();
  66. }
  67. }
  68. }
  69. return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
  70. }
  71. function matchBrackets(cm, autoclear, config) {
  72. // Disable brace matching in long lines, since it'll cause hugely slow updates
  73. var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
  74. var marks = [], ranges = cm.listSelections();
  75. for (var i = 0; i < ranges.length; i++) {
  76. var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);
  77. if (match && cm.getLine(match.from.line).length <= maxHighlightLen) {
  78. var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  79. marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
  80. if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
  81. marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
  82. }
  83. }
  84. if (marks.length) {
  85. // Kludge to work around the IE bug from issue #1193, where text
  86. // input stops going to the textare whever this fires.
  87. if (ie_lt8 && cm.state.focused) cm.focus();
  88. var clear = function() {
  89. cm.operation(function() {
  90. for (var i = 0; i < marks.length; i++) marks[i].clear();
  91. });
  92. };
  93. if (autoclear) setTimeout(clear, 800);
  94. else return clear;
  95. }
  96. }
  97. function doMatchBrackets(cm) {
  98. cm.operation(function() {
  99. if (cm.state.matchBrackets.currentlyHighlighted) {
  100. cm.state.matchBrackets.currentlyHighlighted();
  101. cm.state.matchBrackets.currentlyHighlighted = null;
  102. }
  103. cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
  104. });
  105. }
  106. CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
  107. if (old && old != CodeMirror.Init) {
  108. cm.off("cursorActivity", doMatchBrackets);
  109. if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {
  110. cm.state.matchBrackets.currentlyHighlighted();
  111. cm.state.matchBrackets.currentlyHighlighted = null;
  112. }
  113. }
  114. if (val) {
  115. cm.state.matchBrackets = typeof val == "object" ? val : {};
  116. cm.on("cursorActivity", doMatchBrackets);
  117. }
  118. });
  119. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  120. CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){
  121. // Backwards-compatibility kludge
  122. if (oldConfig || typeof config == "boolean") {
  123. if (!oldConfig) {
  124. config = config ? {strict: true} : null
  125. } else {
  126. oldConfig.strict = config
  127. config = oldConfig
  128. }
  129. }
  130. return findMatchingBracket(this, pos, config)
  131. });
  132. CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
  133. return scanForBracket(this, pos, dir, style, config);
  134. });
  135. });
上海开阖软件有限公司 沪ICP备12045867号-1