| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/parser" |
| 11 |
"go/token" |
| 12 |
"path/filepath" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 15 |
"golang.org/x/tools/gopls/internal/file" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
func (s *Snapshot) ParseGo(ctx context.Context, fh file.Handle, mode parser.Mode) (*parsego.File, error) { |
| 22 |
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), mode, false, fh) |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} |
| 26 |
return pgfs[0], nil |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func parseGoImpl(ctx context.Context, fset *token.FileSet, fh file.Handle, mode parser.Mode, purgeFuncBodies bool) (*parsego.File, error) { |
| 31 |
ext := filepath.Ext(fh.URI().Path()) |
| 32 |
if ext != ".go" && ext != "" { |
| 33 |
return nil, fmt.Errorf("cannot parse non-Go file %s", fh.URI()) |
| 34 |
} |
| 35 |
content, err := fh.Content() |
| 36 |
if err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
|
| 40 |
if ctx.Err() != nil { |
| 41 |
return nil, ctx.Err() |
| 42 |
} |
| 43 |
pgf, _ := parsego.Parse(ctx, fset, fh.URI(), content, mode, purgeFuncBodies) |
| 44 |
return pgf, nil |
| 45 |
} |