Skip to content

Commit

Permalink
Fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Luxorum committed Apr 26, 2023
1 parent 0ec7c60 commit 78ae809
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/domain/mistake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Mistake {
/// Sorted by probability.
final List<String> replacements;

/// A range of this mistake from offset to the end.
int get range => offset + length;

/// Creates a new instance of the [Mistake] class.
const Mistake({
required this.message,
Expand Down
30 changes: 18 additions & 12 deletions lib/presentation/widgets/language_tool_text_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,45 @@ class LanguageToolTextEditingController extends TextEditingController {
}

final lastMistakeIndex = mistakes.length - 1;

for (int i = 0; i < mistakes.length; i++) {
final mistake = mistakes[i];
int previousMistakePosition = 0;
int previousMistakeEnd = 0;
if (i > 0) {
final previousMistake = mistakes[i - 1];
previousMistakePosition =
previousMistake.offset + previousMistake.length;
previousMistakeEnd = previousMistake.offset + previousMistake.length;
}
final mistakeStart = mistake.offset;
final mistakeEnd = mistakeStart + mistake.length;

if (mistakeEnd > text.length) {
if (mistake.range > text.length) {
children.add(
TextSpan(
text: text.substring(previousMistakePosition),
text: text.substring(previousMistakeEnd),
),
);
break;
}

children.add(
TextSpan(text: text.substring(previousMistakePosition, mistakeStart)),
TextSpan(text: text.substring(previousMistakeEnd, mistakeStart)),
);

final textStyle = style ?? const TextStyle();
final mistakeText = text.substring(mistakeStart, mistakeEnd);
final mistakeText = text.substring(mistakeStart, mistake.range);

// WidgetSpans with mistake text characters are used here to calculate the correct caret position, which can be incorrectly positioned because of the WidgetSpan issue, described here: https://github.com/flutter/flutter/issues/107432.
// TextSpan recognizer to process clicks can't be used, because it requires the RichText widget instead of TextField, which we are using. Issue described here: https://github.com/flutter/flutter/issues/34931
// WidgetSpans with mistake text characters are used here to
// calculate the correct caret position, which can be
// incorrectly positioned because of the WidgetSpan issue,
// described here: https://github.com/flutter/flutter/issues/107432.
//
// TextSpan recognizer to process clicks can't be used,
// because it requires the RichText widget but the TextField
// widget does not contain one.
// Issue described here: https://github.com/flutter/flutter/issues/34931

children.add(
TextSpan(
style: textStyle.copyWith(
color: Colors.green,
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationThickness: underlineThickness,
Expand All @@ -78,10 +83,11 @@ class LanguageToolTextEditingController extends TextEditingController {
],
),
);

if (i == lastMistakeIndex) {
children.add(
TextSpan(
text: text.substring(mistakeEnd),
text: text.substring(mistake.range),
),
);
}
Expand Down

0 comments on commit 78ae809

Please sign in to comment.