-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dynamic text document content (#1532)
* WIP * WIP * Add refresh support and test bed code.
- Loading branch information
Showing
12 changed files
with
323 additions
and
10 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
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,84 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import * as vscode from 'vscode'; | ||
import { StaticRegistrationOptions, TextDocumentContentRefreshRequest, TextDocumentContentRequest, type ClientCapabilities, type ServerCapabilities, type TextDocumentContentParams, type TextDocumentContentRegistrationOptions } from 'vscode-languageserver-protocol'; | ||
|
||
import { WorkspaceFeature, ensure, type FeatureClient } from './features'; | ||
import * as UUID from './utils/uuid'; | ||
|
||
|
||
export interface ProvideTextDocumentContentSignature { | ||
(this: void, uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string>; | ||
} | ||
|
||
export interface TextDocumentContentMiddleware { | ||
provideTextDocumentContent?: (this: void, uri: vscode.Uri, token: vscode.CancellationToken, next: ProvideTextDocumentContentSignature) => vscode.ProviderResult<string>; | ||
} | ||
|
||
export interface TextDocumentContentProviderShape { | ||
provider: vscode.TextDocumentContentProvider; | ||
onDidChangeEmitter: vscode.EventEmitter<vscode.Uri>; | ||
} | ||
|
||
export class TextDocumentContentFeature extends WorkspaceFeature<TextDocumentContentRegistrationOptions, TextDocumentContentProviderShape, TextDocumentContentMiddleware> { | ||
|
||
constructor(client: FeatureClient<TextDocumentContentMiddleware>) { | ||
super(client, TextDocumentContentRequest.type); | ||
} | ||
|
||
public fillClientCapabilities(capabilities: ClientCapabilities): void { | ||
const textDocumentContent = ensure(ensure(capabilities, 'workspace')!, 'textDocumentContent')!; | ||
textDocumentContent.dynamicRegistration = true; | ||
} | ||
|
||
public initialize(capabilities: ServerCapabilities): void { | ||
const client = this._client; | ||
client.onRequest(TextDocumentContentRefreshRequest.type, async (params) => { | ||
const uri = client.protocol2CodeConverter.asUri(params.uri); | ||
for (const provider of this.getProviders()) { | ||
provider.onDidChangeEmitter.fire(uri); | ||
} | ||
}); | ||
|
||
if (!capabilities?.workspace?.textDocumentContent) { | ||
return; | ||
} | ||
const capability = capabilities.workspace.textDocumentContent; | ||
const id = StaticRegistrationOptions.hasId(capability) ? capability.id : UUID.generateUuid(); | ||
this.register({ | ||
id: id, | ||
registerOptions: capability | ||
}); | ||
} | ||
|
||
protected registerLanguageProvider(options: TextDocumentContentRegistrationOptions): [vscode.Disposable, TextDocumentContentProviderShape] { | ||
const eventEmitter: vscode.EventEmitter<vscode.Uri> = new vscode.EventEmitter<vscode.Uri>(); | ||
const provider: vscode.TextDocumentContentProvider = { | ||
onDidChange: eventEmitter.event, | ||
provideTextDocumentContent: (uri, token) => { | ||
const client = this._client; | ||
const provideTextDocumentContent: ProvideTextDocumentContentSignature = (uri, token) => { | ||
const params: TextDocumentContentParams = { | ||
uri: client.code2ProtocolConverter.asUri(uri) | ||
}; | ||
return client.sendRequest(TextDocumentContentRequest.type, params, token).then((result) => { | ||
if (token.isCancellationRequested) { | ||
return null; | ||
} | ||
return result; | ||
}, (error) => { | ||
return client.handleFailedRequest(TextDocumentContentRequest.type, token, error, null); | ||
}); | ||
}; | ||
const middleware = client.middleware; | ||
return middleware.provideTextDocumentContent | ||
? middleware.provideTextDocumentContent(uri, token, provideTextDocumentContent) | ||
: provideTextDocumentContent(uri, token); | ||
} | ||
}; | ||
return [vscode.workspace.registerTextDocumentContentProvider(options.scheme, provider), { provider, onDidChangeEmitter: eventEmitter }]; | ||
} | ||
} |
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,98 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import type { DocumentUri } from 'vscode-languageserver-types'; | ||
import type { RequestHandler } from 'vscode-jsonrpc'; | ||
|
||
import { MessageDirection, ProtocolRequestType } from './messages'; | ||
import type { StaticRegistrationOptions } from './protocol'; | ||
|
||
/** | ||
* Client capabilities for a text document content provider. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export type TextDocumentContentClientCapabilities = { | ||
/** | ||
* Text document content provider supports dynamic registration. | ||
*/ | ||
dynamicRegistration?: boolean; | ||
}; | ||
|
||
/** | ||
* Text document content provider options. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export type TextDocumentContentOptions = { | ||
/** | ||
* The scheme for which the server provides content. | ||
*/ | ||
scheme: string; | ||
}; | ||
|
||
/** | ||
* Text document content provider registration options. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export type TextDocumentContentRegistrationOptions = TextDocumentContentOptions & StaticRegistrationOptions; | ||
|
||
/** | ||
* Parameters for the `workspace/textDocumentContent` request. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export interface TextDocumentContentParams { | ||
/** | ||
* The uri of the text document. | ||
*/ | ||
uri: DocumentUri; | ||
} | ||
|
||
/** | ||
* The `workspace/textDocumentContent` request is sent from the client to the | ||
* server to request the content of a text document. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export namespace TextDocumentContentRequest { | ||
export const method: 'workspace/textDocumentContent' = 'workspace/textDocumentContent'; | ||
export const messageDirection: MessageDirection = MessageDirection.clientToServer; | ||
export const type = new ProtocolRequestType<TextDocumentContentParams, string, void, void, TextDocumentContentRegistrationOptions>(method); | ||
export type HandlerSignature = RequestHandler<TextDocumentContentParams, string, void>; | ||
} | ||
|
||
/** | ||
* Parameters for the `workspace/textDocumentContent/refresh` request. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export interface TextDocumentContentRefreshParams { | ||
/** | ||
* The uri of the text document to refresh. | ||
*/ | ||
uri: DocumentUri; | ||
} | ||
|
||
/** | ||
* The `workspace/textDocumentContent` request is sent from the server to the client to refresh | ||
* the content of a specific text document. | ||
* | ||
* @since 3.18.0 | ||
* @proposed | ||
*/ | ||
export namespace TextDocumentContentRefreshRequest { | ||
export const method: `workspace/textDocumentContent/refresh` = `workspace/textDocumentContent/refresh`; | ||
export const messageDirection: MessageDirection = MessageDirection.serverToClient; | ||
export const type = new ProtocolRequestType<TextDocumentContentRefreshParams, void, void, void, void>(method); | ||
export type HandlerSignature = RequestHandler<TextDocumentContentRefreshParams, void, void>; | ||
} |
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
Oops, something went wrong.