matchbrackets.js (6500B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: https://codemirror.net/LICENSE 3 4 (function(mod) { 5 if (typeof exports == "object" && typeof module == "object") // CommonJS 6 mod(require("resource://devtools/client/shared/sourceeditor/codemirror/lib/codemirror.js")); 7 else if (typeof define == "function" && define.amd) // AMD 8 define(["../../lib/codemirror"], mod); 9 else // Plain browser env 10 mod(CodeMirror); 11 })(function(CodeMirror) { 12 var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && 13 (document.documentMode == null || document.documentMode < 8); 14 15 var Pos = CodeMirror.Pos; 16 17 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"}; 18 19 function bracketRegex(config) { 20 return config && config.bracketRegex || /[(){}[\]]/ 21 } 22 23 function findMatchingBracket(cm, where, config) { 24 var line = cm.getLineHandle(where.line), pos = where.ch - 1; 25 var afterCursor = config && config.afterCursor 26 if (afterCursor == null) 27 afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className) 28 var re = bracketRegex(config) 29 30 // A cursor is defined as between two characters, but in in vim command mode 31 // (i.e. not insert mode), the cursor is visually represented as a 32 // highlighted box on top of the 2nd character. Otherwise, we allow matches 33 // from before or after the cursor. 34 var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) || 35 re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)]; 36 if (!match) return null; 37 var dir = match.charAt(1) == ">" ? 1 : -1; 38 if (config && config.strict && (dir > 0) != (pos == where.ch)) return null; 39 var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); 40 41 var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config); 42 if (found == null) return null; 43 return {from: Pos(where.line, pos), to: found && found.pos, 44 match: found && found.ch == match.charAt(0), forward: dir > 0}; 45 } 46 47 // bracketRegex is used to specify which type of bracket to scan 48 // should be a regexp, e.g. /[[\]]/ 49 // 50 // Note: If "where" is on an open bracket, then this bracket is ignored. 51 // 52 // Returns false when no bracket was found, null when it reached 53 // maxScanLines and gave up 54 function scanForBracket(cm, where, dir, style, config) { 55 var maxScanLen = (config && config.maxScanLineLength) || 10000; 56 var maxScanLines = (config && config.maxScanLines) || 1000; 57 58 var stack = []; 59 var re = bracketRegex(config) 60 var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1) 61 : Math.max(cm.firstLine() - 1, where.line - maxScanLines); 62 for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { 63 var line = cm.getLine(lineNo); 64 if (!line) continue; 65 var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; 66 if (line.length > maxScanLen) continue; 67 if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); 68 for (; pos != end; pos += dir) { 69 var ch = line.charAt(pos); 70 if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) { 71 var match = matching[ch]; 72 if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch); 73 else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; 74 else stack.pop(); 75 } 76 } 77 } 78 return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null; 79 } 80 81 function matchBrackets(cm, autoclear, config) { 82 // Disable brace matching in long lines, since it'll cause hugely slow updates 83 var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; 84 var marks = [], ranges = cm.listSelections(); 85 for (var i = 0; i < ranges.length; i++) { 86 var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config); 87 if (match && cm.getLine(match.from.line).length <= maxHighlightLen) { 88 var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; 89 marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style})); 90 if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen) 91 marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style})); 92 } 93 } 94 95 if (marks.length) { 96 // Kludge to work around the IE bug from issue #1193, where text 97 // input stops going to the textare whever this fires. 98 if (ie_lt8 && cm.state.focused) cm.focus(); 99 100 var clear = function() { 101 cm.operation(function() { 102 for (var i = 0; i < marks.length; i++) marks[i].clear(); 103 }); 104 }; 105 if (autoclear) setTimeout(clear, 800); 106 else return clear; 107 } 108 } 109 110 function doMatchBrackets(cm) { 111 cm.operation(function() { 112 if (cm.state.matchBrackets.currentlyHighlighted) { 113 cm.state.matchBrackets.currentlyHighlighted(); 114 cm.state.matchBrackets.currentlyHighlighted = null; 115 } 116 cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets); 117 }); 118 } 119 120 CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) { 121 if (old && old != CodeMirror.Init) { 122 cm.off("cursorActivity", doMatchBrackets); 123 if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) { 124 cm.state.matchBrackets.currentlyHighlighted(); 125 cm.state.matchBrackets.currentlyHighlighted = null; 126 } 127 } 128 if (val) { 129 cm.state.matchBrackets = typeof val == "object" ? val : {}; 130 cm.on("cursorActivity", doMatchBrackets); 131 } 132 }); 133 134 CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); 135 CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){ 136 // Backwards-compatibility kludge 137 if (oldConfig || typeof config == "boolean") { 138 if (!oldConfig) { 139 config = config ? {strict: true} : null 140 } else { 141 oldConfig.strict = config 142 config = oldConfig 143 } 144 } 145 return findMatchingBracket(this, pos, config) 146 }); 147 CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){ 148 return scanForBracket(this, pos, dir, style, config); 149 }); 150 });