11// @flow
22
33import buildQuery from "./build-query" ;
4- import findIndex from "lodash/findIndex" ;
54
65import type { SearchModifiers } from "../../types" ;
76
@@ -31,17 +30,6 @@ function SearchState() {
3130 */
3231function getSearchState ( cm : any , query , modifiers ) {
3332 let state = cm . state . search || ( cm . state . search = new SearchState ( ) ) ;
34-
35- // avoid generating a cursor and iterating over the results for an empty query
36- if ( query ) {
37- let cursor = getSearchCursor ( cm , query , null , modifiers ) ;
38-
39- state . results = [ ] ;
40- while ( cursor . findNext ( ) ) {
41- state . results . push ( cursor . pos ) ;
42- }
43- }
44-
4533 return state ;
4634}
4735
@@ -151,7 +139,8 @@ function getMatchIndex(count: number, currentIndex: number, rev: boolean) {
151139 */
152140function doSearch ( ctx , rev , query , keepSelection , modifiers : SearchModifiers ) {
153141 let { cm } = ctx ;
154- let matchIndex = 0 ;
142+ const matchIndex = - 1 ;
143+
155144 cm . operation ( function ( ) {
156145 if ( ! query || isWhitespace ( query ) ) {
157146 return ;
@@ -163,19 +152,11 @@ function doSearch(ctx, rev, query, keepSelection, modifiers: SearchModifiers) {
163152
164153 updateOverlay ( cm , state , query , modifiers ) ;
165154 updateCursor ( cm , state , keepSelection ) ;
155+ searchNext ( ctx , rev , query , newQuery , modifiers ) ;
166156
167- const nextMatch = searchNext ( ctx , rev , query , newQuery , modifiers ) ;
168- if ( nextMatch ) {
169- if ( state . matchIndex === - 1 ) {
170- matchIndex = findIndex ( state . results , nextMatch ) ;
171- } else {
172- const count = state . results . length ;
173- const currentIndex = state . matchIndex ;
174- matchIndex = getMatchIndex ( count , currentIndex , rev ) ;
175- }
176-
177- state . matchIndex = matchIndex ;
178- }
157+ // NOTE: We would like to find the correct match index based on where the
158+ // match is in the document.
159+ state . matchIndex = matchIndex ;
179160 } ) ;
180161
181162 return matchIndex ;
0 commit comments