public static void Populate(this Region region, NewLineIndex newLineIndex)
{
if (region == null)
{
throw new ArgumentNullException(nameof(region));
}
if (newLineIndex == null)
{
throw new ArgumentNullException(nameof(newLineIndex));
}
// A call to Populate is an implicit indicator that we are working
// with a text region (otherwise the offset and length would be
// sufficient data to constitute the region).
if (region.StartLine == 0)
{
OffsetInfo offsetInfo = newLineIndex.GetOffsetInfoForOffset(region.Offset);
region.StartLine = offsetInfo.LineNumber;
region.StartColumn = offsetInfo.ColumnNumber;
offsetInfo = newLineIndex.GetOffsetInfoForOffset(region.Offset + region.Length);
region.EndLine = offsetInfo.LineNumber;
region.EndColumn = offsetInfo.ColumnNumber;
}
else
{
// Make endColumn and endLine explicit, if not expressed
if (region.EndLine == 0) { region.EndLine = region.StartLine; }
if (region.StartColumn == 0) { region.StartColumn = 1; }
if (region.EndColumn == 0) { region.EndColumn = region.StartColumn; }
LineInfo lineInfo = newLineIndex.GetLineInfoForLine(region.StartLine);
region.Offset = lineInfo.StartOffset + (region.StartColumn - 1);
lineInfo = newLineIndex.GetLineInfoForLine(region.EndLine);
region.Length = lineInfo.StartOffset + (region.EndColumn - 1) - region.Offset;
}
}