matchbrackets.js 3.7 KB

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