-
Notifications
You must be signed in to change notification settings - Fork 285
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 support for showing Macro Expansions #1436
Add LSP support for showing Macro Expansions #1436
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very impressive. This is looking great. All my comments are minor / code style related. The overall design looks great!
Sources/LanguageServerProtocol/Requests/ShowDocumentRequest.swift
Outdated
Show resolved
Hide resolved
Sorry to disturbing you folks in such a busy time, but just to clarify for myself does this implementation would add |
@yaroslavyaroslav well I don't think this implementation would add anything related to what you have asked. There's this Perhaps @ahoppen can answer? @yaroslavyaroslav I guess you can open an issue and discuss this with him to not spam this PR since its not related to this PR. |
Sorry, I don’t fully understand your question @yaroslavyaroslav. If we want to continue discussing it, could you please file an issue so we can have a dedicated thread for it? |
3ccd6fb
to
87a5b67
Compare
Based on the meeting which we had yesterday:
|
I was little skeptical with my implementation of I have resolved all your comments. @ahoppen Kindly review and let me know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great. I have a few minor comments still but everything is very local.
7664f88
to
e715e93
Compare
@ahoppen I have addressed the review comments. When possible, let me know if there is anything else which I should fix. 😌 I have also made a new commit. This is inline with what we discussed in the meeting of adding a GitHub style name notation. The only difference is that instead of having just the starting line number in the file name like The start and end position are equal when new lines are added while expanding. Similarly, they are unequal when new lines are replacing old lines while expanding. And thus, allows the user to just look at the name of the expanded file name to decide whether it is adding or replacing new lines. EDIT: The naming scheme has been updated, kindly check the following comment which I made. |
Based on the discussion about my findings which we had, The benefit of the above naming is that:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few more minor comments inline.
f9ba8b4
to
4786f04
Compare
@ahoppen The last commit which I made tries to convert the sequential edit from sourcekitd into I would like to have you help me debug this issue. The test for attached macros is yet to be written. |
4786f04
to
bcbf634
Compare
As we discussed in the meeting, I removed the |
@ahoppen I wrapped the feature under Further, I will follow up with a new PR for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only have very minor comments now. Could you address them and then we can get the PR merged. My comment about making expandMacro
and semanticRefactoring
to not return the edits is more wide-reaching and I think we should look into that in a follow-up PR to get this one merged.
50d4290
to
f4d93d4
Compare
@freestanding
Macro Expansions
@freestanding
Macro Expansions@freestanding
Macro Expansions
f4d93d4
to
8529f4e
Compare
8529f4e
to
1309b79
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great! Let’s get this 🚢ed.
@swift-ci Please test |
@swift-ci Please test Windows |
------ Simplify `SemanticRefactoring` with new `Refactoring` protocol to handle sourcekitd requests Create and implement `ExpandMacroCommand` while temporarily storing generated expansions. Create test case `testFreestandingMacroExpansion` Manually inject `ExpandMacroCommand` into `retrieveRefactorCodeActions` upon an "Inline Macro" from sourcekitd Address Review Comments Mark `@_spi(Testing) public` for `MacroExpansionEdit` Address Review Comments Create separate directory for each buffer with its name, containing a generated file named as the source file along with position range Fixed generated macro expansion file extension not recognised, by switching to file names which don't contain fragments Address Review Comments Wrap the entire feature under `ExperimentalFeatures` Address Review Comments Make Swift Lint Pass Fix Windows Build not passing
Head branch was pushed to by a user without write access
1309b79
to
fd50560
Compare
@swift-ci Please test |
@swift-ci Please test Windows |
@freestanding
Macro Expansions
Note: This Feature is Experimental 🧪. You have to first enable this feature by passing
--experimental-feature show-macro-expansions
tosourcekit-lsp
This PR aims to bring a streamlined way of showing macro expansions through the LSP.
This allows users to pick a code action on a macro which performs an
ExecuteCommandRequest
with anExpandMacroCommand
. The server then generates the macro expansion (using semantic refactoring request) and stores it in a temporary directory. The server also performs aShowDocumentRequest
(LSP specification) on the client. Thus, displaying the expansion to the users.ShowDocumentRequest
being a part of the LSP spec will allow us to make macro expansions accessible across wide range of editors.The result of this PR is that macro expansions work identical to that of Xcode with the only difference being Xcode showing the expansions inline, while we don't have a standardised LSP spec for displaying inline. (Although some customisation is possible for VS Code alone through their API).
How it works?
sourcekitd
returns an "Inline Macro" Code Action when the editor requests for the available code actions on a macro.ExpandMacroCommand
before passing the response ofsourcekitd
to the editor.ExecuteCommandRequest
containing theExpandMacroCommand
.ExecuteCommandRequest
containing theExpandMacroCommand
, We then asksourcekitd
for the expansion of the given macro by making a semantic refactoring request.MacroExpansion
, and for each edit (expansion / replacement) it suggested, we make a temporary file as follows:<NSTemporaryDirectory>/sourcekit-lsp/GeneratedMacroExpansions/<edit.bufferName>/<SourceFileName>_<LineColumnRange>.<SourceFileExtension>
Here,
<edit.bufferName>
will be the buffer name associated with each edit, it looks like@__swiftmacro_..._.swift
without the ".swift" extension at last.<LineColumnRange>
will look likeLaCb-LcCd
where a & c represent Line number of start and end of expansion and b & d represent Column number of start and end of expansion.ShowDocumentRequest
, containing the URI of temporary file, to the editor.Follow-up Work & Future Directions:
@attached
macro expansions is little buggy. LSP support for@attached
macro expansions will be fixed in the following PR: Add LSP extension to show Macro Expansions (or any document) in a "peeked" editor (and some minor quality improvements) #1479But for people using VS Code, we are looking forward to implement some custom functionality to show expansions inline with the help of VS Code API.
This will be implemented in a future PR.(Implemented in
Add LSP extension to show Macro Expansions (or any document) in a "peeked" editor (and some minor quality improvements) #1479
Handle
PeekDocumentsRequest
to show documents from sourcekit-lsp (like, Macro Expansions) in a "peeked" editor vscode-swift#945)References:
Fixes #755
rdar://110516432
Expansion of Swift Macros in Visual Studio Code - Google Summer Of Code 2024
@lokesh-tr @ahoppen @adam-fowler