lineAndColumnOf static method
Converts the position index in a buffer to a line and column tuple.
const buffer = 'a\nb';
print(Token.lineAndColumnOf(buffer, 0)); // [1, 1]
print(Token.lineAndColumnOf(buffer, 2)); // [2, 1]
Implementation
static List<int> lineAndColumnOf(String buffer, int position) {
var line = 1, offset = 0;
for (final token in newlineParser().token().allMatches(buffer)) {
if (position < token.stop) {
return [line, position - offset + 1];
}
line++;
offset = token.stop;
}
return [line, position - offset + 1];
}