Skip to content

Commit

Permalink
go/token: add (*File).Lines method
Browse files Browse the repository at this point in the history
This method returns the array updated by SetLines, for
use in exporter packages.

Fixes golang#57708

Change-Id: I12ed5e7e1bae7517f40cb25e76e51997c25d84f4
Reviewed-on: https://go-review.googlesource.com/c/go/+/464515
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Alan Donovan <adonovan@google.com>
  • Loading branch information
adonovan authored and johanbrandhorst committed Feb 12, 2023
1 parent adb30af commit 1654166
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/next/57708.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg go/token, method (*File) Lines() []int #57708
9 changes: 9 additions & 0 deletions src/go/token/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ func (f *File) MergeLine(line int) {
f.lines = f.lines[:len(f.lines)-1]
}

// Lines returns the effective line offset table of the form described by SetLines.
// Callers must not mutate the result.
func (f *File) Lines() []int {
f.mutex.Lock()
lines := f.lines
f.mutex.Unlock()
return lines
}

// SetLines sets the line offsets for a file and reports whether it succeeded.
// The line offsets are the offsets of the first character of each line;
// for instance for the content "ab\nc\n" the line offsets are {0, 3}.
Expand Down
3 changes: 3 additions & 0 deletions src/go/token/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func TestPositions(t *testing.T) {
if f.LineCount() != len(test.lines) {
t.Errorf("%s, SetLines: got line count %d; want %d", f.Name(), f.LineCount(), len(test.lines))
}
if !reflect.DeepEqual(f.Lines(), test.lines) {
t.Errorf("%s, Lines after SetLines(v): got %v; want %v", f.Name(), f.Lines(), test.lines)
}
verifyPositions(t, fset, f, test.lines)

// add lines with SetLinesForContent and verify all positions
Expand Down

0 comments on commit 1654166

Please sign in to comment.