Skip to content

Commit

Permalink
Merge pull request #1971 from sabify/fix-lsp
Browse files Browse the repository at this point in the history
fixed lsp panicing on formatting malformed content
  • Loading branch information
josephschorr authored Jul 7, 2024
2 parents f59b53c + 2eb8486 commit 8c5ace6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/lsp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ func (s *Server) getCompiledContents(path lsp.DocumentURI, files *persistent.Map
}

justCompiled, derr, err := development.CompileSchema(file.contents)
if err != nil || derr != nil {
if err != nil {
return nil, err
}
if derr != nil {
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInternalError, Message: derr.String()}
}

files.Set(path, trackedFile{file.contents, justCompiled}, nil)
return justCompiled, nil
Expand Down
31 changes: 31 additions & 0 deletions internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ func TestDocumentFormat(t *testing.T) {
End: lsp.Position{Line: 10000000, Character: 100000000},
}, resp[0].Range)
require.Equal(t, "definition user {}", resp[0].NewText)

// test formatting malformed content without panicing
tester.setFileContents("file:///test", "dfinition user{}")
err, _ := sendAndExpectError(tester, "textDocument/formatting",
lsp.DocumentFormattingParams{
TextDocument: lsp.TextDocumentIdentifier{URI: "file:///test"},
})
require.Error(t, err)
}

func TestDocumentOpenedClosed(t *testing.T) {
Expand Down Expand Up @@ -204,4 +212,27 @@ definition resource {

require.Equal(t, "definition user {}", resp.Contents.Value)
require.Equal(t, "spicedb", resp.Contents.Language)

// test hovering malformed content without panicing
sendAndReceive[any](tester, "textDocument/didOpen", lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: lsp.DocumentURI("file:///test"),
LanguageID: "test",
Version: 1,
Text: `definition user {}
dfinition resource {
relation viewer: user
}
`,
},
})

err, _ := sendAndExpectError(tester, "textDocument/hover", lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: lsp.DocumentURI("file:///test"),
},
Position: lsp.Position{Line: 3, Character: 18},
})
require.Error(t, err)
}

0 comments on commit 8c5ace6

Please sign in to comment.