@@ -173,8 +173,7 @@ func hover(ctx context.Context, snapshot Snapshot, fh FileHandle, pp protocol.Po
173
173
174
174
// TODO(rfindley): we could do much better for inferred signatures.
175
175
if inferred := inferredSignature (pkg .GetTypesInfo (), ident ); inferred != nil {
176
- s := inferredSignatureString (obj , qf , inferred )
177
- if s != "" {
176
+ if s := inferredSignatureString (obj , qf , inferred ); s != "" {
178
177
signature = s
179
178
}
180
179
}
@@ -584,7 +583,7 @@ func hoverLit(pgf *ParsedGoFile, lit *ast.BasicLit, pos token.Pos) (protocol.Ran
584
583
585
584
// inferredSignatureString is a wrapper around the types.ObjectString function
586
585
// that adds more information to inferred signatures. It will return an empty string
587
- // if passed types.Object is not a signature.
586
+ // if the passed types.Object is not a signature.
588
587
func inferredSignatureString (obj types.Object , qf types.Qualifier , inferred * types.Signature ) string {
589
588
// If the signature type was inferred, prefer the inferred signature with a
590
589
// comment showing the generic signature.
@@ -605,12 +604,17 @@ func inferredSignatureString(obj types.Object, qf types.Qualifier, inferred *typ
605
604
606
605
// objectString is a wrapper around the types.ObjectString function.
607
606
// It handles adding more information to the object string.
607
+ // If spec is non-nil, it may be used to format additional declaration
608
+ // syntax, and file must be the token.File describing its positions.
608
609
func objectString (obj types.Object , qf types.Qualifier , declPos token.Pos , file * token.File , spec ast.Spec ) string {
609
610
str := types .ObjectString (obj , qf )
610
611
611
612
switch obj := obj .(type ) {
612
613
case * types.Const :
613
- declaration := obj .Val ().String ()
614
+ var (
615
+ declaration = obj .Val ().String () // default formatted declaration
616
+ comment = "" // if non-empty, a clarifying comment
617
+ )
614
618
615
619
// Try to use the original declaration.
616
620
switch obj .Val ().Kind () {
@@ -619,24 +623,23 @@ func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file
619
623
// Also strings can be very long. So, just use the constant's value.
620
624
621
625
default :
622
- if file == nil || spec == nil {
623
- break
624
- }
625
-
626
- switch spec := spec .(type ) {
627
- case * ast.ValueSpec :
626
+ if spec , _ := spec .(* ast.ValueSpec ); spec != nil {
628
627
for i , name := range spec .Names {
629
628
if declPos == name .Pos () {
630
629
if i < len (spec .Values ) {
631
- declaration = FormatNodeFile (file , spec .Values [i ])
630
+ originalDeclaration := FormatNodeFile (file , spec .Values [i ])
631
+ if originalDeclaration != declaration {
632
+ comment = declaration
633
+ declaration = originalDeclaration
634
+ }
632
635
}
633
636
break
634
637
}
635
638
}
636
639
}
637
640
}
638
641
639
- comment := obj . Val (). String ()
642
+ // Special formatting cases.
640
643
switch typ := obj .Type ().(type ) {
641
644
case * types.Named :
642
645
// Try to add a formatted duration as an inline comment.
@@ -647,9 +650,12 @@ func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file
647
650
}
648
651
}
649
652
}
653
+ if comment == declaration {
654
+ comment = ""
655
+ }
650
656
651
657
str += " = " + declaration
652
- if declaration != comment {
658
+ if comment != "" {
653
659
str += " // " + comment
654
660
}
655
661
}
0 commit comments