PageSourceSearch

https://learnjavaonline.org/static/js/codemirror/addon/edit/matchbrackets.js

js learnjavaonline.org collected 2026-10-03 17:33:31 UTC 3,345 bytes, 74 lines download raw bytes

1(function() {
2  var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
3    (document.documentMode == null || document.documentMode < 8);
4
5  var Pos = CodeMirror.Pos;
6  // Disable brace matching in long lines, since it'll cause hugely slow updates  
7  var maxLineLen = 1000;
8
9  var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
10  function findMatchingBracket(cm) {
11    var cur = cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1;
12    var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
13    if (!match) return null;
14    var forward = match.charAt(1) == ">", d = forward ? 1 : -1;
15    var style = cm.getTokenAt(Pos(cur.line, pos + 1)).type;
16
17    var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
18    function scan(line, lineNo, start) {
19      if (!line.text) return;
20      var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1;
21      if (start != null) pos = start + d;
22      for (; pos != end; pos += d) {
23        var ch = line.text.charAt(pos);
24        if (re.test(ch) && cm.getTokenAt(Pos(lineNo, pos + 1)).type == style) {
25          var match = matching[ch];
26          if (match.charAt(1) == ">" == forward) stack.push(ch);
27          else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
28          else if (!stack.length) return {pos: pos, match: true};
29        }
30      }
31    }
32    for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) {
33      if (i == cur.line) found = scan(line, i, pos);
34      else found = scan(cm.getLineHandle(i), i);
35      if (found) break;
36    }
37    return {from: Pos(cur.line, pos), to: found && Pos(i, found.pos), match: found && found.match};
38  }
39
40  function matchBrackets(cm, autoclear) {
41    var found = findMatchingBracket(cm);
42    if (!found || cm.getLine(found.from.line).length > maxLineLen ||
43       found.to && cm.getLine(found.to.line).length > maxLineLen)
44      return;
45
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
59  var currentlyHighlighted = null;
60  function doMatchBrackets(cm) {
61    cm.operation(function() {
62      if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
63      if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false);
64    });
65  }
66
67  CodeMirror.defineOption("matchBrackets", false, function(cm, val) {
68    if (val) cm.on("cursorActivity", doMatchBrackets);
69    else cm.off("cursorActivity", doMatchBrackets);
70  });
71
72  CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
73  CodeMirror.defineExtension("findMatchingBracket", function(){return findMatchingBracket(this);});
74})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.