Skip to content

Commit a6563f3

Browse files
authored
Merge pull request #43 from bcmi-labs/other-fixes
Other fixes
2 parents d445e1d + b6edaee commit a6563f3

File tree

1 file changed

+94
-29
lines changed

1 file changed

+94
-29
lines changed

Diff for: handler/handler.go

+94-29
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,30 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
149149
inoURI = p.TextDocument.URI
150150
log.Printf("--> didOpen(%s@%d as '%s')", p.TextDocument.URI, p.TextDocument.Version, p.TextDocument.LanguageID)
151151

152-
res, err := handler.didOpen(ctx, p)
153-
154-
if res == nil {
152+
if res, e := handler.didOpen(p); e != nil {
153+
params = nil
154+
err = e
155+
} else if res == nil {
155156
log.Println(" --X notification is not propagated to clangd")
156-
return nil, err // do not propagate to clangd
157+
return nil, nil // do not propagate to clangd
158+
} else {
159+
log.Printf(" --> didOpen(%s@%d as '%s')", res.TextDocument.URI, res.TextDocument.Version, p.TextDocument.LanguageID)
160+
params = res
157161
}
158162

159-
log.Printf(" --> didOpen(%s@%d as '%s')", res.TextDocument.URI, res.TextDocument.Version, p.TextDocument.LanguageID)
160-
params = res
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+
}
161176

162177
case *lsp.DidChangeTextDocumentParams:
163178
// notification "textDocument/didChange"
@@ -243,6 +258,18 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
243258
cppURI = p.TextDocument.URI
244259
log.Printf(" --> formatting(%s)", p.TextDocument.URI)
245260

261+
case *lsp.DocumentRangeFormattingParams:
262+
// Method: "textDocument/rangeFormatting"
263+
log.Printf("--> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Range)
264+
inoURI = p.TextDocument.URI
265+
if cppParams, e := handler.ino2cppDocumentRangeFormattingParams(p); e == nil {
266+
params = cppParams
267+
cppURI = cppParams.TextDocument.URI
268+
log.Printf(" --> %s(%s:%s)", req.Method, cppParams.TextDocument.URI, cppParams.Range)
269+
} else {
270+
err = e
271+
}
272+
246273
case *lsp.TextDocumentPositionParams:
247274
// Method: "textDocument/signatureHelp"
248275
// Method: "textDocument/definition"
@@ -259,27 +286,23 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
259286
err = e
260287
}
261288

262-
case *lsp.DidSaveTextDocumentParams: // "textDocument/didSave":
263-
log.Printf("--X " + req.Method)
264-
return nil, nil
289+
case *lsp.DidSaveTextDocumentParams:
290+
// Method: "textDocument/didSave"
291+
log.Printf("--> %s(%s)", req.Method, p.TextDocument.URI)
265292
inoURI = p.TextDocument.URI
266293
p.TextDocument, err = handler.ino2cppTextDocumentIdentifier(p.TextDocument)
267-
case *lsp.DidCloseTextDocumentParams: // "textDocument/didClose":
268-
log.Printf("--X " + req.Method)
269-
return nil, nil
270-
// uri = p.TextDocument.URI
271-
// err = handler.sketchToBuildPathTextDocumentIdentifier(&p.TextDocument)
272-
// handler.deleteFileData(uri)
294+
cppURI = p.TextDocument.URI
295+
if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) {
296+
log.Printf(" --| didSave not forwarded to clangd")
297+
return nil, nil
298+
}
299+
log.Printf(" --> %s(%s)", req.Method, p.TextDocument.URI)
300+
273301
case *lsp.ReferenceParams: // "textDocument/references":
274302
log.Printf("--X " + req.Method)
275303
return nil, nil
276304
inoURI = p.TextDocument.URI
277305
_, err = handler.ino2cppTextDocumentPositionParams(&p.TextDocumentPositionParams)
278-
case *lsp.DocumentRangeFormattingParams: // "textDocument/rangeFormatting":
279-
log.Printf("--X " + req.Method)
280-
return nil, nil
281-
inoURI = p.TextDocument.URI
282-
err = handler.ino2cppDocumentRangeFormattingParams(p)
283306
case *lsp.DocumentOnTypeFormattingParams: // "textDocument/onTypeFormatting":
284307
log.Printf("--X " + req.Method)
285308
return nil, nil
@@ -516,7 +539,7 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
516539
}
517540
}
518541

519-
func (handler *InoHandler) didOpen(ctx context.Context, inoDidOpen *lsp.DidOpenTextDocumentParams) (*lsp.DidOpenTextDocumentParams, error) {
542+
func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*lsp.DidOpenTextDocumentParams, error) {
520543
// Add the TextDocumentItem in the tracked files list
521544
inoItem := inoDidOpen.TextDocument
522545
handler.docs[inoItem.URI] = &inoItem
@@ -541,6 +564,32 @@ func (handler *InoHandler) didOpen(ctx context.Context, inoDidOpen *lsp.DidOpenT
541564
}, err
542565
}
543566

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+
544593
func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem) (cppItem lsp.TextDocumentItem, err error) {
545594
cppURI, err := handler.ino2cppDocumentURI(inoItem.URI)
546595
if err != nil {
@@ -771,14 +820,30 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.Text
771820
}, nil
772821
}
773822

774-
func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.DocumentRangeFormattingParams) error {
775-
panic("not implemented")
776-
// handler.sketchToBuildPathTextDocumentIdentifier(&params.TextDocument)
777-
// if data, ok := handler.data[params.TextDocument.URI]; ok {
778-
// params.Range = data.sourceMap.InoToCppLSPRange(data.sourceURI, params.Range)
779-
// return nil
780-
// }
781-
return unknownURI(params.TextDocument.URI)
823+
func (handler *InoHandler) ino2cppRange(inoURI lsp.DocumentURI, inoRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
824+
cppURI, err := handler.ino2cppDocumentURI(inoURI)
825+
if err != nil {
826+
return "", lsp.Range{}, err
827+
}
828+
if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) {
829+
cppRange := handler.sketchMapper.InoToCppLSPRange(inoURI, inoRange)
830+
return cppURI, cppRange, nil
831+
}
832+
return cppURI, inoRange, nil
833+
}
834+
835+
func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(inoParams *lsp.DocumentRangeFormattingParams) (*lsp.DocumentRangeFormattingParams, error) {
836+
cppTextDocument, err := handler.ino2cppTextDocumentIdentifier(inoParams.TextDocument)
837+
if err != nil {
838+
return nil, err
839+
}
840+
841+
_, cppRange, err := handler.ino2cppRange(inoParams.TextDocument.URI, inoParams.Range)
842+
return &lsp.DocumentRangeFormattingParams{
843+
TextDocument: cppTextDocument,
844+
Range: cppRange,
845+
Options: inoParams.Options,
846+
}, err
782847
}
783848

784849
func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.DocumentOnTypeFormattingParams) error {

0 commit comments

Comments
 (0)