Skip to content

Stability patchset 1 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"bytes"
"context"
"encoding/json"
"log"
"strings"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/go-paths-helper"
"github.com/bcmi-labs/arduino-language-server/lsp"
"github.com/bcmi-labs/arduino-language-server/streams"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -50,9 +52,53 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
}

// Regenerate preprocessed sketch!
done := make(chan bool)
go func() {
{
// Request a new progress token
req := &lsp.WorkDoneProgressCreateParams{Token: "arduinoLanguageServerRebuild"}
var resp lsp.WorkDoneProgressCreateResult
if err := handler.StdioConn.Call(context.Background(), "window/workDoneProgress/create", req, &resp, nil); err != nil {
log.Printf(" !!! could not create report progress: %s", err)
<-done
return
}
}

req := &lsp.ProgressParams{Token: "arduinoLanguageServerRebuild"}
req.Value = lsp.WorkDoneProgressBegin{
Title: "Building sketch",
}
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
log.Printf(" !!! could not report progress: %s", err)
}
count := 0
dots := []string{".", "..", "..."}
for {
select {
case <-time.After(time.Millisecond * 400):
msg := "compiling" + dots[count%3]
count++
req.Value = lsp.WorkDoneProgressReport{Message: &msg}
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
log.Printf(" !!! could not report progress: %s", err)
}
case <-done:
msg := "done"
req.Value = lsp.WorkDoneProgressEnd{Message: &msg}
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
log.Printf(" !!! could not report progress: %s", err)
}
return
}
}
}()

handler.synchronizer.DataMux.Lock()
handler.initializeWorkbench(nil)
handler.synchronizer.DataMux.Unlock()
done <- true
close(done)
}
}

Expand All @@ -72,13 +118,14 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
}
data.Overrides[rel.String()] = trackedFile.Text
}
var overridesJSON string
var overridesJSON *paths.Path
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
return nil, errors.WithMessage(err, "dumping tracked files")
} else if tmpFile, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
return nil, errors.WithMessage(err, "dumping tracked files")
} else {
overridesJSON = tmpFile.String()
overridesJSON = tmpFile
defer tmpFile.Remove()
}

// XXX: do this from IDE or via gRPC
Expand All @@ -87,7 +134,7 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
"--fqbn", fqbn,
"--only-compilation-database",
"--clean",
"--source-override", overridesJSON,
"--source-override", overridesJSON.String(),
"--format", "json",
sketchDir.String(),
}
Expand Down
20 changes: 10 additions & 10 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
log.Printf("--> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Position)
inoURI = p.TextDocument.URI
if res, e := handler.ino2cppTextDocumentPositionParams(p); e == nil {
cppURI = p.TextDocument.URI
cppURI = res.TextDocument.URI
params = res
log.Printf(" --> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Position)
log.Printf(" --> %s(%s:%s)", req.Method, res.TextDocument.URI, res.Position)
} else {
err = e
}
Expand Down Expand Up @@ -453,7 +453,7 @@ func (handler *InoHandler) refreshCppDocumentSymbols() error {
if err != nil {
return errors.WithMessage(err, "quering source code symbols")
}
result = handler.transformClangdResult("textDocument/documentSymbol", cppURI, "", result)
result = handler.transformClangdResult("textDocument/documentSymbol", cppURI, lsp.NilURI, result)
if symbols, ok := result.([]lsp.DocumentSymbol); !ok {
return errors.WithMessage(err, "quering source code symbols (2)")
} else {
Expand Down Expand Up @@ -748,7 +748,7 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
inside, err := inoPath.IsInsideDir(handler.sketchRoot)
if err != nil {
log.Printf(" could not determine if '%s' is inside '%s'", inoPath, handler.sketchRoot)
return "", unknownURI(inoURI)
return lsp.NilURI, unknownURI(inoURI)
}
if !inside {
log.Printf(" passing doc identifier to '%s' as-is", inoPath)
Expand All @@ -763,7 +763,7 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
}

log.Printf(" could not determine rel-path of '%s' in '%s': %s", inoPath, handler.sketchRoot, err)
return "", err
return lsp.NilURI, err
}

func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
Expand Down Expand Up @@ -791,7 +791,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
inside, err := cppPath.IsInsideDir(handler.buildSketchRoot)
if err != nil {
log.Printf(" could not determine if '%s' is inside '%s'", cppPath, handler.buildSketchRoot)
return "", lsp.Range{}, err
return lsp.NilURI, lsp.Range{}, err
}
if !inside {
log.Printf(" keep doc identifier to '%s' as-is", cppPath)
Expand All @@ -806,7 +806,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
}

log.Printf(" could not determine rel-path of '%s' in '%s': %s", cppPath, handler.buildSketchRoot, err)
return "", lsp.Range{}, err
return lsp.NilURI, lsp.Range{}, err
}

func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.TextDocumentPositionParams) (*lsp.TextDocumentPositionParams, error) {
Expand All @@ -833,7 +833,7 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.Text
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
return lsp.NilURI, lsp.Range{}, err
}
if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) {
cppRange := handler.sketchMapper.InoToCppLSPRange(inoURI, inoRange)
Expand Down Expand Up @@ -919,7 +919,7 @@ func (handler *InoHandler) ino2cppWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
}

func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI lsp.DocumentURI, result interface{}) interface{} {
cppToIno := inoURI != "" && inoURI.AsPath().EquivalentTo(handler.buildSketchCpp)
cppToIno := inoURI != lsp.NilURI && inoURI.AsPath().EquivalentTo(handler.buildSketchCpp)

switch r := result.(type) {
case *lsp.Hover:
Expand Down Expand Up @@ -1423,5 +1423,5 @@ func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageT
}

func unknownURI(uri lsp.DocumentURI) error {
return errors.New("Document is not available: " + string(uri))
return errors.New("Document is not available: " + uri.String())
}
12 changes: 11 additions & 1 deletion lsp/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func TestDocumentSymbolParse(t *testing.T) {
}

func TestVariousMessages(t *testing.T) {
x := &ProgressParams{
Token: "token",
Value: WorkDoneProgressBegin{
Title: "some work",
},
}
data, err := json.Marshal(&x)
require.NoError(t, err)
require.JSONEq(t, `{"token":"token", "value":{"kind":"begin","title":"some work"}}`, string(data))

msg := `{
"capabilities":{
"codeActionProvider":{
Expand Down Expand Up @@ -107,6 +117,6 @@ func TestVariousMessages(t *testing.T) {
},
"serverInfo":{"name":"clangd","version":"clangd version 11.0.0 (https://github.com/llvm/llvm-project 176249bd6732a8044d457092ed932768724a6f06)"}}`
var init InitializeResult
err := json.Unmarshal([]byte(msg), &init)
err = json.Unmarshal([]byte(msg), &init)
require.NoError(t, err)
}
Loading