1package lsp 2 3import ( 4 "context" 5 6 "golang.org/x/tools/internal/lsp/protocol" 7 "golang.org/x/tools/internal/lsp/source" 8 "golang.org/x/tools/internal/span" 9 errors "golang.org/x/xerrors" 10) 11 12func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) { 13 switch params.Command { 14 case "tidy": 15 if len(params.Arguments) == 0 || len(params.Arguments) > 1 { 16 return nil, errors.Errorf("expected one file URI for call to `go mod tidy`, got %v", params.Arguments) 17 } 18 // Confirm that this action is being taken on a go.mod file. 19 uri := span.NewURI(params.Arguments[0].(string)) 20 view, err := s.session.ViewOf(uri) 21 if err != nil { 22 return nil, err 23 } 24 f, err := view.GetFile(ctx, uri) 25 if err != nil { 26 return nil, err 27 } 28 fh := view.Snapshot().Handle(ctx, f) 29 if fh.Identity().Kind != source.Mod { 30 return nil, errors.Errorf("%s is not a mod file", uri) 31 } 32 // Run go.mod tidy on the view. 33 if err := source.ModTidy(ctx, view); err != nil { 34 return nil, err 35 } 36 } 37 return nil, nil 38} 39