@@ -16,6 +16,7 @@ export class SearchHighlighter {
1616 currentFound : number ;
1717 totalFound : number ;
1818 matcher ?: MatchDecorator ;
19+ searchRegexp ?: RegExp ;
1920 private parsedMatches : Match [ ] ;
2021
2122 constructor ( public view : EditorView ) {
@@ -42,6 +43,7 @@ export class SearchHighlighter {
4243 regexp : regex ,
4344 decoration : searchMatchDecoration ,
4445 } ) ;
46+ this . searchRegexp = regex ;
4547 this . #updateSearchData( this . view ) ;
4648 this . #scrollToMatchNearestSelection( ) ;
4749 }
@@ -98,17 +100,21 @@ export class SearchHighlighter {
98100 return ;
99101 }
100102
101- const matches = this . matcher . createDeco ( view ) ;
102- const cursor = matches . iter ( ) ;
103- while ( cursor . value ) {
104- this . parsedMatches . push ( {
105- from : cursor . from ,
106- to : cursor . to
107- } ) ;
108- cursor . next ( ) ;
103+ // Create the match decorator which will automatically highlight matches in the document.
104+ this . matches = this . matcher . createDeco ( view ) ;
105+
106+ // Manually search for matches in the current document in order to get the total number of matches.
107+ const parsedMatches : Match [ ] = [ ] ;
108+ const text = view . state . doc . toString ( ) ;
109+ let match : RegExpExecArray | null | undefined ;
110+ while ( ( match = this . searchRegexp ?. exec ( text ) ) ) {
111+ const from = match . index ?? 0 ;
112+ const to = from + match [ 0 ] . length ;
113+
114+ parsedMatches . push ( { from, to } ) ;
109115 }
110116
111- this . matches = matches ;
117+ this . parsedMatches = parsedMatches ;
112118 this . totalFound = this . parsedMatches . length ;
113119 }
114120
0 commit comments