(autoclear)
| 1729 | |
| 1730 | var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; |
| 1731 | function matchBrackets(autoclear) { |
| 1732 | var head = sel.inverted ? sel.from : sel.to, line = getLine(head.line), pos = head.ch - 1; |
| 1733 | var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; |
| 1734 | if (!match) return; |
| 1735 | var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles; |
| 1736 | for (var off = pos + 1, i = 0, e = st.length; i < e; i+=2) |
| 1737 | if ((off -= st[i].length) <= 0) {var style = st[i+1]; break;} |
| 1738 | |
| 1739 | var stack = [line.text.charAt(pos)], re = /[(){}[\]]/; |
| 1740 | function scan(line, from, to) { |
| 1741 | if (!line.text) return; |
| 1742 | var st = line.styles, pos = forward ? 0 : line.text.length - 1, cur; |
| 1743 | for (var i = forward ? 0 : st.length - 2, e = forward ? st.length : -2; i != e; i += 2*d) { |
| 1744 | var text = st[i]; |
| 1745 | if (st[i+1] != null && st[i+1] != style) {pos += d * text.length; continue;} |
| 1746 | for (var j = forward ? 0 : text.length - 1, te = forward ? text.length : -1; j != te; j += d, pos+=d) { |
| 1747 | if (pos >= from && pos < to && re.test(cur = text.charAt(j))) { |
| 1748 | var match = matching[cur]; |
| 1749 | if (match.charAt(1) == ">" == forward) stack.push(cur); |
| 1750 | else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false}; |
| 1751 | else if (!stack.length) return {pos: pos, match: true}; |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | for (var i = head.line, e = forward ? Math.min(i + 100, doc.size) : Math.max(-1, i - 100); i != e; i+=d) { |
| 1757 | var line = getLine(i), first = i == head.line; |
| 1758 | var found = scan(line, first && forward ? pos + 1 : 0, first && !forward ? pos : line.text.length); |
| 1759 | if (found) break; |
| 1760 | } |
| 1761 | if (!found) found = {pos: null, match: false}; |
| 1762 | var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; |
| 1763 | var one = markText({line: head.line, ch: pos}, {line: head.line, ch: pos+1}, style), |
| 1764 | two = found.pos != null && markText({line: i, ch: found.pos}, {line: i, ch: found.pos + 1}, style); |
| 1765 | var clear = operation(function(){one.clear(); two && two.clear();}); |
| 1766 | if (autoclear) setTimeout(clear, 800); |
| 1767 | else bracketHighlighted = clear; |
| 1768 | } |
| 1769 | |
| 1770 | // Finds the line to start with when starting a parse. Tries to |
| 1771 | // find a line with a stateAfter, so that it can start with a |
no test coverage detected