@@ -984,9 +984,29 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
984
984
}
985
985
}
986
986
987
- if clangTextDocItem , err := ls .ino2cppTextDocumentItem (logger , ideTextDocItem ); err != nil {
987
+ clangURI , _ , err := ls .ide2ClangDocumentURI (logger , ideTextDocItem .URI )
988
+ if err != nil {
988
989
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 {
990
1010
TextDocument : clangTextDocItem ,
991
1011
}); err != nil {
992
1012
// 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
1092
1112
}
1093
1113
}
1094
1114
1095
- func (ls * INOLanguageServer ) TextDocumentDidSaveNotifFromIDE (logger jsonrpc.FunctionLogger , inoParams * lsp.DidSaveTextDocumentParams ) {
1115
+ func (ls * INOLanguageServer ) TextDocumentDidSaveNotifFromIDE (logger jsonrpc.FunctionLogger , ideParams * lsp.DidSaveTextDocumentParams ) {
1096
1116
ls .writeLock (logger , true )
1097
1117
defer ls .writeUnlock (logger )
1098
1118
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" )
1100
1122
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 ()
1119
1124
}
1120
1125
1121
- func (ls * INOLanguageServer ) TextDocumentDidCloseNotifFromIDE (logger jsonrpc.FunctionLogger , inoParams * lsp.DidCloseTextDocumentParams ) {
1126
+ func (ls * INOLanguageServer ) TextDocumentDidCloseNotifFromIDE (logger jsonrpc.FunctionLogger , ideParams * lsp.DidCloseTextDocumentParams ) {
1122
1127
ls .writeLock (logger , true )
1123
1128
defer ls .writeUnlock (logger )
1124
1129
1125
1130
ls .triggerRebuild ()
1126
1131
1127
- logger .Logf ("didClose(%s)" , inoParams .TextDocument )
1132
+ logger .Logf ("didClose(%s)" , ideParams .TextDocument )
1128
1133
1129
- if cppParams , err := ls .didClose (logger , inoParams ); err != nil {
1134
+ if clangParams , err := ls .didClose (logger , ideParams ); err != nil {
1130
1135
logger .Logf ("--E Error: %s" , err )
1131
- } else if cppParams == nil {
1136
+ } else if clangParams == nil {
1132
1137
logger .Logf ("--X Notification is not propagated to clangd" )
1133
1138
} 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 {
1136
1141
// Exit the process and trigger a restart by the client in case of a severe error
1137
1142
logger .Logf ("Error sending notification to clangd server: %v" , err )
1138
1143
logger .Logf ("Please restart the language server." )
@@ -1314,8 +1319,8 @@ func (ls *INOLanguageServer) extractDataFolderFromArduinoCLI(logger jsonrpc.Func
1314
1319
}
1315
1320
}
1316
1321
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
1319
1324
if _ , exist := ls .trackedIdeDocs [inoIdentifier .URI .AsPath ().String ()]; exist {
1320
1325
delete (ls .trackedIdeDocs , inoIdentifier .URI .AsPath ().String ())
1321
1326
} else {
@@ -1340,27 +1345,6 @@ func (ls *INOLanguageServer) didClose(logger jsonrpc.FunctionLogger, inoDidClose
1340
1345
}, err
1341
1346
}
1342
1347
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
-
1364
1348
func (ls * INOLanguageServer ) clang2IdeCodeAction (logger jsonrpc.FunctionLogger , clangCodeAction lsp.CodeAction , origIdeURI lsp.DocumentURI ) * lsp.CodeAction {
1365
1349
ideCodeAction := & lsp.CodeAction {
1366
1350
Title : clangCodeAction .Title ,
0 commit comments