searchText method
Searches file contents for pattern (a regular expression). glob limits
which files are searched; context lines of surrounding text are attached.
Implementation
@override
Future<List<TextMatch>> searchText(
String pattern, {
String? glob,
bool ignoreCase = false,
int context = 0,
int? limit,
}) async {
final re = RegExp(pattern, caseSensitive: !ignoreCase);
final results = <TextMatch>[];
final paths = _files.keys.where((p) => matchesGlob(p, glob)).toList()
..sort();
for (final path in paths) {
final lines = _files[path]!.split('\n');
for (var i = 0; i < lines.length; i++) {
final m = re.firstMatch(lines[i]);
if (m == null) continue;
results.add(
TextMatch(
path: path,
line: i + 1,
column: m.start + 1,
text: lines[i],
before: context > 0
? lines.sublist((i - context).clamp(0, i), i)
: const [],
after: context > 0
? lines.sublist(
(i + 1).clamp(0, lines.length),
(i + 1 + context).clamp(0, lines.length),
)
: const [],
),
);
if (limit != null && results.length >= limit) return results;
}
}
return results;
}