Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import SwiftRefactor
let allSyntaxCodeActions: [SyntaxCodeActionProvider.Type] = [
AddDocumentation.self,
AddSeparatorsToIntegerLiteral.self,
ConvertComputedPropertyToZeroParameterFunction.self
ConvertIntegerLiteral.self,
ConvertJSONToCodableStruct.self,
ConvertZeroParameterFunctionToComputedProperty.self,
FormatRawStringLiteral.self,
MigrateToNewIfLetSyntax.self,
OpaqueParameterToGeneric.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ extension RemoveSeparatorsFromIntegerLiteral: SyntaxRefactoringCodeActionProvide
}
}

extension ConvertZeroParameterFunctionToComputedProperty: SyntaxRefactoringCodeActionProvider {
static var title: String { "Convert to computed property" }

static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> Input? {
return scope.innermostNodeContainingRange?.findParentOfSelf(
ofType: FunctionDeclSyntax.self,
stoppingIf: { $0.is(CodeBlockSyntax.self) || $0.is(MemberBlockSyntax.self) }
)
}
}

extension ConvertComputedPropertyToZeroParameterFunction: SyntaxRefactoringCodeActionProvider {
static var title: String { "Convert to zero parameter function" }

static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> Input? {
return scope.innermostNodeContainingRange?.findParentOfSelf(
ofType: VariableDeclSyntax.self,
stoppingIf: { $0.is(CodeBlockSyntax.self) || $0.is(MemberBlockSyntax.self) }
)
}
}

extension SyntaxProtocol {
/// Finds the innermost parent of the given type while not walking outside of nodes that satisfy `stoppingIf`.
func findParentOfSelf<ParentType: SyntaxProtocol>(
Expand Down
116 changes: 116 additions & 0 deletions Tests/SourceKitLSPTests/CodeActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,122 @@ final class CodeActionTests: XCTestCase {
}
}

func testConvertFunctionZeroParameterToComputedProperty() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: clientCapabilitiesWithCodeActionSupport)
let uri = DocumentURI(for: .swift)

let positions = testClient.openDocument(
"""
1️⃣func someFunction() -> String2️⃣ { return "" }3️⃣
""",
uri: uri
)

let request = CodeActionRequest(
range: positions["1️⃣"]..<positions["2️⃣"],
context: .init(),
textDocument: TextDocumentIdentifier(uri)
)
let result = try await testClient.send(request)

guard case .codeActions(let codeActions) = result else {
XCTFail("Expected code actions")
return
}

let expectedCodeAction = CodeAction(
title: "Convert to computed property",
kind: .refactorInline,
diagnostics: nil,
edit: WorkspaceEdit(
changes: [
uri: [
TextEdit(
range: positions["1️⃣"]..<positions["3️⃣"],
newText: """
var someFunction: String { return "" }
"""
)
]
]
),
command: nil
)

XCTAssertTrue(codeActions.contains(expectedCodeAction))
}

func testConvertZeroParameterFunctionToComputedPropertyIsNotShownFromTheBody() async throws {
try await assertCodeActions(
##"""
func someFunction() -> String 1️⃣{
2️⃣return ""
}3️⃣
"""##,
exhaustive: false
) { uri, positions in
[]
}
}

func testConvertComputedPropertyToZeroParameterFunction() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: clientCapabilitiesWithCodeActionSupport)
let uri = DocumentURI(for: .swift)

let positions = testClient.openDocument(
"""
1️⃣var someFunction: String2️⃣ { return "" }3️⃣
""",
uri: uri
)

let request = CodeActionRequest(
range: positions["1️⃣"]..<positions["2️⃣"],
context: .init(),
textDocument: TextDocumentIdentifier(uri)
)
let result = try await testClient.send(request)

guard case .codeActions(let codeActions) = result else {
XCTFail("Expected code actions")
return
}

let expectedCodeAction = CodeAction(
title: "Convert to zero parameter function",
kind: .refactorInline,
diagnostics: nil,
edit: WorkspaceEdit(
changes: [
uri: [
TextEdit(
range: positions["1️⃣"]..<positions["3️⃣"],
newText: """
func someFunction() -> String { return "" }
"""
)
]
]
),
command: nil
)

XCTAssertTrue(codeActions.contains(expectedCodeAction))
}

func testConvertComputedPropertyToZeroParameterFunctionIsNotShownFromTheBody() async throws {
try await assertCodeActions(
##"""
var someFunction: String 1️⃣{
2️⃣return ""
}3️⃣
"""##,
exhaustive: false
) { uri, positions in
[]
}
}

/// Retrieves the code action at a set of markers and asserts that it matches a list of expected code actions.
///
/// - Parameters:
Expand Down