addToDecodersList static method
Adds entries to the end of the decoders([...]) list in file.
Returns null when the list can't be found.
Implementation
static String? addToDecodersList(String file, String entries) {
RegExpMatch? match = _decodersList.firstMatch(file);
if (match == null) return null;
int open = match.end - 1;
int? close = _closingBrace(file, open, openChar: '[', closeChar: ']');
if (close == null) return null;
List<String> lines = file
.substring(open + 1, close)
.trimRight()
.split('\n');
// The last line of code, skipping comments, decides whether a comma is
// needed. It goes straight after that code, or on its own line when the
// code ends in a comment it would land inside.
int lastCode = lines.lastIndexWhere((line) {
String code = line.trim();
return code.isNotEmpty && !code.startsWith('//');
});
if (lastCode != -1) {
String code = lines[lastCode].trimRight();
if (code.contains('//')) {
if (!code.endsWith(',')) lines.add(',');
} else if (!code.endsWith(',')) {
lines[lastCode] = '$code,';
}
}
return file.replaceRange(open + 1, close, '${lines.join('\n')}\n$entries');
}