codemirror file view: Use new /scip highlighting endpoint
Created by: fkling
Based on #39850
This commit makes minimal and additive changes to the frontend and Go backend to make the new /scip highlighting endpoint available through GraphQL.
There are currently three possible scenarios how a file might get highlighted:
- HTML blob view, default: Highlighted HTML is generated by syntect
- HTML blob view, tree sitter: SCIP data is generated by tree sitter, HTML is generated on the client side
- CodeMirror blob view, default: SCIP is generated by either syntect or tree sitter
So far SCIP data was only generated for specific languages, determined
by the server. With CodeMirror, SCIP also needs to be generated when the
client requests it.
My preferred solution would have been to let the server determine this
based on the requested fields in the GraphQL request, but the library we
are using does not support that yet.
Making the highlighting requests in the field resolvers (i.e. HTML()
and LSIF()
) is also not possible without additional changes because
the Aborted()
field depends on the result of the request.
This led me change the formatOnly
field from a boolean to an enum,
with which the client can now request:
HTML_PLAINTEXT
HTML_HIGHLIGHT
JSON_SCIP
It's not ideal because the server can still return SCIP data depending on whether tree sitter is configured for the language (see second bullet point at the beginning) but I think this is still better than introducing a new field for highlighting.
So, if CodeMirror is disabled, everything works as before. When
CodeMirror is enabled it will explicitly request JSON_SCIP
data and
only include the lsif
field in the GraphQL request. The server will
route that request to the new /scip
endpoint.
Demo:
https://user-images.githubusercontent.com/179026/183742538-8bded4b5-fa82-439e-b934-6e33c7b3ca68.mp4
Test plan
- With CodeMirror file view disabled, inspect search results and file views. Highlighting should work as before.
- With CodeMirror file view enabled, inspect search results and file views. Both should be highlighted.
GraphQL response inspection (can be done via devtools and filtering for graphql?Blob
requests:
- With CodeMirror enabled
- Testing treesitter
- Load https://sourcegraph.test:3443/github.com/sourcegraph/sourcegraph@bd796dc/-/blob/docker-images/syntax-highlighter/crates/sg-syntax/src/snapshots/syntect_files/larger.cs
- The blob GraphQL response should include a non-empty
lsif
field and nohtml
field.
- Testing syntec
- Load https://sourcegraph.test:3443/github.com/sourcegraph/sourcegraph@bd796dc/-/blob/docker-images/syntax-highlighter/crates/sg-syntax/src/snapshots/syntect_files/small.c .
- The blob GraphQL response should include a non-empty
lsif
field and nohtml
field.
- Testing treesitter
- With CodeMirror disabled
- Testing treesitter
- Load https://sourcegraph.test:3443/github.com/sourcegraph/sourcegraph@bd796dc/-/blob/docker-images/syntax-highlighter/crates/sg-syntax/src/snapshots/syntect_files/larger.cs
- The blob GraphQL response should include a non-empty
lsif
field and a non-emptyhtml
field.
- Testing syntext
- Load https://sourcegraph.test:3443/github.com/sourcegraph/sourcegraph@bd796dc/-/blob/docker-images/syntax-highlighter/crates/sg-syntax/src/snapshots/syntect_files/small.c
- The blob GraphQL response should include an empty
lsif
field and a non-emptyhtml
field.
- Testing treesitter