@@ -30,43 +30,41 @@ import (
30
30
// of a given node, for use in various summaries (hover, autocomplete,
31
31
// signature help).
32
32
type HoverContext struct {
33
- // Synopsis is a single sentence synopsis of the symbol's documentation.
34
- Synopsis string `json:"synopsis"`
35
-
36
- // FullDocumentation is the symbol's full documentation.
37
- FullDocumentation string `json:"fullDocumentation"`
38
-
39
33
// signatureSource is the object or node use to derive the hover signature.
40
34
//
41
35
// TODO(rfindley): can we pre-compute the signature, to avoid this indirection?
42
36
signatureSource interface {}
43
37
44
38
// comment is the most relevant comment group associated with the hovered object.
45
- comment * ast.CommentGroup
39
+ Comment * ast.CommentGroup
46
40
}
47
41
48
42
// HoverJSON contains information used by hover. It is also the JSON returned
49
43
// for the "structured" hover format
50
44
type HoverJSON struct {
45
+ // Synopsis is a single sentence synopsis of the symbol's documentation.
46
+ Synopsis string `json:"synopsis"`
47
+
48
+ // FullDocumentation is the symbol's full documentation.
49
+ FullDocumentation string `json:"fullDocumentation"`
50
+
51
51
// Signature is the symbol's signature.
52
52
Signature string `json:"signature"`
53
53
54
54
// SingleLine is a single line describing the symbol.
55
55
// This is recommended only for use in clients that show a single line for hover.
56
56
SingleLine string `json:"singleLine"`
57
57
58
+ // SymbolName is the types.Object.Name for the given symbol.
59
+ SymbolName string `json:"symbolName"`
60
+
58
61
// LinkPath is the pkg.go.dev link for the given symbol.
59
62
// For example, the "go/ast" part of "pkg.go.dev/go/ast#Node".
60
63
LinkPath string `json:"linkPath"`
61
64
62
65
// LinkAnchor is the pkg.go.dev link anchor for the given symbol.
63
66
// For example, the "Node" part of "pkg.go.dev/go/ast#Node".
64
67
LinkAnchor string `json:"linkAnchor"`
65
-
66
- // symbolName is the types.Object.Name for the given symbol.
67
- symbolName string
68
-
69
- HoverContext
70
68
}
71
69
72
70
func Hover (ctx context.Context , snapshot Snapshot , fh FileHandle , position protocol.Position ) (* protocol.Hover , error ) {
@@ -251,15 +249,19 @@ func HoverIdentifier(ctx context.Context, i *IdentifierInfo) (*HoverJSON, error)
251
249
ctx , done := event .Start (ctx , "source.Hover" )
252
250
defer done ()
253
251
254
- fset := i .Snapshot .FileSet ()
255
252
hoverCtx , err := FindHoverContext (ctx , i .Snapshot , i .pkg , i .Declaration .obj , i .Declaration .node , i .Declaration .fullDecl )
256
253
if err != nil {
257
254
return nil , err
258
255
}
259
256
260
- h := & HoverJSON {HoverContext : * hoverCtx }
257
+ h := & HoverJSON {
258
+ FullDocumentation : hoverCtx .Comment .Text (),
259
+ Synopsis : doc .Synopsis (hoverCtx .Comment .Text ()),
260
+ }
261
+
262
+ fset := i .Snapshot .FileSet ()
261
263
// Determine the symbol's signature.
262
- switch x := h .signatureSource .(type ) {
264
+ switch x := hoverCtx .signatureSource .(type ) {
263
265
case * ast.TypeSpec :
264
266
x2 := * x
265
267
// Don't duplicate comments when formatting type specs.
@@ -316,7 +318,7 @@ func HoverIdentifier(ctx context.Context, i *IdentifierInfo) (*HoverJSON, error)
316
318
}
317
319
}
318
320
319
- h .symbolName , h .LinkPath , h .LinkAnchor = linkData (obj , i .enclosing )
321
+ h .SymbolName , h .LinkPath , h .LinkAnchor = linkData (obj , i .enclosing )
320
322
321
323
// See golang/go#36998: don't link to modules matching GOPRIVATE.
322
324
//
@@ -492,7 +494,7 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
492
494
// The package declaration.
493
495
for _ , f := range pkg .GetSyntax () {
494
496
if f .Name == pkgNode {
495
- info = & HoverContext {comment : f .Doc }
497
+ info = & HoverContext {Comment : f .Doc }
496
498
}
497
499
}
498
500
case * ast.ImportSpec :
@@ -506,7 +508,7 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
506
508
// so pick the first file that has a doc comment.
507
509
for _ , file := range imp .GetSyntax () {
508
510
if file .Doc != nil {
509
- info = & HoverContext {signatureSource : obj , comment : file .Doc }
511
+ info = & HoverContext {signatureSource : obj , Comment : file .Doc }
510
512
break
511
513
}
512
514
}
@@ -567,9 +569,9 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
567
569
case * ast.FuncDecl :
568
570
switch obj .(type ) {
569
571
case * types.Func :
570
- info = & HoverContext {signatureSource : obj , comment : node .Doc }
572
+ info = & HoverContext {signatureSource : obj , Comment : node .Doc }
571
573
case * types.Builtin :
572
- info = & HoverContext {signatureSource : node .Type , comment : node .Doc }
574
+ info = & HoverContext {signatureSource : node .Type , Comment : node .Doc }
573
575
case * types.Var :
574
576
// Object is a function param or the field of an anonymous struct
575
577
// declared with ':='. Skip the first one because only fields
@@ -588,7 +590,7 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
588
590
if comment .Text () == "" {
589
591
comment = field .Comment
590
592
}
591
- info = & HoverContext {signatureSource : obj , comment : comment }
593
+ info = & HoverContext {signatureSource : obj , Comment : comment }
592
594
}
593
595
}
594
596
}
@@ -597,11 +599,6 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
597
599
info = & HoverContext {signatureSource : obj }
598
600
}
599
601
600
- if info .comment != nil {
601
- info .FullDocumentation = info .comment .Text ()
602
- info .Synopsis = doc .Synopsis (info .FullDocumentation )
603
- }
604
-
605
602
return info , nil
606
603
}
607
604
@@ -642,9 +639,9 @@ func formatGenDecl(node *ast.GenDecl, spec ast.Spec, fullPos token.Pos, obj type
642
639
case * ast.TypeSpec :
643
640
return formatTypeSpec (spec , node ), nil
644
641
case * ast.ValueSpec :
645
- return & HoverContext {signatureSource : spec , comment : spec .Doc }, nil
642
+ return & HoverContext {signatureSource : spec , Comment : spec .Doc }, nil
646
643
case * ast.ImportSpec :
647
- return & HoverContext {signatureSource : spec , comment : spec .Doc }, nil
644
+ return & HoverContext {signatureSource : spec , Comment : spec .Doc }, nil
648
645
}
649
646
return nil , errors .Errorf ("unable to format spec %v (%T)" , spec , spec )
650
647
}
@@ -659,7 +656,7 @@ func formatTypeSpec(spec *ast.TypeSpec, decl *ast.GenDecl) *HoverContext {
659
656
}
660
657
return & HoverContext {
661
658
signatureSource : spec ,
662
- comment : comment ,
659
+ Comment : comment ,
663
660
}
664
661
}
665
662
@@ -691,18 +688,18 @@ func formatVar(node ast.Spec, fullPos token.Pos, obj types.Object, decl *ast.Gen
691
688
// associated values so that we can augment their hover with more information.
692
689
if _ , ok := obj .(* types.Var ); ok && spec .Type == nil && len (spec .Values ) > 0 {
693
690
if _ , ok := spec .Values [0 ].(* ast.BasicLit ); ok {
694
- return & HoverContext {signatureSource : spec , comment : comment }
691
+ return & HoverContext {signatureSource : spec , Comment : comment }
695
692
}
696
693
}
697
694
698
- return & HoverContext {signatureSource : obj , comment : comment }
695
+ return & HoverContext {signatureSource : obj , Comment : comment }
699
696
}
700
697
701
698
if fieldList != nil {
702
699
comment := findFieldComment (fullPos , fieldList )
703
- return & HoverContext {signatureSource : obj , comment : comment }
700
+ return & HoverContext {signatureSource : obj , Comment : comment }
704
701
}
705
- return & HoverContext {signatureSource : obj , comment : decl .Doc }
702
+ return & HoverContext {signatureSource : obj , Comment : decl .Doc }
706
703
}
707
704
708
705
// extractFieldList recursively tries to extract a field list.
@@ -806,7 +803,7 @@ func formatLink(h *HoverJSON, options *Options) string {
806
803
plainLink := BuildLink (options .LinkTarget , h .LinkPath , h .LinkAnchor )
807
804
switch options .PreferredContentFormat {
808
805
case protocol .Markdown :
809
- return fmt .Sprintf ("[`%s` on %s](%s)" , h .symbolName , options .LinkTarget , plainLink )
806
+ return fmt .Sprintf ("[`%s` on %s](%s)" , h .SymbolName , options .LinkTarget , plainLink )
810
807
case protocol .PlainText :
811
808
return ""
812
809
default :
0 commit comments