swiftlang / vscode-swift

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

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

Open lokesh-tr opened 2 days ago

lokesh-tr commented 2 days 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 sourcekit-lsp from this PR https://github.com/swiftlang/sourcekit-lsp/pull/1479. Then, configure your "swift.sourcekit-lsp.serverPath" to the built sourcekit-lsp.

Since the macro expansions feature is behind an experimental feature flag, configure your "swift.sourcekit-lsp.experimentalFeatures" as follows:

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

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.

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 2 days 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 3 hours 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 3 hours 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. Other than that, 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 3 hours ago

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