@@ -132,8 +132,6 @@ private void resetSession() {
132132
133133 // Restore SpellCheckSpans in pool
134134 for (int i = 0 ; i < mLength ; i ++) {
135- // Resets id and progress to invalidate spell check span
136- mSpellCheckSpans [i ].setSpellCheckInProgress (false );
137135 mIds [i ] = -1 ;
138136 }
139137 mLength = 0 ;
@@ -200,15 +198,16 @@ private int nextSpellCheckSpanIndex() {
200198
201199 private void addSpellCheckSpan (Editable editable , int start , int end ) {
202200 final int index = nextSpellCheckSpanIndex ();
203- editable .setSpan (mSpellCheckSpans [index ], start , end , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
201+ SpellCheckSpan spellCheckSpan = mSpellCheckSpans [index ];
202+ editable .setSpan (spellCheckSpan , start , end , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
203+ spellCheckSpan .setSpellCheckInProgress (false );
204204 mIds [index ] = mSpanSequenceCounter ++;
205205 }
206206
207- public void removeSpellCheckSpan (SpellCheckSpan spellCheckSpan ) {
207+ public void onSpellCheckSpanRemoved (SpellCheckSpan spellCheckSpan ) {
208+ // Recycle any removed SpellCheckSpan (from this code or during text edition)
208209 for (int i = 0 ; i < mLength ; i ++) {
209210 if (mSpellCheckSpans [i ] == spellCheckSpan ) {
210- // Resets id and progress to invalidate spell check span
211- mSpellCheckSpans [i ].setSpellCheckInProgress (false );
212211 mIds [i ] = -1 ;
213212 return ;
214213 }
@@ -387,6 +386,7 @@ public void onGetSuggestions(SuggestionsInfo[] results) {
387386 final SpellCheckSpan spellCheckSpan =
388387 onGetSuggestionsInternal (results [i ], USE_SPAN_RANGE , USE_SPAN_RANGE );
389388 if (spellCheckSpan != null ) {
389+ // onSpellCheckSpanRemoved will recycle this span in the pool
390390 editable .removeSpan (spellCheckSpan );
391391 }
392392 }
@@ -414,11 +414,12 @@ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
414414 suggestionsInfo , offset , length );
415415 if (spellCheckSpan == null && scs != null ) {
416416 // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
417- // SentenceSuggestionsInfo
417+ // SentenceSuggestionsInfo. Removal is deferred after this loop.
418418 spellCheckSpan = scs ;
419419 }
420420 }
421421 if (spellCheckSpan != null ) {
422+ // onSpellCheckSpanRemoved will recycle this span in the pool
422423 editable .removeSpan (spellCheckSpan );
423424 }
424425 }
@@ -633,7 +634,8 @@ public void parse() {
633634 }
634635 break ;
635636 }
636- removeSpellCheckSpan (spellCheckSpan );
637+ // This spellCheckSpan is replaced by the one we are creating
638+ editable .removeSpan (spellCheckSpan );
637639 spellCheckStart = Math .min (spanStart , spellCheckStart );
638640 spellCheckEnd = Math .max (spanEnd , spellCheckEnd );
639641 }
0 commit comments