private Region PopulateTextRegionProperties(NewLineIndex lineIndex, Region inputRegion, string fileText, bool populateSnippet)
{
// A GENERAL NOTE ON THE PROPERTY POPULATION PROCESS:
//
// As a rule, if we find some existing data on the region, we will trust it
// and avoid overwriting it. We will take every opportunity, however, to
// validate that the existing information matches what the new line index
// computes. Note that we could consider making the new line index more
// efficient by deferring its newline computations until they are
// actually requested. If we do so, we could update this code to
// avoid verifying region data in cases where regions are fully
// populated (and we can skip file parsing required to build
// the map of new line offsets).
Assert(!inputRegion.IsBinaryRegion);
// If we have no input source file, there is no work to do
if (lineIndex == null)
{
return(inputRegion);
}
Debug.Assert(fileText != null);
Region region = inputRegion.DeepClone();
if (region.StartLine == 0)
{
// This means we have a region specified entirely via charOffset
PopulatePropertiesFromCharOffsetAndLength(lineIndex, region);
}
else
{
PopulatePropertiesFromStartAndEndProperties(lineIndex, region, fileText);
}
if (populateSnippet &&
region.CharOffset >= 0 &&
region.CharLength >= 0 &&
(region.CharOffset + region.CharLength <= fileText.Length))
{
region.Snippet ??= new ArtifactContent();
string snippetText = fileText.Substring(region.CharOffset, region.CharLength);
if (region.Snippet.Text == null)
{
region.Snippet.Text = snippetText;
}
Assert(region.Snippet.Text == snippetText);
}
return(region);
}