Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/search/match-highlighter.js
1294 views
// Highlighting text that matches the selection1//2// Defines an option highlightSelectionMatches, which, when enabled,3// will style strings that match the selection throughout the4// document.5//6// The option can be set to true to simply enable it, or to a7// {minChars, style, showToken} object to explicitly configure it.8// minChars is the minimum amount of characters that should be9// selected for the behavior to occur, and style is the token style to10// apply to the matches. This will be prefixed by "cm-" to create an11// actual CSS class name. showToken, when enabled, will cause the12// current token to be highlighted when nothing is selected.1314(function() {15var DEFAULT_MIN_CHARS = 2;16var DEFAULT_TOKEN_STYLE = "matchhighlight";17var DEFAULT_DELAY = 100;1819function State(options) {20if (typeof options == "object") {21this.minChars = options.minChars;22this.style = options.style;23this.showToken = options.showToken;24this.delay = options.delay;25}26if (this.style == null) this.style = DEFAULT_TOKEN_STYLE;27if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS;28if (this.delay == null) this.delay = DEFAULT_DELAY;29this.overlay = this.timeout = null;30}3132CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {33if (old && old != CodeMirror.Init) {34var over = cm.state.matchHighlighter.overlay;35if (over) cm.removeOverlay(over);36clearTimeout(cm.state.matchHighlighter.timeout);37cm.state.matchHighlighter = null;38cm.off("cursorActivity", cursorActivity);39}40if (val) {41cm.state.matchHighlighter = new State(val);42highlightMatches(cm);43cm.on("cursorActivity", cursorActivity);44}45});4647function cursorActivity(cm) {48var state = cm.state.matchHighlighter;49clearTimeout(state.timeout);50state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay);51}5253function highlightMatches(cm) {54cm.operation(function() {55var state = cm.state.matchHighlighter;56if (state.overlay) {57cm.removeOverlay(state.overlay);58state.overlay = null;59}60if (!cm.somethingSelected() && state.showToken) {61var re = state.showToken === true ? /[\w$]/ : state.showToken;62var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;63while (start && re.test(line.charAt(start - 1))) --start;64while (end < line.length && re.test(line.charAt(end))) ++end;65if (start < end)66cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style));67return;68}69if (cm.getCursor("head").line != cm.getCursor("anchor").line) return;70var selection = cm.getSelection().replace(/^\s+|\s+$/g, "");71if (selection.length >= state.minChars)72cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style));73});74}7576function boundariesAround(stream, re) {77return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&78(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));79}8081function makeOverlay(query, hasBoundary, style) {82return {token: function(stream) {83if (stream.match(query) &&84(!hasBoundary || boundariesAround(stream, hasBoundary)))85return style;86stream.next();87stream.skipTo(query.charAt(0)) || stream.skipToEnd();88}};89}90})();919293