|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as vscode from "vscode"; |
| 16 | +import * as langclient from "vscode-languageclient/node"; |
| 17 | +import { expect } from "chai"; |
| 18 | +import { LanguageClientManager } from "../../../src/sourcekit-lsp/LanguageClientManager"; |
| 19 | +import { WorkspaceContext } from "../../../src/WorkspaceContext"; |
| 20 | +import { folderContextPromise, globalWorkspaceContextPromise } from "../extension.test"; |
| 21 | +import { testAssetUri } from "../../fixtures"; |
| 22 | +import { FolderContext } from "../../../src/FolderContext"; |
| 23 | +import { waitForEndTaskProcess, waitForNoRunningTasks } from "../../utilities"; |
| 24 | +import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider"; |
| 25 | +import { Version } from "../../../src/utilities/version"; |
| 26 | + |
| 27 | +async function waitForClientState( |
| 28 | + languageClientManager: LanguageClientManager, |
| 29 | + expectedState: langclient.State |
| 30 | +): Promise<langclient.State> { |
| 31 | + let clientState = undefined; |
| 32 | + while (clientState !== expectedState) { |
| 33 | + clientState = await languageClientManager.useLanguageClient(async client => client.state); |
| 34 | + console.warn("Language client is not ready yet. Retrying in 100 ms..."); |
| 35 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 36 | + } |
| 37 | + return clientState; |
| 38 | +} |
| 39 | + |
| 40 | +suite("Integration, Macros Functionality Support with Sourcekit-lsp", function () { |
| 41 | + // Take around 60 seconds if running in isolation, longer than default timeout |
| 42 | + this.timeout(2 * 60 * 1000); |
| 43 | + |
| 44 | + let clientManager: LanguageClientManager; |
| 45 | + let workspaceContext: WorkspaceContext; |
| 46 | + let folderContext: FolderContext; |
| 47 | + |
| 48 | + suiteSetup(async function () { |
| 49 | + workspaceContext = await globalWorkspaceContextPromise; |
| 50 | + |
| 51 | + // Wait for a clean starting point, and build all tasks for the fixture |
| 52 | + await waitForNoRunningTasks(); |
| 53 | + folderContext = await folderContextPromise("swift-macro"); |
| 54 | + await workspaceContext.focusFolder(folderContext); |
| 55 | + const tasks = (await getBuildAllTask(folderContext)) as SwiftTask; |
| 56 | + const exitPromise = waitForEndTaskProcess(tasks); |
| 57 | + await vscode.tasks.executeTask(tasks); |
| 58 | + const exitCode = await exitPromise; |
| 59 | + expect(exitCode).to.equal(0); |
| 60 | + |
| 61 | + // Ensure lsp client is ready |
| 62 | + clientManager = workspaceContext.languageClientManager; |
| 63 | + const clientState = await waitForClientState(clientManager, langclient.State.Running); |
| 64 | + expect(clientState).to.equals(langclient.State.Running); |
| 65 | + }); |
| 66 | + |
| 67 | + test("Inline/Expand Macro", async function () { |
| 68 | + // Focus on the file of interest |
| 69 | + const uri = testAssetUri("swift-macro/Sources/swift-macroClient/main.swift"); |
| 70 | + const editor = await vscode.window.showTextDocument(uri); |
| 71 | + |
| 72 | + // Beginning of macro, # |
| 73 | + const position = new vscode.Position(5, 21); |
| 74 | + |
| 75 | + // Create a range starting and ending at the specified position |
| 76 | + const range = new vscode.Range(position, position); |
| 77 | + |
| 78 | + // Execute the code action provider command |
| 79 | + const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>( |
| 80 | + "vscode.executeCodeActionProvider", |
| 81 | + uri, |
| 82 | + range |
| 83 | + ); |
| 84 | + |
| 85 | + // Log and assert the code actions |
| 86 | + expect(codeActions).to.be.an("array"); |
| 87 | + // Expand Macro action requires Swift 6.10 |
| 88 | + if (workspaceContext.swiftVersion.isGreaterThanOrEqual(new Version(6, 1, 0))) { |
| 89 | + expect(codeActions.length).to.be.equal(2); |
| 90 | + } else { |
| 91 | + expect(codeActions.length).to.be.equal(1); |
| 92 | + } |
| 93 | + |
| 94 | + const expectedMacro = '(a + b, "a + b")'; |
| 95 | + // Loop through the code actions and execute them based on the command id |
| 96 | + for (const action of codeActions) { |
| 97 | + expect(action.command).is.not.undefined; |
| 98 | + const command = action.command!; |
| 99 | + // The id for the action is not clear, the title is "inline macro" |
| 100 | + if (command.command === "semantic.refactor.command") { |
| 101 | + // Run inline macro action |
| 102 | + await vscode.commands.executeCommand(command.command, ...(command.arguments ?? [])); |
| 103 | + |
| 104 | + // Assert that the macro was inlined correctly |
| 105 | + const endPosition = new vscode.Position(5, 37); |
| 106 | + const inlineRange = new vscode.Range(position, endPosition); |
| 107 | + const updatedText = editor.document.getText(inlineRange); |
| 108 | + expect(updatedText).to.equal(expectedMacro); |
| 109 | + |
| 110 | + // Ensure we are refocusing on the text document for the undo step |
| 111 | + await vscode.window.showTextDocument(uri); |
| 112 | + |
| 113 | + // We need to undo the inline macro or subsequent action will fail |
| 114 | + await vscode.commands.executeCommand("undo"); |
| 115 | + } else if (command.command === "expand.macro.command") { |
| 116 | + // Set up a promise that resolves when the expected document is opened |
| 117 | + const expandedMacroUriPromise = new Promise<vscode.TextDocument>( |
| 118 | + (resolve, reject) => { |
| 119 | + const disposable = vscode.workspace.onDidOpenTextDocument( |
| 120 | + openedDocument => { |
| 121 | + if (openedDocument.uri.scheme === "sourcekit-lsp") { |
| 122 | + disposable.dispose(); // Stop listening once we find the desired document |
| 123 | + resolve(openedDocument); |
| 124 | + } |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + // Set a timeout to reject the promise if the document is not found |
| 129 | + setTimeout(() => { |
| 130 | + disposable.dispose(); |
| 131 | + reject( |
| 132 | + new Error( |
| 133 | + "Timed out waiting for sourcekit-lsp document to be opened." |
| 134 | + ) |
| 135 | + ); |
| 136 | + }, 10000); // Wait up to 10 seconds for the document |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + // Run expand macro action |
| 141 | + await vscode.commands.executeCommand(command.command, ...(command.arguments ?? [])); |
| 142 | + |
| 143 | + // Wait for the expanded macro document to be opened |
| 144 | + const referenceDocument = await expandedMacroUriPromise; |
| 145 | + |
| 146 | + // Verify that the reference document was successfully opened |
| 147 | + expect(referenceDocument).to.not.be.undefined; |
| 148 | + |
| 149 | + // Assert that the content contains the expected result |
| 150 | + const content = referenceDocument.getText(); |
| 151 | + expect(content).to.include(expectedMacro); |
| 152 | + } else { |
| 153 | + // New action got added, we need to add a new test case if this is hit. |
| 154 | + expect.fail(); |
| 155 | + } |
| 156 | + } |
| 157 | + }); |
| 158 | +}); |
0 commit comments