-
Notifications
You must be signed in to change notification settings - Fork 441
Code actions to convert between computed properties and zero-parameter functions #2721
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
Merged
ahoppen
merged 1 commit into
swiftlang:main
from
antigluten:code-action-zero-parameter-function
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Sources/SwiftRefactor/ConvertComputedPropertyToZeroParameterFunction.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if swift(>=6) | ||
public import SwiftSyntax | ||
#else | ||
import SwiftSyntax | ||
#endif | ||
|
||
public struct ConvertComputedPropertyToZeroParameterFunction: SyntaxRefactoringProvider { | ||
public static func refactor(syntax: VariableDeclSyntax, in context: Void) -> FunctionDeclSyntax? { | ||
guard syntax.bindings.count == 1, | ||
let binding = syntax.bindings.first, | ||
let identifierPattern = binding.pattern.as(IdentifierPatternSyntax.self) | ||
else { return nil } | ||
|
||
var statements: CodeBlockItemListSyntax | ||
|
||
guard let typeAnnotation = binding.typeAnnotation, | ||
var accessorBlock = binding.accessorBlock | ||
else { return nil } | ||
|
||
var effectSpecifiers: AccessorEffectSpecifiersSyntax? | ||
|
||
switch accessorBlock.accessors { | ||
case .accessors(let accessors): | ||
guard accessors.count == 1, let accessor = accessors.first, | ||
accessor.accessorSpecifier.tokenKind == .keyword(.get), let codeBlock = accessor.body | ||
antigluten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else { return nil } | ||
effectSpecifiers = accessor.effectSpecifiers | ||
statements = codeBlock.statements | ||
let accessorSpecifier = accessor.accessorSpecifier | ||
statements.leadingTrivia = | ||
accessorSpecifier.leadingTrivia + accessorSpecifier.trailingTrivia.droppingLeadingWhitespace | ||
+ codeBlock.leftBrace.leadingTrivia.droppingLeadingWhitespace | ||
+ codeBlock.leftBrace.trailingTrivia.droppingLeadingWhitespace | ||
+ statements.leadingTrivia | ||
statements.trailingTrivia += codeBlock.rightBrace.trivia.droppingLeadingWhitespace | ||
statements.trailingTrivia = statements.trailingTrivia.droppingTrailingWhitespace | ||
case .getter(let codeBlock): | ||
statements = codeBlock | ||
} | ||
|
||
let returnType = typeAnnotation.type | ||
|
||
var returnClause: ReturnClauseSyntax? | ||
let triviaAfterSignature: Trivia | ||
|
||
if !returnType.isVoid { | ||
triviaAfterSignature = .space | ||
returnClause = ReturnClauseSyntax( | ||
arrow: .arrowToken( | ||
leadingTrivia: typeAnnotation.colon.leadingTrivia, | ||
trailingTrivia: typeAnnotation.colon.trailingTrivia | ||
), | ||
type: returnType | ||
) | ||
} else { | ||
triviaAfterSignature = typeAnnotation.colon.leadingTrivia + typeAnnotation.colon.trailingTrivia | ||
} | ||
|
||
accessorBlock.leftBrace.leadingTrivia = accessorBlock.leftBrace.leadingTrivia.droppingLeadingWhitespace | ||
accessorBlock.rightBrace.trailingTrivia = accessorBlock.rightBrace.trailingTrivia.droppingTrailingWhitespace | ||
|
||
let body = CodeBlockSyntax( | ||
leftBrace: accessorBlock.leftBrace, | ||
statements: statements, | ||
rightBrace: accessorBlock.rightBrace | ||
) | ||
|
||
var parameterClause = FunctionParameterClauseSyntax(parameters: []) | ||
parameterClause.trailingTrivia = identifierPattern.identifier.trailingTrivia + triviaAfterSignature | ||
|
||
let functionEffectSpecifiers = FunctionEffectSpecifiersSyntax( | ||
asyncSpecifier: effectSpecifiers?.asyncSpecifier, | ||
throwsClause: effectSpecifiers?.throwsClause | ||
) | ||
let functionSignature = FunctionSignatureSyntax( | ||
parameterClause: parameterClause, | ||
effectSpecifiers: functionEffectSpecifiers, | ||
returnClause: returnClause | ||
) | ||
|
||
return FunctionDeclSyntax( | ||
modifiers: syntax.modifiers, | ||
funcKeyword: .keyword( | ||
.func, | ||
leadingTrivia: syntax.bindingSpecifier.leadingTrivia, | ||
trailingTrivia: syntax.bindingSpecifier.trailingTrivia | ||
), | ||
name: identifierPattern.identifier.with(\.trailingTrivia, []), | ||
signature: functionSignature, | ||
body: body | ||
) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Sources/SwiftRefactor/ConvertZeroParameterFunctionToComputedProperty.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if swift(>=6) | ||
public import SwiftSyntax | ||
#else | ||
import SwiftSyntax | ||
#endif | ||
|
||
public struct ConvertZeroParameterFunctionToComputedProperty: SyntaxRefactoringProvider { | ||
public static func refactor(syntax: FunctionDeclSyntax, in context: ()) -> VariableDeclSyntax? { | ||
guard syntax.signature.parameterClause.parameters.isEmpty, | ||
let body = syntax.body | ||
else { return nil } | ||
|
||
let variableName = PatternSyntax( | ||
IdentifierPatternSyntax( | ||
leadingTrivia: syntax.funcKeyword.trailingTrivia, | ||
identifier: syntax.name | ||
) | ||
) | ||
|
||
let triviaFromParameters = | ||
(syntax.signature.parameterClause.leftParen.trivia + syntax.signature.parameterClause.rightParen.trivia) | ||
.droppingTrailingWhitespace | ||
|
||
var variableType: TypeAnnotationSyntax? | ||
|
||
if let returnClause = syntax.signature.returnClause { | ||
variableType = TypeAnnotationSyntax( | ||
colon: .colonToken( | ||
leadingTrivia: triviaFromParameters + returnClause.arrow.leadingTrivia, | ||
trailingTrivia: returnClause.arrow.trailingTrivia | ||
), | ||
type: returnClause.type | ||
) | ||
} else { | ||
variableType = TypeAnnotationSyntax( | ||
colon: .colonToken( | ||
leadingTrivia: triviaFromParameters, | ||
trailingTrivia: .space | ||
), | ||
type: TypeSyntax("Void").with(\.trailingTrivia, .space) | ||
) | ||
} | ||
|
||
let accessorBlock = AccessorBlockSyntax( | ||
leftBrace: body.leftBrace, | ||
accessors: .getter(body.statements), | ||
rightBrace: body.rightBrace | ||
) | ||
|
||
return VariableDeclSyntax( | ||
modifiers: syntax.modifiers, | ||
.var, | ||
name: variableName, | ||
type: variableType, | ||
accessorBlock: accessorBlock | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if swift(>=6) | ||
public import SwiftSyntax | ||
#else | ||
import SwiftSyntax | ||
#endif | ||
|
||
extension TokenSyntax { | ||
var trivia: Trivia { | ||
return leadingTrivia + trailingTrivia | ||
} | ||
} | ||
|
||
extension Trivia { | ||
var droppingLeadingWhitespace: Trivia { | ||
return Trivia(pieces: self.drop(while: \.isWhitespace)) | ||
} | ||
|
||
var droppingTrailingWhitespace: Trivia { | ||
return Trivia(pieces: self.reversed().drop(while: \.isWhitespace).reversed()) | ||
} | ||
} | ||
|
||
extension TypeSyntax { | ||
var isVoid: Bool { | ||
switch self.as(TypeSyntaxEnum.self) { | ||
case .identifierType(let identifierType) where identifierType.name.text == "Void": return true | ||
case .tupleType(let tupleType) where tupleType.elements.isEmpty: return true | ||
default: return false | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.