Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Hover support for local funcs
Browse files Browse the repository at this point in the history
Implemented cache store in the server to handle hover for local
function.
  • Loading branch information
harry-hov committed Jan 12, 2024
1 parent 5bc9be4 commit dedec21
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
46 changes: 46 additions & 0 deletions internal/lsp/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lsp

import (
cmap "github.com/orcaman/concurrent-map/v2"
)

type Cache struct {
pkgs cmap.ConcurrentMap[string, *Package]
}

func (c *Cache) lookupSymbol(pkgPath, symbol string) (*Symbol, bool) {
pkg, ok := c.pkgs.Get(pkgPath)
if !ok {
return nil, false
}

for _, s := range pkg.Symbols {
if s.Name == symbol {
return s, true
}
}
return nil, false
}

func NewCache() *Cache {
return &Cache{
pkgs: cmap.New[*Package](),
}
}

func (s *server) UpdateCache(pkgPath string) {
files, err := ListGnoFiles(pkgPath)
if err != nil {
// Ignore error
return
}
symbols := []*Symbol{}
for _, file := range files {
symbols = append(symbols, getSymbols(file)...)
}
s.cache.pkgs.Set(pkgPath, &Package{
Name: pkgPath,
ImportPath: "",
Symbols: symbols,
})
}
2 changes: 2 additions & 0 deletions internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"log/slog"
"path/filepath"

"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
Expand All @@ -24,6 +25,7 @@ func (s *server) DidOpen(ctx context.Context, reply jsonrpc2.Replier, req jsonrp
s.snapshot.file.Set(uri.Filename(), file)

slog.Info("open " + string(params.TextDocument.URI.Filename()))
s.UpdateCache(filepath.Dir(string(params.TextDocument.URI.Filename())))
notification := s.publishDiagnostics(ctx, s.conn, file)
return reply(ctx, notification, nil)
}
Expand Down
18 changes: 17 additions & 1 deletion internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"go/ast"
"log/slog"
"path/filepath"
"strings"

"go.lsp.dev/jsonrpc2"
Expand Down Expand Up @@ -57,7 +58,22 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
slog.Info("hover - CALL_EXPR")
switch v := e.Fun.(type) {
case *ast.Ident:
// TODO: is a func? handle.
// TODO: don't show methods
pkgPath := filepath.Dir(params.TextDocument.URI.Filename())
sym, ok := s.cache.lookupSymbol(pkgPath, v.Name)
if !ok {
return reply(ctx, nil, nil)
}
return reply(ctx, protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: sym.String(),
},
Range: posToRange(
int(params.Position.Line),
[]int{0, 4},
),
}, nil)
case *ast.SelectorExpr:
// case pkg.Func
i, ok := v.X.(*ast.Ident)
Expand Down
2 changes: 2 additions & 0 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type server struct {

snapshot *Snapshot
completionStore *CompletionStore
cache *Cache

formatOpt tools.FormattingOption
}
Expand All @@ -38,6 +39,7 @@ func BuildServerHandler(conn jsonrpc2.Conn, env *env.Env) jsonrpc2.Handler {

snapshot: NewSnapshot(),
completionStore: InitCompletionStore(dirs),
cache: NewCache(),

formatOpt: tools.Gofumpt,
}
Expand Down

0 comments on commit dedec21

Please sign in to comment.