Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public void initiateSpellCheck(

/** Calls on the Android spell check API to spell check specified text. */
public void performSpellCheck(@NonNull String locale, @NonNull String text) {
String[] localeCodes = locale.split("-");
Locale localeFromString = LocalizationPlugin.localeFromString(locale);

if (mSpellCheckerSession == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
// Returns an empty array if no spell check suggestions.
- (NSArray<NSDictionary<NSString*, id>*>*)findAllSpellCheckSuggestionsForText:(NSString*)text
inLanguage:(NSString*)language {
// Transform Dart Locale format to iOS language format.
language = [language stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
if (![UITextChecker.availableLanguages containsObject:language]) {
return nil;
}
Expand Down Expand Up @@ -145,7 +147,9 @@ - (instancetype)initWithMisspelledRange:(NSRange)range
- (NSDictionary<NSString*, NSObject*>*)toDictionary {
NSMutableDictionary* result = [[[NSMutableDictionary alloc] initWithCapacity:3] autorelease];
result[@"startIndex"] = @(_misspelledRange.location);
result[@"endIndex"] = @(_misspelledRange.location + _misspelledRange.length - 1);
// The end index represents the next index after the last character of a misspelled word to match
// the behavior of Dart's TextRange: https://api.flutter.dev/flutter/dart-ui/TextRange/end.html
result[@"endIndex"] = @(_misspelledRange.location + _misspelledRange.length);
result[@"suggestions"] = _suggestions;
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ - (void)testFindAllSpellCheckSuggestionsForText {
XCTAssertTrue(capturedResult.count == 2);
NSDictionary* suggestionsJSON1 = capturedResult.firstObject;
XCTAssertEqualObjects(suggestionsJSON1[@"startIndex"], @0);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @4);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right?

This isss misspelled
xxxxx^^^^xx
01234567890

Start index is 5, length is 4, end index is 8, not 9.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://api.flutter.dev/flutter/dart-ui/TextRange-class.html

The next index after the characters in this range.

Well that's confusing, as you experienced from the off by one error... Can you add a comment about that in toDictionary? In Objective-C this is usually expressed with a tuple range of start index + length, that the endIndex is after the range is not intuitive.

Copy link
Member

@jmagman jmagman Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Case '-[FlutterSpellCheckPluginTest testFindAllSpellCheckSuggestionsForText]' started.
/../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSpellCheckPluginTest.mm:164: error: -[FlutterSpellCheckPluginTest testFindAllSpellCheckSuggestionsForText] : ((suggestionsJSON1[@"endIndex"]) equal to (@4)) failed: ("5") is not equal to ("4")
/../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSpellCheckPluginTest.mm:168: error: -[FlutterSpellCheckPluginTest testFindAllSpellCheckSuggestionsForText] : ((suggestionsJSON2[@"endIndex"]) equal to (@9)) failed: ("10") is not equal to ("9")
Test Case '-[FlutterSpellCheckPluginTest testFindAllSpellCheckSuggestionsForText]' failed (0.061 seconds).

XCTAssertEqualObjects(suggestionsJSON1[@"suggestions"], suggestions1);
NSDictionary* suggestionsJSON2 = capturedResult[1];
XCTAssertEqualObjects(suggestionsJSON2[@"startIndex"], @5);
XCTAssertEqualObjects(suggestionsJSON2[@"endIndex"], @9);
XCTAssertEqualObjects(suggestionsJSON2[@"endIndex"], @10);
XCTAssertEqualObjects(suggestionsJSON2[@"suggestions"], suggestions2);
[self.mockTextChecker reset];
[textCheckerClassMock stopMocking];
Expand Down Expand Up @@ -198,11 +198,11 @@ - (void)testStopFindingMoreWhenTheLastWordIsMisspelled {
XCTAssertTrue(capturedResult.count == 2);
NSDictionary* suggestionsJSON1 = capturedResult.firstObject;
XCTAssertEqualObjects(suggestionsJSON1[@"startIndex"], @0);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @4);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @5);
XCTAssertEqualObjects(suggestionsJSON1[@"suggestions"], suggestions1);
NSDictionary* suggestionsJSON2 = capturedResult[1];
XCTAssertEqualObjects(suggestionsJSON2[@"startIndex"], @6);
XCTAssertEqualObjects(suggestionsJSON2[@"endIndex"], @9);
XCTAssertEqualObjects(suggestionsJSON2[@"endIndex"], @10);
XCTAssertEqualObjects(suggestionsJSON2[@"suggestions"], suggestions2);
[self.mockTextChecker reset];
[textCheckerClassMock stopMocking];
Expand All @@ -228,7 +228,7 @@ - (void)testStopFindingMoreWhenTheWholeStringIsAMisspelledWord {
XCTAssertTrue(capturedResult.count == 1);
NSDictionary* suggestionsJSON1 = capturedResult.firstObject;
XCTAssertEqualObjects(suggestionsJSON1[@"startIndex"], @0);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @4);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @5);
XCTAssertEqualObjects(suggestionsJSON1[@"suggestions"], suggestions1);
[self.mockTextChecker reset];
[textCheckerClassMock stopMocking];
Expand Down Expand Up @@ -270,6 +270,29 @@ - (void)testUnsupportedLanguageShouldReturnNil {
[textCheckerClassMock stopMocking];
}

- (void)testSupportSubLanguage {
self.partialMockPlugin = OCMPartialMock(self.plugin);
OCMStub([self.partialMockPlugin textChecker]).andReturn(self.mockTextChecker);
id textCheckerClassMock = OCMClassMock([UITextChecker class]);
[[[textCheckerClassMock stub] andReturn:@[ @"en_us" ]] availableLanguages];
NSArray* suggestions1 = @[ @"suggestion 1", @"suggestion 2" ];

[self mockUITextCheckerWithExpectedMisspelledWordRange:NSMakeRange(0, 5)
startingIndex:0
suggestions:suggestions1];
__block NSArray* capturedResult;
[self.mockMethodChannel invokeMethod:@"SpellCheck.initiateSpellCheck"
arguments:@[ @"en-us", @"hejjo" ]
result:^(id _Nullable result) {
capturedResult = result;
}];
NSDictionary* suggestionsJSON1 = capturedResult.firstObject;
XCTAssertEqualObjects(suggestionsJSON1[@"startIndex"], @0);
XCTAssertEqualObjects(suggestionsJSON1[@"endIndex"], @5);
XCTAssertEqualObjects(suggestionsJSON1[@"suggestions"], suggestions1);
[textCheckerClassMock stopMocking];
}

- (void)testEmptyStringShouldReturnEmptyResults {
self.partialMockPlugin = OCMPartialMock(self.plugin);
OCMStub([self.partialMockPlugin textChecker]).andReturn(self.mockTextChecker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,8 @@ - (void)onUserSettingsChanged:(NSNotification*)notification {
@"textScaleFactor" : @([self textScaleFactor]),
@"alwaysUse24HourFormat" : @([self isAlwaysUse24HourFormat]),
@"platformBrightness" : [self brightnessMode],
@"platformContrast" : [self contrastMode]
@"platformContrast" : [self contrastMode],
@"nativeSpellCheckServiceDefined" : @true
}];
}

Expand Down