From f685eaf5048e842cf9baefa664e1d5489f371dfb Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Fri, 6 Sep 2024 22:47:11 +0200 Subject: [PATCH] Don't show "make suggestion" when there's already a suggestion in the editor (#6202) Fixes #6195 --- package.json | 2 +- src/common/executeCommands.ts | 1 + src/view/reviewCommentController.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ac580d009..36aed03c0 100644 --- a/package.json +++ b/package.json @@ -2507,7 +2507,7 @@ { "command": "pr.makeSuggestion", "group": "inline@3", - "when": "commentController =~ /^github-(browse|review)/" + "when": "commentController =~ /^github-(browse|review)/ && !github:activeCommentHasSuggestion" } ], "comments/commentThread/additionalActions": [ diff --git a/src/common/executeCommands.ts b/src/common/executeCommands.ts index 3660417a9..1b936e563 100644 --- a/src/common/executeCommands.ts +++ b/src/common/executeCommands.ts @@ -17,6 +17,7 @@ export namespace contexts { export const CREATE_PR_PERMISSIONS = 'github:createPrPermissions'; export const RESOLVING_CONFLICTS = 'github:resolvingConflicts'; export const PULL_REQUEST_DESCRIPTION_VISIBLE = 'github:pullRequestDescriptionVisible'; + export const ACTIVE_COMMENT_HAS_SUGGESTION = 'github:activeCommentHasSuggestion'; } export namespace commands { diff --git a/src/view/reviewCommentController.ts b/src/view/reviewCommentController.ts index 157d8c779..0a137c4cb 100644 --- a/src/view/reviewCommentController.ts +++ b/src/view/reviewCommentController.ts @@ -12,6 +12,7 @@ import { CommentHandler, registerCommentHandler, unregisterCommentHandler } from import { DiffSide, IReviewThread, SubjectType } from '../common/comment'; import { getCommentingRanges } from '../common/commentingRanges'; import { mapNewPositionToOld, mapOldPositionToNew } from '../common/diffPositionMapping'; +import { commands, contexts } from '../common/executeCommands'; import { GitChangeType } from '../common/file'; import Logger from '../common/logger'; import { PR_SETTINGS_NAMESPACE, PULL_BRANCH, PULL_PR_BRANCH_BEFORE_CHECKOUT, PullPRBranchVariants } from '../common/settingKeys'; @@ -37,6 +38,7 @@ import { RemoteFileChangeModel } from './fileChangeModel'; import { ReviewManager } from './reviewManager'; import { ReviewModel } from './reviewModel'; import { GitFileChangeNode, gitFileChangeNodeFilter, RemoteFileChangeNode } from './treeNodes/fileChangeNode'; +import { IDisposable } from 'cockatiel'; export interface SuggestionInformation { originalStartLine: number; @@ -357,6 +359,30 @@ export class ReviewCommentController extends CommentControllerBase this.updateResourcesWithCommentingRanges(); }), ); + this._localToDispose.push(vscode.window.onDidChangeActiveTextEditor(e => this.onDidChangeActiveTextEditor(e))); + } + + private _commentContentChangedListner: IDisposable | undefined; + private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) { + this._commentContentChangedListner?.dispose(); + this._commentContentChangedListner = undefined; + if (editor?.document.uri.scheme !== Schemes.Comment) { + return; + } + const updateHasSuggestion = () => { + if (editor.document.getText().includes('```suggestion')) { + commands.setContext(contexts.ACTIVE_COMMENT_HAS_SUGGESTION, true); + } else { + commands.setContext(contexts.ACTIVE_COMMENT_HAS_SUGGESTION, false); + } + }; + this._commentContentChangedListner = vscode.workspace.onDidChangeTextDocument(e => { + if (e.document.uri.toString() !== editor.document.uri.toString()) { + return; + } + updateHasSuggestion(); + }); + updateHasSuggestion(); } public updateCommentExpandState(expand: boolean) {