1
0

matchbrackets.js 3.3 KB

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