Merge "Fix a bug in SpellCheckSpan update logic." into sc-dev

This commit is contained in:
TreeHugger Robot
2021-05-25 18:00:58 +00:00
committed by Android (Google) Code Review
2 changed files with 21 additions and 12 deletions

View File

@@ -375,6 +375,13 @@ public class SpellChecker implements SpellCheckerSessionListener {
final int sequenceNumber = suggestionsInfo.getSequence();
for (int k = 0; k < mLength; ++k) {
if (sequenceNumber == mIds[k]) {
final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
if (spellCheckSpanStart < 0) {
// Skips the suggestion if the matched span has been removed.
return null;
}
final int attributes = suggestionsInfo.getSuggestionsAttributes();
final boolean isInDictionary =
((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
@@ -383,7 +390,11 @@ public class SpellChecker implements SpellCheckerSessionListener {
final boolean looksLikeGrammarError =
((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR) > 0);
final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
// Validates the suggestions range in case the SpellCheckSpan is out-of-date but not
// removed as expected.
if (spellCheckSpanStart + offset + length > editable.length()) {
return spellCheckSpan;
}
//TODO: we need to change that rule for results from a sentence-level spell
// checker that will probably be in dictionary.
if (!isInDictionary && (looksLikeTypo || looksLikeGrammarError)) {
@@ -393,7 +404,6 @@ public class SpellChecker implements SpellCheckerSessionListener {
// Valid word -- isInDictionary || !looksLikeTypo
// Allow the spell checker to remove existing misspelled span by
// overwriting the span over the same place
final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
final int start;
final int end;
@@ -461,7 +471,6 @@ public class SpellChecker implements SpellCheckerSessionListener {
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
final Editable editable = (Editable) mTextView.getText();
final int sentenceLength = editable.length();
for (int i = 0; i < results.length; ++i) {
final SentenceSuggestionsInfo ssi = results[i];
if (ssi == null) {
@@ -475,9 +484,6 @@ public class SpellChecker implements SpellCheckerSessionListener {
}
final int offset = ssi.getOffsetAt(j);
final int length = ssi.getLengthAt(j);
if (offset < 0 || offset + length > sentenceLength) {
continue;
}
final SpellCheckSpan scs = onGetSuggestionsInternal(
suggestionsInfo, offset, length);
if (spellCheckSpan == null && scs != null) {

View File

@@ -10750,12 +10750,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
Editable text = (Editable) mText;
T[] spans = text.getSpans(start, end, type);
final int length = spans.length;
for (int i = 0; i < length; i++) {
final int spanStart = text.getSpanStart(spans[i]);
final int spanEnd = text.getSpanEnd(spans[i]);
if (spanEnd == start || spanStart == end) break;
text.removeSpan(spans[i]);
ArrayList<T> spansToRemove = new ArrayList<>();
for (T span : spans) {
final int spanStart = text.getSpanStart(span);
final int spanEnd = text.getSpanEnd(span);
if (spanEnd == start || spanStart == end) continue;
spansToRemove.add(span);
}
for (T span : spansToRemove) {
text.removeSpan(span);
}
}