aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util/searchcursor.js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-02-15 20:37:48 -0800
committerJose Antonio Marquez2012-02-15 20:37:48 -0800
commit89b5e793ea88ef235b54b6e1d1c379698d3e612b (patch)
tree4d29118f35df77ca6b423119a4ff61694a442cbf /imports/codemirror/lib/util/searchcursor.js
parent9d2c2a80483415d7560b00cda5519153db23e241 (diff)
parentd366c0bd1af6471511217ed574083e15059519b5 (diff)
downloadninja-89b5e793ea88ef235b54b6e1d1c379698d3e612b.tar.gz
Merge branch 'refs/heads/NinjaInternal' into Color
Diffstat (limited to 'imports/codemirror/lib/util/searchcursor.js')
-rwxr-xr-ximports/codemirror/lib/util/searchcursor.js117
1 files changed, 117 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/searchcursor.js b/imports/codemirror/lib/util/searchcursor.js
new file mode 100755
index 00000000..3b77829f
--- /dev/null
+++ b/imports/codemirror/lib/util/searchcursor.js
@@ -0,0 +1,117 @@
1(function(){
2 function SearchCursor(cm, query, pos, caseFold) {
3 this.atOccurrence = false; this.cm = cm;
4 if (caseFold == null) caseFold = typeof query == "string" && query == query.toLowerCase();
5
6 pos = pos ? cm.clipPos(pos) : {line: 0, ch: 0};
7 this.pos = {from: pos, to: pos};
8
9 // The matches method is filled in based on the type of query.
10 // It takes a position and a direction, and returns an object
11 // describing the next occurrence of the query, or null if no
12 // more matches were found.
13 if (typeof query != "string") // Regexp match
14 this.matches = function(reverse, pos) {
15 if (reverse) {
16 var line = cm.getLine(pos.line).slice(0, pos.ch), match = line.match(query), start = 0;
17 while (match) {
18 var ind = line.indexOf(match[0]);
19 start += ind;
20 line = line.slice(ind + 1);
21 var newmatch = line.match(query);
22 if (newmatch) match = newmatch;
23 else break;
24 start++;
25 }
26 }
27 else {
28 var line = cm.getLine(pos.line).slice(pos.ch), match = line.match(query),
29 start = match && pos.ch + line.indexOf(match[0]);
30 }
31 if (match)
32 return {from: {line: pos.line, ch: start},
33 to: {line: pos.line, ch: start + match[0].length},
34 match: match};
35 };
36 else { // String query
37 if (caseFold) query = query.toLowerCase();
38 var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
39 var target = query.split("\n");
40 // Different methods for single-line and multi-line queries
41 if (target.length == 1)
42 this.matches = function(reverse, pos) {
43 var line = fold(cm.getLine(pos.line)), len = query.length, match;
44 if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
45 : (match = line.indexOf(query, pos.ch)) != -1)
46 return {from: {line: pos.line, ch: match},
47 to: {line: pos.line, ch: match + len}};
48 };
49 else
50 this.matches = function(reverse, pos) {
51 var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(cm.getLine(ln));
52 var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
53 if (reverse ? offsetA >= pos.ch || offsetA != match.length
54 : offsetA <= pos.ch || offsetA != line.length - match.length)
55 return;
56 for (;;) {
57 if (reverse ? !ln : ln == cm.lineCount() - 1) return;
58 line = fold(cm.getLine(ln += reverse ? -1 : 1));
59 match = target[reverse ? --idx : ++idx];
60 if (idx > 0 && idx < target.length - 1) {
61 if (line != match) return;
62 else continue;
63 }
64 var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
65 if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
66 return;
67 var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
68 return {from: reverse ? end : start, to: reverse ? start : end};
69 }
70 };
71 }
72 }
73
74 SearchCursor.prototype = {
75 findNext: function() {return this.find(false);},
76 findPrevious: function() {return this.find(true);},
77
78 find: function(reverse) {
79 var self = this, pos = this.cm.clipPos(reverse ? this.pos.from : this.pos.to);
80 function savePosAndFail(line) {
81 var pos = {line: line, ch: 0};
82 self.pos = {from: pos, to: pos};
83 self.atOccurrence = false;
84 return false;
85 }
86
87 for (;;) {
88 if (this.pos = this.matches(reverse, pos)) {
89 this.atOccurrence = true;
90 return this.pos.match || true;
91 }
92 if (reverse) {
93 if (!pos.line) return savePosAndFail(0);
94 pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
95 }
96 else {
97 var maxLine = this.cm.lineCount();
98 if (pos.line == maxLine - 1) return savePosAndFail(maxLine);
99 pos = {line: pos.line+1, ch: 0};
100 }
101 }
102 },
103
104 from: function() {if (this.atOccurrence) return this.pos.from;},
105 to: function() {if (this.atOccurrence) return this.pos.to;},
106
107 replace: function(newText) {
108 var self = this;
109 if (this.atOccurrence)
110 self.pos.to = this.cm.replaceRange(newText, self.pos.from, self.pos.to);
111 }
112 };
113
114 CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
115 return new SearchCursor(this, query, pos, caseFold);
116 });
117})();