@@ -160,6 +160,19 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
160160 params = res
161161 }
162162
163+ case * lsp.DidCloseTextDocumentParams :
164+ // Method: "textDocument/didClose"
165+ inoURI = p .TextDocument .URI
166+ log .Printf ("--> didClose(%s)" , p .TextDocument .URI )
167+
168+ if res , e := handler .didClose (p ); e != nil {
169+ } else if res == nil {
170+ log .Println (" --X notification is not propagated to clangd" )
171+ return nil , nil // do not propagate to clangd
172+ } else {
173+ log .Printf (" --> didClose(%s)" , res .TextDocument .URI )
174+ params = res
175+ }
163176
164177 case * lsp.DidChangeTextDocumentParams :
165178 // notification "textDocument/didChange"
@@ -285,12 +298,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
285298 }
286299 log .Printf (" --> %s(%s)" , req .Method , p .TextDocument .URI )
287300
288- case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
289- log .Printf ("--X " + req .Method )
290- return nil , nil
291- // uri = p.TextDocument.URI
292- // err = handler.sketchToBuildPathTextDocumentIdentifier(&p.TextDocument)
293- // handler.deleteFileData(uri)
294301 case * lsp.ReferenceParams : // "textDocument/references":
295302 log .Printf ("--X " + req .Method )
296303 return nil , nil
@@ -557,6 +564,32 @@ func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*
557564 }, err
558565}
559566
567+ func (handler * InoHandler ) didClose (inoDidClose * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
568+ inoIdentifier := inoDidClose .TextDocument
569+ if _ , exist := handler .docs [inoIdentifier .URI ]; exist {
570+ delete (handler .docs , inoIdentifier .URI )
571+ } else {
572+ log .Printf (" didClose of untracked document: %s" , inoIdentifier .URI )
573+ return nil , unknownURI (inoIdentifier .URI )
574+ }
575+
576+ // If we are tracking a .ino...
577+ if inoIdentifier .URI .Ext () == ".ino" {
578+ handler .sketchTrackedFilesCount --
579+ log .Printf (" decreasing .ino tracked files count: %d" , handler .sketchTrackedFilesCount )
580+
581+ // notify clang that sketchCpp has been close only once all .ino are closed
582+ if handler .sketchTrackedFilesCount != 0 {
583+ return nil , nil
584+ }
585+ }
586+
587+ cppIdentifier , err := handler .ino2cppTextDocumentIdentifier (inoIdentifier )
588+ return & lsp.DidCloseTextDocumentParams {
589+ TextDocument : cppIdentifier ,
590+ }, err
591+ }
592+
560593func (handler * InoHandler ) ino2cppTextDocumentItem (inoItem lsp.TextDocumentItem ) (cppItem lsp.TextDocumentItem , err error ) {
561594 cppURI , err := handler .ino2cppDocumentURI (inoItem .URI )
562595 if err != nil {
0 commit comments