Skip to content

Commit

Permalink
Parse from snapshot instead of disk
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Apr 15, 2024
1 parent 09368a0 commit eb21e08
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
17 changes: 10 additions & 7 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (s *server) Completion(ctx context.Context, reply jsonrpc2.Replier, req jso
return reply(ctx, nil, errors.New("snapshot not found"))
}
// Try parsing current file
pgf, err := file.ParseGno(ctx)
pgf, err := file.ParseGno2(ctx)
if err != nil {
return reply(ctx, nil, errors.New("cannot parse gno file"))
}
Expand All @@ -180,19 +180,22 @@ func (s *server) Completion(ctx context.Context, reply jsonrpc2.Replier, req jso
}
}

// Load pkg from cache
pkg, ok := s.cache.pkgs.Get(filepath.Dir(string(uri.Filename())))
if !ok {
return reply(ctx, nil, nil)
}

// Completion is based on what precedes the cursor.
// Find the path to the position before pos.
paths, _ := astutil.PathEnclosingInterval(pgf.File, token.Pos(offset-1), token.Pos(offset-1))
if paths == nil {
return reply(ctx, nil, nil)
}

// Debug
// slog.Info("COMPLETION", "token", fmt.Sprintf("%s", paths[0]))

// Load pkg from cache
pkg, ok := s.cache.pkgs.Get(filepath.Dir(string(uri.Filename())))
if !ok {
return reply(ctx, nil, nil)
}

switch n := paths[0].(type) {
case *ast.Ident:
_, tv := getTypeAndValue(
Expand Down
22 changes: 22 additions & 0 deletions internal/lsp/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ParsedGnoFile struct {
Src []byte
}

// ParseGno reads src from disk and parse it
func (f *GnoFile) ParseGno(ctx context.Context) (*ParsedGnoFile, error) {
fset := token.NewFileSet()
ast, err := parser.ParseFile(fset, f.URI.Filename(), nil, parser.ParseComments)
Expand All @@ -64,6 +65,27 @@ func (f *GnoFile) ParseGno(ctx context.Context) (*ParsedGnoFile, error) {
return pgf, nil
}

// ParseGno2 parses src from GnoFile instead of reading from disk
// Right now it's only used in `completion.go`
// TODO: Replace content of `ParseGno` with `ParseGno2`
func (f *GnoFile) ParseGno2(ctx context.Context) (*ParsedGnoFile, error) {
fset := token.NewFileSet()
ast, err := parser.ParseFile(fset, f.URI.Filename(), f.Src, parser.ParseComments)
if err != nil {
return nil, err
}

pgf := &ParsedGnoFile{
URI: f.URI,

File: ast,
Fset: fset,
Src: f.Src,
}

return pgf, nil
}

// contains parsed gno.mod file.
type ParsedGnoMod struct {
URI string
Expand Down

0 comments on commit eb21e08

Please sign in to comment.