Skip to content

Commit 79df7c1

Browse files
authored
Merge pull request #48 from bcmi-labs/patch-1
Stability patchset 1
2 parents 8e05a86 + a01225c commit 79df7c1

File tree

6 files changed

+354
-103
lines changed

6 files changed

+354
-103
lines changed

Diff for: handler/builder.go

+50-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"log"
78
"strings"
@@ -10,6 +11,7 @@ import (
1011
"github.com/arduino/arduino-cli/arduino/libraries"
1112
"github.com/arduino/arduino-cli/executils"
1213
"github.com/arduino/go-paths-helper"
14+
"github.com/bcmi-labs/arduino-language-server/lsp"
1315
"github.com/bcmi-labs/arduino-language-server/streams"
1416
"github.com/pkg/errors"
1517
)
@@ -50,9 +52,53 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
5052
}
5153

5254
// Regenerate preprocessed sketch!
55+
done := make(chan bool)
56+
go func() {
57+
{
58+
// Request a new progress token
59+
req := &lsp.WorkDoneProgressCreateParams{Token: "arduinoLanguageServerRebuild"}
60+
var resp lsp.WorkDoneProgressCreateResult
61+
if err := handler.StdioConn.Call(context.Background(), "window/workDoneProgress/create", req, &resp, nil); err != nil {
62+
log.Printf(" !!! could not create report progress: %s", err)
63+
<-done
64+
return
65+
}
66+
}
67+
68+
req := &lsp.ProgressParams{Token: "arduinoLanguageServerRebuild"}
69+
req.Value = lsp.WorkDoneProgressBegin{
70+
Title: "Building sketch",
71+
}
72+
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
73+
log.Printf(" !!! could not report progress: %s", err)
74+
}
75+
count := 0
76+
dots := []string{".", "..", "..."}
77+
for {
78+
select {
79+
case <-time.After(time.Millisecond * 400):
80+
msg := "compiling" + dots[count%3]
81+
count++
82+
req.Value = lsp.WorkDoneProgressReport{Message: &msg}
83+
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
84+
log.Printf(" !!! could not report progress: %s", err)
85+
}
86+
case <-done:
87+
msg := "done"
88+
req.Value = lsp.WorkDoneProgressEnd{Message: &msg}
89+
if err := handler.StdioConn.Notify(context.Background(), "$/progress", req, nil); err != nil {
90+
log.Printf(" !!! could not report progress: %s", err)
91+
}
92+
return
93+
}
94+
}
95+
}()
96+
5397
handler.synchronizer.DataMux.Lock()
5498
handler.initializeWorkbench(nil)
5599
handler.synchronizer.DataMux.Unlock()
100+
done <- true
101+
close(done)
56102
}
57103
}
58104

@@ -72,13 +118,14 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
72118
}
73119
data.Overrides[rel.String()] = trackedFile.Text
74120
}
75-
var overridesJSON string
121+
var overridesJSON *paths.Path
76122
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
77123
return nil, errors.WithMessage(err, "dumping tracked files")
78124
} else if tmpFile, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
79125
return nil, errors.WithMessage(err, "dumping tracked files")
80126
} else {
81-
overridesJSON = tmpFile.String()
127+
overridesJSON = tmpFile
128+
defer tmpFile.Remove()
82129
}
83130

84131
// XXX: do this from IDE or via gRPC
@@ -87,7 +134,7 @@ func (handler *InoHandler) generateBuildEnvironment() (*paths.Path, error) {
87134
"--fqbn", fqbn,
88135
"--only-compilation-database",
89136
"--clean",
90-
"--source-override", overridesJSON,
137+
"--source-override", overridesJSON.String(),
91138
"--format", "json",
92139
sketchDir.String(),
93140
}

Diff for: handler/handler.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
279279
log.Printf("--> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Position)
280280
inoURI = p.TextDocument.URI
281281
if res, e := handler.ino2cppTextDocumentPositionParams(p); e == nil {
282-
cppURI = p.TextDocument.URI
282+
cppURI = res.TextDocument.URI
283283
params = res
284-
log.Printf(" --> %s(%s:%s)", req.Method, p.TextDocument.URI, p.Position)
284+
log.Printf(" --> %s(%s:%s)", req.Method, res.TextDocument.URI, res.Position)
285285
} else {
286286
err = e
287287
}
@@ -453,7 +453,7 @@ func (handler *InoHandler) refreshCppDocumentSymbols() error {
453453
if err != nil {
454454
return errors.WithMessage(err, "quering source code symbols")
455455
}
456-
result = handler.transformClangdResult("textDocument/documentSymbol", cppURI, "", result)
456+
result = handler.transformClangdResult("textDocument/documentSymbol", cppURI, lsp.NilURI, result)
457457
if symbols, ok := result.([]lsp.DocumentSymbol); !ok {
458458
return errors.WithMessage(err, "quering source code symbols (2)")
459459
} else {
@@ -748,7 +748,7 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
748748
inside, err := inoPath.IsInsideDir(handler.sketchRoot)
749749
if err != nil {
750750
log.Printf(" could not determine if '%s' is inside '%s'", inoPath, handler.sketchRoot)
751-
return "", unknownURI(inoURI)
751+
return lsp.NilURI, unknownURI(inoURI)
752752
}
753753
if !inside {
754754
log.Printf(" passing doc identifier to '%s' as-is", inoPath)
@@ -763,7 +763,7 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
763763
}
764764

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

769769
func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
@@ -791,7 +791,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
791791
inside, err := cppPath.IsInsideDir(handler.buildSketchRoot)
792792
if err != nil {
793793
log.Printf(" could not determine if '%s' is inside '%s'", cppPath, handler.buildSketchRoot)
794-
return "", lsp.Range{}, err
794+
return lsp.NilURI, lsp.Range{}, err
795795
}
796796
if !inside {
797797
log.Printf(" keep doc identifier to '%s' as-is", cppPath)
@@ -806,7 +806,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
806806
}
807807

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

812812
func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.TextDocumentPositionParams) (*lsp.TextDocumentPositionParams, error) {
@@ -833,7 +833,7 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(inoParams *lsp.Text
833833
func (handler *InoHandler) ino2cppRange(inoURI lsp.DocumentURI, inoRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
834834
cppURI, err := handler.ino2cppDocumentURI(inoURI)
835835
if err != nil {
836-
return "", lsp.Range{}, err
836+
return lsp.NilURI, lsp.Range{}, err
837837
}
838838
if cppURI.AsPath().EquivalentTo(handler.buildSketchCpp) {
839839
cppRange := handler.sketchMapper.InoToCppLSPRange(inoURI, inoRange)
@@ -919,7 +919,7 @@ func (handler *InoHandler) ino2cppWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
919919
}
920920

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

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

14251425
func unknownURI(uri lsp.DocumentURI) error {
1426-
return errors.New("Document is not available: " + string(uri))
1426+
return errors.New("Document is not available: " + uri.String())
14271427
}

Diff for: lsp/protocol_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func TestDocumentSymbolParse(t *testing.T) {
6666
}
6767

6868
func TestVariousMessages(t *testing.T) {
69+
x := &ProgressParams{
70+
Token: "token",
71+
Value: WorkDoneProgressBegin{
72+
Title: "some work",
73+
},
74+
}
75+
data, err := json.Marshal(&x)
76+
require.NoError(t, err)
77+
require.JSONEq(t, `{"token":"token", "value":{"kind":"begin","title":"some work"}}`, string(data))
78+
6979
msg := `{
7080
"capabilities":{
7181
"codeActionProvider":{
@@ -107,6 +117,6 @@ func TestVariousMessages(t *testing.T) {
107117
},
108118
"serverInfo":{"name":"clangd","version":"clangd version 11.0.0 (https://github.com/llvm/llvm-project 176249bd6732a8044d457092ed932768724a6f06)"}}`
109119
var init InitializeResult
110-
err := json.Unmarshal([]byte(msg), &init)
120+
err = json.Unmarshal([]byte(msg), &init)
111121
require.NoError(t, err)
112122
}

0 commit comments

Comments
 (0)