-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprogress.go
198 lines (168 loc) · 4.29 KB
/
progress.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package ls
import (
"context"
"log"
"sync"
"github.com/arduino/arduino-language-server/streams"
"go.bug.st/lsp"
)
type ProgressProxyHandler struct {
conn *lsp.Server
mux sync.Mutex
actionRequiredCond *sync.Cond
proxies map[string]*progressProxy
}
type progressProxyStatus int
const (
progressProxyNew progressProxyStatus = iota
progressProxyCreated
progressProxyBegin
progressProxyReport
progressProxyEnd
)
type progressProxy struct {
currentStatus progressProxyStatus
requiredStatus progressProxyStatus
beginReq *lsp.WorkDoneProgressBegin
reportReq *lsp.WorkDoneProgressReport
endReq *lsp.WorkDoneProgressEnd
}
func NewProgressProxy(conn *lsp.Server) *ProgressProxyHandler {
res := &ProgressProxyHandler{
conn: conn,
proxies: map[string]*progressProxy{},
}
res.actionRequiredCond = sync.NewCond(&res.mux)
go func() {
defer streams.CatchAndLogPanic()
res.handlerLoop()
}()
return res
}
func (p *ProgressProxyHandler) handlerLoop() {
p.mux.Lock()
defer p.mux.Unlock()
for {
p.actionRequiredCond.Wait()
for id, proxy := range p.proxies {
for proxy.currentStatus != proxy.requiredStatus {
p.handleProxy(id, proxy)
}
}
// Cleanup ended proxies
for id, proxy := range p.proxies {
if proxy.currentStatus == progressProxyEnd {
delete(p.proxies, id)
}
}
}
}
func (p *ProgressProxyHandler) handleProxy(id string, proxy *progressProxy) {
switch proxy.currentStatus {
case progressProxyNew:
p.mux.Unlock()
respErr, err := p.conn.WindowWorkDoneProgressCreate(context.Background(), &lsp.WorkDoneProgressCreateParams{
Token: lsp.EncodeMessage(id),
})
if err != nil {
log.Printf("ProgressHandler: error creating token %s: %v", id, err)
break
}
if respErr != nil {
log.Printf("ProgressHandler: error creating token %s: %v", id, respErr.AsError())
break
}
p.mux.Lock()
proxy.currentStatus = progressProxyCreated
case progressProxyCreated:
err := p.conn.Progress(&lsp.ProgressParams{
Token: lsp.EncodeMessage(id),
Value: lsp.EncodeMessage(proxy.beginReq),
})
proxy.beginReq = nil
if err != nil {
log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err)
} else {
proxy.currentStatus = progressProxyBegin
}
case progressProxyBegin:
if proxy.requiredStatus == progressProxyReport {
err := p.conn.Progress(&lsp.ProgressParams{
Token: lsp.EncodeMessage(id),
Value: lsp.EncodeMessage(proxy.reportReq),
})
proxy.reportReq = nil
if err != nil {
log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err)
} else {
proxy.requiredStatus = progressProxyBegin
}
} else if proxy.requiredStatus == progressProxyEnd {
err := p.conn.Progress(&lsp.ProgressParams{
Token: lsp.EncodeMessage(id),
Value: lsp.EncodeMessage(proxy.endReq),
})
proxy.endReq = nil
if err != nil {
log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err)
} else {
proxy.currentStatus = progressProxyEnd
}
}
}
}
func (p *ProgressProxyHandler) Create(id string) {
p.mux.Lock()
defer p.mux.Unlock()
if _, opened := p.proxies[id]; opened {
// Already created
return
}
p.proxies[id] = &progressProxy{
currentStatus: progressProxyNew,
requiredStatus: progressProxyCreated,
}
p.actionRequiredCond.Broadcast()
}
func (p *ProgressProxyHandler) Begin(id string, req *lsp.WorkDoneProgressBegin) {
p.mux.Lock()
defer p.mux.Unlock()
proxy, ok := p.proxies[id]
if !ok {
return
}
if proxy.requiredStatus == progressProxyReport {
return
}
if proxy.requiredStatus == progressProxyEnd {
return
}
proxy.beginReq = req
proxy.requiredStatus = progressProxyBegin
p.actionRequiredCond.Broadcast()
}
func (p *ProgressProxyHandler) Report(id string, req *lsp.WorkDoneProgressReport) {
p.mux.Lock()
defer p.mux.Unlock()
proxy, ok := p.proxies[id]
if !ok {
return
}
if proxy.requiredStatus == progressProxyEnd {
return
}
proxy.reportReq = req
proxy.requiredStatus = progressProxyReport
p.actionRequiredCond.Broadcast()
}
func (p *ProgressProxyHandler) End(id string, req *lsp.WorkDoneProgressEnd) {
p.mux.Lock()
defer p.mux.Unlock()
proxy, ok := p.proxies[id]
if !ok {
return
}
proxy.endReq = req
proxy.requiredStatus = progressProxyEnd
p.actionRequiredCond.Broadcast()
}