-
Notifications
You must be signed in to change notification settings - Fork 37.6k
TS extension: register call to CopilotRelated with copilot extension #228610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mjbvz
merged 14 commits into
microsoft:main
from
sandersn:typescript-relatedfiles-imports
Oct 7, 2024
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
21b3f66
TS extension: add related-files call to CopilotRelated
sandersn 91024a3
Update to new command name
sandersn afce6a1
Move copilot related-files to language features
sandersn 3ae48ff
Depend on TS 5.7 not 5.6
sandersn 3d5329a
ts-expect-error on upcoming TS 5.7 protocol
sandersn d133bfe
Merge branch 'main' into typescript-relatedfiles-imports
sandersn eac80c5
Remove stray edit
sandersn b4a1d8c
Register copilotRelated, use selector's languages
sandersn c022f8e
add missing ts-expect-error till 5.7
sandersn a5ba473
Merge branch 'main' into typescript-relatedfiles-imports
sandersn c847b76
Update copilotRelated API with Disposable/CancellationToken
sandersn 23508ef
Add new context parameter to API
sandersn 8da7a18
Merge branch 'main' into typescript-relatedfiles-imports
mjbvz 3916183
Merge branch 'main' into typescript-relatedfiles-imports
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
extensions/typescript-language-features/src/languageFeatures/copilotRelated.ts
This file contains hidden or 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,76 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * 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 { nulToken } from '../utils/cancellation'; | ||
| import { jsTsLanguageModes, isSupportedLanguageMode } from '../configuration/languageIds'; | ||
| import { DocumentSelector } from '../configuration/documentSelector'; | ||
| import { API } from '../tsServer/api'; | ||
| import type * as Proto from '../tsServer/protocol/protocol'; | ||
| import { ITypeScriptServiceClient } from '../typescriptService'; | ||
| import { conditionalRegistration, requireMinVersion } from './util/dependentRegistration'; | ||
|
|
||
| const minVersion = API.v570; | ||
| const dummyDisposable = new vscode.Disposable(() => { }); | ||
|
|
||
| export function register( | ||
| _selector: DocumentSelector, | ||
| client: ITypeScriptServiceClient, | ||
| ) { | ||
| return conditionalRegistration([ | ||
| requireMinVersion(client, minVersion), | ||
| ], () => { | ||
| const ext = vscode.extensions.getExtension('github.copilot'); | ||
| if (!ext) { | ||
| return dummyDisposable; | ||
| } | ||
| ext.activate().then(() => { | ||
| const relatedAPI = ext.exports as { | ||
| registerRelatedFilesProvider( | ||
| providerId: { extensionId: string; languageId: string }, | ||
| callback: (uri: vscode.Uri) => Promise<{ entries: vscode.Uri[]; traits?: { name: string; value: string }[] }> | ||
| ): void; | ||
| } | undefined; | ||
| if (relatedAPI?.registerRelatedFilesProvider) { | ||
| for (const languageId of jsTsLanguageModes) { | ||
| const id = { | ||
| extensionId: 'vscode.typescript-language-features', | ||
| languageId | ||
| }; | ||
| relatedAPI.registerRelatedFilesProvider(id, async uri => { | ||
| let document; | ||
| try { | ||
| document = await vscode.workspace.openTextDocument(uri); | ||
| } catch { | ||
| if (!vscode.window.activeTextEditor) { | ||
| vscode.window.showErrorMessage(vscode.l10n.t("Related files provider failed. No active text editor.")); | ||
| return { entries: [] }; | ||
| } | ||
| // something is REALLY wrong if you can't open the active text editor's document, so don't catch that | ||
| document = await vscode.workspace.openTextDocument(vscode.window.activeTextEditor.document.uri); | ||
| } | ||
|
|
||
| if (!isSupportedLanguageMode(document)) { | ||
| vscode.window.showErrorMessage(vscode.l10n.t("Related files provider failed. Copilot requested file with unsupported language mode.")); | ||
| return { entries: [] }; | ||
| } | ||
|
|
||
| const file = client.toOpenTsFilePath(document); | ||
| if (!file) { | ||
| return { entries: [] }; | ||
| } | ||
| // @ts-expect-error until ts5.7 | ||
| const response = await client.execute('copilotRelated', { file, }, nulToken) as Proto.CopilotRelatedResponse; | ||
| if (response.type !== 'response' || !response.body) { | ||
| return { entries: [] }; | ||
| } | ||
| return { entries: response.body.relatedFiles.map(f => client.toResource(f)), traits: [] }; | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| return dummyDisposable; | ||
sandersn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.