5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"), require("./searchcursor"), require("../scroll/annotatescrollbar"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror", "./searchcursor", "../scroll/annotatescrollbar"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineExtension("showMatchesOnScrollbar", function(query, caseFold, options) {
15
if (typeof options == "string") options = {className: options};
16
if (!options) options = {};
17
return new SearchAnnotation(this, query, caseFold, options);
20
function SearchAnnotation(cm, query, caseFold, options) {
22
this.options = options;
23
var annotateOptions = {listenForChanges: false};
24
for (var prop in options) annotateOptions[prop] = options[prop];
25
if (!annotateOptions.className) annotateOptions.className = "CodeMirror-search-match";
26
this.annotation = cm.annotateScrollbar(annotateOptions);
28
this.caseFold = caseFold;
29
this.gap = {from: cm.firstLine(), to: cm.lastLine() + 1};
34
this.annotation.update(this.matches);
37
cm.on("change", this.changeHandler = function(_cm, change) { self.onChange(change); });
40
var MAX_MATCHES = 1000;
42
SearchAnnotation.prototype.findMatches = function() {
43
if (!this.gap) return;
44
for (var i = 0; i < this.matches.length; i++) {
45
var match = this.matches[i];
46
if (match.from.line >= this.gap.to) break;
47
if (match.to.line >= this.gap.from) this.matches.splice(i--, 1);
49
var cursor = this.cm.getSearchCursor(this.query, CodeMirror.Pos(this.gap.from, 0), {caseFold: this.caseFold, multiline: this.options.multiline});
50
var maxMatches = this.options && this.options.maxMatches || MAX_MATCHES;
51
while (cursor.findNext()) {
52
var match = {from: cursor.from(), to: cursor.to()};
53
if (match.from.line >= this.gap.to) break;
54
this.matches.splice(i++, 0, match);
55
if (this.matches.length > maxMatches) break;
60
function offsetLine(line, changeStart, sizeChange) {
61
if (line <= changeStart) return line;
62
return Math.max(changeStart, line + sizeChange);
65
SearchAnnotation.prototype.onChange = function(change) {
66
var startLine = change.from.line;
67
var endLine = CodeMirror.changeEnd(change).line;
68
var sizeChange = endLine - change.to.line;
70
this.gap.from = Math.min(offsetLine(this.gap.from, startLine, sizeChange), change.from.line);
71
this.gap.to = Math.max(offsetLine(this.gap.to, startLine, sizeChange), change.from.line);
73
this.gap = {from: change.from.line, to: endLine + 1};
76
if (sizeChange) for (var i = 0; i < this.matches.length; i++) {
77
var match = this.matches[i];
78
var newFrom = offsetLine(match.from.line, startLine, sizeChange);
79
if (newFrom != match.from.line) match.from = CodeMirror.Pos(newFrom, match.from.ch);
80
var newTo = offsetLine(match.to.line, startLine, sizeChange);
81
if (newTo != match.to.line) match.to = CodeMirror.Pos(newTo, match.to.ch);
83
clearTimeout(this.update);
85
this.update = setTimeout(function() { self.updateAfterChange(); }, 250);
88
SearchAnnotation.prototype.updateAfterChange = function() {
90
this.annotation.update(this.matches);
93
SearchAnnotation.prototype.clear = function() {
94
this.cm.off("change", this.changeHandler);
95
this.annotation.clear();