Skip to content

Commit

Permalink
Add warnings support to the LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Apr 30, 2024
1 parent ece7ea6 commit 0185e09
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
23 changes: 22 additions & 1 deletion internal/lsp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ func (s *Server) computeDiagnostics(ctx context.Context, uri lsp.DocumentURI) ([
return &jsonrpc2.Error{Code: jsonrpc2.CodeInternalError, Message: "file not found"}
}

_, devErrs, err := development.NewDevContext(ctx, &developerv1.RequestContext{
devCtx, devErrs, err := development.NewDevContext(ctx, &developerv1.RequestContext{
Schema: file.contents,
Relationships: nil,
})
if err != nil {
return err
}

// Get errors.
for _, devErr := range devErrs.GetInputErrors() {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: lsp.Error,
Expand All @@ -76,11 +77,31 @@ func (s *Server) computeDiagnostics(ctx context.Context, uri lsp.DocumentURI) ([
})
}

// If there are no errors, we can also check for warnings.
if len(diagnostics) == 0 {
warnings, err := development.GetWarnings(ctx, devCtx)
if err != nil {
return err
}

for _, devWarning := range warnings {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: lsp.Warning,
Range: lsp.Range{
Start: lsp.Position{Line: int(devWarning.Line) - 1, Character: int(devWarning.Column) - 1},
End: lsp.Position{Line: int(devWarning.Line) - 1, Character: int(devWarning.Column) - 1},
},
Message: devWarning.Message,
})
}
}

return nil
}); err != nil {
return nil, err
}

log.Info().Int("diagnostics", len(diagnostics)).Str("uri", string(uri)).Msg("computed diagnostics")
return diagnostics, nil
}

Expand Down
29 changes: 28 additions & 1 deletion internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDocumentNoDiagnostics(t *testing.T) {
require.Len(t, resp.Items, 0)
}

func TestDocumentDiagnostics(t *testing.T) {
func TestDocumentErrorDiagnostics(t *testing.T) {
tester := newLSPTester(t)
tester.initialize()

Expand Down Expand Up @@ -80,6 +80,33 @@ func TestDocumentDiagnostics(t *testing.T) {
require.Len(t, resp.Items, 0)
}

func TestDocumentWarningDiagnostics(t *testing.T) {
tester := newLSPTester(t)
tester.initialize()

tester.setFileContents("file:///test", `
definition user {}
definition resource {
relation viewer: user
permission view_resource = viewer
}
`)

resp, _ := sendAndReceive[FullDocumentDiagnosticReport](tester, "textDocument/diagnostic",
TextDocumentDiagnosticParams{
TextDocument: TextDocument{URI: "file:///test"},
})
require.Equal(t, "full", resp.Kind)
require.Len(t, resp.Items, 1)
require.Equal(t, lsp.DiagnosticSeverity(lsp.Warning), resp.Items[0].Severity)
require.Equal(t, `Permission "view_resource" references parent type "resource" in its name; it is recommended to drop the suffix`, resp.Items[0].Message)
require.Equal(t, lsp.Range{
Start: lsp.Position{Line: 5, Character: 3},
End: lsp.Position{Line: 5, Character: 3},
}, resp.Items[0].Range)
}

func TestDocumentDiagnosticsForTypeError(t *testing.T) {
tester := newLSPTester(t)
tester.initialize()
Expand Down

0 comments on commit 0185e09

Please sign in to comment.