@@ -984,9 +984,29 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
984984 }
985985 }
986986
987- if clangTextDocItem , err := ls .ino2cppTextDocumentItem (logger , ideTextDocItem ); err != nil {
987+ clangURI , _ , err := ls .ide2ClangDocumentURI (logger , ideTextDocItem .URI )
988+ if err != nil {
988989 logger .Logf ("Error: %s" , err )
989- } else if err := ls .Clangd .conn .TextDocumentDidOpen (& lsp.DidOpenTextDocumentParams {
990+ return
991+ }
992+ clangTextDocItem := lsp.TextDocumentItem {
993+ URI : clangURI ,
994+ }
995+ if ls .clangURIRefersToIno (clangURI ) {
996+ clangTextDocItem .LanguageID = "cpp"
997+ clangTextDocItem .Text = ls .sketchMapper .CppText .Text
998+ clangTextDocItem .Version = ls .sketchMapper .CppText .Version
999+ } else {
1000+ clangText , err := clangURI .AsPath ().ReadFile ()
1001+ if err != nil {
1002+ logger .Logf ("Error opening sketch file %s: %s" , clangURI .AsPath (), err )
1003+ }
1004+ clangTextDocItem .LanguageID = ideTextDocItem .LanguageID
1005+ clangTextDocItem .Version = ideTextDocItem .Version
1006+ clangTextDocItem .Text = string (clangText )
1007+ }
1008+
1009+ if err := ls .Clangd .conn .TextDocumentDidOpen (& lsp.DidOpenTextDocumentParams {
9901010 TextDocument : clangTextDocItem ,
9911011 }); err != nil {
9921012 // Exit the process and trigger a restart by the client in case of a severe error
@@ -1092,47 +1112,32 @@ func (ls *INOLanguageServer) TextDocumentDidChangeNotifFromIDE(logger jsonrpc.Fu
10921112 }
10931113}
10941114
1095- func (ls * INOLanguageServer ) TextDocumentDidSaveNotifFromIDE (logger jsonrpc.FunctionLogger , inoParams * lsp.DidSaveTextDocumentParams ) {
1115+ func (ls * INOLanguageServer ) TextDocumentDidSaveNotifFromIDE (logger jsonrpc.FunctionLogger , ideParams * lsp.DidSaveTextDocumentParams ) {
10961116 ls .writeLock (logger , true )
10971117 defer ls .writeUnlock (logger )
10981118
1099- ls .triggerRebuild ()
1119+ // clangd looks in the build directory (where a copy of the preprocessed sketch resides)
1120+ // so we will not forward notification on saves in the sketch folder.
1121+ logger .Logf ("notification is not forwarded to clang" )
11001122
1101- logger .Logf ("didSave(%s) hasText=%v" , inoParams .TextDocument , inoParams .Text != "" )
1102- if cppTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , inoParams .TextDocument ); err != nil {
1103- logger .Logf ("--E Error: %s" , err )
1104- } else if cppTextDocument .URI .AsPath ().EquivalentTo (ls .buildSketchCpp ) {
1105- logger .Logf (" didSave(%s) equals %s" , cppTextDocument , ls .buildSketchCpp )
1106- logger .Logf (" the notification will be not forwarded to clangd" )
1107- } else {
1108- logger .Logf ("LS --> CL NOTIF didSave(%s)" , cppTextDocument )
1109- if err := ls .Clangd .conn .TextDocumentDidSave (& lsp.DidSaveTextDocumentParams {
1110- TextDocument : cppTextDocument ,
1111- Text : inoParams .Text ,
1112- }); err != nil {
1113- // Exit the process and trigger a restart by the client in case of a severe error
1114- logger .Logf ("Connection error with clangd server: %v" , err )
1115- logger .Logf ("Please restart the language server." )
1116- ls .Close ()
1117- }
1118- }
1123+ ls .triggerRebuild ()
11191124}
11201125
1121- func (ls * INOLanguageServer ) TextDocumentDidCloseNotifFromIDE (logger jsonrpc.FunctionLogger , inoParams * lsp.DidCloseTextDocumentParams ) {
1126+ func (ls * INOLanguageServer ) TextDocumentDidCloseNotifFromIDE (logger jsonrpc.FunctionLogger , ideParams * lsp.DidCloseTextDocumentParams ) {
11221127 ls .writeLock (logger , true )
11231128 defer ls .writeUnlock (logger )
11241129
11251130 ls .triggerRebuild ()
11261131
1127- logger .Logf ("didClose(%s)" , inoParams .TextDocument )
1132+ logger .Logf ("didClose(%s)" , ideParams .TextDocument )
11281133
1129- if cppParams , err := ls .didClose (logger , inoParams ); err != nil {
1134+ if clangParams , err := ls .didClose (logger , ideParams ); err != nil {
11301135 logger .Logf ("--E Error: %s" , err )
1131- } else if cppParams == nil {
1136+ } else if clangParams == nil {
11321137 logger .Logf ("--X Notification is not propagated to clangd" )
11331138 } else {
1134- logger .Logf ("--> CL NOTIF didClose(%s)" , cppParams .TextDocument )
1135- if err := ls .Clangd .conn .TextDocumentDidClose (cppParams ); err != nil {
1139+ logger .Logf ("--> CL NOTIF didClose(%s)" , clangParams .TextDocument )
1140+ if err := ls .Clangd .conn .TextDocumentDidClose (clangParams ); err != nil {
11361141 // Exit the process and trigger a restart by the client in case of a severe error
11371142 logger .Logf ("Error sending notification to clangd server: %v" , err )
11381143 logger .Logf ("Please restart the language server." )
@@ -1314,8 +1319,8 @@ func (ls *INOLanguageServer) extractDataFolderFromArduinoCLI(logger jsonrpc.Func
13141319 }
13151320}
13161321
1317- func (ls * INOLanguageServer ) didClose (logger jsonrpc.FunctionLogger , inoDidClose * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
1318- inoIdentifier := inoDidClose .TextDocument
1322+ func (ls * INOLanguageServer ) didClose (logger jsonrpc.FunctionLogger , ideParams * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
1323+ inoIdentifier := ideParams .TextDocument
13191324 if _ , exist := ls .trackedIdeDocs [inoIdentifier .URI .AsPath ().String ()]; exist {
13201325 delete (ls .trackedIdeDocs , inoIdentifier .URI .AsPath ().String ())
13211326 } else {
@@ -1340,27 +1345,6 @@ func (ls *INOLanguageServer) didClose(logger jsonrpc.FunctionLogger, inoDidClose
13401345 }, err
13411346}
13421347
1343- func (ls * INOLanguageServer ) ino2cppTextDocumentItem (logger jsonrpc.FunctionLogger , inoItem lsp.TextDocumentItem ) (cppItem lsp.TextDocumentItem , err error ) {
1344- cppURI , err := ls .ide2ClangDocumentURI (logger , inoItem .URI )
1345- if err != nil {
1346- return cppItem , err
1347- }
1348- cppItem .URI = cppURI
1349-
1350- if cppURI .AsPath ().EquivalentTo (ls .buildSketchCpp ) {
1351- cppItem .LanguageID = "cpp"
1352- cppItem .Text = ls .sketchMapper .CppText .Text
1353- cppItem .Version = ls .sketchMapper .CppText .Version
1354- } else {
1355- cppItem .LanguageID = inoItem .LanguageID
1356- inoPath := inoItem .URI .AsPath ().String ()
1357- cppItem .Text = ls .trackedIdeDocs [inoPath ].Text
1358- cppItem .Version = ls .trackedIdeDocs [inoPath ].Version
1359- }
1360-
1361- return cppItem , nil
1362- }
1363-
13641348func (ls * INOLanguageServer ) clang2IdeCodeAction (logger jsonrpc.FunctionLogger , clangCodeAction lsp.CodeAction , origIdeURI lsp.DocumentURI ) * lsp.CodeAction {
13651349 ideCodeAction := & lsp.CodeAction {
13661350 Title : clangCodeAction .Title ,
0 commit comments