diff --git a/src/extension.ts b/src/extension.ts index 85719f0..86e8a4d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,4 +1,5 @@ import { LanguageClient, ServerOptions, LanguageClientOptions, StreamInfo } from 'vscode-languageclient' +import { EvaluatableExpressionRequest } from './protocol' import * as vscode from 'vscode' import { join } from 'path' @@ -50,6 +51,29 @@ export function activate(context: vscode.ExtensionContext): void { languageClient = createClient(config) languageClient.start() + + context.subscriptions.push( + vscode.languages.registerEvaluatableExpressionProvider('php', { + async provideEvaluatableExpression( + document: vscode.TextDocument, + position: vscode.Position, + token: vscode.CancellationToken + ): Promise { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (languageClient.initializeResult?.capabilities.experimental?.xevaluatableExpressionProvider) { + const eer = await languageClient.sendRequest( + EvaluatableExpressionRequest.type, + languageClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position), + token + ) + if (eer && eer.expression) { + return new vscode.EvaluatableExpression(eer.range, eer.expression) + } + } + return undefined + }, + }) + ) } export function deactivate(): Promise | undefined { diff --git a/src/protocol.ts b/src/protocol.ts new file mode 100644 index 0000000..3f1db66 --- /dev/null +++ b/src/protocol.ts @@ -0,0 +1,35 @@ +/* eslint @typescript-eslint/no-empty-object-type: 0 */ +/* eslint @typescript-eslint/no-namespace: 0 */ +import { Range } from 'vscode' +// import { Range } from 'vscode-languageserver-types' +import { ProtocolRequestType } from 'vscode-languageserver-protocol/lib/messages' +import { + TextDocumentPositionParams, + TextDocumentRegistrationOptions, + WorkDoneProgressOptions, + WorkDoneProgressParams, +} from 'vscode-languageclient' + +interface EvaluatableExpression { + expression?: string + range: Range +} + +export interface EvaluatableExpressionOptions extends WorkDoneProgressOptions {} + +interface EvaluatableExpressionParams extends TextDocumentPositionParams, WorkDoneProgressParams {} + +interface EvaluatableExpressionRegistrationOptions + extends TextDocumentRegistrationOptions, + EvaluatableExpressionOptions {} + +export namespace EvaluatableExpressionRequest { + export const method = 'textDocument/xevaluatableExpression' + export const type = new ProtocolRequestType< + EvaluatableExpressionParams, + EvaluatableExpression | null, + never, + void, + EvaluatableExpressionRegistrationOptions + >(method) +}