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 snapshot := view.Snapshot() 25 fh, err := snapshot.GetFile(uri) 26 if err != nil { 27 return nil, err 28 } 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 // TODO: This should go through the ModTidyHandle on the view. 34 // That will also allow us to move source.InvokeGo into internal/lsp/cache. 35 if _, err := source.InvokeGo(ctx, view.Folder().Filename(), snapshot.Config(ctx).Env, "mod", "tidy"); err != nil { 36 return nil, err 37 } 38 } 39 return nil, nil 40} 41