| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package progress |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"fmt" |
| 13 |
"io" |
| 14 |
"math/rand" |
| 15 |
"strconv" |
| 16 |
"strings" |
| 17 |
"sync" |
| 18 |
|
| 19 |
"golang.org/x/tools/gopls/internal/label" |
| 20 |
"golang.org/x/tools/gopls/internal/protocol" |
| 21 |
"golang.org/x/tools/internal/event" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func NewTracker(client protocol.Client) *Tracker { |
| 27 |
return &Tracker{ |
| 28 |
client: client, |
| 29 |
inProgress: make(map[protocol.ProgressToken]*WorkDone), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
type Tracker struct { |
| 35 |
client protocol.Client |
| 36 |
supportsWorkDoneProgress bool |
| 37 |
|
| 38 |
mu sync.Mutex |
| 39 |
inProgress map[protocol.ProgressToken]*WorkDone |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func (t *Tracker) SetSupportsWorkDoneProgress(b bool) { |
| 49 |
t.supportsWorkDoneProgress = b |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func (t *Tracker) SupportsWorkDoneProgress() bool { |
| 55 |
return t.supportsWorkDoneProgress |
| 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 |
func (t *Tracker) Start(ctx context.Context, title, message string, token protocol.ProgressToken, cancel func()) *WorkDone { |
| 85 |
ctx = context.WithoutCancel(ctx) |
| 86 |
wd := &WorkDone{ |
| 87 |
client: t.client, |
| 88 |
token: token, |
| 89 |
cancel: cancel, |
| 90 |
} |
| 91 |
if !t.supportsWorkDoneProgress { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if err := wd.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
| 100 |
Type: protocol.Log, |
| 101 |
Message: message, |
| 102 |
}); err != nil { |
| 103 |
event.Error(ctx, "showing start message for "+title, err) |
| 104 |
} |
| 105 |
return wd |
| 106 |
} |
| 107 |
if wd.token == nil { |
| 108 |
token = strconv.FormatInt(rand.Int63(), 10) |
| 109 |
err := wd.client.WorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{ |
| 110 |
Token: token, |
| 111 |
}) |
| 112 |
if err != nil { |
| 113 |
wd.err = err |
| 114 |
event.Error(ctx, "starting work for "+title, err) |
| 115 |
return wd |
| 116 |
} |
| 117 |
wd.token = token |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
t.mu.Lock() |
| 122 |
t.inProgress[wd.token] = wd |
| 123 |
t.mu.Unlock() |
| 124 |
wd.cleanup = func() { |
| 125 |
t.mu.Lock() |
| 126 |
delete(t.inProgress, token) |
| 127 |
t.mu.Unlock() |
| 128 |
} |
| 129 |
err := wd.client.Progress(ctx, &protocol.ProgressParams{ |
| 130 |
Token: wd.token, |
| 131 |
Value: &protocol.WorkDoneProgressBegin{ |
| 132 |
Kind: "begin", |
| 133 |
Cancellable: wd.cancel != nil, |
| 134 |
Message: message, |
| 135 |
Title: title, |
| 136 |
}, |
| 137 |
}) |
| 138 |
if err != nil { |
| 139 |
event.Error(ctx, "progress begin", err) |
| 140 |
} |
| 141 |
return wd |
| 142 |
} |
| 143 |
|
| 144 |
func (t *Tracker) Cancel(token protocol.ProgressToken) error { |
| 145 |
t.mu.Lock() |
| 146 |
defer t.mu.Unlock() |
| 147 |
wd, ok := t.inProgress[token] |
| 148 |
if !ok { |
| 149 |
return fmt.Errorf("token %q not found in progress", token) |
| 150 |
} |
| 151 |
if wd.cancel == nil { |
| 152 |
return fmt.Errorf("work %q is not cancellable", token) |
| 153 |
} |
| 154 |
wd.doCancel() |
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
type WorkDone struct { |
| 161 |
client protocol.Client |
| 162 |
|
| 163 |
|
| 164 |
token protocol.ProgressToken |
| 165 |
|
| 166 |
|
| 167 |
err error |
| 168 |
|
| 169 |
cancelMu sync.Mutex |
| 170 |
cancelled bool |
| 171 |
cancel func() |
| 172 |
|
| 173 |
cleanup func() |
| 174 |
} |
| 175 |
|
| 176 |
func (wd *WorkDone) Token() protocol.ProgressToken { |
| 177 |
return wd.token |
| 178 |
} |
| 179 |
|
| 180 |
func (wd *WorkDone) doCancel() { |
| 181 |
wd.cancelMu.Lock() |
| 182 |
defer wd.cancelMu.Unlock() |
| 183 |
if !wd.cancelled { |
| 184 |
wd.cancel() |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
func (wd *WorkDone) Report(ctx context.Context, message string, fraction float64) { |
| 190 |
ctx = context.WithoutCancel(ctx) |
| 191 |
if wd == nil { |
| 192 |
return |
| 193 |
} |
| 194 |
wd.cancelMu.Lock() |
| 195 |
cancelled := wd.cancelled |
| 196 |
wd.cancelMu.Unlock() |
| 197 |
if cancelled { |
| 198 |
return |
| 199 |
} |
| 200 |
if wd.err != nil || wd.token == nil { |
| 201 |
|
| 202 |
|
| 203 |
return |
| 204 |
} |
| 205 |
message = strings.TrimSuffix(message, "\n") |
| 206 |
percentage := uint32(100 * fraction) |
| 207 |
err := wd.client.Progress(ctx, &protocol.ProgressParams{ |
| 208 |
Token: wd.token, |
| 209 |
Value: &protocol.WorkDoneProgressReport{ |
| 210 |
Kind: "report", |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
Cancellable: wd.cancel != nil, |
| 215 |
Message: message, |
| 216 |
Percentage: &percentage, |
| 217 |
}, |
| 218 |
}) |
| 219 |
if err != nil { |
| 220 |
event.Error(ctx, "reporting progress", err) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (wd *WorkDone) End(ctx context.Context, message string) { |
| 226 |
ctx = context.WithoutCancel(ctx) |
| 227 |
if wd == nil { |
| 228 |
return |
| 229 |
} |
| 230 |
var err error |
| 231 |
switch { |
| 232 |
case wd.err != nil: |
| 233 |
|
| 234 |
case wd.token == nil: |
| 235 |
|
| 236 |
err = wd.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
| 237 |
Type: protocol.Info, |
| 238 |
Message: message, |
| 239 |
}) |
| 240 |
default: |
| 241 |
err = wd.client.Progress(ctx, &protocol.ProgressParams{ |
| 242 |
Token: wd.token, |
| 243 |
Value: &protocol.WorkDoneProgressEnd{ |
| 244 |
Kind: "end", |
| 245 |
Message: message, |
| 246 |
}, |
| 247 |
}) |
| 248 |
} |
| 249 |
if err != nil { |
| 250 |
event.Error(ctx, "ending work", err) |
| 251 |
} |
| 252 |
if wd.cleanup != nil { |
| 253 |
wd.cleanup() |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
func NewEventWriter(ctx context.Context, operation string) io.Writer { |
| 261 |
return &eventWriter{ctx: ctx, operation: operation} |
| 262 |
} |
| 263 |
|
| 264 |
type eventWriter struct { |
| 265 |
ctx context.Context |
| 266 |
operation string |
| 267 |
} |
| 268 |
|
| 269 |
func (ew *eventWriter) Write(p []byte) (n int, err error) { |
| 270 |
event.Log(ew.ctx, string(p), label.Operation.Of(ew.operation)) |
| 271 |
return len(p), nil |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
func NewWorkDoneWriter(ctx context.Context, wd *WorkDone) io.Writer { |
| 277 |
return &workDoneWriter{ctx: ctx, wd: wd} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
type workDoneWriter struct { |
| 283 |
|
| 284 |
ctx context.Context |
| 285 |
wd *WorkDone |
| 286 |
} |
| 287 |
|
| 288 |
func (wdw *workDoneWriter) Write(p []byte) (n int, err error) { |
| 289 |
wdw.wd.Report(wdw.ctx, string(p), 0) |
| 290 |
|
| 291 |
return len(p), nil |
| 292 |
} |