Skip to content

Showing Macro Expansion #564

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

Closed
filip-sakel opened this issue Jul 4, 2023 · 5 comments · Fixed by swiftlang/sourcekit-lsp#1479 or #945
Closed

Showing Macro Expansion #564

filip-sakel opened this issue Jul 4, 2023 · 5 comments · Fixed by swiftlang/sourcekit-lsp#1479 or #945
Labels
enhancement New feature or request sourcekit-lsp SourceKit-LSP issue

Comments

@filip-sakel
Copy link

filip-sakel commented Jul 4, 2023

Is your feature request related to a problem? Please describe.
Without seeing what code macros generate, it's hard to understand macro-related errors. For example, a macro might try to access a variable "_count" but forget the underscore prefix: "count". Being able to see the macro expansion would make it easy to identify the problem, which is critical for developing macros.

Describe the solution you'd like
An Xcode-like action could be shown when opening the context menu (when right clicking) on a macro. Then, after finding the expansion, the extension could be shown below the code using the macro (similar to inlay hints). Otherwise, a pop up showing expanded code could appear (similar to when inspecting documentation).

Describe alternatives you've considered
The alternative is to use a compiler flag to see macro expansions. That lacks specificity, as all macro expansions are shown. Even if the compiler gains the ability to only show the expansion of one macro, typing that macro's name or source location into the command line can hamper productivity. Instead, it's much more convenient to click on the macro, right in the editor, and be shown its expansion.

Additional context
Related SourceKit LSP issue swiftlang/sourcekit-lsp#755

@filip-sakel filip-sakel added the enhancement New feature or request label Jul 4, 2023
@adam-fowler
Copy link
Contributor

As you have seen I've already added an issue for sourcekit-lsp. I've had a look through the VSCode APIs and so far haven't found an easy way to display extra content inside an TextEditor window. I'll need to investigate further.

@filip-sakel
Copy link
Author

I found this discussion in the cpp-tools repo, where macro expansions look like so:

image

@adam-fowler
Copy link
Contributor

As an aside there is already a code action "Inline Macro" which will expand the macro into your file. You can use that plus Ctrl+Z to view the macro and revert

@filip-sakel
Copy link
Author

Thanks! I tried that but no code action showed up :( Are there specific version requirements (for Swift or even VSCode) for this to work?

@adam-fowler
Copy link
Contributor

Probably best to install Xcode beta 3. Beta 2 version of sourcekit-lsp had issues.
You click on the macro and a little lightbulb should appear next to it, click on the lightbulb and a series of options are shown including "Inline Macro"

@daveyc123 daveyc123 added the sourcekit-lsp SourceKit-LSP issue label Apr 19, 2024
lokesh-tr added a commit to lokesh-tr/sourcekit-lsp that referenced this issue 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 added a commit to lokesh-tr/sourcekit-lsp that referenced this issue 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 added a commit to lokesh-tr/sourcekit-lsp that referenced this issue 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 )
@github-project-automation github-project-automation bot moved this from Backlog to Done in Visual Code Swift Extension Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment