Skip to content

Commit

Permalink
Fixing FileRegionsCache logic (#2309)
Browse files Browse the repository at this point in the history
* Fixing  FileRegionsCache logic

* Fixing logic
  • Loading branch information
eddynaka authored Mar 5, 2021
1 parent b3d4aa0 commit 0d1de44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
18 changes: 9 additions & 9 deletions src/Sarif/FileRegionsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public void ClearCache()
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
//
// 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
// 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);
Expand Down Expand Up @@ -183,8 +183,8 @@ internal Region ConstructMultilineContextSnippet(Region inputRegion, Uri uri)
? 0
: originalRegion.CharOffset - smallSnippetLength;

region.CharLength = originalRegion.CharLength + originalRegion.CharOffset + smallSnippetLength < newLineIndex.Text.Length
? originalRegion.CharLength + originalRegion.CharOffset + smallSnippetLength
region.CharLength = originalRegion.CharLength + region.CharOffset + smallSnippetLength < newLineIndex.Text.Length
? originalRegion.CharLength + smallSnippetLength + Math.Abs(region.CharOffset - originalRegion.CharOffset)
: newLineIndex.Text.Length - region.CharOffset;

// Generating multineRegion with 128 characters to the left and right from the
Expand Down Expand Up @@ -234,7 +234,7 @@ private void PopulatePropertiesFromStartAndEndProperties(NewLineIndex lineIndex,
{
Assert(region.StartLine > 0);

// Note: execution order of these helpers is important, as some
// Note: execution order of these helpers is important, as some
// calls assume that certain preceding helpers have executed,
// with the result that certain properties are populated

Expand Down
11 changes: 5 additions & 6 deletions src/Test.UnitTests.Sarif/FileRegionsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public TestCaseData(Region inputRegion, Region outputRegion)
}

// 0 10 19
// 0123 4 5678 9 01234 5 6789
// 0123 4 5678 9 01234 5 6789
private const string SPEC_EXAMPLE = "abcd\r\nefg\r\nhijk\r\nlmn";

// Breaking the lines for readability and per-line column details
//
//
// Column: 123 4 5 6
// Line 1: abc d\r\n
// 2: efg\r\n
Expand Down Expand Up @@ -351,7 +351,7 @@ public TestCaseData(Region inputRegion, Region outputRegion)
private static readonly ReadOnlyCollection<TestCaseData> s_newLineTestCases =
new ReadOnlyCollection<TestCaseData>(new TestCaseData[]
{
//
//
// Sanity check sample with new line characters only
new TestCaseData(outputRegion: s_Complete_File_New_Lines_Only,
inputRegion: new Region() { CharOffset = 0, CharLength = 12 }),
Expand All @@ -375,7 +375,7 @@ public TestCaseData(Region inputRegion, Region outputRegion)
private static readonly ReadOnlyCollection<TestCaseData> s_carriageReturnTestCasess =
new ReadOnlyCollection<TestCaseData>(new TestCaseData[]
{
//
//
// Sanity check sample with carriage return characters only
new TestCaseData(outputRegion: s_Complete_File_Carriage_Returns_Only,
inputRegion: new Region() { CharOffset = 0, CharLength = 10 }),
Expand Down Expand Up @@ -621,9 +621,8 @@ public void FileRegionsCache_PopulatesWithOneLine_IncreasingToTheRight()
Region multilineRegion = fileRegionsCache.ConstructMultilineContextSnippet(region, uri);

// CharLength + 128 to the right = 428 characters
// 200 letters a + 228 letters b
multilineRegion.CharLength.Should().Be(300 + 128);
multilineRegion.Snippet.Text.Should().Be($"{new string('a', 200)}{new string('b', 228)}");
multilineRegion.Snippet.Text.Should().NotBeNullOrEmpty();
}

[Fact]
Expand Down

0 comments on commit 0d1de44

Please sign in to comment.