Вход Регистрация
Файл: engine/skins/mirror/addon/edit/matchbrackets.js
Строк: 238
<?php
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE

(function(mod) {
  if (
typeof exports == "object" && typeof module == "object"// CommonJS
    
mod(require("../../lib/codemirror"));
  else if (
typeof define == "function" && define.amd// AMD
    
define(["../../lib/codemirror"], mod);
  else 
// Plain browser env
    
mod(CodeMirror);
})(function(
CodeMirror) {
  var 
ie_lt8 = /MSIE d/.test(navigator.userAgent) &&
    (
document.documentMode == null || document.documentMode 8);

  var 
Pos CodeMirror.Pos;

  var 
matching = {"("")>"")""(<""[""]>""]""[<""{""}>""}""{<"};

  function 
findMatchingBracket(cmwherestrictconfig) {
    var 
line cm.getLineHandle(where.line), pos where.ch 1;
    var 
match = (pos >= && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
    if (!
match) return null;
    var 
dir match.charAt(1) == ">" : -1;
    if (
strict && (dir 0) != (pos == where.ch)) return null;
    var 
style cm.getTokenTypeAt(Pos(where.linepos 1));

    var 
found scanForBracket(cmPos(where.linepos + (dir 0)), dirstyle || nullconfig);
    if (
found == null) return null;
    return {
fromPos(where.linepos), tofound && found.pos,
            
matchfound && found.ch == match.charAt(0), forwarddir 0};
  }

  
// bracketRegex is used to specify which type of bracket to scan
  // should be a regexp, e.g. /[[]]/
  //
  // Note: If "where" is on an open bracket, then this bracket is ignored.
  //
  // Returns false when no bracket was found, null when it reached
  // maxScanLines and gave up
  
function scanForBracket(cmwheredirstyleconfig) {
    var 
maxScanLen = (config && config.maxScanLineLength) || 10000;
    var 
maxScanLines = (config && config.maxScanLines) || 1000;

    var 
stack = [];
    var 
re config && config.bracketRegex config.bracketRegex : /[(){}[]]/;
    var 
lineEnd dir Math.min(where.line maxScanLinescm.lastLine() + 1)
                          : 
Math.max(cm.firstLine() - 1where.line maxScanLines);
    for (var 
lineNo where.linelineNo != lineEndlineNo += dir) {
      var 
line cm.getLine(lineNo);
      if (!
line) continue;
      var 
pos dir line.length 1end dir line.length : -1;
      if (
line.length maxScanLen) continue;
      if (
lineNo == where.linepos where.ch - (dir 0);
      for (; 
pos != endpos += dir) {
        var 
ch line.charAt(pos);
        if (
re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNopos 1)) == style)) {
          var 
match matching[ch];
          if ((
match.charAt(1) == ">") == (dir 0)) stack.push(ch);
          else if (!
stack.length) return {posPos(lineNopos), chch};
          else 
stack.pop();
        }
      }
    }
    return 
lineNo dir == (dir cm.lastLine() : cm.firstLine()) ? false null;
  }

  function 
matchBrackets(cmautoclearconfig) {
    
// Disable brace matching in long lines, since it'll cause hugely slow updates
    
var maxHighlightLen cm.state.matchBrackets.maxHighlightLineLength || 1000;
    var 
marks = [], ranges cm.listSelections();
    for (var 
0ranges.lengthi++) {
      var 
match ranges[i].empty() && findMatchingBracket(cmranges[i].headfalseconfig);
      if (
match && cm.getLine(match.from.line).length <= maxHighlightLen) {
        var 
style match.match "CodeMirror-matchingbracket" "CodeMirror-nonmatchingbracket";
        
marks.push(cm.markText(match.fromPos(match.from.linematch.from.ch 1), {classNamestyle}));
        if (
match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
          
marks.push(cm.markText(match.toPos(match.to.linematch.to.ch 1), {classNamestyle}));
      }
    }

    if (
marks.length) {
      
// Kludge to work around the IE bug from issue #1193, where text
      // input stops going to the textare whever this fires.
      
if (ie_lt8 && cm.state.focusedcm.display.input.focus();

      var 
clear = function() {
        
cm.operation(function() {
          for (var 
0marks.lengthi++) marks[i].clear();
        });
      };
      if (
autoclearsetTimeout(clear800);
      else return 
clear;
    }
  }

  var 
currentlyHighlighted null;
  function 
doMatchBrackets(cm) {
    
cm.operation(function() {
      if (
currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted null;}
      
currentlyHighlighted matchBrackets(cmfalsecm.state.matchBrackets);
    });
  }

  
CodeMirror.defineOption("matchBrackets"false, function(cmvalold) {
    if (
old && old != CodeMirror.Init)
      
cm.off("cursorActivity"doMatchBrackets);
    if (
val) {
      
cm.state.matchBrackets typeof val == "object" val : {};
      
cm.on("cursorActivity"doMatchBrackets);
    }
  });

  
CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(thistrue);});
  
CodeMirror.defineExtension("findMatchingBracket", function(posstrictconfig){
    return 
findMatchingBracket(thisposstrictconfig);
  });
  
CodeMirror.defineExtension("scanForBracket", function(posdirstyleconfig){
    return 
scanForBracket(thisposdirstyleconfig);
  });
});
?>
Онлайн: 1
Реклама