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 / protocol / context.go
1 package protocol
2
3 import (
4         "bytes"
5         "context"
6
7         "golang.org/x/tools/internal/event"
8         "golang.org/x/tools/internal/event/core"
9         "golang.org/x/tools/internal/event/export"
10         "golang.org/x/tools/internal/event/label"
11         "golang.org/x/tools/internal/xcontext"
12 )
13
14 type contextKey int
15
16 const (
17         clientKey = contextKey(iota)
18 )
19
20 func WithClient(ctx context.Context, client Client) context.Context {
21         return context.WithValue(ctx, clientKey, client)
22 }
23
24 func LogEvent(ctx context.Context, ev core.Event, tags label.Map) context.Context {
25         if !event.IsLog(ev) {
26                 return ctx
27         }
28         client, ok := ctx.Value(clientKey).(Client)
29         if !ok {
30                 return ctx
31         }
32         buf := &bytes.Buffer{}
33         p := export.Printer{}
34         p.WriteEvent(buf, ev, tags)
35         msg := &LogMessageParams{Type: Info, Message: buf.String()}
36         if event.IsError(ev) {
37                 msg.Type = Error
38         }
39         go client.LogMessage(xcontext.Detach(ctx), msg)
40         return ctx
41 }