1
1
package handler
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
@@ -515,7 +516,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
515
516
return errors .WithMessage (err , "reading generated cpp file from sketch" )
516
517
}
517
518
518
- compilers := examineCompileCommandsJSON (handler .buildPath )
519
+ canonicalizeCompileCommandsJSON (handler .buildPath )
519
520
520
521
if params == nil {
521
522
// If we are restarting re-synchronize clangd
@@ -538,7 +539,11 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
538
539
}
539
540
} else {
540
541
// Otherwise start clangd!
541
- clangdStdout , clangdStdin , clangdStderr := startClangd (handler .buildPath , handler .buildSketchCpp , compilers )
542
+ dataFolder , err := extractDataFolderFromArduinoCLI ()
543
+ if err != nil {
544
+ log .Printf (" error: %s" , err )
545
+ }
546
+ clangdStdout , clangdStdin , clangdStderr := startClangd (handler .buildPath , handler .buildSketchCpp , dataFolder )
542
547
clangdStdio := streams .NewReadWriteCloser (clangdStdin , clangdStdout )
543
548
if enableLogging {
544
549
clangdStdio = streams .LogReadWriteCloserAs (clangdStdio , "inols-clangd.log" )
@@ -572,6 +577,38 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
572
577
return nil
573
578
}
574
579
580
+ func extractDataFolderFromArduinoCLI () (* paths.Path , error ) {
581
+ // XXX: do this from IDE or via gRPC
582
+ args := []string {globalCliPath ,
583
+ "config" ,
584
+ "dump" ,
585
+ "--format" , "json" ,
586
+ }
587
+ cmd , err := executils .NewProcess (args ... )
588
+ if err != nil {
589
+ return nil , errors .Errorf ("running %s: %s" , strings .Join (args , " " ), err )
590
+ }
591
+ cmdOutput := & bytes.Buffer {}
592
+ cmd .RedirectStdoutTo (cmdOutput )
593
+ log .Println ("running: " , strings .Join (args , " " ))
594
+ if err := cmd .Run (); err != nil {
595
+ return nil , errors .Errorf ("running %s: %s" , strings .Join (args , " " ), err )
596
+ }
597
+
598
+ type cmdRes struct {
599
+ Directories struct {
600
+ Data string `json:"data"`
601
+ } `json:"directories"`
602
+ }
603
+ var res cmdRes
604
+ if err := json .Unmarshal (cmdOutput .Bytes (), & res ); err != nil {
605
+ return nil , errors .Errorf ("parsing arduino-cli output: %s" , err )
606
+ }
607
+ // Return only the build path
608
+ log .Println ("Arduino Data Dir -> " , res .Directories .Data )
609
+ return paths .New (res .Directories .Data ), nil
610
+ }
611
+
575
612
func (handler * InoHandler ) refreshCppDocumentSymbols (prefix string ) error {
576
613
// Query source code symbols
577
614
cppURI := lsp .NewDocumentURIFromPath (handler .buildSketchCpp )
@@ -668,7 +705,7 @@ func (handler *InoHandler) CheckCppIncludesChanges() {
668
705
}
669
706
}
670
707
671
- func examineCompileCommandsJSON (compileCommandsDir * paths.Path ) map [string ]bool {
708
+ func canonicalizeCompileCommandsJSON (compileCommandsDir * paths.Path ) map [string ]bool {
672
709
// Open compile_commands.json and find the main cross-compiler executable
673
710
compileCommandsJSONPath := compileCommandsDir .Join ("compile_commands.json" )
674
711
compileCommands , err := builder .LoadCompilationDatabase (compileCommandsJSONPath )
@@ -698,15 +735,15 @@ func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool
698
735
return compilers
699
736
}
700
737
701
- func startClangd (compileCommandsDir , sketchCpp * paths.Path , compilers map [ string ] bool ) (io.WriteCloser , io.ReadCloser , io.ReadCloser ) {
738
+ func startClangd (compileCommandsDir , sketchCpp * paths.Path , dataFolder * paths. Path ) (io.WriteCloser , io.ReadCloser , io.ReadCloser ) {
702
739
// Start clangd
703
740
args := []string {
704
741
globalClangdPath ,
705
742
"-log=verbose" ,
706
743
fmt .Sprintf (`--compile-commands-dir=%s` , compileCommandsDir ),
707
744
}
708
- for compiler := range compilers {
709
- args = append (args , fmt .Sprintf ("-query-driver=%s" , compiler ))
745
+ if dataFolder != nil {
746
+ args = append (args , fmt .Sprintf ("-query-driver=%s" , dataFolder . Join ( "packages" , "**" ) ))
710
747
}
711
748
if enableLogging {
712
749
log .Println (" Starting clangd:" , strings .Join (args , " " ))
0 commit comments