-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathgenerate.go
136 lines (120 loc) · 3.82 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lsp
import (
"context"
"io"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/lsp/debug/tag"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/xerrors"
)
// GenerateWorkDoneTitle is the title used in progress reporting for go
// generate commands. It is exported for testing purposes.
const GenerateWorkDoneTitle = "generate"
func (s *Server) runGenerate(ctx context.Context, dir string, recursive bool) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
er := &eventWriter{ctx: ctx}
wc := s.newProgressWriter(ctx, cancel)
defer wc.Close()
args := []string{"-x"}
if recursive {
args = append(args, "./...")
}
inv := &gocommand.Invocation{
Verb: "generate",
Args: args,
Env: s.session.Options().Env,
WorkingDir: dir,
}
stderr := io.MultiWriter(er, wc)
err := inv.RunPiped(ctx, er, stderr)
if err != nil {
event.Error(ctx, "generate: command error", err, tag.Directory.Of(dir))
if !xerrors.Is(err, context.Canceled) {
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
Type: protocol.Error,
Message: "go generate exited with an error, check gopls logs",
})
}
}
}
// eventWriter writes every incoming []byte to
// event.Print with the operation=generate tag
// to distinguish its logs from others.
type eventWriter struct {
ctx context.Context
}
func (ew *eventWriter) Write(p []byte) (n int, err error) {
event.Log(ew.ctx, string(p), tag.Operation.Of("generate"))
return len(p), nil
}
// newProgressWriter returns an io.WriterCloser that can be used
// to report progress on the "go generate" command based on the
// client capabilities.
func (s *Server) newProgressWriter(ctx context.Context, cancel func()) io.WriteCloser {
if s.supportsWorkDoneProgress {
wd := s.StartWork(ctx, GenerateWorkDoneTitle, "running go generate", cancel)
return &workDoneWriter{ctx, wd}
}
mw := &messageWriter{ctx, cancel, s.client}
mw.start()
return mw
}
// messageWriter implements progressWriter
// and only tells the user that "go generate"
// has started through window/showMessage but does not
// report anything afterwards. This is because each
// log shows up as a separate window and therefore
// would be obnoxious to show every incoming line.
// Request cancellation happens synchronously through
// the ShowMessageRequest response.
type messageWriter struct {
ctx context.Context
cancel func()
client protocol.Client
}
func (lw *messageWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (lw *messageWriter) start() {
go func() {
msg, err := lw.client.ShowMessageRequest(lw.ctx, &protocol.ShowMessageRequestParams{
Type: protocol.Log,
Message: "go generate has started, check logs for progress",
Actions: []protocol.MessageActionItem{{
Title: "Cancel",
}},
})
if err != nil {
event.Error(lw.ctx, "error sending initial generate msg", err)
return
}
if msg != nil && msg.Title == "Cancel" {
lw.cancel()
}
}()
}
func (lw *messageWriter) Close() error {
return lw.client.ShowMessage(lw.ctx, &protocol.ShowMessageParams{
Type: protocol.Info,
Message: "go generate has finished",
})
}
// workDoneWriter implements progressWriter by sending $/progress notifications
// to the client. Request cancellations happens separately through the
// window/workDoneProgress/cancel request, in which case the given context will
// be rendered done.
type workDoneWriter struct {
ctx context.Context
wd *WorkDone
}
func (wdw *workDoneWriter) Write(p []byte) (n int, err error) {
return len(p), wdw.wd.Progress(wdw.ctx, string(p), 0)
}
func (wdw *workDoneWriter) Close() error {
return wdw.wd.End(wdw.ctx, "finished")
}