| 1003 | } |
| 1004 | |
| 1005 | class WillSaveWaitUntilFeature implements DynamicFeature<TextDocumentRegistrationOptions> { |
| 1006 | |
| 1007 | private _listener: Disposable | undefined; |
| 1008 | private _selectors: Map<string, DocumentSelector> = new Map<string, DocumentSelector>(); |
| 1009 | |
| 1010 | constructor(private _client: BaseLanguageClient) { |
| 1011 | } |
| 1012 | |
| 1013 | public get messages(): RPCMessageType { |
| 1014 | return WillSaveTextDocumentWaitUntilRequest.type; |
| 1015 | } |
| 1016 | |
| 1017 | public fillClientCapabilities(capabilities: ClientCapabilities): void { |
| 1018 | let value = ensure(ensure(capabilities, 'textDocument')!, 'synchronization')!; |
| 1019 | value.willSaveWaitUntil = true; |
| 1020 | } |
| 1021 | |
| 1022 | public initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector): void { |
| 1023 | let textDocumentSyncOptions = (capabilities as ResolvedTextDocumentSyncCapabilities).resolvedTextDocumentSync; |
| 1024 | if (documentSelector && textDocumentSyncOptions && textDocumentSyncOptions.willSaveWaitUntil) { |
| 1025 | this.register(this.messages, { |
| 1026 | id: UUID.generateUuid(), |
| 1027 | registerOptions: { documentSelector: documentSelector } |
| 1028 | }); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | public register(_message: RPCMessageType, data: RegistrationData<TextDocumentRegistrationOptions>): void { |
| 1033 | if (!data.registerOptions.documentSelector) { |
| 1034 | return; |
| 1035 | } |
| 1036 | if (!this._listener) { |
| 1037 | this._listener = Workspace.onWillSaveTextDocument(this.callback, this); |
| 1038 | } |
| 1039 | this._selectors.set(data.id, data.registerOptions.documentSelector); |
| 1040 | } |
| 1041 | |
| 1042 | private callback(event: TextDocumentWillSaveEvent): void { |
| 1043 | if (DocumentNotifiactions.textDocumentFilter(this._selectors.values(), event.document)) { |
| 1044 | let middleware = this._client.clientOptions.middleware!; |
| 1045 | let willSaveWaitUntil = (event: TextDocumentWillSaveEvent): Thenable<VTextEdit[]> => { |
| 1046 | return this._client.sendRequest(WillSaveTextDocumentWaitUntilRequest.type, |
| 1047 | this._client.code2ProtocolConverter.asWillSaveTextDocumentParams(event)).then((edits) => { |
| 1048 | let vEdits = this._client.protocol2CodeConverter.asTextEdits(edits); |
| 1049 | return vEdits === void 0 ? [] : vEdits; |
| 1050 | }); |
| 1051 | }; |
| 1052 | event.waitUntil( |
| 1053 | middleware.willSaveWaitUntil |
| 1054 | ? middleware.willSaveWaitUntil(event, willSaveWaitUntil) |
| 1055 | : willSaveWaitUntil(event) |
| 1056 | ); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | public unregister(id: string): void { |
| 1061 | this._selectors.delete(id); |
| 1062 | if (this._selectors.size === 0 && this._listener) { |
nothing calls this directly
no outgoing calls
no test coverage detected