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.

161 lines
6.7KB

  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, 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 ||
  62. (cm.getTokenTypeAt(Pos(lineNo, pos + 1)) || "") == (style || ""))) {
  63. var match = matching[ch];
  64. if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
  65. else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
  66. else stack.pop();
  67. }
  68. }
  69. }
  70. return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
  71. }
  72. function matchBrackets(cm, autoclear, config) {
  73. // Disable brace matching in long lines, since it'll cause hugely slow updates
  74. var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000,
  75. highlightNonMatching = config && config.highlightNonMatching;
  76. var marks = [], ranges = cm.listSelections();
  77. for (var i = 0; i < ranges.length; i++) {
  78. var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);
  79. if (match && (match.match || highlightNonMatching !== false) && cm.getLine(match.from.line).length <= maxHighlightLen) {
  80. var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  81. marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
  82. if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
  83. marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
  84. }
  85. }
  86. if (marks.length) {
  87. // Kludge to work around the IE bug from issue #1193, where text
  88. // input stops going to the textarea whenever this fires.
  89. if (ie_lt8 && cm.state.focused) cm.focus();
  90. var clear = function() {
  91. cm.operation(function() {
  92. for (var i = 0; i < marks.length; i++) marks[i].clear();
  93. });
  94. };
  95. if (autoclear) setTimeout(clear, 800);
  96. else return clear;
  97. }
  98. }
  99. function doMatchBrackets(cm) {
  100. cm.operation(function() {
  101. if (cm.state.matchBrackets.currentlyHighlighted) {
  102. cm.state.matchBrackets.currentlyHighlighted();
  103. cm.state.matchBrackets.currentlyHighlighted = null;
  104. }
  105. cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
  106. });
  107. }
  108. function clearHighlighted(cm) {
  109. if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {
  110. cm.state.matchBrackets.currentlyHighlighted();
  111. cm.state.matchBrackets.currentlyHighlighted = null;
  112. }
  113. }
  114. CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
  115. if (old && old != CodeMirror.Init) {
  116. cm.off("cursorActivity", doMatchBrackets);
  117. cm.off("focus", doMatchBrackets)
  118. cm.off("blur", clearHighlighted)
  119. clearHighlighted(cm);
  120. }
  121. if (val) {
  122. cm.state.matchBrackets = typeof val == "object" ? val : {};
  123. cm.on("cursorActivity", doMatchBrackets);
  124. cm.on("focus", doMatchBrackets)
  125. cm.on("blur", clearHighlighted)
  126. }
  127. });
  128. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  129. CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){
  130. // Backwards-compatibility kludge
  131. if (oldConfig || typeof config == "boolean") {
  132. if (!oldConfig) {
  133. config = config ? {strict: true} : null
  134. } else {
  135. oldConfig.strict = config
  136. config = oldConfig
  137. }
  138. }
  139. return findMatchingBracket(this, pos, config)
  140. });
  141. CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
  142. return scanForBracket(this, pos, dir, style, config);
  143. });
  144. });