@@ -22,6 +22,7 @@ import (
2222 "github.com/bcmi-labs/arduino-language-server/handler/textutils"
2323 "github.com/bcmi-labs/arduino-language-server/lsp"
2424 "github.com/bcmi-labs/arduino-language-server/streams"
25+ "github.com/fatih/color"
2526 "github.com/pkg/errors"
2627 "github.com/sourcegraph/jsonrpc2"
2728)
@@ -71,6 +72,34 @@ type InoHandler struct {
7172 config lsp.BoardConfig
7273}
7374
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+
74103// NewInoHandler creates and configures an InoHandler.
75104func NewInoHandler (stdio io.ReadWriteCloser , board lsp.Board ) * InoHandler {
76105 handler := & InoHandler {
@@ -124,7 +153,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
124153 } else {
125154 prefix += fmt .Sprintf ("%s %v " , req .Method , req .ID )
126155 }
127- defer log .Printf (prefix + "(done)" )
128156
129157 params , err := lsp .ReadParams (req .Method , req .Params )
130158 if err != nil {
@@ -134,23 +162,22 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
134162 params = req .Params
135163 }
136164
137- log .Printf (prefix + "(queued)" )
138165 switch req .Method {
139166 case // Write lock
140167 "initialize" ,
141168 "textDocument/didOpen" ,
142169 "textDocument/didChange" ,
143170 "textDocument/didClose" :
144- handler .dataMux . Lock ( )
145- defer handler .dataMux . Unlock ( )
171+ handler .dataLock ( prefix )
172+ defer handler .dataUnlock ( prefix )
146173 case // Read lock
147174 "textDocument/publishDiagnostics" ,
148175 "workspace/applyEdit" :
149- handler .dataMux . RLock ( )
150- defer handler .dataMux . RUnlock ( )
176+ handler .dataRLock ( prefix )
177+ defer handler .dataRUnlock ( prefix )
151178 default : // Default to read lock
152- handler .dataMux . RLock ( )
153- defer handler .dataMux . RUnlock ( )
179+ handler .dataRLock ( prefix )
180+ defer handler .dataRUnlock ( prefix )
154181 }
155182
156183 switch req .Method {
@@ -161,16 +188,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
161188 // Wait for clangd start-up
162189 if handler .ClangdConn == nil {
163190 log .Printf (prefix + "(throttled: waiting for clangd)" )
164- handler .clangdStarted . Wait ( )
191+ handler .waitClangdStart ( prefix )
165192 if handler .ClangdConn == nil {
166193 log .Printf (prefix + "clangd startup failed: aborting call" )
167194 return nil , errors .New ("could not start clangd, aborted" )
168195 }
169196 }
170197 }
171198
172- log .Printf (prefix + "(running)" )
173-
174199 // Handle LSP methods: transform parameters and send to clangd
175200 var inoURI , cppURI lsp.DocumentURI
176201
@@ -180,19 +205,19 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
180205
181206 go func () {
182207 defer streams .CatchAndLogPanic ()
208+ prefix := "INIT--- "
209+ log .Printf (prefix + "initializing workbench" )
183210
184211 // 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 )
188214
189- log .Printf ("LS --- initializing workbench (running)" )
190215 handler .initializeWorkbench (ctx , p )
191216
192217 // clangd should be running now...
193218 handler .clangdStarted .Broadcast ()
194219
195- log .Printf ("LS --- initializing workbench (done)" )
220+ log .Printf (prefix + " initializing workbench (done)" )
196221 }()
197222
198223 T := true
@@ -542,19 +567,15 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
542567}
543568
544569func (handler * InoHandler ) refreshCppDocumentSymbols () error {
545- prefix := "RFSH--- "
546- defer log .Printf (prefix + "(done)" )
547-
548570 // Query source code symbols
549571 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+
551574 result , err := lsp .SendRequest (context .Background (), handler .ClangdConn , "textDocument/documentSymbol" , & lsp.DocumentSymbolParams {
552575 TextDocument : lsp.TextDocumentIdentifier {URI : cppURI },
553576 })
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 )
558579
559580 if err != nil {
560581 log .Printf (prefix + "error: %s" , err )
@@ -1513,8 +1534,8 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
15131534
15141535 // Default to read lock
15151536 log .Printf (prefix + "(queued)" )
1516- handler .dataMux . RLock ( )
1517- defer handler .dataMux . RUnlock ( )
1537+ handler .dataRLock ( prefix )
1538+ defer handler .dataRUnlock ( prefix )
15181539 log .Printf (prefix + "(running)" )
15191540
15201541 params , err := lsp .ReadParams (req .Method , req .Params )
0 commit comments