(function(f){"object"==typeof exports&&"object"==typeof module?f(require("../../lib/codemirror"),require("./searchcursor"),require("../scroll/annotatescrollbar")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./searchcursor","../scroll/annotatescrollbar"],f):f(CodeMirror)})(function(f){function g(a,c,b,d){this.cm=a;this.options=d;var e={listenForChanges:!1},h;for(h in d)e[h]=d[h];e.className||(e.className="CodeMirror-search-match");this.annotation=a.annotateScrollbar(e);this.query=
c;this.caseFold=b;this.gap={from:a.firstLine(),to:a.lastLine()+1};this.matches=[];this.update=null;this.findMatches();this.annotation.update(this.matches);var l=this;a.on("change",this.changeHandler=function(n,m){l.onChange(m)})}function k(a,c,b){return a<=c?a:Math.max(c,a+b)}f.defineExtension("showMatchesOnScrollbar",function(a,c,b){"string"==typeof b&&(b={className:b});b||(b={});return new g(this,a,c,b)});g.prototype.findMatches=function(){if(this.gap){for(var a=0;a<this.matches.length;a++){var c=
this.matches[a];if(c.from.line>=this.gap.to)break;c.to.line>=this.gap.from&&this.matches.splice(a--,1)}for(var b=this.cm.getSearchCursor(this.query,f.Pos(this.gap.from,0),{caseFold:this.caseFold,multiline:this.options.multiline}),d=this.options&&this.options.maxMatches||1E3;b.findNext();){c={from:b.from(),to:b.to()};if(c.from.line>=this.gap.to)break;this.matches.splice(a++,0,c);if(this.matches.length>d)break}this.gap=null}};g.prototype.onChange=function(a){var c=a.from.line,b=f.changeEnd(a).line,
d=b-a.to.line;this.gap?(this.gap.from=Math.min(k(this.gap.from,c,d),a.from.line),this.gap.to=Math.max(k(this.gap.to,c,d),a.from.line)):this.gap={from:a.from.line,to:b+1};if(d)for(a=0;a<this.matches.length;a++){b=this.matches[a];var e=k(b.from.line,c,d);e!=b.from.line&&(b.from=f.Pos(e,b.from.ch));e=k(b.to.line,c,d);e!=b.to.line&&(b.to=f.Pos(e,b.to.ch))}clearTimeout(this.update);var h=this;this.update=setTimeout(function(){h.updateAfterChange()},250)};g.prototype.updateAfterChange=function(){this.findMatches();
this.annotation.update(this.matches)};g.prototype.clear=function(){this.cm.off("change",this.changeHandler);this.annotation.clear()}});
