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.
13 "golang.org/x/tools/internal/event"
14 "golang.org/x/tools/internal/lsp/debug/tag"
15 "golang.org/x/tools/internal/lsp/protocol"
16 "golang.org/x/tools/internal/xcontext"
17 errors "golang.org/x/xerrors"
20 type progressTracker struct {
21 client protocol.Client
22 supportsWorkDoneProgress bool
25 inProgress map[protocol.ProgressToken]*workDone
28 func newProgressTracker(client protocol.Client) *progressTracker {
29 return &progressTracker{
31 inProgress: make(map[protocol.ProgressToken]*workDone),
35 // start notifies the client of work being done on the server. It uses either
36 // ShowMessage RPCs or $/progress messages, depending on the capabilities of
37 // the client. The returned WorkDone handle may be used to report incremental
38 // progress, and to report work completion. In particular, it is an error to
39 // call start and not call end(...) on the returned WorkDone handle.
41 // If token is empty, a token will be randomly generated.
43 // The progress item is considered cancellable if the given cancel func is
44 // non-nil. In this case, cancel is called when the work done
47 // func Generate(ctx) (err error) {
48 // ctx, cancel := context.WithCancel(ctx)
50 // work := s.progress.start(ctx, "generate", "running go generate", cancel)
53 // work.end(ctx, fmt.Sprintf("generate failed: %v", err))
55 // work.end(ctx, "done")
61 func (t *progressTracker) start(ctx context.Context, title, message string, token protocol.ProgressToken, cancel func()) *workDone {
63 ctx: xcontext.Detach(ctx),
68 if !t.supportsWorkDoneProgress {
69 // Previous iterations of this fallback attempted to retain cancellation
70 // support by using ShowMessageCommand with a 'Cancel' button, but this is
71 // not ideal as the 'Cancel' dialog stays open even after the command
74 // Just show a simple message. Clients can implement workDone progress
75 // reporting to get cancellation support.
76 if err := wd.client.ShowMessage(wd.ctx, &protocol.ShowMessageParams{
80 event.Error(ctx, "showing start message for "+title, err)
85 token = strconv.FormatInt(rand.Int63(), 10)
86 err := wd.client.WorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{
91 event.Error(ctx, "starting work for "+title, err)
96 // At this point we have a token that the client knows about. Store the token
97 // before starting work.
99 t.inProgress[wd.token] = wd
101 wd.cleanup = func() {
103 delete(t.inProgress, token)
106 err := wd.client.Progress(ctx, &protocol.ProgressParams{
108 Value: &protocol.WorkDoneProgressBegin{
110 Cancellable: wd.cancel != nil,
116 event.Error(ctx, "generate progress begin", err)
121 func (t *progressTracker) cancel(ctx context.Context, token protocol.ProgressToken) error {
124 wd, ok := t.inProgress[token]
126 return errors.Errorf("token %q not found in progress", token)
128 if wd.cancel == nil {
129 return errors.Errorf("work %q is not cancellable", token)
135 // workDone represents a unit of work that is reported to the client via the
137 type workDone struct {
138 // ctx is detached, for sending $/progress updates.
140 client protocol.Client
141 // If token is nil, this workDone object uses the ShowMessage API, rather
143 token protocol.ProgressToken
144 // err is set if progress reporting is broken for some reason (for example,
145 // if there was an initial error creating a token).
155 func (wd *workDone) doCancel() {
157 defer wd.cancelMu.Unlock()
163 // report reports an update on WorkDone report back to the client.
164 func (wd *workDone) report(message string, percentage float64) {
166 cancelled := wd.cancelled
171 if wd.err != nil || wd.token == nil {
172 // Not using the workDone API, so we do nothing. It would be far too spammy
173 // to send incremental messages.
176 err := wd.client.Progress(wd.ctx, &protocol.ProgressParams{
178 Value: &protocol.WorkDoneProgressReport{
180 // Note that in the LSP spec, the value of Cancellable may be changed to
181 // control whether the cancel button in the UI is enabled. Since we don't
182 // yet use this feature, the value is kept constant here.
183 Cancellable: wd.cancel != nil,
185 Percentage: percentage,
189 event.Error(wd.ctx, "reporting progress", err)
193 // end reports a workdone completion back to the client.
194 func (wd *workDone) end(message string) {
198 // There is a prior error.
199 case wd.token == nil:
200 // We're falling back to message-based reporting.
201 err = wd.client.ShowMessage(wd.ctx, &protocol.ShowMessageParams{
206 err = wd.client.Progress(wd.ctx, &protocol.ProgressParams{
208 Value: &protocol.WorkDoneProgressEnd{
215 event.Error(wd.ctx, "ending work", err)
217 if wd.cleanup != nil {
222 // eventWriter writes every incoming []byte to
223 // event.Print with the operation=generate tag
224 // to distinguish its logs from others.
225 type eventWriter struct {
230 func (ew *eventWriter) Write(p []byte) (n int, err error) {
231 event.Log(ew.ctx, string(p), tag.Operation.Of(ew.operation))
235 // workDoneWriter wraps a workDone handle to provide a Writer interface,
236 // so that workDone reporting can more easily be hooked into commands.
237 type workDoneWriter struct {
241 func (wdw workDoneWriter) Write(p []byte) (n int, err error) {
242 wdw.wd.report(string(p), 0)
243 // Don't fail just because of a failure to report progress.