getMarkers method
Mark the start of each occurrence of each token with a number equal to the length of the token.
This can be thought of as the amount of "ink" that the highlighter should have when starting from this point.
e.g. For the text "abc def"
with tokens
["ab", "de"]
the marker array will be [2,0,0,0,2,0,0]
, and the highlighted
segments of the string will be computed as [*ab*,c ,*de*,f]
Subclasses of TextHighlighter can override this method to provide custom text highlighting behavior.
Implementation
@protected
List<int> getMarkers(String text, List<String?> tokens) {
var matchText = caseSensitive ? text : text.toLowerCase();
List<int> markers = List.filled(matchText.length, 0);
for (String? token in tokens) {
// Prevents an infinite loop, since there are "infinite" occurrences of
// the empty string.
if (token!.isEmpty) continue;
if (!caseSensitive) {
token = token.toLowerCase();
}
int index = 0;
while (true) {
index = matchText.indexOf(token, index);
if (index == -1) {
break;
} else {
String? wrapperToken = index != 0 ? matchText[index - 1] : null;
if (!matchFromStartOfWord ||
(index == 0 ||
// Some suggestions will have an alternate label appended to
// the end of the suggestion and enclosed in parenthesis or
// square brackets. We want to highlight matches to words that
// are wrapped in square brackets and parenthesis so we add
// this check here.
wrapperToken == "(" ||
wrapperToken == " " ||
wrapperToken == "[")) {
markers[index] = max(markers[index], token.length);
}
index += token.length;
}
}
}
return markers;
}