Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 1f1e8a5

Browse files
author
Narciso Jaramillo
committed
Store query info in state instead of looking up in preferences
1 parent 3f0076e commit 1f1e8a5

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/search/FindReplace.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ define(function (require, exports, module) {
8484

8585
function SearchState() {
8686
this.searchStartPos = null;
87-
this.query = null;
87+
this.queryInfo = null;
8888
this.foundAny = false;
8989
this.marked = [];
9090
}
@@ -96,10 +96,9 @@ define(function (require, exports, module) {
9696
return cm._searchState;
9797
}
9898

99-
function getSearchCursor(cm, query, pos) {
99+
function getSearchCursor(cm, query, queryInfo, pos) {
100100
// Heuristic: if the query string is all lowercase, do a case insensitive search.
101-
// TODO: store the query info object and get the caseSensitive info from there
102-
return cm.getSearchCursor(query, pos, !PreferencesManager.getViewState("caseSensitive"));
101+
return cm.getSearchCursor(query, pos, !queryInfo.isCaseSensitive);
103102
}
104103

105104
function parseQuery(queryInfo) {
@@ -126,6 +125,22 @@ define(function (require, exports, module) {
126125
return queryInfo.query;
127126
}
128127
}
128+
129+
/**
130+
* @private
131+
* Determine the query from the given info and store it in the state.
132+
* @param {SearchState} state The state to store the query in
133+
* @param {{query: string, caseSensitive: boolean, isRegexp: boolean}} queryInfo
134+
* The query info object as returned by FindBar.getQueryInfo()
135+
*/
136+
function setQueryInfo(state, queryInfo) {
137+
state.queryInfo = queryInfo;
138+
if (!queryInfo) {
139+
state.query = null;
140+
} else {
141+
state.query = parseQuery(queryInfo);
142+
}
143+
}
129144

130145
// NOTE: we can't just use the ordinary replace() function here because the string has been
131146
// extracted from the original text and so might be missing some context that the regexp matched.
@@ -161,12 +176,12 @@ define(function (require, exports, module) {
161176
function _getNextMatch(editor, rev, pos, wrap) {
162177
var cm = editor._codeMirror;
163178
var state = getSearchState(cm);
164-
var cursor = getSearchCursor(cm, state.query, pos || editor.getCursorPos(false, rev ? "start" : "end"));
179+
var cursor = getSearchCursor(cm, state.query, state.queryInfo, pos || editor.getCursorPos(false, rev ? "start" : "end"));
165180

166181
state.lastMatch = cursor.find(rev);
167182
if (!state.lastMatch && wrap !== false) {
168183
// If no result found before hitting edge of file, try wrapping around
169-
cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
184+
cursor = getSearchCursor(cm, state.query, state.queryInfo, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
170185
state.lastMatch = cursor.find(rev);
171186
}
172187
if (!state.lastMatch) {
@@ -290,7 +305,7 @@ define(function (require, exports, module) {
290305
// We store this as a query in the state so that if the user next does a "Find Next",
291306
// it will use the same query (but throw away the existing selection).
292307
var state = getSearchState(editor._codeMirror);
293-
state.query = searchText;
308+
setQueryInfo(state, { query: searchText, isCaseSensitive: false, isRegexp: false });
294309

295310
// Skip over matches that are already in the selection.
296311
var searchStart = primarySel.end,
@@ -356,7 +371,7 @@ define(function (require, exports, module) {
356371
var searchStart = {line: 0, ch: 0},
357372
state = getSearchState(editor._codeMirror),
358373
nextMatch;
359-
state.query = editor.document.getRange(sel.start, sel.end);
374+
setQueryInfo(state, { query: editor.document.getRange(sel.start, sel.end), isCaseSensitive: false, isRegexp: false });
360375

361376
while ((nextMatch = _getNextMatch(editor, false, searchStart, false)) !== null) {
362377
if (_selEq(sel, nextMatch)) {
@@ -411,10 +426,10 @@ define(function (require, exports, module) {
411426
function clearSearch(cm) {
412427
cm.operation(function () {
413428
var state = getSearchState(cm);
414-
if (!state.query) {
429+
if (!state.query || !state.queryInfo) {
415430
return;
416431
}
417-
state.query = null;
432+
setQueryInfo(state, null);
418433

419434
clearHighlights(cm, state);
420435
});
@@ -456,7 +471,7 @@ define(function (require, exports, module) {
456471
clearHighlights(cm, state);
457472
}
458473

459-
if (!state.query) {
474+
if (!state.query || !state.queryInfo) {
460475
// Search field is empty - no results
461476
if (findBar) {
462477
findBar.showFindCount("");
@@ -468,7 +483,7 @@ define(function (require, exports, module) {
468483

469484
// Find *all* matches, searching from start of document
470485
// (Except on huge documents, where this is too expensive)
471-
var cursor = getSearchCursor(cm, state.query);
486+
var cursor = getSearchCursor(cm, state.query, state.queryInfo);
472487
if (cm.getValue().length <= FIND_MAX_FILE_SIZE) {
473488
// FUTURE: if last query was prefix of this one, could optimize by filtering last result set
474489
var resultSet = [];
@@ -526,10 +541,10 @@ define(function (require, exports, module) {
526541
* In that case, we don't want to change the selection unnecessarily.
527542
*/
528543
function handleQueryChange(editor, state, initial) {
529-
state.query = parseQuery(findBar && findBar.getQueryInfo());
544+
setQueryInfo(state, findBar && findBar.getQueryInfo());
530545
updateResultSet(editor);
531546

532-
if (state.query) {
547+
if (state.query && state.queryInfo) {
533548
// 3rd arg: prefer to avoid scrolling if result is anywhere within view, since in this case user
534549
// is in the middle of typing, not navigating explicitly; viewport jumping would be distracting.
535550
findNext(editor, false, true, state.searchStartPos);
@@ -614,7 +629,7 @@ define(function (require, exports, module) {
614629
*/
615630
function doSearch(editor, rev) {
616631
var state = getSearchState(editor._codeMirror);
617-
if (state.query) {
632+
if (state.query && state.queryInfo) {
618633
findNext(editor, rev);
619634
return;
620635
}
@@ -654,10 +669,10 @@ define(function (require, exports, module) {
654669
* @param {string|RegExp} replaceWhat - Query that will be passed into CodeMirror Cursor to search for results.
655670
* @param {string} replaceWith - String that should be used to replace chosen results.
656671
*/
657-
function _showReplaceAllPanel(editor, replaceWhat, replaceWith) {
672+
function _showReplaceAllPanel(editor, replaceWhat, queryInfo, replaceWith) {
658673
var results = [],
659674
cm = editor._codeMirror,
660-
cursor = getSearchCursor(cm, replaceWhat),
675+
cursor = getSearchCursor(cm, replaceWhat, queryInfo),
661676
from,
662677
to,
663678
line,
@@ -751,7 +766,7 @@ define(function (require, exports, module) {
751766

752767
if (all) {
753768
findBar.close();
754-
_showReplaceAllPanel(editor, state.query, replaceText);
769+
_showReplaceAllPanel(editor, state.query, state.queryInfo, replaceText);
755770
} else {
756771
cm.replaceSelection(typeof state.query === "string" ? replaceText : parseDollars(replaceText, state.lastMatch));
757772

test/spec/FindReplace-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ define(function (require, exports, module) {
10441044

10451045
toggleRegexp(true);
10461046
toggleCaseSensitive(true);
1047-
enterSearchText("foo");
1047+
enterSearchText("f.o");
10481048
expectHighlightedMatches(expectedSelections);
10491049
expectSelection(expectedSelections[0]);
10501050
});
@@ -1062,7 +1062,7 @@ define(function (require, exports, module) {
10621062

10631063
toggleRegexp(true);
10641064
toggleCaseSensitive(false);
1065-
enterSearchText("foo");
1065+
enterSearchText("f.o");
10661066
expectHighlightedMatches(expectedSelections);
10671067
expectSelection(expectedSelections[0]);
10681068
});

0 commit comments

Comments
 (0)