@@ -22,6 +22,7 @@ import (
22
22
"github.com/bcmi-labs/arduino-language-server/handler/textutils"
23
23
"github.com/bcmi-labs/arduino-language-server/lsp"
24
24
"github.com/bcmi-labs/arduino-language-server/streams"
25
+ "github.com/fatih/color"
25
26
"github.com/pkg/errors"
26
27
"github.com/sourcegraph/jsonrpc2"
27
28
)
@@ -71,6 +72,34 @@ type InoHandler struct {
71
72
config lsp.BoardConfig
72
73
}
73
74
75
+ var yellow = color .New (color .FgHiYellow )
76
+
77
+ func (handler * InoHandler ) dataLock (msg string ) {
78
+ handler .dataMux .Lock ()
79
+ log .Println (msg + yellow .Sprintf (" locked" ))
80
+ }
81
+
82
+ func (handler * InoHandler ) dataUnlock (msg string ) {
83
+ log .Println (msg + yellow .Sprintf (" unlocked" ))
84
+ handler .dataMux .Unlock ()
85
+ }
86
+
87
+ func (handler * InoHandler ) dataRLock (msg string ) {
88
+ handler .dataMux .RLock ()
89
+ log .Println (msg + yellow .Sprintf (" read-locked" ))
90
+ }
91
+
92
+ func (handler * InoHandler ) dataRUnlock (msg string ) {
93
+ log .Println (msg + yellow .Sprintf (" read-unlocked" ))
94
+ handler .dataMux .RUnlock ()
95
+ }
96
+
97
+ func (handler * InoHandler ) waitClangdStart (msg string ) {
98
+ log .Println (msg + yellow .Sprintf (" unlocked (waiting clangd)" ))
99
+ handler .clangdStarted .Wait ()
100
+ log .Println (msg + yellow .Sprintf (" locked (waiting clangd)" ))
101
+ }
102
+
74
103
// NewInoHandler creates and configures an InoHandler.
75
104
func NewInoHandler (stdio io.ReadWriteCloser , board lsp.Board ) * InoHandler {
76
105
handler := & InoHandler {
@@ -124,7 +153,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
124
153
} else {
125
154
prefix += fmt .Sprintf ("%s %v " , req .Method , req .ID )
126
155
}
127
- defer log .Printf (prefix + "(done)" )
128
156
129
157
params , err := lsp .ReadParams (req .Method , req .Params )
130
158
if err != nil {
@@ -134,23 +162,22 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
134
162
params = req .Params
135
163
}
136
164
137
- log .Printf (prefix + "(queued)" )
138
165
switch req .Method {
139
166
case // Write lock
140
167
"initialize" ,
141
168
"textDocument/didOpen" ,
142
169
"textDocument/didChange" ,
143
170
"textDocument/didClose" :
144
- handler .dataMux . Lock ( )
145
- defer handler .dataMux . Unlock ( )
171
+ handler .dataLock ( prefix )
172
+ defer handler .dataUnlock ( prefix )
146
173
case // Read lock
147
174
"textDocument/publishDiagnostics" ,
148
175
"workspace/applyEdit" :
149
- handler .dataMux . RLock ( )
150
- defer handler .dataMux . RUnlock ( )
176
+ handler .dataRLock ( prefix )
177
+ defer handler .dataRUnlock ( prefix )
151
178
default : // Default to read lock
152
- handler .dataMux . RLock ( )
153
- defer handler .dataMux . RUnlock ( )
179
+ handler .dataRLock ( prefix )
180
+ defer handler .dataRUnlock ( prefix )
154
181
}
155
182
156
183
switch req .Method {
@@ -161,16 +188,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
161
188
// Wait for clangd start-up
162
189
if handler .ClangdConn == nil {
163
190
log .Printf (prefix + "(throttled: waiting for clangd)" )
164
- handler .clangdStarted . Wait ( )
191
+ handler .waitClangdStart ( prefix )
165
192
if handler .ClangdConn == nil {
166
193
log .Printf (prefix + "clangd startup failed: aborting call" )
167
194
return nil , errors .New ("could not start clangd, aborted" )
168
195
}
169
196
}
170
197
}
171
198
172
- log .Printf (prefix + "(running)" )
173
-
174
199
// Handle LSP methods: transform parameters and send to clangd
175
200
var inoURI , cppURI lsp.DocumentURI
176
201
@@ -180,19 +205,19 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
180
205
181
206
go func () {
182
207
defer streams .CatchAndLogPanic ()
208
+ prefix := "INIT--- "
209
+ log .Printf (prefix + "initializing workbench" )
183
210
184
211
// Start clangd asynchronously
185
- log .Printf ("LS --- initializing workbench (queued)" )
186
- handler .dataMux .Lock ()
187
- defer handler .dataMux .Unlock ()
212
+ handler .dataLock (prefix )
213
+ defer handler .dataUnlock (prefix )
188
214
189
- log .Printf ("LS --- initializing workbench (running)" )
190
215
handler .initializeWorkbench (ctx , p )
191
216
192
217
// clangd should be running now...
193
218
handler .clangdStarted .Broadcast ()
194
219
195
- log .Printf ("LS --- initializing workbench (done)" )
220
+ log .Printf (prefix + " initializing workbench (done)" )
196
221
}()
197
222
198
223
T := true
@@ -542,19 +567,15 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
542
567
}
543
568
544
569
func (handler * InoHandler ) refreshCppDocumentSymbols () error {
545
- prefix := "RFSH--- "
546
- defer log .Printf (prefix + "(done)" )
547
-
548
570
// Query source code symbols
549
571
cppURI := lsp .NewDocumentURIFromPath (handler .buildSketchCpp )
550
- log .Printf (prefix + "sent to clangd: documentSymbol(%s)" , cppURI )
572
+ log .Printf (prefix + "requesting documentSymbol for %s" , cppURI )
573
+
551
574
result , err := lsp .SendRequest (context .Background (), handler .ClangdConn , "textDocument/documentSymbol" , & lsp.DocumentSymbolParams {
552
575
TextDocument : lsp.TextDocumentIdentifier {URI : cppURI },
553
576
})
554
-
555
- log .Printf (prefix + "(queued answer)" )
556
- handler .dataMux .Lock ()
557
- defer handler .dataMux .Unlock ()
577
+ handler .dataLock (prefix )
578
+ defer handler .dataUnlock (prefix )
558
579
559
580
if err != nil {
560
581
log .Printf (prefix + "error: %s" , err )
@@ -1513,8 +1534,8 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1513
1534
1514
1535
// Default to read lock
1515
1536
log .Printf (prefix + "(queued)" )
1516
- handler .dataMux . RLock ( )
1517
- defer handler .dataMux . RUnlock ( )
1537
+ handler .dataRLock ( prefix )
1538
+ defer handler .dataRUnlock ( prefix )
1518
1539
log .Printf (prefix + "(running)" )
1519
1540
1520
1541
params , err := lsp .ReadParams (req .Method , req .Params )
0 commit comments