indexFile method
Build a word-position index for path for fast repeated searches.
Implementation
Future<FileIndex> indexFile(String path) async {
final content = await File(path).readAsString();
final lines = content.split('\n');
final positions = <String, List<(int, int)>>{};
final wordRe = RegExp(r'\w+');
for (var i = 0; i < lines.length; i++) {
for (final match in wordRe.allMatches(lines[i])) {
final word = match.group(0)!.toLowerCase();
positions.putIfAbsent(word, () => []).add((i + 1, match.start));
}
}
final index = FileIndex(
path: path,
wordPositions: positions,
indexedAt: DateTime.now(),
);
_indexCache[path] = index;
return index;
}