-
Notifications
You must be signed in to change notification settings - Fork 11
/
find.js
281 lines (241 loc) · 11 KB
/
find.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, doReplace, brackets */
/*unittests: FindReplace*/
/*
* Adds Find and Replace commands
*
* Originally based on the code in CodeMirror2/lib/util/search.js.
*/
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule("command/Commands"),
Strings = brackets.getModule("strings"),
ScrollTrackMarkers = brackets.getModule("search/ScrollTrackMarkers"),
Editor = brackets.getModule("editor/Editor"),
EditorManager = brackets.getModule("editor/EditorManager");
var isFindFirst = false;
function SearchState() {
this.posFrom = this.posTo = this.query = null;
this.marked = [];
}
function BuiltinSearchState() {
//NOTE: these could be subject to change in the future
this.searchStartPos = null;
this.query = null;
this.foundAny = false;
this.marked = [];
}
function getSearchState(cm) {
// NOTE: this does NOT use the builtin search state anymore. If there was a way for extensions to access the builtin search API it should use that instead of this file which is an outdated copy.
// previously cm._searchState
if (!cm._searchStateQuickSearch) {
cm._searchStateQuickSearch = new SearchState();
}
return cm._searchStateQuickSearch;
}
function getSearchCursor(cm, query, pos) {
// Heuristic: if the query string is all lowercase, do a case insensitive search.
return cm.getSearchCursor(query, pos, typeof query === "string" && query === query.toLowerCase());
}
function parseQuery(query) {
var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
//$(".modal-bar .message").css("display", "inline-block");
//$(".modal-bar .error").css("display", "none");
try {
if (isRE && isRE[1]) { // non-empty regexp
return new RegExp(isRE[1], isRE[2].indexOf("i") === -1 ? "" : "i");
} else {
return query;
}
} catch (e) {
//$(".modal-bar .message").css("display", "none");
//$(".modal-bar .error")
// .css("display", "inline-block")
// .html("<div class='alert-message' style='margin-bottom: 0'>" + e.message + "</div>");
return "";
}
}
function updateBuiltinSearchState(editor, query) {
var cm = editor._codeMirror;
var searchState = cm._searchState;
if (searchState) {
//NOTE: these could be subject to change in the future
searchState.query = parseQuery(query);
searchState.searchStartPos = null;
searchState.foundAny = false;
cm.operation(function () {
searchState.marked.forEach(function (markedRange) {
markedRange.clear();
});
});
searchState.marked.length = 0;
} else {
cm._searchState = new BuiltinSearchState();
cm._searchState.query = parseQuery(query);
}
}
function findNext(editor, rev) {
var cm = editor._codeMirror;
var found = true;
cm.operation(function () {
var state = getSearchState(cm);
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
if (!cursor.find(rev)) {
// If no result found before hitting edge of file, try wrapping around
cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
// No result found, period: clear selection & bail
if (!cursor.find(rev)) {
cm.setCursor(cm.getCursor()); // collapses selection, keeping cursor in place to avoid scrolling
found = false;
return;
}
}
var centerOptions = (isFindFirst) ? Editor.BOUNDARY_IGNORE_TOP : Editor.BOUNDARY_CHECK_NORMAL;
editor.setSelection(cursor.from(), cursor.to(), true, centerOptions);
state.posFrom = cursor.from();
state.posTo = cursor.to();
state.findNextCalled = true;
});
return found;
}
function clearHighlights(state) {
state.marked.forEach(function (markedRange) {
markedRange.clear();
});
state.marked.length = 0;
ScrollTrackMarkers.clear();
}
function clearSearch(cm) {
cm.operation(function () {
var state = getSearchState(cm);
if (!state.query) {
return;
}
state.query = null;
clearHighlights(state);
});
}
function clear(editor) {
var cm = editor._codeMirror;
var state = getSearchState(cm);
if (!state.query) {
return;
}
ScrollTrackMarkers.setVisible(editor, false);
clearSearch(cm);
//clearHighlights(state);
// As soon as focus goes back to the editor, restore normal selection color
$(cm.getWrapperElement()).removeClass("find-highlighting");
}
/**
* If no search pending, opens the search dialog. If search is already open, moves to
* next/prev result (depending on 'rev')
*/
function doSearch(editor, rev, initialQuery, rawQuery) {
var cm = editor._codeMirror;
var state = getSearchState(cm);
if (state.query) {
//findNext(editor, rev);
return;
}
// Use the selection start as the searchStartPos. This way if you
// start with a pre-populated search and enter an additional character,
// it will extend the initial selection instead of jumping to the next
// occurrence.
var searchStartPos = cm.getCursor(true);
// Called each time the search query changes while being typed. Jumps to the first matching
// result, starting from the original cursor position
function findFirst(query) {
isFindFirst = true;
cm.operation(function () {
if (state.query) {
clearHighlights(getSearchState(cm));
}
state.query = parseQuery(query);
if (!state.query) {
//BUG this caused an issue where the default select highlighting is removed,
// there doesn't seem to be any problem removing this
//cm.setCursor(searchStartPos);
return;
}
// Highlight all matches
// (Except on huge documents, where this is too expensive)
if (cm.getValue().length < 500000) {
// Temporarily change selection color to improve highlighting - see LESS code for details
$(cm.getWrapperElement()).addClass("find-highlighting");
var scrollTrackPositions = [];
// FUTURE: if last query was prefix of this one, could optimize by filtering existing result set
var cursor = getSearchCursor(cm, state.query);
while (cursor.findNext()) {
scrollTrackPositions.push(cursor.from());
var foundText = editor.document.getRange(cursor.from(), cursor.to());
if (foundText === rawQuery) {
state.marked.push(cm.markText(cursor.from(), cursor.to(), { className: "CodeMirror-searching" }));
} else {
state.marked.push(cm.markText(cursor.from(), cursor.to(), { className: "CodeMirror-matchingtag"}));
}
//Remove this section when https://github.com/marijnh/CodeMirror/issues/1155 will be fixed
if (cursor.pos.match && cursor.pos.match[0] === "") {
if (cursor.to().line + 1 === cm.lineCount()) {
break;
}
cursor = getSearchCursor(cm, state.query, {line: cursor.to().line + 1, ch: 0});
}
}
ScrollTrackMarkers.setVisible(editor, true);
ScrollTrackMarkers.addTickmarks(editor, scrollTrackPositions);
}
state.posFrom = state.posTo = searchStartPos;
//NOTE: this was causing the cursor to move to the end of the search word
//var foundAny = findNext(editor, rev);
});
isFindFirst = false;
}
// Prepopulate the search field with the current selection, if any.
if (initialQuery !== undefined) {
findFirst(initialQuery);
// Clear the "findNextCalled" flag here so we have a clean start
state.findNextCalled = false;
}
}
/*function _findNext() {
var editor = EditorManager.getActiveEditor();
if (editor) {
doSearch(editor);
}
}
function _findPrevious() {
var editor = EditorManager.getActiveEditor();
if (editor) {
doSearch(editor, true);
}
}*/
exports.doSearch = doSearch;
exports.clear = clear;
exports.updateBuiltinSearchState = updateBuiltinSearchState;
//CommandManager.register(Strings.CMD_FIND_NEXT, Commands.EDIT_FIND_NEXT, _findNext);
//CommandManager.register(Strings.CMD_FIND_PREVIOUS, Commands.EDIT_FIND_PREVIOUS, _findPrevious);
});