Skip to content

Commit 8cc75a7

Browse files
committed
fixes after code review
1 parent a7fbd1e commit 8cc75a7

File tree

7 files changed

+162
-150
lines changed

7 files changed

+162
-150
lines changed

gopls/internal/lsp/regtest/marker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func RunMarkerTests(t *testing.T, dir string) {
308308
if _, err := fmt.Sscanf(test.minGoVersion, "go1.%d", &go1point); err != nil {
309309
t.Fatalf("parsing -min_go version: %v", err)
310310
}
311-
testenv.NeedsGo1Point(t, 18)
311+
testenv.NeedsGo1Point(t, go1point)
312312
}
313313
config := fake.EditorConfig{
314314
Settings: test.settings,

gopls/internal/lsp/source/hover.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ func hover(ctx context.Context, snapshot Snapshot, fh FileHandle, pp protocol.Po
173173

174174
// TODO(rfindley): we could do much better for inferred signatures.
175175
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 != "" {
178177
signature = s
179178
}
180179
}
@@ -584,7 +583,7 @@ func hoverLit(pgf *ParsedGoFile, lit *ast.BasicLit, pos token.Pos) (protocol.Ran
584583

585584
// inferredSignatureString is a wrapper around the types.ObjectString function
586585
// 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.
588587
func inferredSignatureString(obj types.Object, qf types.Qualifier, inferred *types.Signature) string {
589588
// If the signature type was inferred, prefer the inferred signature with a
590589
// comment showing the generic signature.
@@ -605,12 +604,17 @@ func inferredSignatureString(obj types.Object, qf types.Qualifier, inferred *typ
605604

606605
// objectString is a wrapper around the types.ObjectString function.
607606
// 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.
608609
func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file *token.File, spec ast.Spec) string {
609610
str := types.ObjectString(obj, qf)
610611

611612
switch obj := obj.(type) {
612613
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+
)
614618

615619
// Try to use the original declaration.
616620
switch obj.Val().Kind() {
@@ -619,24 +623,23 @@ func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file
619623
// Also strings can be very long. So, just use the constant's value.
620624

621625
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 {
628627
for i, name := range spec.Names {
629628
if declPos == name.Pos() {
630629
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+
}
632635
}
633636
break
634637
}
635638
}
636639
}
637640
}
638641

639-
comment := obj.Val().String()
642+
// Special formatting cases.
640643
switch typ := obj.Type().(type) {
641644
case *types.Named:
642645
// 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
647650
}
648651
}
649652
}
653+
if comment == declaration {
654+
comment = ""
655+
}
650656

651657
str += " = " + declaration
652-
if declaration != comment {
658+
if comment != "" {
653659
str += " // " + comment
654660
}
655661
}

gopls/internal/lsp/testdata/godef/a/g.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

gopls/internal/lsp/testdata/godef/a/g.go.golden

Lines changed: 0 additions & 67 deletions
This file was deleted.

gopls/internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SemanticTokenCount = 3
1616
SuggestedFixCount = 65
1717
FunctionExtractionCount = 27
1818
MethodExtractionCount = 6
19-
DefinitionsCount = 60
19+
DefinitionsCount = 46
2020
TypeDefinitionsCount = 18
2121
HighlightsCount = 69
2222
InlayHintsCount = 4

gopls/internal/lsp/testdata/summary_go1.18.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SemanticTokenCount = 3
1616
SuggestedFixCount = 71
1717
FunctionExtractionCount = 27
1818
MethodExtractionCount = 6
19-
DefinitionsCount = 60
19+
DefinitionsCount = 46
2020
TypeDefinitionsCount = 18
2121
HighlightsCount = 69
2222
InlayHintsCount = 5

0 commit comments

Comments
 (0)