Spell check range error (#153055)
Fixed a bug in spell checkers, especially custom ones, where deleting characters rapidly threw an error.
This commit is contained in:
committed by
GitHub
parent
cd54675eed
commit
ed47c4bb1f
@@ -52,6 +52,11 @@ class SuggestionSpan {
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(range.start, range.end, Object.hashAll(suggestions));
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'SuggestionSpan(range: $range, suggestions: $suggestions)';
|
||||
}
|
||||
}
|
||||
|
||||
/// A data structure grouping together the [SuggestionSpan]s and related text of
|
||||
@@ -85,6 +90,11 @@ class SpellCheckResults {
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(spellCheckedText, Object.hashAll(suggestionSpans));
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'SpellCheckResults(spellCheckText: $spellCheckedText, suggestionSpans: $suggestionSpans)';
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines how spell check results are received for text input.
|
||||
@@ -93,6 +103,10 @@ abstract class SpellCheckService {
|
||||
///
|
||||
/// Returns a [Future] that resolves with a [List] of [SuggestionSpan]s for
|
||||
/// all misspelled words in the given [String] for the given [Locale].
|
||||
///
|
||||
/// A return value that resolves to null indicates that fetching the spell
|
||||
/// check suggestions was unsuccessful. If fetching the suggestions succeeded
|
||||
/// but none were found, the [Future] should resolve to an empty list.
|
||||
Future<List<SuggestionSpan>?> fetchSpellCheckSuggestions(
|
||||
Locale locale, String text
|
||||
);
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
/// @docImport 'editable_text.dart';
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/painting.dart';
|
||||
import 'package:flutter/services.dart'
|
||||
@@ -157,7 +159,7 @@ List<SuggestionSpan> _correctSpellCheckResults(
|
||||
);
|
||||
|
||||
// Start search for the next misspelled word at the end of currentSpan.
|
||||
searchStart = currentSpan.range.end + 1 + offset;
|
||||
searchStart = math.min(currentSpan.range.end + 1 + offset, newText.length);
|
||||
correctedSpellCheckResults.add(adjustedSpan);
|
||||
} else if (currentSpanFoundElsewhere) {
|
||||
// Word was pushed forward but not modified.
|
||||
@@ -170,7 +172,7 @@ List<SuggestionSpan> _correctSpellCheckResults(
|
||||
|
||||
// Start search for the next misspelled word at the end of the
|
||||
// adjusted currentSpan.
|
||||
searchStart = adjustedSpanEnd + 1;
|
||||
searchStart = math.min(adjustedSpanEnd + 1, newText.length);
|
||||
// Adjust offset to reflect the difference between where currentSpan
|
||||
// was positioned in resultsText versus in newText.
|
||||
offset = adjustedSpanStart - currentSpan.range.start;
|
||||
|
||||
34
packages/flutter/test/services/spell_check_test.dart
Normal file
34
packages/flutter/test/services/spell_check_test.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test('SuggestionSpan.toString', () {
|
||||
const SuggestionSpan suggestionSpan = SuggestionSpan(
|
||||
TextRange(start: 12, end: 17),
|
||||
<String>[
|
||||
'weird',
|
||||
],
|
||||
);
|
||||
|
||||
expect(suggestionSpan.toString(), 'SuggestionSpan(range: TextRange(start: 12, end: 17), suggestions: [weird])');
|
||||
});
|
||||
|
||||
test('SpellCheckResults.toString', () {
|
||||
const SuggestionSpan suggestionSpan = SuggestionSpan(
|
||||
TextRange(start: 12, end: 17),
|
||||
<String>[
|
||||
'weird',
|
||||
],
|
||||
);
|
||||
const SpellCheckResults spellCheckResults = SpellCheckResults(
|
||||
'i before e except after c is so wierd.',
|
||||
<SuggestionSpan>[suggestionSpan],
|
||||
);
|
||||
|
||||
expect(spellCheckResults.toString(), 'SpellCheckResults(spellCheckText: i before e except after c is so wierd., suggestionSpans: [SuggestionSpan(range: TextRange(start: 12, end: 17), suggestions: [weird])])');
|
||||
});
|
||||
}
|
||||
@@ -384,4 +384,58 @@ void main() {
|
||||
expect(textSpanTree, equals(expectedTextSpanTree));
|
||||
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }),
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'buildTextSpanWithSpellCheckSuggestions does not throw when text is being deleted',
|
||||
(WidgetTester tester) async {
|
||||
// Regression test for https://github.com/flutter/flutter/issues/152272.
|
||||
const String text = 'Lorem ipsum dolor ';
|
||||
const String resultsText = 'Lorem ipsum dolor sit ';
|
||||
const TextEditingValue value = TextEditingValue(
|
||||
text: text,
|
||||
selection: TextSelection.collapsed(offset: text.length),
|
||||
);
|
||||
const bool composingRegionOutOfRange = false;
|
||||
const SpellCheckResults spellCheckResults = SpellCheckResults(
|
||||
resultsText,
|
||||
<SuggestionSpan>[
|
||||
SuggestionSpan(
|
||||
TextRange(start: 0, end: 5),
|
||||
<String>['suggestion1', 'suggestion2'],
|
||||
),
|
||||
SuggestionSpan(
|
||||
TextRange(start: 6, end: 11),
|
||||
<String>['suggestion1', 'suggestion2'],
|
||||
),
|
||||
SuggestionSpan(
|
||||
TextRange(start: 12, end: 17),
|
||||
<String>['suggestion1', 'suggestion2'],
|
||||
),
|
||||
SuggestionSpan(
|
||||
TextRange(start: 18, end: 21),
|
||||
<String>['suggestion1', 'suggestion2'],
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
final TextSpan expectedTextSpanTree = TextSpan(children: <TextSpan>[
|
||||
TextSpan(text: 'Lorem', style: misspelledTextStyle),
|
||||
const TextSpan(text: ' '),
|
||||
TextSpan(text: 'ipsum', style: misspelledTextStyle),
|
||||
const TextSpan(text: ' '),
|
||||
TextSpan(text: 'dolor', style: misspelledTextStyle),
|
||||
const TextSpan(text: ' '),
|
||||
]);
|
||||
final TextSpan textSpanTree = buildTextSpanWithSpellCheckSuggestions(
|
||||
value,
|
||||
composingRegionOutOfRange,
|
||||
null,
|
||||
misspelledTextStyle,
|
||||
spellCheckResults,
|
||||
);
|
||||
|
||||
expect(tester.takeException(), null);
|
||||
expect(textSpanTree, equals(expectedTextSpanTree));
|
||||
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user