forked from lkesteloot/pad
/
search_pane.js
240 lines (201 loc) · 7.25 KB
/
search_pane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2014 Lawrence Kesteloot
"use strict";
var util = require("util");
var EditorPane = require("./editor_pane");
var SearchKeys = require("./search_keys");
var trace = require("./trace");
var Line = require("./line");
var Fragment = require("./fragment");
var term = require("./term");
var Attr = require("./attr");
// Subclass of EditorPane.
var SearchPane = function (window, x, y, width, height, mainPane) {
EditorPane.call(this, window, x, y, width, height, mainPane);
this.keys = new SearchKeys();
this.hits = [];
this.selected = 0;
this.origDocIndex = mainPane.docIndex;
this.searchText = "";
mainPane.events.on("format", this.onMainPaneFormat.bind(this));
mainPane.events.on("change", this.onMainPaneChange.bind(this));
};
util.inherits(SearchPane, EditorPane);
SearchPane.prototype.setSearchText = function (searchText) {
this.doc.setString(searchText);
};
SearchPane.prototype.setSelected = function (selected) {
this.selected = selected;
if (this.selected > this.hits.length - 1) {
this.selected = this.hits.length - 1;
}
if (this.selected < 0) {
this.selected = 0;
}
if (this.selected >= 0 && this.selected < this.hits.length) {
var hit = this.hits[this.selected];
this.mainPane.desiredDocIndex = hit.start;
this.highlightMainPane();
}
this.layoutDirty = true;
};
SearchPane.prototype.performSearch = function () {
var endOfLine = this.doc.findEndOfLine(0);
var searchText = this.doc.toString(0, endOfLine);
var searchTextChanged = searchText != this.searchText;
this.searchText = searchText;
this.hits = getSearchHits(this.mainPane.doc, searchText);
if (this.selected > this.hits.length - 1) {
this.selected = this.hits.length - 1;
}
if (this.selected < 0) {
this.selected = 0;
}
if (searchTextChanged) {
// If main pane's cursor isn't at a search hit, move it to the next search hit.
var preferredHit = null;
var docIndex = this.origDocIndex;
for (var i = 0; i < this.hits.length; i++) {
var hit = this.hits[i];
if (hit.start === docIndex) {
// Already at hit. Leave it.
this.selected = i;
break;
} else if (preferredHit === null && hit.start > docIndex) {
// First hit after cursor. Jump to it.
this.selected = i;
this.mainPane.desiredDocIndex = hit.start;
break;
}
}
// Highlight main pane.
this.highlightMainPane();
this.layoutDirty = true;
}
};
// Override.
SearchPane.prototype.format = function () {
var lines = [];
lines.push(new Line(this.searchText, 0, true, 0));
lines.push(new Line("", 0, true, null));
for (var i = 0; i < this.hits.length; i++) {
var hit = this.hits[i];
// Find doc index of context around match. We want to align the matches
// vertically, but also have them more or less centered. Assume that the
// length of the search string is about the length of the matched word.
var context = Math.floor((this.contentWidth - this.searchText.length)/2);
var before = Math.max(hit.start - context, hit.doc.findStartOfLine(hit.start));
var after = Math.min(hit.doc.findEndOfLine(hit.start), before + this.contentWidth);
// Get what we want to show and highlight it.
var extract = hit.doc.toString(before, after);
var line = new Line(extract, 0, true, null);
var category = Line.SYNTAX_CATEGORY;
var normalAttr;
var highlightAttr;
if (i === this.selected) {
normalAttr = Attr.DIM_HIGHLIGHT;
highlightAttr = Attr.HIGHLIGHT;
} else {
normalAttr = Attr.DIM;
highlightAttr = Attr.NORMAL;
}
if (hit.start > before) {
line.addFragment(category, new Fragment(0, hit.start - before, normalAttr));
}
line.addFragment(category, new Fragment(hit.start - before, hit.end - before, highlightAttr));
if (after > hit.end) {
line.addFragment(category, new Fragment(hit.end - before, after - before, normalAttr));
}
lines.push(line);
}
this.layout.lines = lines;
};
SearchPane.prototype.onMainPaneFormat = function () {
this.highlightMainPane();
};
SearchPane.prototype.onMainPaneChange = function () {
this.performSearch();
};
// Override
SearchPane.prototype.onDocModified = function () {
this.performSearch();
EditorPane.prototype.onDocModified.call(this);
};
SearchPane.prototype.highlightMainPane = function () {
var hitIndex = 0;
this.mainPane.layout.lines.forEach(function (line) {
line.clearFragments(Line.SEARCH_CATEGORY);
if (line.docIndex === null) {
// Not from a doc.
return;
}
var lineStart = line.docIndex;
var lineEnd = lineStart + line.text.length;
// Find the hit for this line.
while (hitIndex < this.hits.length && this.hits[hitIndex].end <= lineStart) {
hitIndex++;
}
// Go through all hits, highlighting this line.
for (var i = hitIndex; i < this.hits.length; i++) {
var hit = this.hits[i];
if (hit.start >= lineEnd) {
// No more hits apply to this line.
break;
}
var start = Math.max(hit.start - lineStart, 0);
var end = Math.min(hit.end - lineStart, line.text.length);
var attr = i === this.selected ? Attr.HIGHLIGHT : Attr.DIM_HIGHLIGHT;
line.addFragment(Line.SEARCH_CATEGORY, new Fragment(start, end, attr));
}
}.bind(this));
this.mainPane.redrawDirty = true;
this.mainPane.queueRedraw();
};
/**
* Returns an array of hits, each of which is an object with:
*
* doc: Document
* start: Doc index where hit starts.
* end: Doc index where hit ends.
*/
var getSearchHits = function (doc, searchText) {
var text = doc.toString();
var re;
try {
re = new RegExp(searchText, "mg");
} catch (e) {
// Syntax error.
return [];
}
var match;
var lastStart = -1;
var hits = [];
while ((match = re.exec(text)) !== null) {
var word = match[0];
if (word === "") {
// Not matching anything. This isn't good, we won't make
// any forward progress and it's not useful to the user.
// XXX Will this trigger when we search for ^$ and hit
// an empty line?
break;
}
// Find doc index of match.
var start = match.index;
if (start === lastStart) {
// Not making forward progress. This can happen when the search
// string is empty or allows empty matches, such as "a*". I don't
// think this can happen since we check for word === "" above,
// but I'm leaving this in just in case, since it would result in
// an infinite loop.
break;
}
var end = start + word.length;
hits.push({
doc: doc,
start: start,
end: end
});
lastStart = start;
}
return hits;
};
module.exports = SearchPane;