PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 2.0.2
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v2.0.2
4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 All 340 releases
profile-builder / assets / lib / codemirror / addon / edit / matchbrackets.js

matchbrackets.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 2.0.2, at assets/lib/codemirror/addon/edit/matchbrackets.js

87 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
7 var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"};
8 function findMatchingBracket(cm, where, strict) {
9 var state = cm.state.matchBrackets;
10 var maxScanLen = (state && state.maxScanLineLength) || 10000;
11
12 var cur = where || cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1;
13 var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)];
14 if (!match) return null;
15 var forward = match.charAt(1) == ">", d = forward ? 1 : -1;
16 if (strict && forward != (pos == cur.ch)) return null;
17 var style = cm.getTokenTypeAt(Pos(cur.line, pos + 1));
18
19 var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
20 function scan(line, lineNo, start) {
21 if (!line.text) return;
22 var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1;
23 if (line.text.length > maxScanLen) return null;
24 if (start != null) pos = start + d;
25 for (; pos != end; pos += d) {
26 var ch = line.text.charAt(pos);
27 if (re.test(ch) && cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style) {
28 var match = matching[ch];
29 if (match.charAt(1) == ">" == forward) stack.push(ch);
30 else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false};
31 else if (!stack.length) return {pos: pos, match: true};
32 }
33 }
34 }
35 for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) {
36 if (i == cur.line) found = scan(line, i, pos);
37 else found = scan(cm.getLineHandle(i), i);
38 if (found) break;
39 }
40 return {from: Pos(cur.line, pos), to: found && Pos(i, found.pos),
41 match: found && found.match, forward: forward};
42 }
43
44 function matchBrackets(cm, autoclear) {
45 // Disable brace matching in long lines, since it'll cause hugely slow updates
46 var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000;
47 var found = findMatchingBracket(cm);
48 if (!found || cm.getLine(found.from.line).length > maxHighlightLen ||
49 found.to && cm.getLine(found.to.line).length > maxHighlightLen)
50 return;
51
52 var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
53 var one = cm.markText(found.from, Pos(found.from.line, found.from.ch + 1), {className: style});
54 var two = found.to && cm.markText(found.to, Pos(found.to.line, found.to.ch + 1), {className: style});
55 // Kludge to work around the IE bug from issue #1193, where text
56 // input stops going to the textare whever this fires.
57 if (ie_lt8 && cm.state.focused) cm.display.input.focus();
58 var clear = function() {
59 cm.operation(function() { one.clear(); two && two.clear(); });
60 };
61 if (autoclear) setTimeout(clear, 800);
62 else return clear;
63 }
64
65 var currentlyHighlighted = null;
66 function doMatchBrackets(cm) {
67 cm.operation(function() {
68 if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;}
69 if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false);
70 });
71 }
72
73 CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
74 if (old && old != CodeMirror.Init)
75 cm.off("cursorActivity", doMatchBrackets);
76 if (val) {
77 cm.state.matchBrackets = typeof val == "object" ? val : {};
78 cm.on("cursorActivity", doMatchBrackets);
79 }
80 });
81
82 CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
83 CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){
84 return findMatchingBracket(this, pos, strict);
85 });
86 })();
87