Skip to content

Commit 328c41b

Browse files
committed
internal: avoid use of (*token.File).Name
When line directives are in use, we want the logical file name, not the one we found the bytes in. This matters most for cgo, where the file we parsed is not the one the user wants to see. Updates golang/go#35720. Change-Id: I495328071d8865e6895cb731467f1601f11e93db Reviewed-on: https://go-review.googlesource.com/c/tools/+/208100 Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 4bf2f40 commit 328c41b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

internal/lsp/cache/view.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (v *view) FindPosInPackage(searchpkg source.Package, pos token.Pos) (*ast.F
450450
if tok == nil {
451451
return nil, nil, nil, errors.Errorf("no file for pos in package %s", searchpkg.ID())
452452
}
453-
uri := span.FileURI(tok.Name())
453+
uri := span.FileURI(tok.Position(pos).Filename)
454454

455455
// Special case for ignored files.
456456
var (

internal/span/token.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (r Range) Span() (Span, error) {
6464
if f == nil {
6565
return Span{}, fmt.Errorf("file not found in FileSet")
6666
}
67-
s := Span{v: span{URI: FileURI(f.Name())}}
67+
s := Span{}
6868
var err error
6969
s.v.Start.Offset, err = offset(f, r.Start)
7070
if err != nil {
@@ -76,6 +76,16 @@ func (r Range) Span() (Span, error) {
7676
return Span{}, err
7777
}
7878
}
79+
// In the presence of line directives, a single File can have sections from
80+
// multiple file names.
81+
filename := f.Position(r.Start).Filename
82+
if r.End.IsValid() {
83+
if endFilename := f.Position(r.End).Filename; filename != endFilename {
84+
return Span{}, fmt.Errorf("span begins in file %q but ends in %q", filename, endFilename)
85+
}
86+
}
87+
s.v.URI = FileURI(filename)
88+
7989
s.v.Start.clean()
8090
s.v.End.clean()
8191
s.v.clean()

0 commit comments

Comments
 (0)