swiftlang / vscode-swift

Visual Studio Code Extension for Swift
https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang
Apache License 2.0
755 stars 54 forks source link

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

Closed lokesh-tr closed 4 months ago

lokesh-tr commented 4 months ago

This PR utilises the following PR in sourcekit-lsp: https://github.com/swiftlang/sourcekit-lsp/pull/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 https://github.com/swiftlang/vscode-swift/issues/564


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

plemarquand commented 4 months ago

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

lokesh-tr commented 4 months ago

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 commented 4 months ago

@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 (https://github.com/swiftlang/sourcekit-lsp/pull/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 commented 4 months ago

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

lokesh-tr commented 4 months ago

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