From b3f97b994d465749fc2ceb3258032d0a1518ff6e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 4 Jan 2021 17:43:38 +0100 Subject: [PATCH 1/4] Minimal didSave implementation --- handler/handler.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 028c4be..becc9c7 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -259,11 +259,18 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr err = e } - case *lsp.DidSaveTextDocumentParams: // "textDocument/didSave": - log.Printf("--X " + req.Method) - return nil, nil + case *lsp.DidSaveTextDocumentParams: + // Method: "textDocument/didSave" + log.Printf("--> %s(%s)", req.Method, p.TextDocument.URI) inoURI = p.TextDocument.URI p.TextDocument, err = handler.ino2cppTextDocumentIdentifier(p.TextDocument) + cppURI = p.TextDocument.URI + if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) { + log.Printf(" --| didSave not forwarded to clangd") + return nil, nil + } + log.Printf(" --> %s(%s)", req.Method, p.TextDocument.URI) + case *lsp.DidCloseTextDocumentParams: // "textDocument/didClose": log.Printf("--X " + req.Method) return nil, nil From 5902ba6f9ced217716b698d1cc46f75a8a784af1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 4 Jan 2021 18:59:44 +0100 Subject: [PATCH 2/4] added rangeFormatting --- handler/handler.go | 49 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index becc9c7..05b8310 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -243,6 +243,18 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr cppURI = p.TextDocument.URI log.Printf(" --> formatting(%s)", p.TextDocument.URI) + case *lsp.DocumentRangeFormattingParams: + // Method: "textDocument/rangeFormatting" + log.Printf("--> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Range) + inoURI = p.TextDocument.URI + if cppParams, e := handler.ino2cppDocumentRangeFormattingParams(p); e == nil { + params = cppParams + cppURI = cppParams.TextDocument.URI + log.Printf(" --> %s(%s:%s)", req.Method, cppParams.TextDocument.URI, cppParams.Range) + } else { + err = e + } + case *lsp.TextDocumentPositionParams: // Method: "textDocument/signatureHelp" // Method: "textDocument/definition" @@ -282,11 +294,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr return nil, nil inoURI = p.TextDocument.URI _, err = handler.ino2cppTextDocumentPositionParams(&p.TextDocumentPositionParams) - case *lsp.DocumentRangeFormattingParams: // "textDocument/rangeFormatting": - log.Printf("--X " + req.Method) - return nil, nil - inoURI = p.TextDocument.URI - err = handler.ino2cppDocumentRangeFormattingParams(p) case *lsp.DocumentOnTypeFormattingParams: // "textDocument/onTypeFormatting": log.Printf("--X " + req.Method) return nil, nil @@ -778,14 +785,30 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.Text }, nil } -func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.DocumentRangeFormattingParams) error { - panic("not implemented") - // handler.sketchToBuildPathTextDocumentIdentifier(¶ms.TextDocument) - // if data, ok := handler.data[params.TextDocument.URI]; ok { - // params.Range = data.sourceMap.InoToCppLSPRange(data.sourceURI, params.Range) - // return nil - // } - return unknownURI(params.TextDocument.URI) +func (handler *InoHandler) ino2cppRange(inoURI lsp.DocumentURI, inoRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) { + cppURI, err := handler.ino2cppDocumentURI(inoURI) + if err != nil { + return "", lsp.Range{}, err + } + if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) { + cppRange := handler.sketchMapper.InoToCppLSPRange(inoURI, inoRange) + return cppURI, cppRange, nil + } + return cppURI, inoRange, nil +} + +func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(inoParams *lsp.DocumentRangeFormattingParams) (*lsp.DocumentRangeFormattingParams, error) { + cppTextDocument, err := handler.ino2cppTextDocumentIdentifier(inoParams.TextDocument) + if err != nil { + return nil, err + } + + _, cppRange, err := handler.ino2cppRange(inoParams.TextDocument.URI, inoParams.Range) + return &lsp.DocumentRangeFormattingParams{ + TextDocument: cppTextDocument, + Range: cppRange, + Options: inoParams.Options, + }, err } func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.DocumentOnTypeFormattingParams) error { From e86003c6ac9c27a55630e2665be2c8bde261046b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 4 Jan 2021 19:15:33 +0100 Subject: [PATCH 3/4] fixed didOpen error return --- handler/handler.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 05b8310..5f0c18f 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -149,15 +149,17 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr inoURI = p.TextDocument.URI log.Printf("--> didOpen(%s@%d as '%s')", p.TextDocument.URI, p.TextDocument.Version, p.TextDocument.LanguageID) - res, err := handler.didOpen(ctx, p) - - if res == nil { + if res, e := handler.didOpen(p); e != nil { + params = nil + err = e + } else if res == nil { log.Println(" --X notification is not propagated to clangd") - return nil, err // do not propagate to clangd + return nil, nil // do not propagate to clangd + } else { + log.Printf(" --> didOpen(%s@%d as '%s')", res.TextDocument.URI, res.TextDocument.Version, p.TextDocument.LanguageID) + params = res } - log.Printf(" --> didOpen(%s@%d as '%s')", res.TextDocument.URI, res.TextDocument.Version, p.TextDocument.LanguageID) - params = res case *lsp.DidChangeTextDocumentParams: // notification "textDocument/didChange" @@ -530,7 +532,7 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io. } } -func (handler *InoHandler) didOpen(ctx context.Context, inoDidOpen *lsp.DidOpenTextDocumentParams) (*lsp.DidOpenTextDocumentParams, error) { +func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*lsp.DidOpenTextDocumentParams, error) { // Add the TextDocumentItem in the tracked files list inoItem := inoDidOpen.TextDocument handler.docs[inoItem.URI] = &inoItem From b6edaee455329d239ece8fa3ec6593459c4c594b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 4 Jan 2021 19:15:43 +0100 Subject: [PATCH 4/4] Implemented didClose message --- handler/handler.go | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 5f0c18f..e07153a 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -160,6 +160,19 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr params = res } + case *lsp.DidCloseTextDocumentParams: + // Method: "textDocument/didClose" + inoURI = p.TextDocument.URI + log.Printf("--> didClose(%s)", p.TextDocument.URI) + + if res, e := handler.didClose(p); e != nil { + } else if res == nil { + log.Println(" --X notification is not propagated to clangd") + return nil, nil // do not propagate to clangd + } else { + log.Printf(" --> didClose(%s)", res.TextDocument.URI) + params = res + } case *lsp.DidChangeTextDocumentParams: // notification "textDocument/didChange" @@ -285,12 +298,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr } log.Printf(" --> %s(%s)", req.Method, p.TextDocument.URI) - case *lsp.DidCloseTextDocumentParams: // "textDocument/didClose": - log.Printf("--X " + req.Method) - return nil, nil - // uri = p.TextDocument.URI - // err = handler.sketchToBuildPathTextDocumentIdentifier(&p.TextDocument) - // handler.deleteFileData(uri) case *lsp.ReferenceParams: // "textDocument/references": log.Printf("--X " + req.Method) return nil, nil @@ -557,6 +564,32 @@ func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (* }, err } +func (handler *InoHandler) didClose(inoDidClose *lsp.DidCloseTextDocumentParams) (*lsp.DidCloseTextDocumentParams, error) { + inoIdentifier := inoDidClose.TextDocument + if _, exist := handler.docs[inoIdentifier.URI]; exist { + delete(handler.docs, inoIdentifier.URI) + } else { + log.Printf(" didClose of untracked document: %s", inoIdentifier.URI) + return nil, unknownURI(inoIdentifier.URI) + } + + // If we are tracking a .ino... + if inoIdentifier.URI.Ext() == ".ino" { + handler.sketchTrackedFilesCount-- + log.Printf(" decreasing .ino tracked files count: %d", handler.sketchTrackedFilesCount) + + // notify clang that sketchCpp has been close only once all .ino are closed + if handler.sketchTrackedFilesCount != 0 { + return nil, nil + } + } + + cppIdentifier, err := handler.ino2cppTextDocumentIdentifier(inoIdentifier) + return &lsp.DidCloseTextDocumentParams{ + TextDocument: cppIdentifier, + }, err +} + func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem) (cppItem lsp.TextDocumentItem, err error) { cppURI, err := handler.ino2cppDocumentURI(inoItem.URI) if err != nil {