Skip to content

Commit 1877291

Browse files
committed
go/token: add (*File).Lines method
This method returns the array updated by SetLines, for use in exporter packages. Fixes #57708 Change-Id: I12ed5e7e1bae7517f40cb25e76e51997c25d84f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/464515 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent cb3c50d commit 1877291

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

api/next/57708.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg go/token, method (*File) Lines() []int #57708

src/go/token/position.go

+9
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ func (f *File) MergeLine(line int) {
159159
f.lines = f.lines[:len(f.lines)-1]
160160
}
161161

162+
// Lines returns the effective line offset table of the form described by SetLines.
163+
// Callers must not mutate the result.
164+
func (f *File) Lines() []int {
165+
f.mutex.Lock()
166+
lines := f.lines
167+
f.mutex.Unlock()
168+
return lines
169+
}
170+
162171
// SetLines sets the line offsets for a file and reports whether it succeeded.
163172
// The line offsets are the offsets of the first character of each line;
164173
// for instance for the content "ab\nc\n" the line offsets are {0, 3}.

src/go/token/position_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ func TestPositions(t *testing.T) {
130130
if f.LineCount() != len(test.lines) {
131131
t.Errorf("%s, SetLines: got line count %d; want %d", f.Name(), f.LineCount(), len(test.lines))
132132
}
133+
if !reflect.DeepEqual(f.Lines(), test.lines) {
134+
t.Errorf("%s, Lines after SetLines(v): got %v; want %v", f.Name(), f.Lines(), test.lines)
135+
}
133136
verifyPositions(t, fset, f, test.lines)
134137

135138
// add lines with SetLinesForContent and verify all positions

0 commit comments

Comments
 (0)