ensureWhiteSpaceController method
void
ensureWhiteSpaceController()
A listener that ensures that there is always whitespace around a tag.
If there is no whitespace to the left of a tag, the tag is removed.
Implementation
void ensureWhiteSpaceController() {
final List<(Match?, Match?)> matchPairs = getTagMatchPairs(text);
for (int i = 0; i < matchPairs.length; i = i + 1) {
final (start as Match, end as Match) = matchPairs[i];
// Remove tag if there is no whitespace to the left of the tag
final isFirstWord = start.start == 0;
if (!isFirstWord &&
!tagStyles.keys.contains(text.substring(start.end, start.end + 1))) {
value = TextEditingValue(
text: text.replaceRange(
start.start,
end.end,
text.substring(start.end, end.start),
),
// Characters are removed to the right -> cursor can remain at same spot
selection: TextSelection.collapsed(offset: start.start),
);
return;
}
// Remove tag if there is no whitespace to the right of the tag
final isLastWord = end.end == text.length;
if (!isLastWord && text.substring(end.end, end.end + 1) != ' ') {
final userDidBackspace = previousCursorPosition > selection.baseOffset;
value = TextEditingValue(
text: text.replaceRange(
start.start,
end.end,
text.substring(start.end, end.start),
),
selection: TextSelection.collapsed(
offset: end.end - 2 + (userDidBackspace ? 0 : 1)),
);
return;
}
}
}