Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename getReferenceDocument request to textDocumentContent #1639

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Aug 22, 2024

Support for retrieving documents via custom URI schemes has recently been upstreamed and will be supported in the upcoming version 3.18 of the Language Server Protocol:

sourcekit-lsp currently uses a non-standard workspace/getReferenceDocument LSP extension for this, specifically for retrieving macro expansions. Since this requires either client-side support or workarounds (e.g. generation of temporary files), this PR migrates our LSP extension to the upstream workspace/textDocumentContent request.

The branch compiles and already works with the sibling PR for the VSCode extension:

Once LSP 3.18 ships, we can remove the client-side integration completely in favor of upstream LSP support:

Assuming the proposed request does not change further, all we need to do to support LSP clients implementing the standardized textDocumentContent request is to relax the capability check slightly to additionally query clientCapabilities.workspace?.textDocumentContent1:

if case .dictionary(let experimentalCapabilities) = self.capabilityRegistry.clientCapabilities.experimental,
case .bool(true) = experimentalCapabilities["workspace/peekDocuments"],
case .bool(true) = experimentalCapabilities["workspace/textDocumentContent"]

Footnotes

  1. We'll do this in a future PR since it is not clear how this capability is structured, given that VSCode doesn't send it yet.

@ahoppen
Copy link
Member

ahoppen commented Aug 22, 2024

CC @lokesh-tr

@lokesh-tr
Copy link
Contributor

lokesh-tr commented Aug 22, 2024

Wow! It got merged 14 days back. @fwcd You rock, bro! 🔥

When I get some time, I will look into this in much detail. I do believe that It won't be hard to migrate.

@fwcd
Copy link
Member Author

fwcd commented Aug 23, 2024

Can confirm that this works (in conjunction with the VSCode extension PR):

image

@fwcd fwcd marked this pull request as ready for review August 23, 2024 02:13
@fwcd fwcd requested a review from ahoppen as a code owner August 23, 2024 02:13
Copy link
Contributor

@lokesh-tr lokesh-tr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have a few minor comments but, everything else looks good to me.

One more thought:
Given that the LSP now supports TextDocumentContentRequest, I think we should now remove the code that generates temporary files for editors that doesn't support PeekDocumentsRequest, and ensure that we generate macro expansions on the fly for that and display them by passing a ReferenceDocumentURL to the ShowDocumentRequest.

case .bool(true) = experimentalCapabilities["workspace/peekDocuments"],
case .bool(true) = experimentalCapabilities["workspace/getReferenceDocument"]
case .bool(true) = experimentalCapabilities["workspace/peekDocuments"]
// TODO: Check if client supports LSP 3.18's workspace/textDocumentContent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this check should be in place before we merge the PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you address this TODO? I don’t like having TODOs in the codebase without an associated issue because in practice they never get fixed.

Copy link
Member Author

@fwcd fwcd Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we have to revert this for now. Once the request is upstreamed, we can relax this condition to support clients that don't explicitly declare it as an experimental capability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've re-added this and added a short remark to the PR description for how this check has to be updated once we wish to support the upstream client capability.

@lokesh-tr
Copy link
Contributor

@fwcd As per what we discussed, I guess it would be nice to mark this as "Draft" (or atleast with some "Do NOT Merge") just to ensure that this doesn't get merged before the LSP spec gets finalised.

@fwcd fwcd marked this pull request as draft August 23, 2024 15:26
@ahoppen
Copy link
Member

ahoppen commented Aug 26, 2024

Given that the LSP now supports TextDocumentContentRequest, I think we should now remove the code that generates temporary files for editors that doesn't support PeekDocumentsRequest, and ensure that we generate macro expansions on the fly for that and display them by passing a ReferenceDocumentURL to the ShowDocumentRequest.

I think it’s still valuable to have that logic for editors that don’t support the workspace/textDocumentContent request, which will probably be most editors for now. All these newer requests are optional and it’s possible for editors to not declare that capability.

As per what we discussed, I guess it would be nice to mark this as "Draft" (or atleast with some "Do NOT Merge") just to ensure that this doesn't get merged before the LSP spec gets finalised.

Just so I understand, what was the reasoning why we want to wait for LSP 3.18 to get finalized before merging this? Would there be any harm in switching to the workspace/textDocumentContent request now, implement the middleware to handle in in VS Code and then if other editors start implementing LSP 3.18 early, great and if not, we aren’t off any worse than we are right now?

@fwcd
Copy link
Member Author

fwcd commented Aug 26, 2024

We could do that, but I wasn't sure if using a beta version of vscode-languageclient for the extension is acceptable (and I'm not sure if the request ships there before it is stabilized in LSP). If it is, then sure.

@ahoppen
Copy link
Member

ahoppen commented Aug 26, 2024

Oh, I was thinking that the Swift VS Code extension could continue intercepting the workspace/textDocumentContent request in the middleware for now with the existing logic. And once VS Code adds native support for workspace/textDocumentContent, we can just remove that middleware. Would that work?

@fwcd
Copy link
Member Author

fwcd commented Aug 26, 2024

That's a nice idea, I don't see why that wouldn't work and as far as I understand we haven't shipped a version of sourcekit-lsp with this yet, so we wouldn't need to keep the legacy request name around for compatibility.

@ahoppen
Copy link
Member

ahoppen commented Aug 26, 2024

Exactly 😉

@fwcd
Copy link
Member Author

fwcd commented Sep 3, 2024

I'm marking the PR as ready for review again, since I have successfully tested it together with swiftlang/vscode-swift#1050, which just adapts our existing client-side integration to the new request name/format.

This means we should be able to safely merge it before LSP 3.18 is stabilized. We should probably still keep an eye on upstream, so we can incorporate any changes to the API as it is updated, but that could be done in future PRs.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Just a few minor comments.

Contributor Documentation/LSP Extensions.md Show resolved Hide resolved
case .bool(true) = experimentalCapabilities["workspace/peekDocuments"],
case .bool(true) = experimentalCapabilities["workspace/getReferenceDocument"]
case .bool(true) = experimentalCapabilities["workspace/peekDocuments"]
// TODO: Check if client supports LSP 3.18's workspace/textDocumentContent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you address this TODO? I don’t like having TODOs in the codebase without an associated issue because in practice they never get fixed.

Once the request is upstreamed to LSP 3.18 we should relax this
condition to support clients that don't declare it as an experimental
capability (since it will become a standard LSP request).
Since we 'vendor' it as an LSP extension for now, we'll document it here
until it is fully upstreamed to LSP 3.18.
@fwcd fwcd force-pushed the text-document-content-request branch from 3e38261 to dd7669a Compare September 4, 2024 21:35
@fwcd fwcd changed the title Migrate getReferenceDocument to upstream textDocumentContent request Rename getReferenceDocument request to textDocumentContent Sep 4, 2024
@fwcd
Copy link
Member Author

fwcd commented Sep 4, 2024

Tested with the VSCode extension and could successfully verify that it works. I had to re-add some of the experimental capability logic in 99836e0, but we'll need that anyway until the request is fully supported by upstream LSP.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I was wondering how we got away without the capabilities in initializationOptions but I took your word for it 😆 But guess it means that we do need them 🤷🏽

@ahoppen
Copy link
Member

ahoppen commented Sep 4, 2024

@swift-ci Please test

@fwcd
Copy link
Member Author

fwcd commented Sep 4, 2024

I was wondering how we got away without the capabilities

By removing the capability check, which I still did under the assumption that we'd hold off with this PR until the request is landed upstream (i.e. where I wrote the todo comment) 😄

@ahoppen
Copy link
Member

ahoppen commented Sep 4, 2024

@swift-ci Please test Windows

@lokesh-tr
Copy link
Contributor

This is great! 🚀 🚀 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants