//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2019 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 // //===----------------------------------------------------------------------===// @_spi(SourceKitLSP) import LanguageServerProtocol import SKLogging import SKOptions import SKTestSupport @_spi(Testing) import SourceKitLSP import SwiftExtensions import SwiftLanguageService import XCTest final class ExecuteCommandTests: SourceKitLSPTestCase { func testLocationSemanticRefactoring() async throws { let testClient = try await TestSourceKitLSPClient() let uri = DocumentURI(for: .swift) let positions = testClient.openDocument( """ func foo() { 1️⃣"hello2️⃣"3️⃣ } """, uri: uri ) let args = SemanticRefactorCommand( title: "Localize String", actionString: "source.refactoring.kind.localize.string", positionRange: Range(positions["2️⃣"]), textDocument: TextDocumentIdentifier(uri) ) let metadata = SourceKitLSPCommandMetadata(textDocument: TextDocumentIdentifier(uri)) var command = args.asCommand() command.arguments?.append(metadata.encodeToLSPAny()) let request = ExecuteCommandRequest(command: command.command, arguments: command.arguments) let expectation = self.expectation(description: "Handle ApplyEditRequest") let applyEditTitle = ThreadSafeBox(initialValue: nil) let applyEditWorkspaceEdit = ThreadSafeBox(initialValue: nil) testClient.handleSingleRequest { (req: ApplyEditRequest) -> ApplyEditResponse in applyEditTitle.value = req.label applyEditWorkspaceEdit.value = req.edit expectation.fulfill() return ApplyEditResponse(applied: true, failureReason: nil) } let _ = try await testClient.send(request) try await fulfillmentOfOrThrow(expectation) let label = try XCTUnwrap(applyEditTitle.value) let edit = try XCTUnwrap(applyEditWorkspaceEdit.value) XCTAssertEqual(label, "Localize String") XCTAssertEqual( edit, WorkspaceEdit(changes: [ uri: [ TextEdit( range: Range(positions["1️⃣"]), newText: "NSLocalizedString(" ), TextEdit( range: Range(positions["3️⃣"]), newText: ", comment: \"\")" ), ] ]) ) } func testRangeSemanticRefactoring() async throws { let testClient = try await TestSourceKitLSPClient() let uri = DocumentURI(for: .swift) let positions = testClient.openDocument( """ func foo() -> String { 1️⃣var a = "hello" return a2️⃣ } """, uri: uri ) let args = SemanticRefactorCommand( title: "Extract Method", actionString: "source.refactoring.kind.extract.function", positionRange: positions["1️⃣"]..(initialValue: nil) let applyEditWorkspaceEdit = ThreadSafeBox(initialValue: nil) testClient.handleSingleRequest { (req: ApplyEditRequest) -> ApplyEditResponse in applyEditTitle.value = req.label applyEditWorkspaceEdit.value = req.edit expectation.fulfill() return ApplyEditResponse(applied: true, failureReason: nil) } let _ = try await testClient.send(request) try await fulfillmentOfOrThrow(expectation) let label = try XCTUnwrap(applyEditTitle.value) let edit = try XCTUnwrap(applyEditWorkspaceEdit.value) XCTAssertEqual(label, "Extract Method") XCTAssertEqual( edit, WorkspaceEdit(changes: [ uri: [ TextEdit( range: Range(Position(line: 0, utf16index: 0)), newText: """ fileprivate func extractedFunc() -> String { var a = "hello" return a } """ ), TextEdit( range: positions["1️⃣"]..