diff --git a/packages/flutter/lib/src/services/spell_check.dart b/packages/flutter/lib/src/services/spell_check.dart index 1b665b3ff3..3a2a4b6dc4 100644 --- a/packages/flutter/lib/src/services/spell_check.dart +++ b/packages/flutter/lib/src/services/spell_check.dart @@ -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?> fetchSpellCheckSuggestions( Locale locale, String text ); diff --git a/packages/flutter/lib/src/widgets/spell_check.dart b/packages/flutter/lib/src/widgets/spell_check.dart index d7c1bd1dc6..166db05e27 100644 --- a/packages/flutter/lib/src/widgets/spell_check.dart +++ b/packages/flutter/lib/src/widgets/spell_check.dart @@ -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 _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 _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; diff --git a/packages/flutter/test/services/spell_check_test.dart b/packages/flutter/test/services/spell_check_test.dart new file mode 100644 index 0000000000..7f149a2287 --- /dev/null +++ b/packages/flutter/test/services/spell_check_test.dart @@ -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), + [ + '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), + [ + 'weird', + ], + ); + const SpellCheckResults spellCheckResults = SpellCheckResults( + 'i before e except after c is so wierd.', + [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])])'); + }); +} diff --git a/packages/flutter/test/widgets/spell_check_test.dart b/packages/flutter/test/widgets/spell_check_test.dart index 7db696d6c9..7bd3169eb8 100644 --- a/packages/flutter/test/widgets/spell_check_test.dart +++ b/packages/flutter/test/widgets/spell_check_test.dart @@ -384,4 +384,58 @@ void main() { expect(textSpanTree, equals(expectedTextSpanTree)); }, variant: const TargetPlatformVariant({ 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( + TextRange(start: 0, end: 5), + ['suggestion1', 'suggestion2'], + ), + SuggestionSpan( + TextRange(start: 6, end: 11), + ['suggestion1', 'suggestion2'], + ), + SuggestionSpan( + TextRange(start: 12, end: 17), + ['suggestion1', 'suggestion2'], + ), + SuggestionSpan( + TextRange(start: 18, end: 21), + ['suggestion1', 'suggestion2'], + ), + ], + ); + + final TextSpan expectedTextSpanTree = TextSpan(children: [ + 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.android, TargetPlatform.iOS }), + ); }