forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#228683 from microsoft/aeschli/respectabl…
…e-sailfish-565 registerMappedEditsProvider2
- Loading branch information
Showing
12 changed files
with
400 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
import { Disposable, DisposableMap, IDisposable } from '../../../base/common/lifecycle.js'; | ||
import { URI } from '../../../base/common/uri.js'; | ||
import { ICodeMapperProvider, ICodeMapperRequest, ICodeMapperResponse, ICodeMapperService } from '../../contrib/chat/common/chatCodeMapperService.js'; | ||
import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js'; | ||
import { ExtHostCodeMapperShape, ExtHostContext, ICodeMapperProgressDto, ICodeMapperRequestDto, MainContext, MainThreadCodeMapperShape } from '../common/extHost.protocol.js'; | ||
|
||
@extHostNamedCustomer(MainContext.MainThreadCodeMapper) | ||
export class MainThreadChatCodemapper extends Disposable implements MainThreadCodeMapperShape { | ||
|
||
private providers = this._register(new DisposableMap<number, IDisposable>()); | ||
private readonly _proxy: ExtHostCodeMapperShape; | ||
private static _requestHandlePool: number = 0; | ||
private _responseMap = new Map<string, ICodeMapperResponse>(); | ||
|
||
constructor( | ||
extHostContext: IExtHostContext, | ||
@ICodeMapperService private readonly codeMapperService: ICodeMapperService | ||
) { | ||
super(); | ||
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostCodeMapper); | ||
} | ||
|
||
$registerCodeMapperProvider(handle: number): void { | ||
const impl: ICodeMapperProvider = { | ||
mapCode: async (uiRequest: ICodeMapperRequest, response: ICodeMapperResponse, token: CancellationToken) => { | ||
const requestId = String(MainThreadChatCodemapper._requestHandlePool++); | ||
this._responseMap.set(requestId, response); | ||
const extHostRequest: ICodeMapperRequestDto = { | ||
requestId, | ||
codeBlocks: uiRequest.codeBlocks, | ||
conversation: uiRequest.conversation | ||
}; | ||
try { | ||
return await this._proxy.$mapCode(handle, extHostRequest, token).then((result) => result ?? undefined); | ||
} finally { | ||
this._responseMap.delete(requestId); | ||
} | ||
} | ||
}; | ||
|
||
const disposable = this.codeMapperService.registerCodeMapperProvider(handle, impl); | ||
this.providers.set(handle, disposable); | ||
} | ||
|
||
$unregisterCodeMapperProvider(handle: number): void { | ||
this.providers.deleteAndDispose(handle); | ||
} | ||
|
||
$handleProgress(requestId: string, data: ICodeMapperProgressDto): Promise<void> { | ||
const response = this._responseMap.get(requestId); | ||
if (response) { | ||
const resource = URI.revive(data.uri); | ||
for (const edit of data.edits) { | ||
response.textEdit(edit, resource); | ||
} | ||
} | ||
return Promise.resolve(); | ||
} | ||
} |
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,68 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import type * as vscode from 'vscode'; | ||
import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js'; | ||
import { ICodeMapperResult } from '../../contrib/chat/common/chatCodeMapperService.js'; | ||
import * as extHostProtocol from './extHost.protocol.js'; | ||
import { TextEdit } from './extHostTypeConverters.js'; | ||
import { URI } from '../../../base/common/uri.js'; | ||
|
||
export class ExtHostCodeMapper implements extHostProtocol.ExtHostCodeMapperShape { | ||
|
||
private static _providerHandlePool: number = 0; | ||
private readonly _proxy: extHostProtocol.MainThreadCodeMapperShape; | ||
private readonly providers = new Map<number, vscode.MappedEditsProvider2>(); | ||
|
||
constructor( | ||
mainContext: extHostProtocol.IMainContext | ||
) { | ||
this._proxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadCodeMapper); | ||
} | ||
|
||
async $mapCode(handle: number, internalRequest: extHostProtocol.ICodeMapperRequestDto, token: CancellationToken): Promise<ICodeMapperResult | null> { | ||
// Received request to map code from the main thread | ||
const provider = this.providers.get(handle); | ||
if (!provider) { | ||
throw new Error(`Received request to map code for unknown provider handle ${handle}`); | ||
} | ||
|
||
// Construct a response object to pass to the provider | ||
const stream: vscode.MappedEditsResponseStream = { | ||
textEdit: (target: vscode.Uri, edits: vscode.TextEdit | vscode.TextEdit[]) => { | ||
edits = (Array.isArray(edits) ? edits : [edits]); | ||
this._proxy.$handleProgress(internalRequest.requestId, { | ||
uri: target, | ||
edits: edits.map(TextEdit.from) | ||
}); | ||
} | ||
}; | ||
|
||
const request: vscode.MappedEditsRequest = { | ||
codeBlocks: internalRequest.codeBlocks.map(block => { | ||
return { | ||
code: block.code, | ||
resource: URI.revive(block.resource) | ||
}; | ||
}), | ||
conversation: internalRequest.conversation | ||
}; | ||
|
||
const result = await provider.provideMappedEdits(request, stream, token); | ||
return result ?? null; | ||
} | ||
|
||
registerMappedEditsProvider(extension: IExtensionDescription, provider: vscode.MappedEditsProvider2): vscode.Disposable { | ||
const handle = ExtHostCodeMapper._providerHandlePool++; | ||
this._proxy.$registerCodeMapperProvider(handle); | ||
this.providers.set(handle, provider); | ||
return { | ||
dispose: () => { | ||
return this._proxy.$unregisterCodeMapperProvider(handle); | ||
} | ||
}; | ||
} | ||
} |
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.