LaravelTest
167 строк · 6.1 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Highlighting text that matches the selection
5//
6// Defines an option highlightSelectionMatches, which, when enabled,
7// will style strings that match the selection throughout the
8// document.
9//
10// The option can be set to true to simply enable it, or to a
11// {minChars, style, wordsOnly, showToken, delay} object to explicitly
12// configure it. minChars is the minimum amount of characters that should be
13// selected for the behavior to occur, and style is the token style to
14// apply to the matches. This will be prefixed by "cm-" to create an
15// actual CSS class name. If wordsOnly is enabled, the matches will be
16// highlighted only if the selected text is a word. showToken, when enabled,
17// will cause the current token to be highlighted when nothing is selected.
18// delay is used to specify how much time to wait, in milliseconds, before
19// highlighting the matches. If annotateScrollbar is enabled, the occurrences
20// will be highlighted on the scrollbar via the matchesonscrollbar addon.
21
22(function(mod) {23if (typeof exports == "object" && typeof module == "object") // CommonJS24mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));25else if (typeof define == "function" && define.amd) // AMD26define(["../../lib/codemirror", "./matchesonscrollbar"], mod);27else // Plain browser env28mod(CodeMirror);29})(function(CodeMirror) {30"use strict";31
32var defaults = {33style: "matchhighlight",34minChars: 2,35delay: 100,36wordsOnly: false,37annotateScrollbar: false,38showToken: false,39trim: true40}41
42function State(options) {43this.options = {}44for (var name in defaults)45this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]46this.overlay = this.timeout = null;47this.matchesonscroll = null;48this.active = false;49}50
51CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {52if (old && old != CodeMirror.Init) {53removeOverlay(cm);54clearTimeout(cm.state.matchHighlighter.timeout);55cm.state.matchHighlighter = null;56cm.off("cursorActivity", cursorActivity);57cm.off("focus", onFocus)58}59if (val) {60var state = cm.state.matchHighlighter = new State(val);61if (cm.hasFocus()) {62state.active = true63highlightMatches(cm)64} else {65cm.on("focus", onFocus)66}67cm.on("cursorActivity", cursorActivity);68}69});70
71function cursorActivity(cm) {72var state = cm.state.matchHighlighter;73if (state.active || cm.hasFocus()) scheduleHighlight(cm, state)74}75
76function onFocus(cm) {77var state = cm.state.matchHighlighter78if (!state.active) {79state.active = true80scheduleHighlight(cm, state)81}82}83
84function scheduleHighlight(cm, state) {85clearTimeout(state.timeout);86state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay);87}88
89function addOverlay(cm, query, hasBoundary, style) {90var state = cm.state.matchHighlighter;91cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style));92if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {93var searchFor = hasBoundary ? new RegExp((/\w/.test(query.charAt(0)) ? "\\b" : "") +94query.replace(/[\\\[.+*?(){|^$]/g, "\\$&") +95(/\w/.test(query.charAt(query.length - 1)) ? "\\b" : "")) : query;96state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,97{className: "CodeMirror-selection-highlight-scrollbar"});98}99}100
101function removeOverlay(cm) {102var state = cm.state.matchHighlighter;103if (state.overlay) {104cm.removeOverlay(state.overlay);105state.overlay = null;106if (state.matchesonscroll) {107state.matchesonscroll.clear();108state.matchesonscroll = null;109}110}111}112
113function highlightMatches(cm) {114cm.operation(function() {115var state = cm.state.matchHighlighter;116removeOverlay(cm);117if (!cm.somethingSelected() && state.options.showToken) {118var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;119var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;120while (start && re.test(line.charAt(start - 1))) --start;121while (end < line.length && re.test(line.charAt(end))) ++end;122if (start < end)123addOverlay(cm, line.slice(start, end), re, state.options.style);124return;125}126var from = cm.getCursor("from"), to = cm.getCursor("to");127if (from.line != to.line) return;128if (state.options.wordsOnly && !isWord(cm, from, to)) return;129var selection = cm.getRange(from, to)130if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "")131if (selection.length >= state.options.minChars)132addOverlay(cm, selection, false, state.options.style);133});134}135
136function isWord(cm, from, to) {137var str = cm.getRange(from, to);138if (str.match(/^\w+$/) !== null) {139if (from.ch > 0) {140var pos = {line: from.line, ch: from.ch - 1};141var chr = cm.getRange(pos, from);142if (chr.match(/\W/) === null) return false;143}144if (to.ch < cm.getLine(from.line).length) {145var pos = {line: to.line, ch: to.ch + 1};146var chr = cm.getRange(to, pos);147if (chr.match(/\W/) === null) return false;148}149return true;150} else return false;151}152
153function boundariesAround(stream, re) {154return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&155(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));156}157
158function makeOverlay(query, hasBoundary, style) {159return {token: function(stream) {160if (stream.match(query) &&161(!hasBoundary || boundariesAround(stream, hasBoundary)))162return style;163stream.next();164stream.skipTo(query.charAt(0)) || stream.skipToEnd();165}};166}167});168