|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/bcmi-labs/arduino-language-server/lsp" |
| 9 | + "github.com/bcmi-labs/arduino-language-server/streams" |
| 10 | + "github.com/sourcegraph/jsonrpc2" |
| 11 | +) |
| 12 | + |
| 13 | +type ProgressProxyHandler struct { |
| 14 | + conn *jsonrpc2.Conn |
| 15 | + mux sync.Mutex |
| 16 | + actionRequiredCond *sync.Cond |
| 17 | + proxies map[string]*progressProxy |
| 18 | +} |
| 19 | + |
| 20 | +type progressProxyStatus int |
| 21 | + |
| 22 | +const ( |
| 23 | + progressProxyNew progressProxyStatus = iota |
| 24 | + progressProxyCreated |
| 25 | + progressProxyBegin |
| 26 | + progressProxyReport |
| 27 | + progressProxyEnd |
| 28 | +) |
| 29 | + |
| 30 | +type progressProxy struct { |
| 31 | + currentStatus progressProxyStatus |
| 32 | + requiredStatus progressProxyStatus |
| 33 | + beginReq *lsp.WorkDoneProgressBegin |
| 34 | + reportReq *lsp.WorkDoneProgressReport |
| 35 | + endReq *lsp.WorkDoneProgressEnd |
| 36 | +} |
| 37 | + |
| 38 | +func NewProgressProxy(conn *jsonrpc2.Conn) *ProgressProxyHandler { |
| 39 | + res := &ProgressProxyHandler{ |
| 40 | + conn: conn, |
| 41 | + proxies: map[string]*progressProxy{}, |
| 42 | + } |
| 43 | + res.actionRequiredCond = sync.NewCond(&res.mux) |
| 44 | + go res.handlerLoop() |
| 45 | + return res |
| 46 | +} |
| 47 | + |
| 48 | +func (p *ProgressProxyHandler) handlerLoop() { |
| 49 | + defer streams.CatchAndLogPanic() |
| 50 | + |
| 51 | + p.mux.Lock() |
| 52 | + defer p.mux.Unlock() |
| 53 | + |
| 54 | + for { |
| 55 | + p.actionRequiredCond.Wait() |
| 56 | + |
| 57 | + for id, proxy := range p.proxies { |
| 58 | + for proxy.currentStatus != proxy.requiredStatus { |
| 59 | + p.handleProxy(id, proxy) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Cleanup ended proxies |
| 64 | + for id, proxy := range p.proxies { |
| 65 | + if proxy.currentStatus == progressProxyEnd { |
| 66 | + delete(p.proxies, id) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (p *ProgressProxyHandler) handleProxy(id string, proxy *progressProxy) { |
| 73 | + ctx := context.Background() |
| 74 | + switch proxy.currentStatus { |
| 75 | + case progressProxyNew: |
| 76 | + p.mux.Unlock() |
| 77 | + var res lsp.WorkDoneProgressCreateResult |
| 78 | + err := p.conn.Call(ctx, "window/workDoneProgress/create", &lsp.WorkDoneProgressCreateParams{Token: id}, &res) |
| 79 | + p.mux.Lock() |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + log.Printf("ProgressHandler: error creating token %s: %v", id, err) |
| 83 | + } else { |
| 84 | + proxy.currentStatus = progressProxyCreated |
| 85 | + } |
| 86 | + |
| 87 | + case progressProxyCreated: |
| 88 | + p.mux.Unlock() |
| 89 | + err := p.conn.Notify(ctx, "$/progress", lsp.ProgressParams{ |
| 90 | + Token: id, |
| 91 | + Value: lsp.Raw(proxy.beginReq), |
| 92 | + }) |
| 93 | + p.mux.Lock() |
| 94 | + |
| 95 | + proxy.beginReq = nil |
| 96 | + if err != nil { |
| 97 | + log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err) |
| 98 | + } else { |
| 99 | + proxy.currentStatus = progressProxyBegin |
| 100 | + } |
| 101 | + |
| 102 | + case progressProxyBegin: |
| 103 | + if proxy.requiredStatus == progressProxyReport { |
| 104 | + p.mux.Unlock() |
| 105 | + err := p.conn.Notify(ctx, "$/progress", &lsp.ProgressParams{ |
| 106 | + Token: id, |
| 107 | + Value: lsp.Raw(proxy.reportReq)}) |
| 108 | + p.mux.Lock() |
| 109 | + |
| 110 | + proxy.reportReq = nil |
| 111 | + if err != nil { |
| 112 | + log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err) |
| 113 | + } else { |
| 114 | + proxy.requiredStatus = progressProxyBegin |
| 115 | + } |
| 116 | + |
| 117 | + } else if proxy.requiredStatus == progressProxyEnd { |
| 118 | + p.mux.Unlock() |
| 119 | + err := p.conn.Notify(ctx, "$/progress", &lsp.ProgressParams{ |
| 120 | + Token: id, |
| 121 | + Value: lsp.Raw(proxy.endReq), |
| 122 | + }) |
| 123 | + p.mux.Lock() |
| 124 | + |
| 125 | + proxy.endReq = nil |
| 126 | + if err != nil { |
| 127 | + log.Printf("ProgressHandler: error sending begin req token %s: %v", id, err) |
| 128 | + } else { |
| 129 | + proxy.currentStatus = progressProxyEnd |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (p *ProgressProxyHandler) Create(id string) { |
| 137 | + p.mux.Lock() |
| 138 | + defer p.mux.Unlock() |
| 139 | + |
| 140 | + if _, opened := p.proxies[id]; opened { |
| 141 | + // Already created |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + p.proxies[id] = &progressProxy{ |
| 146 | + currentStatus: progressProxyNew, |
| 147 | + requiredStatus: progressProxyCreated, |
| 148 | + } |
| 149 | + p.actionRequiredCond.Broadcast() |
| 150 | +} |
| 151 | + |
| 152 | +func (p *ProgressProxyHandler) Begin(id string, req *lsp.WorkDoneProgressBegin) { |
| 153 | + p.mux.Lock() |
| 154 | + defer p.mux.Unlock() |
| 155 | + |
| 156 | + proxy, ok := p.proxies[id] |
| 157 | + if !ok { |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + proxy.beginReq = req |
| 162 | + proxy.requiredStatus = progressProxyBegin |
| 163 | + p.actionRequiredCond.Broadcast() |
| 164 | +} |
| 165 | + |
| 166 | +func (p *ProgressProxyHandler) Report(id string, req *lsp.WorkDoneProgressReport) { |
| 167 | + p.mux.Lock() |
| 168 | + defer p.mux.Unlock() |
| 169 | + |
| 170 | + proxy, ok := p.proxies[id] |
| 171 | + if !ok { |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + proxy.reportReq = req |
| 176 | + proxy.requiredStatus = progressProxyReport |
| 177 | + p.actionRequiredCond.Broadcast() |
| 178 | +} |
| 179 | + |
| 180 | +func (p *ProgressProxyHandler) End(id string, req *lsp.WorkDoneProgressEnd) { |
| 181 | + p.mux.Lock() |
| 182 | + defer p.mux.Unlock() |
| 183 | + |
| 184 | + proxy, ok := p.proxies[id] |
| 185 | + if !ok { |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + proxy.endReq = req |
| 190 | + proxy.requiredStatus = progressProxyEnd |
| 191 | + p.actionRequiredCond.Broadcast() |
| 192 | +} |
0 commit comments