-
Notifications
You must be signed in to change notification settings - Fork 376
Add type annotation to convertStoredPropertyToComputed #2496
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Foundation | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly out of curiosity: Why do we need to import Foundation here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imported
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You’r no longer using that anymore though, right? |
||
| @_spi(SourceKitLSP) import LanguageServerProtocol | ||
| import SourceKitLSP | ||
| import SwiftRefactor | ||
| import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
|
|
||
| extension ConvertStoredPropertyToComputed: SyntaxCodeActionProvider { | ||
| static func codeActions(in scope: SyntaxCodeActionScope) -> [CodeAction] { | ||
| guard | ||
| let variableDecl = scope.innermostNodeContainingRange?.as(VariableDeclSyntax.self) | ||
| ?? scope.innermostNodeContainingRange?.parent?.as(VariableDeclSyntax.self) | ||
| else { return [] } | ||
|
|
||
| if variableDecl.bindings.first?.typeAnnotation?.type != nil { | ||
| let context = ConvertStoredPropertyToComputed.Context() | ||
| guard let refactored = try? Self.refactor(syntax: variableDecl, in: context) else { return [] } | ||
|
|
||
| let declRange = scope.snapshot.range(of: variableDecl) | ||
| let edit = TextEdit( | ||
| range: declRange, | ||
| newText: refactored.description | ||
| ) | ||
|
|
||
| return [ | ||
| CodeAction( | ||
| title: "Convert Stored Property to Computed Property", | ||
| kind: .refactorInline, | ||
| edit: WorkspaceEdit(changes: [scope.snapshot.uri: [edit]]) | ||
| ) | ||
| ] | ||
| } | ||
|
|
||
| return [ | ||
| CodeAction( | ||
| title: "Convert Stored Property to Computed Property", | ||
| kind: .refactorInline, | ||
| data: .dictionary([ | ||
| "action": .string("Convert Stored Property to Computed Property"), | ||
| "uri": .string(scope.snapshot.uri.stringValue), | ||
| "offset": .int(scope.range.lowerBound.utf8Offset), | ||
| ]) | ||
| ) | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,14 +44,18 @@ struct SyntaxCodeActionScope { | |
| /// The innermost node that contains the entire selected source range | ||
| var innermostNodeContainingRange: Syntax? | ||
|
|
||
| var cursorInfo: [CursorInfo] = [] | ||
|
|
||
| init?( | ||
| snapshot: DocumentSnapshot, | ||
| syntaxTree file: SourceFileSyntax, | ||
| request: CodeActionRequest | ||
| request: CodeActionRequest, | ||
| cursorInfo: [CursorInfo] = [] | ||
| ) { | ||
| self.snapshot = snapshot | ||
| self.request = request | ||
| self.file = file | ||
| self.cursorInfo = cursorInfo | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think we set
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing that out! You’re right, I missed that I’ll remove Please let me know if this approach aligns with your expectations or if there’s anything else I should consider.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
ahoppen marked this conversation as resolved.
|
||
|
|
||
| guard let left = tokenForRefactoring(at: request.range.lowerBound, snapshot: snapshot, syntaxTree: file), | ||
| let right = tokenForRefactoring(at: request.range.upperBound, snapshot: snapshot, syntaxTree: file) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.