@@ -24,30 +24,13 @@ import (
2424 "go.bug.st/lsp/textedits"
2525)
2626
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-
4427// INOLanguageServer is a JSON-RPC handler that delegates messages to clangd.
4528type INOLanguageServer struct {
29+ config * Config
4630 IDE * IDELSPServer
4731 Clangd * ClangdLSPClient
4832
49- progressHandler * ProgressProxyHandler
50-
33+ progressHandler * ProgressProxyHandler
5134 closing chan bool
5235 clangdStarted * sync.Cond
5336 dataMux sync.RWMutex
@@ -62,20 +45,16 @@ type INOLanguageServer struct {
6245 trackedInoDocs map [string ]lsp.TextDocumentItem
6346 inoDocsWithDiagnostics map [lsp.DocumentURI ]bool
6447 sketchRebuilder * SketchRebuilder
65-
66- config BoardConfig
6748}
6849
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
7958}
8059
8160var yellow = color .New (color .FgHiYellow )
@@ -129,17 +108,13 @@ func (ls *INOLanguageServer) readUnlock(logger jsonrpc.FunctionLogger) {
129108}
130109
131110// 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 {
133112 logger := NewLSPFunctionLogger (color .HiWhiteString , "LS: " )
134113 ls := & INOLanguageServer {
135114 trackedInoDocs : map [string ]lsp.TextDocumentItem {},
136115 inoDocsWithDiagnostics : map [lsp.DocumentURI ]bool {},
137116 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 ,
143118 }
144119 ls .clangdStarted = sync .NewCond (& ls .dataMux )
145120 ls .sketchRebuilder = NewSketchBuilder (ls )
@@ -157,12 +132,10 @@ func NewINOLanguageServer(stdin io.Reader, stdout io.Writer, board Board) *INOLa
157132 ls .buildSketchRoot = ls .buildPath .Join ("sketch" )
158133 }
159134
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" ))
166139
167140 ls .IDE = NewIDELSPServer (logger , stdin , stdout , ls )
168141 ls .progressHandler = NewProgressProxy (ls .IDE .conn )
@@ -210,14 +183,14 @@ func (ls *INOLanguageServer) InitializeReqFromIDE(ctx context.Context, logger js
210183 }
211184
212185 // Retrieve data folder
213- dataFolder , err := extractDataFolderFromArduinoCLI (logger )
186+ dataFolder , err := ls . extractDataFolderFromArduinoCLI (logger )
214187 if err != nil {
215188 logger .Logf ("error retrieving data folder from arduino-cli: %s" , err )
216189 return
217190 }
218191
219192 // Start clangd
220- ls .Clangd = NewClangdLSPClient (logger , ls . buildPath , ls . buildSketchCpp , dataFolder , ls )
193+ ls .Clangd = NewClangdLSPClient (logger , dataFolder , ls )
221194 go func () {
222195 defer streams .CatchAndLogPanic ()
223196 ls .Clangd .Run ()
@@ -1043,10 +1016,10 @@ func (ls *INOLanguageServer) CleanUp() {
10431016 }
10441017}
10451018
1046- func extractDataFolderFromArduinoCLI (logger jsonrpc.FunctionLogger ) (* paths.Path , error ) {
1019+ func ( ls * INOLanguageServer ) extractDataFolderFromArduinoCLI (logger jsonrpc.FunctionLogger ) (* paths.Path , error ) {
10471020 // 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 () ,
10501023 "config" ,
10511024 "dump" ,
10521025 "--format" , "json" ,
@@ -1806,9 +1779,9 @@ WhitespaceSensitiveMacros: []
18061779 if sketchFormatterConf := ls .sketchRoot .Join (".clang-format" ); sketchFormatterConf .Exist () {
18071780 // If a custom config is present in the sketch folder, use that one
18081781 try (sketchFormatterConf )
1809- } else if globalFormatterConf != nil && globalFormatterConf .Exist () {
1782+ } else if ls . config . FormatterConf != nil && ls . config . FormatterConf .Exist () {
18101783 // Otherwise if a global config file is present, use that one
1811- try (globalFormatterConf )
1784+ try (ls . config . FormatterConf )
18121785 }
18131786
18141787 targetFile := cppuri .AsPath ()
0 commit comments