|
| 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 { testAssetUri } from "../../fixtures"; |
| 21 | +import { FolderContext } from "../../../src/FolderContext"; |
| 22 | +import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities"; |
| 23 | +import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider"; |
| 24 | +import { Version } from "../../../src/utilities/version"; |
| 25 | +import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities"; |
| 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 | + activateExtensionForSuite({ |
| 49 | + async setup(ctx) { |
| 50 | + workspaceContext = ctx; |
| 51 | + // Expand Macro support in Swift started from 6.1 |
| 52 | + if (workspaceContext.swiftVersion.isLessThan(new Version(6, 1, 0))) { |
| 53 | + this.skip(); |
| 54 | + } |
| 55 | + |
| 56 | + // Wait for a clean starting point, and build all tasks for the fixture |
| 57 | + await waitForNoRunningTasks(); |
| 58 | + folderContext = await folderInRootWorkspace("swift-macro", workspaceContext); |
| 59 | + await workspaceContext.focusFolder(folderContext); |
| 60 | + const tasks = (await getBuildAllTask(folderContext)) as SwiftTask; |
| 61 | + const { exitCode, output } = await executeTaskAndWaitForResult(tasks); |
| 62 | + expect(exitCode, `${output}`).to.equal(0); |
| 63 | + |
| 64 | + // Ensure lsp client is ready |
| 65 | + clientManager = workspaceContext.languageClientManager; |
| 66 | + const clientState = await waitForClientState(clientManager, langclient.State.Running); |
| 67 | + expect(clientState).to.equals(langclient.State.Running); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + test("Expand Macro", async function () { |
| 72 | + // Focus on the file of interest |
| 73 | + const uri = testAssetUri("swift-macro/Sources/swift-macroClient/main.swift"); |
| 74 | + await vscode.window.showTextDocument(uri); |
| 75 | + |
| 76 | + // Beginning of macro, # |
| 77 | + const position = new vscode.Position(5, 21); |
| 78 | + |
| 79 | + // Create a range starting and ending at the specified position |
| 80 | + const range = new vscode.Range(position, position); |
| 81 | + |
| 82 | + // Execute the code action provider command |
| 83 | + const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>( |
| 84 | + "vscode.executeCodeActionProvider", |
| 85 | + uri, |
| 86 | + range |
| 87 | + ); |
| 88 | + |
| 89 | + const expectedMacro = '(a + b, "a + b")'; |
| 90 | + |
| 91 | + // Find the "expand.macro.command" action |
| 92 | + const expandMacroAction = codeActions.find( |
| 93 | + action => action.command?.command === "expand.macro.command" |
| 94 | + ); |
| 95 | + |
| 96 | + // Assert that the expand macro command is available |
| 97 | + expect(expandMacroAction).is.not.undefined; |
| 98 | + |
| 99 | + // Set up a promise that resolves when the expected document is opened |
| 100 | + const expandedMacroUriPromise = new Promise<vscode.TextDocument>((resolve, reject) => { |
| 101 | + const disposable = vscode.workspace.onDidOpenTextDocument(openedDocument => { |
| 102 | + if (openedDocument.uri.scheme === "sourcekit-lsp") { |
| 103 | + disposable.dispose(); // Stop listening once we find the desired document |
| 104 | + resolve(openedDocument); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // Set a timeout to reject the promise if the document is not found |
| 109 | + setTimeout(() => { |
| 110 | + disposable.dispose(); |
| 111 | + reject(new Error("Timed out waiting for sourcekit-lsp document to be opened.")); |
| 112 | + }, 10000); // Wait up to 10 seconds for the document |
| 113 | + }); |
| 114 | + |
| 115 | + // Run expand macro action |
| 116 | + const command = expandMacroAction!.command!; |
| 117 | + expect(command.arguments).is.not.undefined; |
| 118 | + const commandArgs = command.arguments!; |
| 119 | + await vscode.commands.executeCommand(command.command, ...commandArgs); |
| 120 | + |
| 121 | + // Wait for the expanded macro document to be opened |
| 122 | + const referenceDocument = await expandedMacroUriPromise; |
| 123 | + |
| 124 | + // Verify that the reference document was successfully opened |
| 125 | + expect(referenceDocument).to.not.be.undefined; |
| 126 | + |
| 127 | + // Assert that the content contains the expected result |
| 128 | + const content = referenceDocument.getText(); |
| 129 | + expect(content).to.include(expectedMacro); |
| 130 | + }); |
| 131 | +}); |
0 commit comments