Skip to content
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

bindings/go: Fix bug in RangeText function #242

Merged
merged 2 commits into from
May 3, 2024
Merged

Conversation

varungandhi-src
Copy link
Contributor

Fixes #241
Fixes ENG-23731

Test plan

Added unit tests

@@ -69,7 +69,7 @@ func (d *SourceFile) String() string {
// RangeText returns the substring of the source file contents that enclose the provided range.
func (d *SourceFile) RangeText(position Range) string {
result := strings.Builder{}
for line := position.Start.Line; line < position.End.Line; line++ {
for line := position.Start.Line; line <= position.End.Line && int(line) < len(d.Lines); line++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I'm reading this right, can we use line <= math.Min(position.End.Line, d.Lines - 1) or something here?

can probably normalise the start line as well with respect to 0/d.Lines bounds

Copy link
Contributor Author

@varungandhi-src varungandhi-src May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this function already assumes the range is in bounds, so this was the wrong fix. I've simplified this to only do line <= position.End.Line. The caller can do bounds checks if needed, or recover from the panic if the range is not in bounds.

@varungandhi-src varungandhi-src merged commit 6495bfb into main May 3, 2024
2 checks passed
@varungandhi-src varungandhi-src deleted the vg/fix-RangeText branch May 3, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RangeText bug
2 participants