Skip to content

Commit

Permalink
applied fixes for mistake list
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarski committed Apr 27, 2023
1 parent ee0f0ec commit e11c6f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 51 deletions.
61 changes: 11 additions & 50 deletions lib/core/controllers/colored_text_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ class ColoredTextEditingController extends TextEditingController {
this.highlightStyle = const HighlightStyle(),
});

/// Clear mistakes list when text mas modified
void _handleTextChange(String newValue) {
if (newValue.length != text.length) {
_mistakes.clear();
}
}

/// Generates TextSpan from Mistake list
@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing,
}) {
final int textLength = text.length;
final Iterable<TextSpan> spanList =
_generateSpans(textLength: textLength, style: style);
final Iterable<TextSpan> spanList = _generateSpans(
style: style,
);

return TextSpan(
children: spanList.toList(),
Expand All @@ -41,19 +48,12 @@ class ColoredTextEditingController extends TextEditingController {

/// Generator function to create TextSpan instances
Iterable<TextSpan> _generateSpans({
required int textLength,
// required int textLength,
TextStyle? style,
}) sync* {
int currentOffset = 0; // enter index

for (final Mistake mistake in _mistakes) {
/// Breaks the loop if iterated Mistake offset is bigger than text
/// length.
if (mistake.offset > textLength ||
mistake.offset + mistake.length > textLength) {
break;
}

/// TextSpan before mistake
yield TextSpan(
text: text.substring(
Expand Down Expand Up @@ -90,45 +90,6 @@ class ColoredTextEditingController extends TextEditingController {
);
}

/// Apply changes to Mistake list while new data being fetched
void _handleTextChange(String newText) {
final int deltaLength = newText.length - text.length;

/// Update the _mistakes list in-place based on the text modifications
_mistakes = _mistakes
.map((mistake) {
int newOffset = mistake.offset;
int newLength = mistake.length;

/// If the text modification starts within the mistake
if (selection.start >= mistake.offset &&
selection.start <= mistake.offset + mistake.length) {
newLength += deltaLength;
}

/// If the text modification starts before the mistake
else if (selection.start < mistake.offset) {
newOffset += deltaLength;
}

/// Return the updated mistake (if the length is greater than 0)
return newLength > 0
? Mistake(
message: mistake.message,
type: mistake.type,
offset: newOffset,
length: newLength,
replacements: mistake.replacements,
)
: null;
})
.whereType<Mistake>()
.toList();

/// Notify listeners to rebuild the widget
notifyListeners();
}

/// A method sets new list of Mistake and triggers buildTextSpan
void highlightMistakes(List<Mistake> list) {
_mistakes = list;
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/core/controllers/colored_text_editing_controller.dart';
import 'package:languagetool_textfield/domain/highlight_style.dart';
import 'package:languagetool_textfield/domain/language_check_service.dart';

/// A TextField widget that checks the grammar using the given [langService]
Expand Down

0 comments on commit e11c6f2

Please sign in to comment.