github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/muted/lib/matchbrackets.js (about)

     1  (function(mod) {
     2    if (typeof exports == "object" && typeof module == "object") // CommonJS
     3      mod(require("../../lib/codemirror"));
     4    else if (typeof define == "function" && define.amd) // AMD
     5      define(["../../lib/codemirror"], mod);
     6    else // Plain browser env
     7      mod(CodeMirror);
     8  })(function(CodeMirror) {
     9    var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
    10      (document.documentMode == null || document.documentMode < 8);
    11  
    12    var Pos = CodeMirror.Pos;
    13  
    14    var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
    15  
    16    function findMatchingBracket(cm, where, strict, config) {
    17      var line = cm.getLineHandle(where.line), pos = where.ch - 1;
    18      var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
    19      if (!match) return null;
    20      var dir = match.charAt(1) == ">" ? 1 : -1;
    21      if (strict && (dir > 0) != (pos == where.ch)) return null;
    22      var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
    23  
    24      var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config);
    25      if (found == null) return null;
    26      return {from: Pos(where.line, pos), to: found && found.pos,
    27              match: found && found.ch == match.charAt(0), forward: dir > 0};
    28    }
    29  
    30    // bracketRegex is used to specify which type of bracket to scan
    31    // should be a regexp, e.g. /[[\]]/
    32    //
    33    // Note: If "where" is on an open bracket, then this bracket is ignored.
    34    //
    35    // Returns false when no bracket was found, null when it reached
    36    // maxScanLines and gave up
    37    function scanForBracket(cm, where, dir, style, config) {
    38      var maxScanLen = (config && config.maxScanLineLength) || 10000;
    39      var maxScanLines = (config && config.maxScanLines) || 1000;
    40  
    41      var stack = [];
    42      var re = config && config.bracketRegex ? config.bracketRegex : /[(){}[\]]/;
    43      var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
    44                            : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
    45      for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
    46        var line = cm.getLine(lineNo);
    47        if (!line) continue;
    48        var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
    49        if (line.length > maxScanLen) continue;
    50        if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
    51        for (; pos != end; pos += dir) {
    52          var ch = line.charAt(pos);
    53          if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) {
    54            var match = matching[ch];
    55            if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
    56            else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
    57            else stack.pop();
    58          }
    59        }
    60      }
    61      return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
    62    }
    63  
    64    function matchBrackets(cm, autoclear, config) {
    65      // Disable brace matching in long lines, since it'll cause hugely slow updates
    66      var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
    67      var marks = [], ranges = cm.listSelections();
    68      for (var i = 0; i < ranges.length; i++) {
    69        var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config);
    70        if (match && cm.getLine(match.from.line).length <= maxHighlightLen) {
    71          var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
    72          marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
    73          if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
    74            marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
    75        }
    76      }
    77  
    78      if (marks.length) {
    79        // Kludge to work around the IE bug from issue #1193, where text
    80        // input stops going to the textare whever this fires.
    81        if (ie_lt8 && cm.state.focused) cm.display.input.focus();
    82  
    83        var clear = function() {
    84          cm.operation(function() {
    85            for (var i = 0; i < marks.length; i++) marks[i].clear();
    86          });
    87        };
    88        if (autoclear) setTimeout(clear, 800);
    89        else return clear;
    90      }
    91    }
    92  
    93    var currentlyHighlighted = null;
    94    function doMatchBrackets(cm) {
    95      cm.operation(function() {
    96        if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
    97        currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
    98      });
    99    }
   100  
   101    CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
   102      if (old && old != CodeMirror.Init)
   103        cm.off("cursorActivity", doMatchBrackets);
   104      if (val) {
   105        cm.state.matchBrackets = typeof val == "object" ? val : {};
   106        cm.on("cursorActivity", doMatchBrackets);
   107      }
   108    });
   109  
   110    CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
   111    CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config){
   112      return findMatchingBracket(this, pos, strict, config);
   113    });
   114    CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
   115      return scanForBracket(this, pos, dir, style, config);
   116    });
   117  });