@@ -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.
260260ident , 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+
261273if obj == nil || ident == nil {
262274return 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 ) {
704711importPath := metadata .UnquoteImportPath (imp )
705712if importPath == "" {
706- return protocol. Range {}, nil , fmt .Errorf ("invalid import path" )
713+ return nil , fmt .Errorf ("invalid import path" )
707714}
708715impID := pkg .Metadata ().DepsByImpPath [importPath ]
709716if 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}
712719impMetadata := snapshot .Metadata (impID )
713720if 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
720727fh , err := snapshot .ReadFile (ctx , f )
721728if err != nil {
722729if ctx .Err () != nil {
723- return protocol. Range {}, nil , ctx .Err ()
730+ return nil , ctx .Err ()
724731}
725732continue
726733}
727734pgf , err := snapshot .ParseGo (ctx , fh , parsego .Header )
728735if err != nil {
729736if ctx .Err () != nil {
730- return protocol. Range {}, nil , ctx .Err ()
737+ return nil , ctx .Err ()
731738}
732739continue
733740}
@@ -738,13 +745,56 @@ func hoverImport(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Packa
738745}
739746
740747docText := comment .Text ()
741- return rng , & hoverResult {
748+ return & hoverResult {
742749signature : "package " + string (impMetadata .Name ),
743750synopsis : doc .Synopsis (docText ),
744751fullDocumentation : 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+ rng , err := pgf .NodeRange (ident )
784+ if err != nil {
785+ return protocol.Range {}, nil , err
786+ }
787+ hoverRes , err := hoverPackageRef (ctx , snapshot , pkg , spec )
788+ if err != nil {
789+ return protocol.Range {}, nil , err
790+ }
791+ return rng , hoverRes , nil // (hoverRes may be nil)
792+ }
793+ }
794+
795+ return protocol.Range {}, nil , fmt .Errorf ("invalid import path" )
796+ }
797+
748798// hoverPackageName computes hover information for the package name of the file
749799// pgf in pkg.
750800func hoverPackageName (pkg * cache.Package , pgf * parsego.File ) (protocol.Range , * hoverResult , error ) {
0 commit comments