Skip to content

Commit 441f9f4

Browse files
Add tags to diagnostics
Also removes the diagnostic.Data from the new diagnostics, which AFAICS is unused; we can save ourselves the json marshaling. As a part of this, added a first pass at adding tests for diagnostics - we don't currently have any. These are a bit different because we haven't implemented [PullDiagnostics][1] (we can add this, but we'd need a custom implementation, or wait until [go.lsp.dev upgrades to 3.17][2]). Instead, we intercept the notifications from the server. Lastly, since we don't actually want the tests to wait around, adds synctest and only runs the tests on Go 1.25 for now (1.26 should be released next month and we'll be able to upgrade to 1.25 as our minimum). This is in preparation to eventually landing relatedInformation (other spans) in diagnostics, from bufbuild/protocompile#659. [1]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics [2]: go-language-server/protocol#52
1 parent 08df2b3 commit 441f9f4

File tree

9 files changed

+426
-49
lines changed

9 files changed

+426
-49
lines changed

private/buf/buflsp/buflsp_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,20 @@ func setupLSPServer(
121121

122122
stream := jsonrpc2.NewStream(serverConn)
123123

124-
go func() {
125-
conn, err := buflsp.Serve(
126-
ctx,
127-
"test",
128-
wktBucket,
129-
appextContainer,
130-
controller,
131-
wasmRuntime,
132-
stream,
133-
queryExecutor,
134-
)
135-
if err != nil {
136-
t.Errorf("Failed to start server: %v", err)
137-
return
138-
}
139-
t.Cleanup(func() {
140-
require.NoError(t, conn.Close())
141-
})
142-
<-ctx.Done()
143-
}()
124+
conn, err := buflsp.Serve(
125+
ctx,
126+
"test",
127+
wktBucket,
128+
appextContainer,
129+
controller,
130+
wasmRuntime,
131+
stream,
132+
queryExecutor,
133+
)
134+
require.NoError(t, err)
135+
t.Cleanup(func() {
136+
require.NoError(t, conn.Close())
137+
})
144138

145139
clientStream := jsonrpc2.NewStream(clientConn)
146140
clientJSONConn := jsonrpc2.NewConn(clientStream)

private/buf/buflsp/diagnostic.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
package buflsp
1818

1919
import (
20-
"encoding/json"
21-
"strings"
22-
2320
"github.com/bufbuild/protocompile/experimental/report"
21+
"github.com/bufbuild/protocompile/experimental/report/tags"
2422
"github.com/bufbuild/protocompile/experimental/source/length"
2523
"go.lsp.dev/protocol"
2624
)
@@ -30,14 +28,6 @@ import (
3028
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
3129
const positionalEncoding = length.UTF16
3230

33-
// diagnosticData is a structure to hold the [report.Diagnostic] notes, help, and debug
34-
// messages, to marshal into JSON for the [protocol.Diagnostic].Data field.
35-
type diagnosticData struct {
36-
Notes string `json:"notes,omitempty"`
37-
Help string `json:"help,omitempty"`
38-
Debug string `json:"debug,omitempty"`
39-
}
40-
4131
// reportLevelToDiagnosticSeverity is a mapping of [report.Level] to [protocol.DiagnosticSeverity].
4232
var reportLevelToDiagnosticSeverity = map[report.Level]protocol.DiagnosticSeverity{
4333
report.ICE: protocol.DiagnosticSeverityError,
@@ -70,20 +60,10 @@ func reportDiagnosticToProtocolDiagnostic(
7060
},
7161
}
7262
}
73-
data := diagnosticData{
74-
Notes: strings.Join(reportDiagnostic.Notes(), "\n"),
75-
Help: strings.Join(reportDiagnostic.Help(), "\n"),
76-
Debug: strings.Join(reportDiagnostic.Debug(), "\n"),
77-
}
78-
bytes, err := json.Marshal(data)
79-
if err != nil {
80-
return protocol.Diagnostic{}, err
81-
}
82-
if bytes != nil {
83-
// We serialize the bytes into a string before providing the structure to diagnostic.Data
84-
// because diagnostic.Data is an interface{}, which is treated as a JSON "any", which
85-
// will not cleanly deserialize.
86-
diagnostic.Data = string(bytes)
63+
if reportDiagnostic.Tag() == tags.UnusedImport {
64+
diagnostic.Tags = []protocol.DiagnosticTag{
65+
protocol.DiagnosticTagUnnecessary,
66+
}
8767
}
8868
return diagnostic, nil
8969
}

0 commit comments

Comments
 (0)