forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix macro expansion test (swiftlang#1301)
* Fix macro expansion test Wait for document to be indexed before proceeding Issue: swiftlang#1286 * Catch unexpected errors * Use _pollIndex request * Some cleanup
- Loading branch information
Showing
3 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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,87 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the VS Code Swift open source project | ||
// | ||
// Copyright (c) 2025 the VS Code Swift project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import * as vscode from "vscode"; | ||
import * as langclient from "vscode-languageclient/node"; | ||
import { LanguageClientManager } from "../../../src/sourcekit-lsp/LanguageClientManager"; | ||
|
||
export async function waitForClient<Result>( | ||
languageClientManager: LanguageClientManager, | ||
getResult: ( | ||
c: langclient.LanguageClient, | ||
token: langclient.CancellationToken | ||
) => Promise<Result>, | ||
match: (r: Result | undefined) => boolean | ||
): Promise<Result | undefined> { | ||
let result: Result | undefined = undefined; | ||
while (!match(result)) { | ||
result = await languageClientManager.useLanguageClient<Result>(getResult); | ||
console.warn("Language client is not ready yet. Retrying in 100 ms..."); | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
} | ||
return result; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
export namespace PollIndexRequest { | ||
export const method = "workspace/_pollIndex" as const; | ||
export const messageDirection: langclient.MessageDirection = | ||
langclient.MessageDirection.clientToServer; | ||
export const type = new langclient.RequestType<object, object, never>(method); | ||
} | ||
|
||
export async function waitForIndex(languageClientManager: LanguageClientManager): Promise<void> { | ||
await languageClientManager.useLanguageClient(async (client, token) => | ||
client.sendRequest(PollIndexRequest.type, {}, token) | ||
); | ||
} | ||
|
||
export async function waitForClientState( | ||
languageClientManager: LanguageClientManager, | ||
expectedState: langclient.State | ||
): Promise<langclient.State | undefined> { | ||
return await waitForClient( | ||
languageClientManager, | ||
async c => c.state, | ||
s => s === expectedState | ||
); | ||
} | ||
|
||
export async function waitForCodeActions( | ||
languageClientManager: LanguageClientManager, | ||
uri: vscode.Uri, | ||
range: vscode.Range | ||
): Promise<(langclient.CodeAction | langclient.Command)[]> { | ||
return ( | ||
(await waitForClient( | ||
languageClientManager, | ||
async (client, token) => { | ||
try { | ||
return client.sendRequest( | ||
langclient.CodeActionRequest.type, | ||
{ | ||
context: langclient.CodeActionContext.create([]), | ||
textDocument: langclient.TextDocumentIdentifier.create(uri.toString()), | ||
range, | ||
}, | ||
token | ||
); | ||
} catch (e) { | ||
// Ignore | ||
} | ||
}, | ||
s => (s || []).length > 0 | ||
)) || [] | ||
); | ||
} |