Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / internal / lsp / command.go
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package lsp
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "fmt"
12         "io"
13         "io/ioutil"
14         "path/filepath"
15
16         "golang.org/x/tools/internal/event"
17         "golang.org/x/tools/internal/gocommand"
18         "golang.org/x/tools/internal/lsp/cache"
19         "golang.org/x/tools/internal/lsp/protocol"
20         "golang.org/x/tools/internal/lsp/source"
21         "golang.org/x/tools/internal/span"
22         "golang.org/x/tools/internal/xcontext"
23         errors "golang.org/x/xerrors"
24 )
25
26 func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
27         var command *source.Command
28         for _, c := range source.Commands {
29                 if c.ID() == params.Command {
30                         command = c
31                         break
32                 }
33         }
34         if command == nil {
35                 return nil, fmt.Errorf("no known command")
36         }
37         var match bool
38         for _, name := range s.session.Options().SupportedCommands {
39                 if command.ID() == name {
40                         match = true
41                         break
42                 }
43         }
44         if !match {
45                 return nil, fmt.Errorf("%s is not a supported command", command.ID())
46         }
47         // Some commands require that all files are saved to disk. If we detect
48         // unsaved files, warn the user instead of running the commands.
49         unsaved := false
50         for _, overlay := range s.session.Overlays() {
51                 if !overlay.Saved() {
52                         unsaved = true
53                         break
54                 }
55         }
56         if unsaved {
57                 switch params.Command {
58                 case source.CommandTest.ID(),
59                         source.CommandGenerate.ID(),
60                         source.CommandToggleDetails.ID(),
61                         source.CommandAddDependency.ID(),
62                         source.CommandUpgradeDependency.ID(),
63                         source.CommandRemoveDependency.ID(),
64                         source.CommandVendor.ID():
65                         // TODO(PJW): for Toggle, not an error if it is being disabled
66                         err := errors.New("all files must be saved first")
67                         s.showCommandError(ctx, command.Title, err)
68                         return nil, err
69                 }
70         }
71         ctx, cancel := context.WithCancel(xcontext.Detach(ctx))
72
73         var work *workDone
74         // Don't show progress for suggested fixes. They should be quick.
75         if !command.IsSuggestedFix() {
76                 // Start progress prior to spinning off a goroutine specifically so that
77                 // clients are aware of the work item before the command completes. This
78                 // matters for regtests, where having a continuous thread of work is
79                 // convenient for assertions.
80                 work = s.progress.start(ctx, command.Title, "Running...", params.WorkDoneToken, cancel)
81         }
82         if command.Async {
83                 go func() {
84                         defer cancel()
85                         s.runCommand(ctx, work, command, params.Arguments)
86                 }()
87                 return nil, nil
88         }
89         defer cancel()
90         return nil, s.runCommand(ctx, work, command, params.Arguments)
91 }
92
93 func (s *Server) runSuggestedFixCommand(ctx context.Context, command *source.Command, args []json.RawMessage) error {
94         var uri protocol.DocumentURI
95         var rng protocol.Range
96         if err := source.UnmarshalArgs(args, &uri, &rng); err != nil {
97                 return err
98         }
99         snapshot, fh, ok, release, err := s.beginFileRequest(ctx, uri, source.Go)
100         defer release()
101         if !ok {
102                 return err
103         }
104         edits, err := command.SuggestedFix(ctx, snapshot, fh, rng)
105         if err != nil {
106                 return err
107         }
108         r, err := s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{
109                 Edit: protocol.WorkspaceEdit{
110                         DocumentChanges: edits,
111                 },
112         })
113         if err != nil {
114                 return err
115         }
116         if !r.Applied {
117                 return errors.New(r.FailureReason)
118         }
119         return nil
120 }
121
122 func (s *Server) showCommandError(ctx context.Context, title string, err error) {
123         // Command error messages should not be cancelable.
124         ctx = xcontext.Detach(ctx)
125         if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
126                 Type:    protocol.Error,
127                 Message: fmt.Sprintf("%s failed: %v", title, err),
128         }); err != nil {
129                 event.Error(ctx, title+": failed to show message", err)
130         }
131 }
132
133 func (s *Server) runCommand(ctx context.Context, work *workDone, command *source.Command, args []json.RawMessage) (err error) {
134         defer func() {
135                 switch {
136                 case errors.Is(err, context.Canceled):
137                         work.end(command.Title + ": canceled")
138                 case err != nil:
139                         event.Error(ctx, fmt.Sprintf("%s: command error", command.Title), err)
140                         work.end(command.Title + ": failed")
141                         // Show a message when work completes with error, because the progress end
142                         // message is typically dismissed immediately by LSP clients.
143                         s.showCommandError(ctx, command.Title, err)
144                 default:
145                         work.end(command.ID() + ": completed")
146                 }
147         }()
148         // If the command has a suggested fix function available, use it and apply
149         // the edits to the workspace.
150         if command.IsSuggestedFix() {
151                 return s.runSuggestedFixCommand(ctx, command, args)
152         }
153         switch command {
154         case source.CommandTest:
155                 var uri protocol.DocumentURI
156                 var tests, benchmarks []string
157                 if err := source.UnmarshalArgs(args, &uri, &tests, &benchmarks); err != nil {
158                         return err
159                 }
160                 snapshot, _, ok, release, err := s.beginFileRequest(ctx, uri, source.UnknownKind)
161                 defer release()
162                 if !ok {
163                         return err
164                 }
165                 return s.runTests(ctx, snapshot, uri, work, tests, benchmarks)
166         case source.CommandGenerate:
167                 var uri protocol.DocumentURI
168                 var recursive bool
169                 if err := source.UnmarshalArgs(args, &uri, &recursive); err != nil {
170                         return err
171                 }
172                 snapshot, _, ok, release, err := s.beginFileRequest(ctx, uri, source.UnknownKind)
173                 defer release()
174                 if !ok {
175                         return err
176                 }
177                 return s.runGoGenerate(ctx, snapshot, uri.SpanURI(), recursive, work)
178         case source.CommandRegenerateCgo:
179                 var uri protocol.DocumentURI
180                 if err := source.UnmarshalArgs(args, &uri); err != nil {
181                         return err
182                 }
183                 mod := source.FileModification{
184                         URI:    uri.SpanURI(),
185                         Action: source.InvalidateMetadata,
186                 }
187                 return s.didModifyFiles(ctx, []source.FileModification{mod}, FromRegenerateCgo)
188         case source.CommandTidy, source.CommandVendor:
189                 var uri protocol.DocumentURI
190                 if err := source.UnmarshalArgs(args, &uri); err != nil {
191                         return err
192                 }
193                 // The flow for `go mod tidy` and `go mod vendor` is almost identical,
194                 // so we combine them into one case for convenience.
195                 a := "tidy"
196                 if command == source.CommandVendor {
197                         a = "vendor"
198                 }
199                 return s.directGoModCommand(ctx, uri, "mod", a)
200         case source.CommandAddDependency, source.CommandUpgradeDependency, source.CommandRemoveDependency:
201                 var uri protocol.DocumentURI
202                 var goCmdArgs []string
203                 var addRequire bool
204                 if err := source.UnmarshalArgs(args, &uri, &addRequire, &goCmdArgs); err != nil {
205                         return err
206                 }
207                 if addRequire {
208                         // Using go get to create a new dependency results in an
209                         // `// indirect` comment we may not want. The only way to avoid it
210                         // is to add the require as direct first. Then we can use go get to
211                         // update go.sum and tidy up.
212                         if err := s.directGoModCommand(ctx, uri, "mod", append([]string{"edit", "-require"}, goCmdArgs...)...); err != nil {
213                                 return err
214                         }
215                 }
216                 return s.directGoModCommand(ctx, uri, "get", append([]string{"-d"}, goCmdArgs...)...)
217         case source.CommandToggleDetails:
218                 var fileURI span.URI
219                 if err := source.UnmarshalArgs(args, &fileURI); err != nil {
220                         return err
221                 }
222                 pkgDir := span.URIFromPath(filepath.Dir(fileURI.Filename()))
223                 s.gcOptimizationDetailsMu.Lock()
224                 if _, ok := s.gcOptimizationDetails[pkgDir]; ok {
225                         delete(s.gcOptimizationDetails, pkgDir)
226                 } else {
227                         s.gcOptimizationDetails[pkgDir] = struct{}{}
228                 }
229                 s.gcOptimizationDetailsMu.Unlock()
230                 // need to recompute diagnostics.
231                 // so find the snapshot
232                 sv, err := s.session.ViewOf(fileURI)
233                 if err != nil {
234                         return err
235                 }
236                 snapshot, release := sv.Snapshot(ctx)
237                 defer release()
238                 s.diagnoseSnapshot(snapshot, nil)
239         case source.CommandGenerateGoplsMod:
240                 var v source.View
241                 if len(args) == 0 {
242                         views := s.session.Views()
243                         if len(views) != 1 {
244                                 return fmt.Errorf("cannot resolve view: have %d views", len(views))
245                         }
246                         v = views[0]
247                 } else {
248                         var uri protocol.DocumentURI
249                         if err := source.UnmarshalArgs(args, &uri); err != nil {
250                                 return err
251                         }
252                         var err error
253                         v, err = s.session.ViewOf(uri.SpanURI())
254                         if err != nil {
255                                 return err
256                         }
257                 }
258                 snapshot, release := v.Snapshot(ctx)
259                 defer release()
260                 modFile, err := cache.BuildGoplsMod(ctx, v.Folder(), snapshot)
261                 if err != nil {
262                         return errors.Errorf("getting workspace mod file: %w", err)
263                 }
264                 content, err := modFile.Format()
265                 if err != nil {
266                         return errors.Errorf("formatting mod file: %w", err)
267                 }
268                 filename := filepath.Join(v.Folder().Filename(), "gopls.mod")
269                 if err := ioutil.WriteFile(filename, content, 0644); err != nil {
270                         return errors.Errorf("writing mod file: %w", err)
271                 }
272         default:
273                 return fmt.Errorf("unsupported command: %s", command.ID())
274         }
275         return nil
276 }
277
278 func (s *Server) directGoModCommand(ctx context.Context, uri protocol.DocumentURI, verb string, args ...string) error {
279         view, err := s.session.ViewOf(uri.SpanURI())
280         if err != nil {
281                 return err
282         }
283         snapshot, release := view.Snapshot(ctx)
284         defer release()
285         _, err = snapshot.RunGoCommandDirect(ctx, source.UpdateUserModFile, &gocommand.Invocation{
286                 Verb:       verb,
287                 Args:       args,
288                 WorkingDir: filepath.Dir(uri.SpanURI().Filename()),
289         })
290         return err
291 }
292
293 func (s *Server) runTests(ctx context.Context, snapshot source.Snapshot, uri protocol.DocumentURI, work *workDone, tests, benchmarks []string) error {
294         pkgs, err := snapshot.PackagesForFile(ctx, uri.SpanURI(), source.TypecheckWorkspace)
295         if err != nil {
296                 return err
297         }
298         if len(pkgs) == 0 {
299                 return fmt.Errorf("package could not be found for file: %s", uri.SpanURI().Filename())
300         }
301         pkgPath := pkgs[0].PkgPath()
302
303         // create output
304         buf := &bytes.Buffer{}
305         ew := &eventWriter{ctx: ctx, operation: "test"}
306         out := io.MultiWriter(ew, workDoneWriter{work}, buf)
307
308         // Run `go test -run Func` on each test.
309         var failedTests int
310         for _, funcName := range tests {
311                 inv := &gocommand.Invocation{
312                         Verb:       "test",
313                         Args:       []string{pkgPath, "-v", "-count=1", "-run", fmt.Sprintf("^%s$", funcName)},
314                         WorkingDir: filepath.Dir(uri.SpanURI().Filename()),
315                 }
316                 if err := snapshot.RunGoCommandPiped(ctx, source.Normal, inv, out, out); err != nil {
317                         if errors.Is(err, context.Canceled) {
318                                 return err
319                         }
320                         failedTests++
321                 }
322         }
323
324         // Run `go test -run=^$ -bench Func` on each test.
325         var failedBenchmarks int
326         for _, funcName := range benchmarks {
327                 inv := &gocommand.Invocation{
328                         Verb:       "test",
329                         Args:       []string{pkgPath, "-v", "-run=^$", "-bench", fmt.Sprintf("^%s$", funcName)},
330                         WorkingDir: filepath.Dir(uri.SpanURI().Filename()),
331                 }
332                 if err := snapshot.RunGoCommandPiped(ctx, source.Normal, inv, out, out); err != nil {
333                         if errors.Is(err, context.Canceled) {
334                                 return err
335                         }
336                         failedBenchmarks++
337                 }
338         }
339
340         var title string
341         if len(tests) > 0 && len(benchmarks) > 0 {
342                 title = "tests and benchmarks"
343         } else if len(tests) > 0 {
344                 title = "tests"
345         } else if len(benchmarks) > 0 {
346                 title = "benchmarks"
347         } else {
348                 return errors.New("No functions were provided")
349         }
350         message := fmt.Sprintf("all %s passed", title)
351         if failedTests > 0 && failedBenchmarks > 0 {
352                 message = fmt.Sprintf("%d / %d tests failed and %d / %d benchmarks failed", failedTests, len(tests), failedBenchmarks, len(benchmarks))
353         } else if failedTests > 0 {
354                 message = fmt.Sprintf("%d / %d tests failed", failedTests, len(tests))
355         } else if failedBenchmarks > 0 {
356                 message = fmt.Sprintf("%d / %d benchmarks failed", failedBenchmarks, len(benchmarks))
357         }
358         messageType := protocol.Info
359         if failedTests > 0 || failedBenchmarks > 0 {
360                 messageType = protocol.Error
361                 message += "\n" + buf.String()
362         }
363
364         return s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
365                 Type:    messageType,
366                 Message: message,
367         })
368 }
369
370 func (s *Server) runGoGenerate(ctx context.Context, snapshot source.Snapshot, dir span.URI, recursive bool, work *workDone) error {
371         ctx, cancel := context.WithCancel(ctx)
372         defer cancel()
373
374         er := &eventWriter{ctx: ctx, operation: "generate"}
375
376         pattern := "."
377         if recursive {
378                 pattern = "..."
379         }
380
381         inv := &gocommand.Invocation{
382                 Verb:       "generate",
383                 Args:       []string{"-x", pattern},
384                 WorkingDir: dir.Filename(),
385         }
386         stderr := io.MultiWriter(er, workDoneWriter{work})
387         if err := snapshot.RunGoCommandPiped(ctx, source.Normal, inv, er, stderr); err != nil {
388                 return err
389         }
390         return nil
391 }