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

Add LSP extension to show Macro Expansions (or any document) in a "peeked" editor (and some minor quality improvements) #1479

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

lokesh-tr
Copy link
Contributor

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

Intro:

Note: This Feature is Experimental 🧪. You have to first enable this feature by passing --experimental-feature show-macro-expansions to sourcekit-lsp

The previous PR (#1436) lays the foundation for adding LSP support to show Macro Expansions of any kind. This PR brings better macro expansion functionality to all editors and some custom functionality through LSP extension for VS Code (, which other editors can also utilise by custom request handling.)

If you are interested in bringing the custom functionality to your own editor, I highly recommend going through the PR below:
Accompanying PR in "Swift for VS Code" Extension": swiftlang/vscode-swift#945

About this pull request:

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.

This PR also does a lot of quality improvements.

List of all changes:

  • changes the shown Line and Column numbers to be One-based indexing instead of Zero-based indexing, so that users can match the macro expansions' line and column numbers with their editor's line and column numbers.

  • adds a test case for @attached Macro Expansion, thereby assures LSP support for @attached Macro Expansions.

  • fixes a bug where ShowDocumentRequest fails due to invalid selection

  • makes ShowDocumentRequest to use only a single file to display all the expansions

  • implements a general PeekDocumentsRequest that can be used to make the editor to open files in a peeked editor

  • if the client sets the experimental client capability peekDocuments (or alternatively, in the initializationOptions), then the ExpandMacroCommand sends a PeekDocumentsRequest instead of a ShowDocumentRequest. (Accompanying PR in vscode-swift extension: Handle PeekDocumentsRequest to show documents from sourcekit-lsp (like, Macro Expansions) in a "peeked" editor  vscode-swift#945)

  • Fixes Showing Macro Expansion vscode-swift#564

  • updated all ExecuteCommandTests appropriately

  • Fixes Don’t return edits from ExecuteCommandRequest #1498 (rdar://130207754)

  • Added Documentation wherever necessary

  • updates folder structure to utilise MD5 hash for ShowDocumentRequest generated macro expansions

References:

Previous PR which laid the foundation: #1436
Accompanying PR in the Swift Extension for VS Code repository: swiftlang/vscode-swift#945


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

@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch from be4204c to 8529f4e Compare June 20, 2024 12:28
@lokesh-tr lokesh-tr changed the title Add support for showing Attached Macro Expansions 🚥 Add LSP support for showing Attached Macro Expansions 🚥 Jun 20, 2024
@lokesh-tr lokesh-tr changed the title Add LSP support for showing Attached Macro Expansions 🚥 Add LSP support for showing @attached Macro Expansions 🚥 Jun 20, 2024
@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch 3 times, most recently from e5c84c1 to 35f821e Compare June 21, 2024 13:46
@lokesh-tr lokesh-tr changed the title Add LSP support for showing @attached Macro Expansions 🚥 Add LSP support for showing @attached Macro Expansions Jun 21, 2024
@lokesh-tr lokesh-tr marked this pull request as ready for review June 21, 2024 13:56
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.

Nice. I only have a few suggestions to simplify the test

Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch from 35f821e to 4e9bcf6 Compare June 21, 2024 19:43
@lokesh-tr
Copy link
Contributor Author

lokesh-tr commented Jun 21, 2024

@ahoppen I have also updated the PR to fix the bug where it fails to perform ShowDocumentRequest due to invalid selection, and I believe that the current implementation of DictionaryStorageMacro is the simplest form that works.

@lokesh-tr
Copy link
Contributor Author

lokesh-tr commented Jun 28, 2024

The last commit which I made breaks the test cases and is also a proof-of-concept implementation of the feature which will be implemented in a full-fledged manner.

Edit: Now, the test cases all pass and it's implemented in a full-fledged manner

Here's the accompanying PR swiftlang/vscode-swift#945 in the VS Code Swift Extension Repository.

@lokesh-tr lokesh-tr changed the title Add LSP support for showing @attached Macro Expansions Add LSP support for showing @attached Macro Expansions 🚥 Jun 30, 2024
@lokesh-tr lokesh-tr marked this pull request as draft June 30, 2024 09:07
@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch from 6237f58 to 2e14de9 Compare June 30, 2024 20:41
@lokesh-tr lokesh-tr changed the title Add LSP support for showing @attached Macro Expansions 🚥 Add LSP extension to show Macro Expansions in a "peeked" editor (and some minor improvements) Jun 30, 2024
@lokesh-tr lokesh-tr marked this pull request as ready for review June 30, 2024 21:03
@lokesh-tr
Copy link
Contributor Author

@ahoppen I have updated everything as per last call, I will squash the commits as soon as this passes the review stage.

@lokesh-tr lokesh-tr changed the title Add LSP extension to show Macro Expansions in a "peeked" editor (and some minor improvements) Add LSP extension to show Macro Expansions in a "peeked" editor (and some minor quality improvements) Jun 30, 2024
@lokesh-tr lokesh-tr changed the title Add LSP extension to show Macro Expansions in a "peeked" editor (and some minor quality improvements) Add LSP extension to show Macro Expansions (or any document) in a "peeked" editor (and some minor quality improvements) Jun 30, 2024
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.

Wohooo 🚀

Documentation/LSP Extensions.md Outdated Show resolved Hide resolved
Documentation/LSP Extensions.md Outdated Show resolved Hide resolved
Documentation/LSP Extensions.md Outdated Show resolved Hide resolved
Documentation/LSP Extensions.md Outdated Show resolved Hide resolved
Sources/SourceKitLSP/Swift/SemanticRefactoring.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
Tests/SourceKitLSPTests/ExecuteCommandTests.swift Outdated Show resolved Hide resolved
@lokesh-tr lokesh-tr requested a review from ahoppen July 1, 2024 19:04
@lokesh-tr
Copy link
Contributor Author

@ahoppen Let me know if there's anything else needed to be fixed, I shall proceed to squash the commits if they are all good. I have also updated the vscode-swift PR to incorporate the new changes.

Documentation/LSP Extensions.md Outdated Show resolved Hide resolved
Sources/SourceKitLSP/Swift/MacroExpansion.swift Outdated Show resolved Hide resolved
Sources/SourceKitLSP/Swift/MacroExpansion.swift Outdated Show resolved Hide resolved
Sources/SourceKitLSP/Swift/MacroExpansion.swift Outdated Show resolved Hide resolved
Sources/SourceKitLSP/Swift/SemanticRefactoring.swift Outdated Show resolved Hide resolved
@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch 2 times, most recently from 91a53c7 to a9f2c7e Compare July 2, 2024 17:10
@lokesh-tr
Copy link
Contributor Author

lokesh-tr commented Jul 2, 2024

@ahoppen I have addressed your comments, let me know if there are any other concerns. I have also squashed the commits. I guess its ready to 🚢

@lokesh-tr lokesh-tr requested a review from ahoppen July 2, 2024 17:17
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.

Let’s see what CI has to say 🚀

@ahoppen
Copy link
Member

ahoppen commented Jul 2, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 2, 2024 20:32
@lokesh-tr
Copy link
Contributor Author

@ahoppen trigger Test windows ?

@ahoppen
Copy link
Member

ahoppen commented Jul 3, 2024

@swift-ci Please test Windows

@lokesh-tr
Copy link
Contributor Author

lokesh-tr commented Jul 3, 2024

@ahoppen All the test cases pass. But, seems like a merge conflict happened in between. But the conflicting file Sources/SourceKitLSP/Swift/Refactoring.swift is already deleted / renamed to RefactoringResponse.swift. Do you want me to rebase once again and go through the tests once again? I guess it would be fine if you merge it directly by removing this file. 🤔

Edit: I rebased it anyways

lokesh-tr added 2 commits July 3, 2024 15:32
-------------------------------------------------------------------------------

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 )
auto-merge was automatically disabled July 3, 2024 10:02

Head branch was pushed to by a user without write access

@lokesh-tr lokesh-tr force-pushed the show-attached-macro-expansions branch from a9f2c7e to 0221475 Compare July 3, 2024 10:02
@ahoppen
Copy link
Member

ahoppen commented Jul 3, 2024

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Jul 3, 2024

@swift-ci Please test Windows

@ahoppen ahoppen enabled auto-merge July 3, 2024 10:09
@ahoppen ahoppen merged commit 607292a into swiftlang:main Jul 3, 2024
3 checks passed
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.

Don’t return edits from ExecuteCommandRequest Showing Macro Expansion
2 participants