@@ -24,30 +24,13 @@ import (
24
24
"go.bug.st/lsp/textedits"
25
25
)
26
26
27
- var globalCliPath string
28
- var globalCliConfigPath string
29
- var globalClangdPath string
30
- var globalFormatterConf * paths.Path
31
- var enableLogging bool
32
-
33
- // Setup initializes global variables.
34
- func Setup (cliPath , cliConfigPath , clangdPath , formatFilePath string , _enableLogging bool ) {
35
- globalCliPath = cliPath
36
- globalCliConfigPath = cliConfigPath
37
- globalClangdPath = clangdPath
38
- if formatFilePath != "" {
39
- globalFormatterConf = paths .New (formatFilePath )
40
- }
41
- enableLogging = _enableLogging
42
- }
43
-
44
27
// INOLanguageServer is a JSON-RPC handler that delegates messages to clangd.
45
28
type INOLanguageServer struct {
29
+ config * Config
46
30
IDE * IDELSPServer
47
31
Clangd * ClangdLSPClient
48
32
49
- progressHandler * ProgressProxyHandler
50
-
33
+ progressHandler * ProgressProxyHandler
51
34
closing chan bool
52
35
clangdStarted * sync.Cond
53
36
dataMux sync.RWMutex
@@ -62,20 +45,16 @@ type INOLanguageServer struct {
62
45
trackedInoDocs map [string ]lsp.TextDocumentItem
63
46
inoDocsWithDiagnostics map [lsp.DocumentURI ]bool
64
47
sketchRebuilder * SketchRebuilder
65
-
66
- config BoardConfig
67
48
}
68
49
69
- // BoardConfig describes the board and port selected by the user.
70
- type BoardConfig struct {
71
- SelectedBoard Board `json:"selectedBoard"`
72
- SelectedPort string `json:"selectedPort"`
73
- }
74
-
75
- // Board structure.
76
- type Board struct {
77
- Name string `json:"name"`
78
- Fqbn string `json:"fqbn"`
50
+ // Config describes the language server configuration.
51
+ type Config struct {
52
+ Fqbn string
53
+ CliPath * paths.Path
54
+ CliConfigPath * paths.Path
55
+ ClangdPath * paths.Path
56
+ FormatterConf * paths.Path
57
+ EnableLogging bool
79
58
}
80
59
81
60
var yellow = color .New (color .FgHiYellow )
@@ -129,17 +108,13 @@ func (ls *INOLanguageServer) readUnlock(logger jsonrpc.FunctionLogger) {
129
108
}
130
109
131
110
// NewINOLanguageServer creates and configures an Arduino Language Server.
132
- func NewINOLanguageServer (stdin io.Reader , stdout io.Writer , board Board ) * INOLanguageServer {
111
+ func NewINOLanguageServer (stdin io.Reader , stdout io.Writer , config * Config ) * INOLanguageServer {
133
112
logger := NewLSPFunctionLogger (color .HiWhiteString , "LS: " )
134
113
ls := & INOLanguageServer {
135
114
trackedInoDocs : map [string ]lsp.TextDocumentItem {},
136
115
inoDocsWithDiagnostics : map [lsp.DocumentURI ]bool {},
137
116
closing : make (chan bool ),
138
- // buildSketchSymbolsLoad: make(chan bool, 1),
139
- // buildSketchSymbolsCheck: make(chan bool, 1),
140
- config : BoardConfig {
141
- SelectedBoard : board ,
142
- },
117
+ config : config ,
143
118
}
144
119
ls .clangdStarted = sync .NewCond (& ls .dataMux )
145
120
ls .sketchRebuilder = NewSketchBuilder (ls )
@@ -157,12 +132,10 @@ func NewINOLanguageServer(stdin io.Reader, stdout io.Writer, board Board) *INOLa
157
132
ls .buildSketchRoot = ls .buildPath .Join ("sketch" )
158
133
}
159
134
160
- if enableLogging {
161
- logger .Logf ("Initial board configuration: %s" , board )
162
- logger .Logf ("Language server build path: %s" , ls .buildPath )
163
- logger .Logf ("Language server build sketch root: %s" , ls .buildSketchRoot )
164
- logger .Logf ("Language server compile-commands: %s" , ls .compileCommandsDir .Join ("compile_commands.json" ))
165
- }
135
+ logger .Logf ("Initial board configuration: %s" , ls .config .Fqbn )
136
+ logger .Logf ("Language server build path: %s" , ls .buildPath )
137
+ logger .Logf ("Language server build sketch root: %s" , ls .buildSketchRoot )
138
+ logger .Logf ("Language server compile-commands: %s" , ls .compileCommandsDir .Join ("compile_commands.json" ))
166
139
167
140
ls .IDE = NewIDELSPServer (logger , stdin , stdout , ls )
168
141
ls .progressHandler = NewProgressProxy (ls .IDE .conn )
@@ -210,14 +183,14 @@ func (ls *INOLanguageServer) InitializeReqFromIDE(ctx context.Context, logger js
210
183
}
211
184
212
185
// Retrieve data folder
213
- dataFolder , err := extractDataFolderFromArduinoCLI (logger )
186
+ dataFolder , err := ls . extractDataFolderFromArduinoCLI (logger )
214
187
if err != nil {
215
188
logger .Logf ("error retrieving data folder from arduino-cli: %s" , err )
216
189
return
217
190
}
218
191
219
192
// Start clangd
220
- ls .Clangd = NewClangdLSPClient (logger , ls . buildPath , ls . buildSketchCpp , dataFolder , ls )
193
+ ls .Clangd = NewClangdLSPClient (logger , dataFolder , ls )
221
194
go func () {
222
195
defer streams .CatchAndLogPanic ()
223
196
ls .Clangd .Run ()
@@ -1043,10 +1016,10 @@ func (ls *INOLanguageServer) CleanUp() {
1043
1016
}
1044
1017
}
1045
1018
1046
- func extractDataFolderFromArduinoCLI (logger jsonrpc.FunctionLogger ) (* paths.Path , error ) {
1019
+ func ( ls * INOLanguageServer ) extractDataFolderFromArduinoCLI (logger jsonrpc.FunctionLogger ) (* paths.Path , error ) {
1047
1020
// XXX: do this from IDE or via gRPC
1048
- args := []string {globalCliPath ,
1049
- "--config-file" , globalCliConfigPath ,
1021
+ args := []string {ls . config . CliPath . String () ,
1022
+ "--config-file" , ls . config . CliConfigPath . String () ,
1050
1023
"config" ,
1051
1024
"dump" ,
1052
1025
"--format" , "json" ,
@@ -1806,9 +1779,9 @@ WhitespaceSensitiveMacros: []
1806
1779
if sketchFormatterConf := ls .sketchRoot .Join (".clang-format" ); sketchFormatterConf .Exist () {
1807
1780
// If a custom config is present in the sketch folder, use that one
1808
1781
try (sketchFormatterConf )
1809
- } else if globalFormatterConf != nil && globalFormatterConf .Exist () {
1782
+ } else if ls . config . FormatterConf != nil && ls . config . FormatterConf .Exist () {
1810
1783
// Otherwise if a global config file is present, use that one
1811
- try (globalFormatterConf )
1784
+ try (ls . config . FormatterConf )
1812
1785
}
1813
1786
1814
1787
targetFile := cppuri .AsPath ()
0 commit comments