@@ -258,6 +258,18 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
258258 // The general case: compute hover information for the object referenced by
259259 // the identifier at pos.
260260 ident , obj , selectedType := referencedObject (pkg , pgf , pos )
261+
262+ if pkgName , ok := obj .(* types.PkgName ); ok {
263+ rng , hoverRes , err := hoverPackageIdent (ctx , snapshot , pkg , pgf , ident , pkgName .Imported ().Path ())
264+ if err != nil {
265+ return protocol.Range {}, nil , err
266+ }
267+ if hoverRange == nil {
268+ hoverRange = & rng
269+ }
270+ return * hoverRange , hoverRes , nil // (hoverRes may be nil)
271+ }
272+
261273 if obj == nil || ident == nil {
262274 return protocol.Range {}, nil , nil // no object to hover
263275 }
@@ -691,27 +703,22 @@ func hoverBuiltin(ctx context.Context, snapshot *cache.Snapshot, obj types.Objec
691703 }, nil
692704}
693705
694- // hoverImport computes hover information when hovering over the import path of
695- // imp in the file pgf of pkg.
706+ // hoverPackageRef computes hover information when hovering over the import path or ident of
707+ // imp in the file pgf of pkg or over the identifier for an imported pkg .
696708//
697709// If we do not have metadata for the hovered import, it returns _
698- func hoverImport (ctx context.Context , snapshot * cache.Snapshot , pkg * cache.Package , pgf * parsego.File , imp * ast.ImportSpec ) (protocol.Range , * hoverResult , error ) {
699- rng , err := pgf .NodeRange (imp .Path )
700- if err != nil {
701- return protocol.Range {}, nil , err
702- }
703-
710+ func hoverPackageRef (ctx context.Context , snapshot * cache.Snapshot , pkg * cache.Package , imp * ast.ImportSpec ) (* hoverResult , error ) {
704711 importPath := metadata .UnquoteImportPath (imp )
705712 if importPath == "" {
706- return protocol. Range {}, nil , fmt .Errorf ("invalid import path" )
713+ return nil , fmt .Errorf ("invalid import path" )
707714 }
708715 impID := pkg .Metadata ().DepsByImpPath [importPath ]
709716 if impID == "" {
710- return protocol. Range {}, nil , fmt .Errorf ("no package data for import %q" , importPath )
717+ return nil , fmt .Errorf ("no package data for import %q" , importPath )
711718 }
712719 impMetadata := snapshot .Metadata (impID )
713720 if impMetadata == nil {
714- return protocol. Range {}, nil , bug .Errorf ("failed to resolve import ID %q" , impID )
721+ return nil , bug .Errorf ("failed to resolve import ID %q" , impID )
715722 }
716723
717724 // Find the first file with a package doc comment.
@@ -720,14 +727,14 @@ func hoverImport(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Packa
720727 fh , err := snapshot .ReadFile (ctx , f )
721728 if err != nil {
722729 if ctx .Err () != nil {
723- return protocol. Range {}, nil , ctx .Err ()
730+ return nil , ctx .Err ()
724731 }
725732 continue
726733 }
727734 pgf , err := snapshot .ParseGo (ctx , fh , parsego .Header )
728735 if err != nil {
729736 if ctx .Err () != nil {
730- return protocol. Range {}, nil , ctx .Err ()
737+ return nil , ctx .Err ()
731738 }
732739 continue
733740 }
@@ -738,13 +745,57 @@ func hoverImport(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Packa
738745 }
739746
740747 docText := comment .Text ()
741- return rng , & hoverResult {
748+ return & hoverResult {
742749 signature : "package " + string (impMetadata .Name ),
743750 synopsis : doc .Synopsis (docText ),
744751 fullDocumentation : docText ,
745752 }, nil
746753}
747754
755+ // hoverImport computes hover information when hovering over the import path of
756+ // imp in the file pgf of pkg.
757+ //
758+ // If we do not have metadata for the hovered import, it returns _
759+ func hoverImport (ctx context.Context , snapshot * cache.Snapshot , pkg * cache.Package , pgf * parsego.File , imp * ast.ImportSpec ) (protocol.Range , * hoverResult , error ) {
760+ rng , err := pgf .NodeRange (imp .Path )
761+ if err != nil {
762+ return protocol.Range {}, nil , err
763+ }
764+ hoverRes , err := hoverPackageRef (ctx , snapshot , pkg , imp )
765+ if err != nil {
766+ return protocol.Range {}, nil , err
767+ }
768+ return rng , hoverRes , err
769+ }
770+
771+ // hoverPackageIdent computes hover information when hovering over the identifier
772+ // of an imported pkg.
773+ //
774+ // If we do not have metadata for the hovered import, it returns _
775+ func hoverPackageIdent (ctx context.Context , snapshot * cache.Snapshot , pkg * cache.Package , pgf * parsego.File , ident * ast.Ident , path string ) (protocol.Range , * hoverResult , error ) {
776+
777+ for _ , spec := range pgf .File .Imports {
778+ importPathString , err := strconv .Unquote (spec .Path .Value )
779+ if err != nil {
780+ return protocol.Range {}, nil , err
781+ }
782+ if importPathString != path {
783+ continue
784+ }
785+ rng , err := pgf .NodeRange (ident )
786+ if err != nil {
787+ return protocol.Range {}, nil , err
788+ }
789+ hoverRes , err := hoverPackageRef (ctx , snapshot , pkg , spec )
790+ if err != nil {
791+ return protocol.Range {}, nil , err
792+ }
793+ return rng , hoverRes , nil // (hoverRes may be nil)
794+ }
795+
796+ return protocol.Range {}, nil , fmt .Errorf ("invalid import path" )
797+ }
798+
748799// hoverPackageName computes hover information for the package name of the file
749800// pgf in pkg.
750801func hoverPackageName (pkg * cache.Package , pgf * parsego.File ) (protocol.Range , * hoverResult , error ) {
0 commit comments