(function (mod){ if (typeof exports == "object" && typeof module == "object") mod(require("../../lib/codemirror"), require("./matchesonscrollbar")); else if (typeof define == "function" && define.amd) define(["../../lib/codemirror", "./matchesonscrollbar"] , mod); else mod(CodeMirror); } )(function (CodeMirror){ "use strict"; var DEFAULT_MIN_CHARS = 2; var DEFAULT_TOKEN_STYLE = "matchhighlight"; var DEFAULT_DELAY = 100; var DEFAULT_WORDS_ONLY = false ; function State(options){ if (typeof options == "object") { this.minChars = options.minChars; this.style = options.style; this.showToken = options.showToken; this.delay = options.delay; this.wordsOnly = options.wordsOnly; this.annotateScrollbar = options.annotateScrollbar; } if (this.style == null ) this.style = DEFAULT_TOKEN_STYLE; if (this.minChars == null ) this.minChars = DEFAULT_MIN_CHARS; if (this.delay == null ) this.delay = DEFAULT_DELAY; if (this.wordsOnly == null ) this.wordsOnly = DEFAULT_WORDS_ONLY; this.overlay = this.timeout = null ; this.matchesonscroll = null ; } CodeMirror.defineOption("highlightSelectionMatches", false , function (cm, val, old){ if (old && old != CodeMirror.Init) { removeOverlay(cm); clearTimeout(cm.state.matchHighlighter.timeout); cm.state.matchHighlighter = null ; cm.off("cursorActivity", cursorActivity); } if (val) { cm.state.matchHighlighter = new State(val); highlightMatches(cm); cm.on("cursorActivity", cursorActivity); } } ); function cursorActivity(cm){ var state = cm.state.matchHighlighter; clearTimeout(state.timeout); state.timeout = _AN_Call_settimeout("setTimeout", window, function (){ highlightMatches(cm); } , state.delay); } function addOverlay(cm, query, hasBoundary, style){ var state = cm.state.matchHighlighter; cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style)); if (state.annotateScrollbar) { var searchFor = hasBoundary? new RegExp("\\b" + query + "\\b"): query; state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, true , { className: "CodeMirror-selection-highlight-scrollbar"} ); } } function removeOverlay(cm){ var state = cm.state.matchHighlighter; if (state.overlay) { cm.removeOverlay(state.overlay); state.overlay = null ; if (state.annotateScrollbar) { _AN_Call_clear("clear", state.matchesonscroll); state.matchesonscroll = null ; } } } function highlightMatches(cm){ cm.operation(function (){ var state = cm.state.matchHighlighter; removeOverlay(cm); if (!cm.somethingSelected() && state.showToken) { var re = state.showToken === true ? /[\w$]/: state.showToken; var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start; while (start && re.test(line.charAt(start - 1)))--start; while (end < _AN_Read_length("length", line) && re.test(line.charAt(end)))++end; if (start < end) addOverlay(cm, line.slice(start, end), re, state.style); return ; } var from = cm.getCursor("from"), to = cm.getCursor("to"); if (from.line != to.line) return ; if (state.wordsOnly && !isWord(cm, from, to)) return ; var selection = _AN_Call_replace("replace", cm.getRange(from, to), /^\s+|\s+$/g, ""); if (_AN_Read_length("length", selection) >= state.minChars) addOverlay(cm, selection, false , state.style); } ); } function isWord(cm, from, to){ var str = cm.getRange(from, to); if (str.match(/^\w+$/) !== null ) { if (from.ch > 0) { var pos = { line: from.line, ch: from.ch - 1} ; var chr = cm.getRange(pos, from); if (chr.match(/\W/) === null ) return false ; } if (to.ch < _AN_Read_length("length", cm.getLine(from.line))) { var pos = { line: to.line, ch: to.ch + 1} ; var chr = cm.getRange(to, pos); if (chr.match(/\W/) === null ) return false ; } return true ; } else return false ; } function boundariesAround(stream, re){ return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) && (stream.pos == _AN_Read_length("length", stream.string) || !re.test(stream.string.charAt(stream.pos))); } function makeOverlay(query, hasBoundary, style){ return { token: function (stream){ if (stream.match(query) && (!hasBoundary || boundariesAround(stream, hasBoundary))) return style; stream.next(); stream.skipTo(query.charAt(0)) || stream.skipToEnd(); } } ; } } );