Add type annotation to convertStoredPropertyToComputed#2496
Add type annotation to convertStoredPropertyToComputed#2496Padmashree06 wants to merge 3 commits into
Conversation
ahoppen
left a comment
There was a problem hiding this comment.
A few preliminary review comments before I dive deeper into the implementation.
| @@ -0,0 +1,60 @@ | |||
| import Foundation | |||
There was a problem hiding this comment.
Mostly out of curiosity: Why do we need to import Foundation here?
There was a problem hiding this comment.
I imported Foundation because I was using String.range(of:) in the extractType helper. However, if we'd prefer to keep the dependencies minimal, I'm happy to switch to native Swift String methods like firstRange(of:) and remove the import entirely. Let me know what you'd prefer!
There was a problem hiding this comment.
You’r no longer using that anymore though, right?
| } | ||
|
|
||
| private static func extractType(from annotatedDecl: String) -> String? { | ||
|
|
There was a problem hiding this comment.
Thank you for the feedback! I'll remove the unnecessary lines.
| import SwiftSyntaxBuilder | ||
|
|
||
| extension ConvertStoredPropertyToComputed: SyntaxCodeActionProvider { | ||
|
|
| self.snapshot = snapshot | ||
| self.request = request | ||
| self.file = file | ||
| self.cursorInfo = cursorInfo |
There was a problem hiding this comment.
I don’t think we set cursorInfo anywhere, do we? Also, we should implement this in a way where the cursor info is only retrieved when it’s actually needed by a refactoring action instead of being set for every syntactic refactoring action.
There was a problem hiding this comment.
Thank you for pointing that out!
You’re right, I missed that cursorInfo is not being initialized anywhere. Triggering a cursorInfo request for every syntactic refactoring would indeed be unnecessary overhead.
I’ll remove cursorInfo from SyntaxCodeActionScope. Instead, I’ll first perform a syntactic check to determine whether the type information is missing. Only if semantic resolution is actually required for the refactoring will I trigger a cursorInfo request within the refactoring operation itself, using SwiftLanguageService.cursorInfo.
Please let me know if this approach aligns with your expectations or if there’s anything else I should consider.
There was a problem hiding this comment.
Yes, that’s a lot better now with the asynchronous command and it’s the approach I would have preferred until a few weeks ago but now we have codeAction/resolve, which is designed for exactly this workflow. Could you adopt it?
|
|
||
| private static func extractType(from annotatedDecl: String) -> String? { | ||
|
|
||
| guard let start = annotatedDecl.range(of: "<type>"), |
There was a problem hiding this comment.
Instead of parsing the annotated description, we should use the typename key.
There was a problem hiding this comment.
Yes thank you! I'll use typename key instead!
|
If you have addressed my comments, it seems like you haven’t pushed your latest changes. |
|
I haven’t pushed the changes yet. I was waiting for confirmation before doing so. I’ll push them shortly. |
b80521f to
1ef63e7
Compare
|
@ahoppen I’ve updated the implementation based on your feedback.
|
| kind: .refactorInline, | ||
| command: Command( | ||
| title: "Convert Stored Property to Computed Property", | ||
| command: "semantic.refactor.convertStoredPropertyToComputed", |
| @@ -0,0 +1,60 @@ | |||
| import Foundation | |||
There was a problem hiding this comment.
You’r no longer using that anymore though, right?
| self.snapshot = snapshot | ||
| self.request = request | ||
| self.file = file | ||
| self.cursorInfo = cursorInfo |
There was a problem hiding this comment.
Yes, that’s a lot better now with the asynchronous command and it’s the approach I would have preferred until a few weeks ago but now we have codeAction/resolve, which is designed for exactly this workflow. Could you adopt it?
|
Yes, I will modify the implementation to use |
1ef63e7 to
5d2c8b2
Compare
|
@ahoppen I've updated the implementation to use |
This is the first syntactic refactoring action that needs to perform a cursor info request on `codeAction/resolve`, so the majority of this PR is to add infrastructure for that. Based on swiftlang#2496. Co-Authored-By: Padmashree S S <padmashreess2006@gmail.com>
|
Sorry for the delayed response, I was on vacation for the last couple of weeks. I just took a deeper dive into the implementation and was a little uneasy with how ad-hoc the implementation was for this particular refactoring action because I think we need similar support for more refactoring actions in the future. I would have really liked to make some suggestion for a better design but needed to really dive into it myself to explore alternatives and once I arrived at one, I pretty much had a working implementation ready, so I ended up opening #2622. I would appreciate your feedback on that PR, let’s continue the conversation over there. Thanks for the work you did here to get this started. |
Issue: #2459
This PR implements the
ConvertStoredPropertyToComputedrefactoring in SourceKit-LSP, adopting the new API recently merged in swift-syntax (#3249).Summary
It integrates
cursorInfointo theSyntaxCodeActionScope, allowing the refactoring to resolve types for inferred properties (e.g.,let x = 5) by extracting the semantic type from SourceKit's annotated declaration.Changes
SyntaxCodeActionScopeto include[CursorInfo]cursorInforequestSyntaxCodeActionScopeConvertStoredPropertyToComputedtoallSyntaxCodeActionProvidersinSyntaxCodeActions.swiftImplementation Details
Implemented
SyntaxCodeActionProviderforConvertStoredPropertyToComputedwhich:cursorInfo