-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improving multiline context snippet #2288
Changes from 2 commits
b04dfc1
d3886be
fef1246
58cf640
6f9318c
e493b7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,15 +156,42 @@ internal Region ConstructMultilineContextSnippet(Region inputRegion, Uri uri) | |
return null; | ||
} | ||
|
||
const int maxSnippetLength = 512; | ||
const int reasonableSnippetLength = 256; | ||
const int smallSnippetLength = 128; | ||
|
||
// If this is big enough, we don't need to create a new snippet. | ||
if (inputRegion.Snippet?.Text.Length >= reasonableSnippetLength) | ||
{ | ||
return null; | ||
} | ||
|
||
int maxLineNumber = newLineIndex.MaximumLineNumber; | ||
|
||
// Currently, we just grab a single line before and after the region start | ||
// and end lines, respectively. In the future, we could make this configurable. | ||
var region = new Region() | ||
var region = new Region | ||
{ | ||
StartLine = inputRegion.StartLine == 1 ? 1 : inputRegion.StartLine - 1, | ||
EndLine = inputRegion.EndLine == maxLineNumber ? maxLineNumber : inputRegion.EndLine + 1 | ||
}; | ||
|
||
Region multilineContextSnippet = this.PopulateTextRegionProperties(region, uri, populateSnippet: true); | ||
|
||
// If the new snippet is big enough, we will use. Otherwise, we will | ||
// generate a new snippet | ||
if (multilineContextSnippet.Snippet.Text.Length <= maxSnippetLength) | ||
{ | ||
return multilineContextSnippet; | ||
} | ||
|
||
// We are going to grab 128 characters from left and from right if possible. | ||
region.CharOffset = inputRegion.CharOffset < smallSnippetLength | ||
? 0 | ||
: inputRegion.CharOffset - smallSnippetLength; | ||
|
||
region.CharLength = inputRegion.CharLength + inputRegion.CharOffset + smallSnippetLength < newLineIndex.Text.Length | ||
? inputRegion.CharLength + inputRegion.CharOffset + smallSnippetLength | ||
: newLineIndex.Text.Length - region.CharOffset; | ||
|
||
return this.PopulateTextRegionProperties(region, uri, populateSnippet: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
the end of this method could be a good place to introduce a debug assert that verifies we have limited the size of the region that we return. #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the debug.Assert to verify if the new length >= the original In reply to: 578572270 [](ancestors = 578572270) |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we actually be checking the region size, not the snippet size? basically, if a region exceeds a certain size, no context region is required. so for this case, the region should have the snippet. your code will permit the following case:
region is 10k in size but has no snippet
we then create a context region + snippet which is smaller than the region! this breaks the SARIF spec. :) #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to the charLength.
In reply to: 578048420 [](ancestors = 578048420)