@@ -27,23 +27,24 @@ import (
27
27
var globalCliPath string
28
28
var globalClangdPath string
29
29
var enableLogging bool
30
- var asyncProcessing bool
31
30
32
31
// Setup initializes global variables.
33
- func Setup (cliPath string , clangdPath string , _enableLogging bool , _asyncProcessing bool ) {
32
+ func Setup (cliPath string , clangdPath string , _enableLogging bool ) {
34
33
globalCliPath = cliPath
35
34
globalClangdPath = clangdPath
36
35
enableLogging = _enableLogging
37
- asyncProcessing = _asyncProcessing
38
36
}
39
37
40
38
// CLangdStarter starts clangd and returns its stdin/out/err
41
39
type CLangdStarter func () (stdin io.WriteCloser , stdout io.ReadCloser , stderr io.ReadCloser )
42
40
43
41
// InoHandler is a JSON-RPC handler that delegates messages to clangd.
44
42
type InoHandler struct {
45
- StdioConn * jsonrpc2.Conn
46
- ClangdConn * jsonrpc2.Conn
43
+ StdioConn * jsonrpc2.Conn
44
+ ClangdConn * jsonrpc2.Conn
45
+
46
+ clangdStarted * sync.Cond
47
+ dataMux sync.RWMutex
47
48
lspInitializeParams * lsp.InitializeParams
48
49
buildPath * paths.Path
49
50
buildSketchRoot * paths.Path
@@ -61,8 +62,7 @@ type InoHandler struct {
61
62
docs map [string ]* lsp.TextDocumentItem
62
63
inoDocsWithDiagnostics map [string ]bool
63
64
64
- config lsp.BoardConfig
65
- synchronizer Synchronizer
65
+ config lsp.BoardConfig
66
66
}
67
67
68
68
// NewInoHandler creates and configures an InoHandler.
@@ -74,15 +74,9 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
74
74
SelectedBoard : board ,
75
75
},
76
76
}
77
-
77
+ handler . clangdStarted = sync . NewCond ( & handler . dataMux )
78
78
stdStream := jsonrpc2 .NewBufferedStream (stdio , jsonrpc2.VSCodeObjectCodec {})
79
79
var stdHandler jsonrpc2.Handler = jsonrpc2 .HandlerWithError (handler .HandleMessageFromIDE )
80
- if asyncProcessing {
81
- stdHandler = AsyncHandler {
82
- handler : stdHandler ,
83
- synchronizer : & handler .synchronizer ,
84
- }
85
- }
86
80
handler .StdioConn = jsonrpc2 .NewConn (context .Background (), stdStream , stdHandler )
87
81
if enableLogging {
88
82
log .Println ("Initial board configuration:" , board )
@@ -118,11 +112,23 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
118
112
"textDocument/didClose" : true ,
119
113
}
120
114
if needsWriteLock [req .Method ] {
121
- handler .synchronizer . DataMux .Lock ()
122
- defer handler .synchronizer . DataMux .Unlock ()
115
+ handler .dataMux .Lock ()
116
+ defer handler .dataMux .Unlock ()
123
117
} else {
124
- handler .synchronizer .DataMux .RLock ()
125
- defer handler .synchronizer .DataMux .RUnlock ()
118
+ handler .dataMux .RLock ()
119
+ defer handler .dataMux .RUnlock ()
120
+ }
121
+
122
+ // Wait for clangd start-up
123
+ doNotNeedClangd := map [string ]bool {
124
+ "initialize" : true ,
125
+ "initialized" : true ,
126
+ }
127
+ if handler .ClangdConn == nil && ! doNotNeedClangd [req .Method ] {
128
+ handler .clangdStarted .Wait ()
129
+ if handler .ClangdConn == nil {
130
+ return nil , errors .New ("could not run clangd, aborted" )
131
+ }
126
132
}
127
133
128
134
// Handle LSP methods: transform parameters and send to clangd
0 commit comments