Skip to content

Commit f42edf5

Browse files
committed
Implement diff view detection for #openPullRequest tool
Check if active editor is showing PR diff content (either pr: or review: scheme) and determine the associated PR. This addresses the feedback to detect PRs from diff views instead of just checking for checked out PRs.
1 parent 2c8786f commit f42edf5

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

src/lm/tools/openPullRequestTool.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7+
import { fromPRUri, fromReviewUri, Schemes } from '../../common/uri';
78
import { PullRequestModel } from '../../github/pullRequestModel';
89
import { PullRequestOverviewPanel } from '../../github/pullRequestOverview';
910
import { PullRequestTool } from './activePullRequestTool';
@@ -18,33 +19,48 @@ export class OpenPullRequestTool extends PullRequestTool {
1819
return panelPR;
1920
}
2021

21-
// If no overview panel is open, check if there's an active PR (checked out locally)
22-
// This covers the case where users are viewing PR diffs without the overview panel
23-
const folderManager = this.folderManagers.folderManagers.find((manager) => manager.activePullRequest);
24-
return folderManager?.activePullRequest;
25-
}
22+
// Check if the active file is a diff view or multidiff view showing PR content
23+
const activeEditor = vscode.window.activeTextEditor;
24+
if (activeEditor?.document.uri) {
25+
const uri = activeEditor.document.uri;
2626

27-
protected _confirmationTitle(): string {
28-
return vscode.l10n.t('Open Pull Request');
29-
}
27+
if (uri.scheme === Schemes.Pr) {
28+
// This is a PR diff from GitHub
29+
const prParams = fromPRUri(uri);
30+
if (prParams) {
31+
return this._findPullRequestByNumber(prParams.prNumber, prParams.remoteName);
32+
}
33+
} else if (uri.scheme === Schemes.Review) {
34+
// This is a review diff from a checked out PR
35+
const reviewParams = fromReviewUri(uri.query);
36+
if (reviewParams) {
37+
// For review scheme, find the active/checked out PR
38+
const folderManager = this.folderManagers.folderManagers.find(manager => manager.activePullRequest);
39+
return folderManager?.activePullRequest;
40+
}
41+
}
42+
}
3043

31-
override async prepareInvocation(): Promise<vscode.PreparedToolInvocation> {
32-
const pullRequest = this._findActivePullRequest();
33-
return {
34-
pastTenseMessage: pullRequest ? vscode.l10n.t('Read pull request "{0}"', pullRequest.title) : vscode.l10n.t('No open pull request'),
35-
invocationMessage: pullRequest ? vscode.l10n.t('Reading pull request "{0}"', pullRequest.title) : vscode.l10n.t('Reading open pull request'),
36-
confirmationMessages: { title: this._confirmationTitle(), message: pullRequest ? vscode.l10n.t('Allow reading the details of "{0}"?', pullRequest.title) : vscode.l10n.t('Allow reading the details of the open pull request?') },
37-
};
44+
return undefined;
3845
}
3946

40-
override async invoke(options: vscode.LanguageModelToolInvocationOptions<any>, token: vscode.CancellationToken): Promise<vscode.ExtendedLanguageModelToolResult | undefined> {
41-
let pullRequest = this._findActivePullRequest();
42-
43-
if (!pullRequest) {
44-
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart('There is no open pull request')]);
47+
private _findPullRequestByNumber(prNumber: number, remoteName: string): PullRequestModel | undefined {
48+
for (const manager of this.folderManagers.folderManagers) {
49+
for (const repo of manager.gitHubRepositories) {
50+
if (repo.remote.remoteName === remoteName) {
51+
// Look for the PR in the repository's PR cache
52+
for (const pr of repo.pullRequestModels) {
53+
if (pr.number === prNumber) {
54+
return pr;
55+
}
56+
}
57+
}
58+
}
4559
}
60+
return undefined;
61+
}
4662

47-
// Delegate to the base class for the actual implementation
48-
return super.invoke(options, token);
63+
protected _confirmationTitle(): string {
64+
return vscode.l10n.t('Open Pull Request');
4965
}
50-
}
66+
}

0 commit comments

Comments
 (0)