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