mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/token: add (*File).End method
Also, use it in a number of places. + test, api, relnote Fixes #75849 Change-Id: I44acf5b8190b964fd3975009aa407d7c82cee19b Reviewed-on: https://go-review.googlesource.com/c/go/+/720061 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
65c09eafdf
commit
1297fae708
8 changed files with 22 additions and 7 deletions
1
api/next/75849.txt
Normal file
1
api/next/75849.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pkg go/token, method (*File) End() Pos #75849
|
||||||
1
doc/next/6-stdlib/99-minor/go/token/75849.md
Normal file
1
doc/next/6-stdlib/99-minor/go/token/75849.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
The new [File.End] convenience method returns the file's end position.
|
||||||
|
|
@ -30,10 +30,10 @@ func TestIssue33649(t *testing.T) {
|
||||||
tf = f
|
tf = f
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
tfEnd := tf.Base() + tf.Size()
|
tfEnd := tf.End()
|
||||||
|
|
||||||
fd := f.Decls[len(f.Decls)-1].(*ast.FuncDecl)
|
fd := f.Decls[len(f.Decls)-1].(*ast.FuncDecl)
|
||||||
fdEnd := int(fd.End())
|
fdEnd := fd.End()
|
||||||
|
|
||||||
if fdEnd != tfEnd {
|
if fdEnd != tfEnd {
|
||||||
t.Errorf("%q: got fdEnd = %d; want %d (base = %d, size = %d)", src, fdEnd, tfEnd, tf.Base(), tf.Size())
|
t.Errorf("%q: got fdEnd = %d; want %d (base = %d, size = %d)", src, fdEnd, tfEnd, tf.Base(), tf.Size())
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ func ParseFile(fset *token.FileSet, filename string, src any, mode Mode) (f *ast
|
||||||
// Ensure the start/end are consistent,
|
// Ensure the start/end are consistent,
|
||||||
// whether parsing succeeded or not.
|
// whether parsing succeeded or not.
|
||||||
f.FileStart = token.Pos(file.Base())
|
f.FileStart = token.Pos(file.Base())
|
||||||
f.FileEnd = token.Pos(file.Base() + file.Size())
|
f.FileEnd = file.End()
|
||||||
|
|
||||||
p.errors.Sort()
|
p.errors.Sort()
|
||||||
err = p.errors.Err()
|
err = p.errors.Err()
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,11 @@ func (f *File) Size() int {
|
||||||
return f.size
|
return f.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// End returns the end position of file f as registered with AddFile.
|
||||||
|
func (f *File) End() Pos {
|
||||||
|
return Pos(f.base + f.size)
|
||||||
|
}
|
||||||
|
|
||||||
// LineCount returns the number of lines in file f.
|
// LineCount returns the number of lines in file f.
|
||||||
func (f *File) LineCount() int {
|
func (f *File) LineCount() int {
|
||||||
f.mutex.Lock()
|
f.mutex.Lock()
|
||||||
|
|
|
||||||
|
|
@ -572,7 +572,7 @@ func fsetString(fset *FileSet) string {
|
||||||
buf.WriteRune('{')
|
buf.WriteRune('{')
|
||||||
sep := ""
|
sep := ""
|
||||||
fset.Iterate(func(f *File) bool {
|
fset.Iterate(func(f *File) bool {
|
||||||
fmt.Fprintf(&buf, "%s%s:%d-%d", sep, f.Name(), f.Base(), f.Base()+f.Size())
|
fmt.Fprintf(&buf, "%s%s:%d-%d", sep, f.Name(), f.Base(), f.End())
|
||||||
sep = " "
|
sep = " "
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
@ -643,3 +643,11 @@ func TestRemovedFileFileReturnsNil(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFile_End(t *testing.T) {
|
||||||
|
f := NewFileSet().AddFile("a.go", 100, 42)
|
||||||
|
got := fmt.Sprintf("%d, %d", f.Base(), f.End())
|
||||||
|
if want := "100, 142"; got != want {
|
||||||
|
t.Errorf("Base, End = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -313,8 +313,8 @@ func (t *tree) add(file *File) {
|
||||||
}
|
}
|
||||||
if prev := (*pos).file; prev != file {
|
if prev := (*pos).file; prev != file {
|
||||||
panic(fmt.Sprintf("file %s (%d-%d) overlaps with file %s (%d-%d)",
|
panic(fmt.Sprintf("file %s (%d-%d) overlaps with file %s (%d-%d)",
|
||||||
prev.Name(), prev.Base(), prev.Base()+prev.Size(),
|
prev.Name(), prev.Base(), prev.End(),
|
||||||
file.Name(), file.Base(), file.Base()+file.Size()))
|
file.Name(), file.Base(), file.End()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -247,7 +247,7 @@ func (check *Checker) collectObjects() {
|
||||||
// Be conservative and use the *ast.File extent if we don't have a *token.File.
|
// Be conservative and use the *ast.File extent if we don't have a *token.File.
|
||||||
pos, end := file.Pos(), file.End()
|
pos, end := file.Pos(), file.End()
|
||||||
if f := check.fset.File(file.Pos()); f != nil {
|
if f := check.fset.File(file.Pos()); f != nil {
|
||||||
pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
|
pos, end = token.Pos(f.Base()), f.End()
|
||||||
}
|
}
|
||||||
fileScope := NewScope(pkg.scope, pos, end, check.filename(fileNo))
|
fileScope := NewScope(pkg.scope, pos, end, check.filename(fileNo))
|
||||||
fileScopes[fileNo] = fileScope
|
fileScopes[fileNo] = fileScope
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue