@@ -3,7 +3,6 @@ package handler
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "encoding/json"
7
6
"fmt"
8
7
"io"
9
8
"log"
@@ -803,7 +802,58 @@ func (handler *InoHandler) cpp2inoSymbolInformation(syms []*lsp.SymbolInformatio
803
802
804
803
// FromClangd handles a message received from clangd.
805
804
func (handler * InoHandler ) FromClangd (ctx context.Context , connection * jsonrpc2.Conn , req * jsonrpc2.Request ) (interface {}, error ) {
806
- params , _ , err := handler .transformParamsToStdio (req .Method , req .Params )
805
+ handler .synchronizer .DataMux .RLock ()
806
+ defer handler .synchronizer .DataMux .RUnlock ()
807
+
808
+ params , err := readParams (req .Method , req .Params )
809
+ if err != nil {
810
+ return nil , errors .WithMessage (err , "parsing JSON message from clangd" )
811
+ }
812
+ if params == nil {
813
+ // passthrough
814
+ params = req .Params
815
+ }
816
+ switch p := params .(type ) {
817
+ case * lsp.PublishDiagnosticsParams :
818
+ // "textDocument/publishDiagnostics"
819
+ if newPathFromURI (p .URI ).EquivalentTo (handler .buildSketchCpp ) {
820
+ // we should transform back N diagnostics of sketch.cpp.ino into
821
+ // their .ino counter parts (that may span over multiple files...)
822
+
823
+ convertedDiagnostics := map [string ][]lsp.Diagnostic {}
824
+ for _ , cppDiag := range p .Diagnostics {
825
+ inoSource , inoRange := handler .sketchMapper .CppToInoRange (cppDiag .Range )
826
+ inoDiag := cppDiag
827
+ inoDiag .Range = inoRange
828
+ if inoDiags , ok := convertedDiagnostics [inoSource ]; ! ok {
829
+ convertedDiagnostics [inoSource ] = []lsp.Diagnostic {inoDiag }
830
+ } else {
831
+ convertedDiagnostics [inoSource ] = append (inoDiags , inoDiag )
832
+ }
833
+ }
834
+
835
+ // Push back to IDE the converted diagnostics
836
+ for filename , inoDiags := range convertedDiagnostics {
837
+ msg := lsp.PublishDiagnosticsParams {
838
+ URI : pathToURI (filename ),
839
+ Diagnostics : inoDiags ,
840
+ }
841
+ if err := handler .StdioConn .Notify (ctx , req .Method , msg ); err != nil {
842
+ return nil , err
843
+ }
844
+ if enableLogging {
845
+ log .Println ("--> " , req .Method )
846
+ }
847
+ }
848
+
849
+ return nil , err
850
+ }
851
+
852
+ case * ApplyWorkspaceEditParams :
853
+ // "workspace/applyEdit"
854
+ p .Edit = * handler .cpp2inoWorkspaceEdit (& p .Edit )
855
+ }
856
+
807
857
if err != nil {
808
858
log .Println ("From clangd: Method:" , req .Method , "Error:" , err )
809
859
return nil , err
@@ -823,46 +873,6 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
823
873
return result , err
824
874
}
825
875
826
- func (handler * InoHandler ) transformParamsToStdio (method string , raw * json.RawMessage ) (params interface {}, uri lsp.DocumentURI , err error ) {
827
- handler .synchronizer .DataMux .RLock ()
828
- defer handler .synchronizer .DataMux .RUnlock ()
829
-
830
- params , err = readParams (method , raw )
831
- if err != nil {
832
- return
833
- } else if params == nil {
834
- params = raw
835
- return
836
- }
837
- switch method {
838
- case "textDocument/publishDiagnostics" :
839
- p := params .(* lsp.PublishDiagnosticsParams )
840
- uri = p .URI
841
- err = handler .cpp2inoPublishDiagnosticsParams (p )
842
- case "workspace/applyEdit" :
843
- p := params .(* ApplyWorkspaceEditParams )
844
- p .Edit = * handler .cpp2inoWorkspaceEdit (& p .Edit )
845
- }
846
- return
847
- }
848
-
849
- func (handler * InoHandler ) cpp2inoPublishDiagnosticsParams (params * lsp.PublishDiagnosticsParams ) error {
850
- if data , ok := handler .data [params .URI ]; ok {
851
- params .URI = data .sourceURI
852
- newDiagnostics := make ([]lsp.Diagnostic , 0 , len (params .Diagnostics ))
853
- for index := range params .Diagnostics {
854
- r := & params .Diagnostics [index ].Range
855
- if _ , startLine , ok := data .sourceMap .CppToInoLineOk (r .Start .Line ); ok {
856
- r .Start .Line = startLine
857
- _ , r .End .Line = data .sourceMap .CppToInoLine (r .End .Line )
858
- newDiagnostics = append (newDiagnostics , params .Diagnostics [index ])
859
- }
860
- }
861
- params .Diagnostics = newDiagnostics
862
- }
863
- return nil
864
- }
865
-
866
876
func (handler * InoHandler ) parseCommandArgument (rawArg interface {}) interface {} {
867
877
if m1 , ok := rawArg .(map [string ]interface {}); ok && len (m1 ) == 1 && m1 ["changes" ] != nil {
868
878
m2 := m1 ["changes" ].(map [string ]interface {})
0 commit comments