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

Handle PeekDocumentsRequest to show documents from sourcekit-lsp (like, Macro Expansions) in a "peeked" editor #945

Merged

Conversation

lokesh-tr
Copy link
Contributor

@lokesh-tr lokesh-tr commented Jun 28, 2024

This PR utilises the following PR in sourcekit-lsp: swiftlang/sourcekit-lsp#1479
This takes a custom PeekDocumentsRequest and opens a peek window to display each given document. In the current state, this request is being used by ExpandMacroCommand in sourcekit-lsp to display Macro Expansions.

To check out this functionality, build the main branch of sourcekit-lsp. Then, configure your "swift.sourcekit-lsp.serverPath" to the built sourcekit-lsp. (This step will not be necessary once this build of sourcekit-lsp is packaged with Swift itself given that you use the latest Swift Toolchain)

Since the macro expansions feature is behind an experimental feature flag, configure your swift extension settings as follows:

"swift.sourcekit-lsp.serverArguments": [
  "--experimental-feature", "show-macro-expansions"
],

(The above step will not be necessary when showing macro expansions comes to a mature stage in the sourcekit-lsp side and thus will be out of its experimental feature flag)

The "Expand Macro" will be available as a Code Action when the cursor sits on a Macro annotation, and selecting the code action will trigger the ExpandMacroCommand -> PeekDocumentsRequest, and thus shows the Macro Expansions inside a "peeked" editor.

Note for extension / plugin developers of other editor ecosystems:
If you are an extension developer wanting to implement this custom functionality in your own editor, set the experimental client capability workspace/peekDocuments to true, in order to make sourcekit-lsp to send the PeekDocumentsRequest. From there, you can handle the request and display the documents in any way as you wish, as long as it is "inline" to the editor (or) "peeks" the documents inside the editor.

Fixes #564


Expansion of Swift Macros in Visual Studio Code - Google Summer Of Code 2024
@lokesh-tr @ahoppen @adam-fowler

@plemarquand
Copy link
Contributor

This looks great! I tried it out and expanded swift-testing's #expect macro. Then I ran the test and it behaved exactly the same, as expected.

This fixes #564

Copy link
Contributor

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

@lokesh-tr this all looks good. Shocking how little code it is on the VSCode side. As Alex said in the call let's not make this macro specific and call it PeekDocument or something similar.

If we could somehow work out how to display multiple peek views this would be even better.

@lokesh-tr lokesh-tr changed the title Handle custom PeekMacroRequest to show macro expansions in a peeked editor 🚥 Handle PeekDocumentsRequest from sourcekit-lsp to show documents (macro expansions in particular) in a "peeked" editor Jun 30, 2024
@lokesh-tr lokesh-tr changed the title Handle PeekDocumentsRequest from sourcekit-lsp to show documents (macro expansions in particular) in a "peeked" editor Handle PeekDocumentsRequest to show documents from sourcekit-lsp (like, Macro Expansions) in a "peeked" editor Jun 30, 2024
@lokesh-tr
Copy link
Contributor Author

This looks great! I tried it out and expanded swift-testing's #expect macro. Then I ran the test and it behaved exactly the same, as expected.

This fixes #564

Thanks @plemarquand for testing this out. 😄

@lokesh-tr
Copy link
Contributor Author

lokesh-tr commented Jun 30, 2024

@adam-fowler

@lokesh-tr this all looks good. Shocking how little code it is on the VSCode side

This is due to the fact that we didn't try to create a custom scheme and handle the fetching of documents through the scheme, etc. And, I had finished the major heavy lifting works in sourcekit-lsp side already.

As Alex said in the call let's not make this macro specific and call it PeekDocument or something similar.

I have just now pushed the code to the PR (swiftlang/sourcekit-lsp#1479) with all the necessary updates as we discussed in the call. Let's see how the review goes.

If we could somehow work out how to display multiple peek views this would be even better.

I wish that's possible. I am still trying to research on this and find a solution, but so far, no clue.

@lokesh-tr lokesh-tr marked this pull request as ready for review June 30, 2024 21:18
@lokesh-tr lokesh-tr requested review from 0xTim and award999 as code owners June 30, 2024 21:18
@lokesh-tr
Copy link
Contributor Author

@adam-fowler I guess this PR is now ready for review. I will squash the commits, once we pass the review stage.

@lokesh-tr lokesh-tr requested a review from adam-fowler June 30, 2024 21:19
@lokesh-tr lokesh-tr force-pushed the gsoc24-expansion-of-macros-in-vscode branch from 333b2d9 to 50ffdcd Compare July 1, 2024 19:03
lokesh-tr added a commit to lokesh-tr/sourcekit-lsp that referenced this pull request Jul 2, 2024
-------------------------------------------------------------------------------

This implements an LSP Extension `PeekDocumentsRequest` to let `ExpandMacroCommand` to open the macro expansions in a "peeked" editor window.
For this to work, the client has to pass "workspace/peekDocuments" enabled to `ClientCapabilities.experimental` and the client should handle the `PeekDocumentsRequest` and show the expansions in a "peeked" editor window.

PR to support the above capability in the "Swift for VS Code" Extension: swiftlang/vscode-swift#945
The "Swift for VS Code" extension cannot send the client capability, so it instead passes the same through `initializationOptions` in the `InitializeRequest`.

For editors which doesn't support this capability, `sourcekit-lsp` sends a `ShowDocumentRequest`.
The `ShowDocumentRequest` is updated to show all the macro expansions in a single generated file. Moreover, its folder structure is updated to use hex string of MD5 hash of concatenation of buffer names of expansions.

Fixes swiftlang/vscode-swift#564
Fixes swiftlang#1498 ( rdar://130207754 )
@lokesh-tr lokesh-tr force-pushed the gsoc24-expansion-of-macros-in-vscode branch from 2c60285 to 4007fd7 Compare July 2, 2024 17:14
lokesh-tr added a commit to lokesh-tr/sourcekit-lsp that referenced this pull request Jul 3, 2024
-------------------------------------------------------------------------------

This implements an LSP Extension `PeekDocumentsRequest` to let `ExpandMacroCommand` to open the macro expansions in a "peeked" editor window.
For this to work, the client has to pass "workspace/peekDocuments" enabled to `ClientCapabilities.experimental` and the client should handle the `PeekDocumentsRequest` and show the expansions in a "peeked" editor window.

PR to support the above capability in the "Swift for VS Code" Extension: swiftlang/vscode-swift#945
The "Swift for VS Code" extension cannot send the client capability, so it instead passes the same through `initializationOptions` in the `InitializeRequest`.

For editors which doesn't support this capability, `sourcekit-lsp` sends a `ShowDocumentRequest`.
The `ShowDocumentRequest` is updated to show all the macro expansions in a single generated file. Moreover, its folder structure is updated to use hex string of MD5 hash of concatenation of buffer names of expansions.

Fixes swiftlang/vscode-swift#564
Fixes swiftlang#1498 ( rdar://130207754 )
@lokesh-tr
Copy link
Contributor Author

@adam-fowler The PR in sourcekit-lsp has been merged: swiftlang/sourcekit-lsp#1479. I would like to have your comments if there are any. I had also squashed the commits. Let's get this shipped. 🚀

Copy link
Contributor

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

A couple of minor changes, but everything else looks good

src/sourcekit-lsp/LanguageClientManager.ts Outdated Show resolved Hide resolved
src/sourcekit-lsp/LanguageClientManager.ts Show resolved Hide resolved
lokesh-tr added a commit to lokesh-tr/sourcekit-lsp that referenced this pull request Jul 4, 2024
-------------------------------------------------------------------------------

This implements an LSP Extension `PeekDocumentsRequest` to let `ExpandMacroCommand` to open the macro expansions in a "peeked" editor window.
For this to work, the client has to pass "workspace/peekDocuments" enabled to `ClientCapabilities.experimental` and the client should handle the `PeekDocumentsRequest` and show the expansions in a "peeked" editor window.

PR to support the above capability in the "Swift for VS Code" Extension: swiftlang/vscode-swift#945
The "Swift for VS Code" extension cannot send the client capability, so it instead passes the same through `initializationOptions` in the `InitializeRequest`.

For editors which doesn't support this capability, `sourcekit-lsp` sends a `ShowDocumentRequest`.
The `ShowDocumentRequest` is updated to show all the macro expansions in a single generated file. Moreover, its folder structure is updated to use hex string of MD5 hash of concatenation of buffer names of expansions.

Fixes swiftlang/vscode-swift#564
Fixes swiftlang#1498 ( rdar://130207754 )
@lokesh-tr lokesh-tr force-pushed the gsoc24-expansion-of-macros-in-vscode branch from 4007fd7 to 8c2aab9 Compare July 4, 2024 08:26
@lokesh-tr lokesh-tr requested a review from adam-fowler July 4, 2024 08:27
Copy link
Contributor

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

we are good

@adam-fowler adam-fowler merged commit 028e064 into swiftlang:main Jul 4, 2024
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.

Showing Macro Expansion
4 participants