| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  | // CodeMirror, copyright (c) by Marijn Haverbeke and others
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | // Distributed under an MIT license: https://codemirror.net/LICENSE
 | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | (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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 09:59:07 +02:00
										 |  |  |   var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function bracketRegex(config) { | 
					
						
							|  |  |  |     return config && config.bracketRegex || /[(){}[\]]/ | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function findMatchingBracket(cm, where, config) { | 
					
						
							|  |  |  |     var line = cm.getLineHandle(where.line), pos = where.ch - 1; | 
					
						
							|  |  |  |     var afterCursor = config && config.afterCursor | 
					
						
							|  |  |  |     if (afterCursor == null) | 
					
						
							|  |  |  |       afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className) | 
					
						
							| 
									
										
										
										
											2019-06-02 09:59:07 +02:00
										 |  |  |     var re = bracketRegex(config) | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // A cursor is defined as between two characters, but in in vim command mode
 | 
					
						
							|  |  |  |     // (i.e. not insert mode), the cursor is visually represented as a
 | 
					
						
							|  |  |  |     // highlighted box on top of the 2nd character. Otherwise, we allow matches
 | 
					
						
							|  |  |  |     // from before or after the cursor.
 | 
					
						
							| 
									
										
										
										
											2019-06-02 09:59:07 +02:00
										 |  |  |     var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) || | 
					
						
							|  |  |  |         re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)]; | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     if (!match) return null; | 
					
						
							|  |  |  |     var dir = match.charAt(1) == ">" ? 1 : -1; | 
					
						
							|  |  |  |     if (config && config.strict && (dir > 0) != (pos == where.ch)) return null; | 
					
						
							|  |  |  |     var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |     var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style, config); | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     if (found == null) return null; | 
					
						
							|  |  |  |     return {from: Pos(where.line, pos), to: found && found.pos, | 
					
						
							|  |  |  |             match: found && found.ch == match.charAt(0), forward: dir > 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(cm, where, dir, style, config) { | 
					
						
							|  |  |  |     var maxScanLen = (config && config.maxScanLineLength) || 10000; | 
					
						
							|  |  |  |     var maxScanLines = (config && config.maxScanLines) || 1000; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var stack = []; | 
					
						
							| 
									
										
										
										
											2019-06-02 09:59:07 +02:00
										 |  |  |     var re = bracketRegex(config) | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1) | 
					
						
							|  |  |  |                           : Math.max(cm.firstLine() - 1, where.line - maxScanLines); | 
					
						
							|  |  |  |     for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { | 
					
						
							|  |  |  |       var line = cm.getLine(lineNo); | 
					
						
							|  |  |  |       if (!line) continue; | 
					
						
							|  |  |  |       var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; | 
					
						
							|  |  |  |       if (line.length > maxScanLen) continue; | 
					
						
							|  |  |  |       if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); | 
					
						
							|  |  |  |       for (; pos != end; pos += dir) { | 
					
						
							|  |  |  |         var ch = line.charAt(pos); | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |         if (re.test(ch) && (style === undefined || | 
					
						
							|  |  |  |                             (cm.getTokenTypeAt(Pos(lineNo, pos + 1)) || "") == (style || ""))) { | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |           var match = matching[ch]; | 
					
						
							| 
									
										
										
										
											2019-06-02 09:59:07 +02:00
										 |  |  |           if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch); | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |           else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; | 
					
						
							|  |  |  |           else stack.pop(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function matchBrackets(cm, autoclear, config) { | 
					
						
							|  |  |  |     // Disable brace matching in long lines, since it'll cause hugely slow updates
 | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |     var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000, | 
					
						
							|  |  |  |       highlightNonMatching = config && config.highlightNonMatching; | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     var marks = [], ranges = cm.listSelections(); | 
					
						
							|  |  |  |     for (var i = 0; i < ranges.length; i++) { | 
					
						
							|  |  |  |       var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config); | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |       if (match && (match.match || highlightNonMatching !== false) && cm.getLine(match.from.line).length <= maxHighlightLen) { | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |         var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; | 
					
						
							|  |  |  |         marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style})); | 
					
						
							|  |  |  |         if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen) | 
					
						
							|  |  |  |           marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style})); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (marks.length) { | 
					
						
							|  |  |  |       // Kludge to work around the IE bug from issue #1193, where text
 | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |       // input stops going to the textarea whenever this fires.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |       if (ie_lt8 && cm.state.focused) cm.focus(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var clear = function() { | 
					
						
							|  |  |  |         cm.operation(function() { | 
					
						
							|  |  |  |           for (var i = 0; i < marks.length; i++) marks[i].clear(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       if (autoclear) setTimeout(clear, 800); | 
					
						
							|  |  |  |       else return clear; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function doMatchBrackets(cm) { | 
					
						
							|  |  |  |     cm.operation(function() { | 
					
						
							| 
									
										
										
										
											2018-02-21 23:09:52 -05:00
										 |  |  |       if (cm.state.matchBrackets.currentlyHighlighted) { | 
					
						
							|  |  |  |         cm.state.matchBrackets.currentlyHighlighted(); | 
					
						
							|  |  |  |         cm.state.matchBrackets.currentlyHighlighted = null; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets); | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |   function clearHighlighted(cm) { | 
					
						
							|  |  |  |     if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) { | 
					
						
							|  |  |  |       cm.state.matchBrackets.currentlyHighlighted(); | 
					
						
							|  |  |  |       cm.state.matchBrackets.currentlyHighlighted = null; | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-20 21:41:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |   CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) { | 
					
						
							| 
									
										
										
										
											2020-09-20 21:41:40 +02:00
										 |  |  |     if (old && old != CodeMirror.Init) { | 
					
						
							|  |  |  |       cm.off("cursorActivity", doMatchBrackets); | 
					
						
							|  |  |  |       cm.off("focus", doMatchBrackets) | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |       cm.off("blur", clearHighlighted) | 
					
						
							|  |  |  |       clearHighlighted(cm); | 
					
						
							| 
									
										
										
										
											2020-09-20 21:41:40 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     if (val) { | 
					
						
							|  |  |  |       cm.state.matchBrackets = typeof val == "object" ? val : {}; | 
					
						
							|  |  |  |       cm.on("cursorActivity", doMatchBrackets); | 
					
						
							| 
									
										
										
										
											2020-09-20 21:41:40 +02:00
										 |  |  |       cm.on("focus", doMatchBrackets) | 
					
						
							| 
									
										
										
										
											2021-04-06 22:16:34 +02:00
										 |  |  |       cm.on("blur", clearHighlighted) | 
					
						
							| 
									
										
										
										
											2018-01-28 11:19:31 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); | 
					
						
							|  |  |  |   CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){ | 
					
						
							|  |  |  |     // Backwards-compatibility kludge
 | 
					
						
							|  |  |  |     if (oldConfig || typeof config == "boolean") { | 
					
						
							|  |  |  |       if (!oldConfig) { | 
					
						
							|  |  |  |         config = config ? {strict: true} : null | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         oldConfig.strict = config | 
					
						
							|  |  |  |         config = oldConfig | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return findMatchingBracket(this, pos, config) | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){ | 
					
						
							|  |  |  |     return scanForBracket(this, pos, dir, style, config); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |