matchbrackets.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function() {
  2. var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
  3. (document.documentMode == null || document.documentMode < 8);
  4. var Pos = CodeMirror.Pos;
  5. var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
  6. function findMatchingBracket(cm) {
  7. var maxScanLen = cm.state._matchBrackets.maxScanLineLength || 10000;
  8. var cur = cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1;
  9. var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
  10. if (!match) return null;
  11. var forward = match.charAt(1) == ">", d = forward ? 1 : -1;
  12. var style = cm.getTokenAt(Pos(cur.line, pos + 1)).type;
  13. var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
  14. function scan(line, lineNo, start) {
  15. if (!line.text) return;
  16. var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1;
  17. if (line.text.length > maxScanLen) return null;
  18. var checkTokenStyles = line.text.length < 1000;
  19. if (start != null) pos = start + d;
  20. for (; pos != end; pos += d) {
  21. var ch = line.text.charAt(pos);
  22. if (re.test(ch) && (!checkTokenStyles || cm.getTokenAt(Pos(lineNo, pos + 1)).type == style)) {
  23. var match = matching[ch];
  24. if (match.charAt(1) == ">" == forward) stack.push(ch);
  25. else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
  26. else if (!stack.length) return {pos: pos, match: true};
  27. }
  28. }
  29. }
  30. for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) {
  31. if (i == cur.line) found = scan(line, i, pos);
  32. else found = scan(cm.getLineHandle(i), i);
  33. if (found) break;
  34. }
  35. return {from: Pos(cur.line, pos), to: found && Pos(i, found.pos), match: found && found.match};
  36. }
  37. function matchBrackets(cm, autoclear) {
  38. // Disable brace matching in long lines, since it'll cause hugely slow updates
  39. var maxHighlightLen = cm.state._matchBrackets.maxHighlightLineLength || 1000;
  40. var found = findMatchingBracket(cm);
  41. if (!found || cm.getLine(found.from.line).length > maxHighlightLen ||
  42. found.to && cm.getLine(found.to.line).length > maxHighlightLen)
  43. return;
  44. var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  45. var one = cm.markText(found.from, Pos(found.from.line, found.from.ch + 1), {className: style});
  46. var two = found.to && cm.markText(found.to, Pos(found.to.line, found.to.ch + 1), {className: style});
  47. // Kludge to work around the IE bug from issue #1193, where text
  48. // input stops going to the textare whever this fires.
  49. if (ie_lt8 && cm.state.focused) cm.display.input.focus();
  50. var clear = function() {
  51. cm.operation(function() { one.clear(); two && two.clear(); });
  52. };
  53. if (autoclear) setTimeout(clear, 800);
  54. else return clear;
  55. }
  56. var currentlyHighlighted = null;
  57. function doMatchBrackets(cm) {
  58. cm.operation(function() {
  59. if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
  60. if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false);
  61. });
  62. }
  63. CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
  64. if (old && old != CodeMirror.Init)
  65. cm.off("cursorActivity", doMatchBrackets);
  66. if (val) {
  67. cm.state._matchBrackets = typeof val == "object" ? val : {};
  68. cm.on("cursorActivity", doMatchBrackets);
  69. }
  70. });
  71. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  72. CodeMirror.defineExtension("findMatchingBracket", function(){return findMatchingBracket(this);});
  73. })();