From 255d749847a4ef7cae62dcc5f7a855391b285f6b Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Wed, 24 Jan 2024 23:16:50 +0530 Subject: [PATCH] `getIdentNodes()` to get idents from Expr --- internal/lsp/hover.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/lsp/hover.go b/internal/lsp/hover.go index 53800e6..0d9e548 100644 --- a/internal/lsp/hover.go +++ b/internal/lsp/hover.go @@ -239,6 +239,23 @@ func (s *server) hoverPackageIdent(ctx context.Context, reply jsonrpc2.Replier, return reply(ctx, nil, nil) } +// getIdentNodes return idents from Expr +// Note: only handles *ast.SelectorExpr and *ast.CallExpr +func getIdentNodes(n ast.Node) []*ast.Ident { + res := []*ast.Ident{} + switch t := n.(type) { + case *ast.Ident: + res = append(res, t) + case *ast.SelectorExpr: + res = append(res, t.Sel) + res = append(res, getIdentNodes(t.X)...) + case *ast.CallExpr: + res = append(res, getIdentNodes(t.Fun)...) + } + + return res +} + func (s *server) hoverVariableIdent(ctx context.Context, reply jsonrpc2.Replier, pgf *ParsedGnoFile, params protocol.HoverParams, i *ast.Ident) error { if i.Obj != nil { switch u := i.Obj.Decl.(type) {