mirror of
https://github.com/golang/go
synced 2024-11-18 18:44:42 -07:00
internal/lsp: fix a few nil pointer exceptions in definition
Jumping to the definition of a builtin function or basic kind would cause a nil pointer because these have no position. Change-Id: I043a61a148757b127ff1123c8429ce23858bd13a Reviewed-on: https://go-review.googlesource.com/c/157597 Reviewed-by: Ian Cottrell <iancottrell@google.com> Run-TryBot: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
36f37f8f5c
commit
bf090417da
@ -36,11 +36,11 @@ func fromProtocolLocation(ctx context.Context, v *cache.View, loc protocol.Locat
|
|||||||
|
|
||||||
// toProtocolLocation converts from a source range back to a protocol location.
|
// toProtocolLocation converts from a source range back to a protocol location.
|
||||||
func toProtocolLocation(fset *token.FileSet, r source.Range) protocol.Location {
|
func toProtocolLocation(fset *token.FileSet, r source.Range) protocol.Location {
|
||||||
tokFile := fset.File(r.Start)
|
tok := fset.File(r.Start)
|
||||||
uri := source.ToURI(tokFile.Name())
|
uri := source.ToURI(tok.Name())
|
||||||
return protocol.Location{
|
return protocol.Location{
|
||||||
URI: protocol.DocumentURI(uri),
|
URI: protocol.DocumentURI(uri),
|
||||||
Range: toProtocolRange(tokFile, r),
|
Range: toProtocolRange(tok, r),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,15 +36,15 @@ func Definition(ctx context.Context, v View, f File, pos token.Pos) (Range, erro
|
|||||||
return Range{}, fmt.Errorf("no object")
|
return Range{}, fmt.Errorf("no object")
|
||||||
}
|
}
|
||||||
if i.wasEmbeddedField {
|
if i.wasEmbeddedField {
|
||||||
// the original position was on the embedded field declaration
|
// The original position was on the embedded field declaration, so we
|
||||||
// so we try to dig out the type and jump to that instead
|
// try to dig out the type and jump to that instead.
|
||||||
if v, ok := obj.(*types.Var); ok {
|
if v, ok := obj.(*types.Var); ok {
|
||||||
if n, ok := v.Type().(*types.Named); ok {
|
if n, ok := v.Type().(*types.Named); ok {
|
||||||
obj = n.Obj()
|
obj = n.Obj()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return objToRange(ctx, v, obj), nil
|
return objToRange(ctx, v, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TypeDefinition(ctx context.Context, v View, f File, pos token.Pos) (Range, error) {
|
func TypeDefinition(ctx context.Context, v View, f File, pos token.Pos) (Range, error) {
|
||||||
@ -71,7 +71,7 @@ func TypeDefinition(ctx context.Context, v View, f File, pos token.Pos) (Range,
|
|||||||
if obj == nil {
|
if obj == nil {
|
||||||
return Range{}, fmt.Errorf("no object for type %s", typ.String())
|
return Range{}, fmt.Errorf("no object for type %s", typ.String())
|
||||||
}
|
}
|
||||||
return objToRange(ctx, v, obj), nil
|
return objToRange(ctx, v, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func typeToObject(typ types.Type) (obj types.Object) {
|
func typeToObject(typ types.Type) (obj types.Object) {
|
||||||
@ -129,8 +129,11 @@ func checkIdentifier(f *ast.File, pos token.Pos) (ident, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func objToRange(ctx context.Context, v View, obj types.Object) Range {
|
func objToRange(ctx context.Context, v View, obj types.Object) (Range, error) {
|
||||||
p := obj.Pos()
|
p := obj.Pos()
|
||||||
|
if !p.IsValid() {
|
||||||
|
return Range{}, fmt.Errorf("invalid position for %v", obj.Name())
|
||||||
|
}
|
||||||
tok := v.FileSet().File(p)
|
tok := v.FileSet().File(p)
|
||||||
pos := tok.Position(p)
|
pos := tok.Position(p)
|
||||||
if pos.Column == 1 {
|
if pos.Column == 1 {
|
||||||
@ -161,7 +164,7 @@ Return:
|
|||||||
return Range{
|
return Range{
|
||||||
Start: p,
|
Start: p,
|
||||||
End: p + token.Pos(identifierLen(obj.Name())),
|
End: p + token.Pos(identifierLen(obj.Name())),
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This needs to be fixed to address golang.org/issue/29149.
|
// TODO: This needs to be fixed to address golang.org/issue/29149.
|
||||||
|
Loading…
Reference in New Issue
Block a user