matchbrackets.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function() {
  2. var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
  3. function findMatchingBracket(cm) {
  4. var cur = cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1;
  5. var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
  6. if (!match) return null;
  7. var forward = match.charAt(1) == ">", d = forward ? 1 : -1;
  8. var style = cm.getTokenAt({line: cur.line, ch: pos + 1}).type;
  9. var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
  10. function scan(line, lineNo, start) {
  11. if (!line.text) return;
  12. var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1;
  13. if (start != null) pos = start + d;
  14. for (; pos != end; pos += d) {
  15. var ch = line.text.charAt(pos);
  16. if (re.test(ch) && cm.getTokenAt({line: lineNo, ch: pos + 1}).type == style) {
  17. var match = matching[ch];
  18. if (match.charAt(1) == ">" == forward) stack.push(ch);
  19. else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
  20. else if (!stack.length) return {pos: pos, match: true};
  21. }
  22. }
  23. }
  24. for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) {
  25. if (i == cur.line) found = scan(line, i, pos);
  26. else found = scan(cm.getLineHandle(i), i);
  27. if (found) break;
  28. }
  29. return {from: {line: cur.line, ch: pos}, to: found && {line: i, ch: found.pos}, match: found && found.match};
  30. }
  31. function matchBrackets(cm, autoclear) {
  32. var found = findMatchingBracket(cm);
  33. if (!found) return;
  34. var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  35. var one = cm.markText(found.from, {line: found.from.line, ch: found.from.ch + 1},
  36. {className: style});
  37. var two = found.to && cm.markText(found.to, {line: found.to.line, ch: found.to.ch + 1},
  38. {className: style});
  39. var clear = function() {
  40. cm.operation(function() { one.clear(); two && two.clear(); });
  41. };
  42. if (autoclear) setTimeout(clear, 800);
  43. else return clear;
  44. }
  45. var currentlyHighlighted = null;
  46. function doMatchBrackets(cm) {
  47. cm.operation(function() {
  48. if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
  49. if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false);
  50. });
  51. }
  52. CodeMirror.defineOption("matchBrackets", false, function(cm, val) {
  53. if (val) cm.on("cursorActivity", doMatchBrackets);
  54. else cm.off("cursorActivity", doMatchBrackets);
  55. });
  56. CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
  57. CodeMirror.defineExtension("findMatchingBracket", function(){return findMatchingBracket(this);});
  58. })();