Skip to content

Commit 6495bfb

Browse files
bindings/go: Fix bug in RangeText function (#242)
1 parent dc6f8c6 commit 6495bfb

File tree

4 files changed

+130
-26
lines changed

4 files changed

+130
-26
lines changed

bindings/go/scip/source_file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (d *SourceFile) String() string {
6969
// RangeText returns the substring of the source file contents that enclose the provided range.
7070
func (d *SourceFile) RangeText(position Range) string {
7171
result := strings.Builder{}
72-
for line := position.Start.Line; line < position.End.Line; line++ {
72+
for line := position.Start.Line; line <= position.End.Line; line++ {
7373
start := position.Start.Character
7474
if line > position.Start.Line {
7575
result.WriteString("\n")

bindings/go/scip/source_file_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package scip
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hexops/autogold/v2"
8+
)
9+
10+
const testFileContents = `Mary had a little lamb,
11+
Its fleece was white as snow.
12+
And everywhere that Mary went,
13+
The lamb was sure to go.
14+
`
15+
16+
func TestRangeText(t *testing.T) {
17+
// Update expect values with `go test ./bindings/go/scip -update`
18+
testCases := []struct {
19+
range_ []int32
20+
expect autogold.Value
21+
}{
22+
{range_: []int32{0, 0, 0, 0}, expect: autogold.Expect("")},
23+
{range_: []int32{0, 0, 0, 8}, expect: autogold.Expect("Mary had")},
24+
{range_: []int32{0, 0, 1, 0}, expect: autogold.Expect("Mary had a little lamb,\n")},
25+
{range_: []int32{0, 0, 1, 10}, expect: autogold.Expect("Mary had a little lamb,\nIts fleece")},
26+
{range_: []int32{0, 0, 4, 0}, expect: autogold.Expect(`Mary had a little lamb,
27+
Its fleece was white as snow.
28+
And everywhere that Mary went,
29+
The lamb was sure to go.
30+
`)},
31+
}
32+
33+
sourceFile := NewSourceFile("", "", testFileContents)
34+
35+
for _, testCase := range testCases {
36+
r := testCase.range_
37+
range_ := Range{Start: Position{Line: r[0], Character: r[1]}, End: Position{Line: r[2], Character: r[3]}}
38+
t.Run(fmt.Sprintf("%v", range_), func(t *testing.T) {
39+
testCase.expect.Equal(t, sourceFile.RangeText(range_))
40+
})
41+
}
42+
}

go.mod

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/bufbuild/buf v1.25.0
77
github.com/google/go-cmp v0.5.9
88
github.com/google/gofuzz v1.1.0
9+
github.com/hexops/autogold/v2 v2.2.1
910
github.com/hexops/gotextdiff v1.0.3
1011
github.com/hhatto/gocloc v0.4.2
1112
github.com/k0kubun/pp/v3 v3.1.0
@@ -15,7 +16,7 @@ require (
1516
github.com/sourcegraph/sourcegraph/lib v0.0.0-20220511160847-5a43d3ea24eb
1617
github.com/stretchr/testify v1.8.4
1718
github.com/urfave/cli/v2 v2.25.7
18-
golang.org/x/tools v0.11.0
19+
golang.org/x/tools v0.12.0
1920
google.golang.org/protobuf v1.31.0
2021
)
2122

@@ -41,6 +42,7 @@ require (
4142
github.com/docker/go-connections v0.4.0 // indirect
4243
github.com/docker/go-units v0.5.0 // indirect
4344
github.com/envoyproxy/protoc-gen-validate v0.3.0-java // indirect
45+
github.com/fatih/color v1.15.0 // indirect
4446
github.com/felixge/fgprof v0.9.3 // indirect
4547
github.com/getsentry/sentry-go v0.12.0 // indirect
4648
github.com/go-chi/chi/v5 v5.0.10 // indirect
@@ -54,6 +56,7 @@ require (
5456
github.com/google/go-containerregistry v0.15.2 // indirect
5557
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
5658
github.com/google/uuid v1.3.0 // indirect
59+
github.com/hexops/valast v1.4.4 // indirect
5760
github.com/huandu/xstrings v1.0.0 // indirect
5861
github.com/imdario/mergo v0.3.4 // indirect
5962
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -63,22 +66,23 @@ require (
6366
github.com/klauspost/pgzip v1.2.6 // indirect
6467
github.com/kr/pretty v0.3.1 // indirect
6568
github.com/kr/text v0.2.0 // indirect
66-
github.com/mattn/go-colorable v0.1.12 // indirect
67-
github.com/mattn/go-isatty v0.0.14 // indirect
69+
github.com/mattn/go-colorable v0.1.13 // indirect
70+
github.com/mattn/go-isatty v0.0.19 // indirect
6871
github.com/mitchellh/go-homedir v1.1.0 // indirect
6972
github.com/moby/term v0.5.0 // indirect
7073
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7174
github.com/modern-go/reflect2 v1.0.2 // indirect
7275
github.com/morikuni/aec v1.0.0 // indirect
7376
github.com/mwitkow/go-proto-validators v0.0.0-20180403085117-0950a7990007 // indirect
77+
github.com/nightlyone/lockfile v1.0.0 // indirect
7478
github.com/opencontainers/go-digest v1.0.0 // indirect
7579
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
7680
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
7781
github.com/pkg/errors v0.9.1 // indirect
7882
github.com/pkg/profile v1.7.0 // indirect
7983
github.com/pmezard/go-difflib v1.0.0 // indirect
8084
github.com/pseudomuto/protokit v0.2.0 // indirect
81-
github.com/rogpeppe/go-internal v1.9.0 // indirect
85+
github.com/rogpeppe/go-internal v1.10.0 // indirect
8286
github.com/rs/cors v1.9.0 // indirect
8387
github.com/russross/blackfriday/v2 v2.1.0 // indirect
8488
github.com/sirupsen/logrus v1.9.3 // indirect
@@ -94,13 +98,14 @@ require (
9498
go.uber.org/atomic v1.11.0 // indirect
9599
go.uber.org/multierr v1.11.0 // indirect
96100
go.uber.org/zap v1.24.0 // indirect
97-
golang.org/x/crypto v0.11.0 // indirect
101+
golang.org/x/crypto v0.12.0 // indirect
98102
golang.org/x/mod v0.12.0 // indirect
99-
golang.org/x/net v0.12.0 // indirect
103+
golang.org/x/net v0.14.0 // indirect
100104
golang.org/x/sync v0.3.0 // indirect
101-
golang.org/x/sys v0.10.0 // indirect
102-
golang.org/x/term v0.10.0 // indirect
103-
golang.org/x/text v0.11.0 // indirect
105+
golang.org/x/sys v0.11.0 // indirect
106+
golang.org/x/term v0.11.0 // indirect
107+
golang.org/x/text v0.12.0 // indirect
104108
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4 // indirect
105109
gopkg.in/yaml.v3 v3.0.1 // indirect
110+
mvdan.cc/gofumpt v0.5.0 // indirect
106111
)

0 commit comments

Comments
 (0)