LaravelTest
97 строк · 3.8 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"), require("./searchcursor"), require("../scroll/annotatescrollbar"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror", "./searchcursor", "../scroll/annotatescrollbar"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineExtension("showMatchesOnScrollbar", function(query, caseFold, options) {15if (typeof options == "string") options = {className: options};16if (!options) options = {};17return new SearchAnnotation(this, query, caseFold, options);18});19
20function SearchAnnotation(cm, query, caseFold, options) {21this.cm = cm;22this.options = options;23var annotateOptions = {listenForChanges: false};24for (var prop in options) annotateOptions[prop] = options[prop];25if (!annotateOptions.className) annotateOptions.className = "CodeMirror-search-match";26this.annotation = cm.annotateScrollbar(annotateOptions);27this.query = query;28this.caseFold = caseFold;29this.gap = {from: cm.firstLine(), to: cm.lastLine() + 1};30this.matches = [];31this.update = null;32
33this.findMatches();34this.annotation.update(this.matches);35
36var self = this;37cm.on("change", this.changeHandler = function(_cm, change) { self.onChange(change); });38}39
40var MAX_MATCHES = 1000;41
42SearchAnnotation.prototype.findMatches = function() {43if (!this.gap) return;44for (var i = 0; i < this.matches.length; i++) {45var match = this.matches[i];46if (match.from.line >= this.gap.to) break;47if (match.to.line >= this.gap.from) this.matches.splice(i--, 1);48}49var cursor = this.cm.getSearchCursor(this.query, CodeMirror.Pos(this.gap.from, 0), {caseFold: this.caseFold, multiline: this.options.multiline});50var maxMatches = this.options && this.options.maxMatches || MAX_MATCHES;51while (cursor.findNext()) {52var match = {from: cursor.from(), to: cursor.to()};53if (match.from.line >= this.gap.to) break;54this.matches.splice(i++, 0, match);55if (this.matches.length > maxMatches) break;56}57this.gap = null;58};59
60function offsetLine(line, changeStart, sizeChange) {61if (line <= changeStart) return line;62return Math.max(changeStart, line + sizeChange);63}64
65SearchAnnotation.prototype.onChange = function(change) {66var startLine = change.from.line;67var endLine = CodeMirror.changeEnd(change).line;68var sizeChange = endLine - change.to.line;69if (this.gap) {70this.gap.from = Math.min(offsetLine(this.gap.from, startLine, sizeChange), change.from.line);71this.gap.to = Math.max(offsetLine(this.gap.to, startLine, sizeChange), change.from.line);72} else {73this.gap = {from: change.from.line, to: endLine + 1};74}75
76if (sizeChange) for (var i = 0; i < this.matches.length; i++) {77var match = this.matches[i];78var newFrom = offsetLine(match.from.line, startLine, sizeChange);79if (newFrom != match.from.line) match.from = CodeMirror.Pos(newFrom, match.from.ch);80var newTo = offsetLine(match.to.line, startLine, sizeChange);81if (newTo != match.to.line) match.to = CodeMirror.Pos(newTo, match.to.ch);82}83clearTimeout(this.update);84var self = this;85this.update = setTimeout(function() { self.updateAfterChange(); }, 250);86};87
88SearchAnnotation.prototype.updateAfterChange = function() {89this.findMatches();90this.annotation.update(this.matches);91};92
93SearchAnnotation.prototype.clear = function() {94this.cm.off("change", this.changeHandler);95this.annotation.clear();96};97});98