diff --git a/src/test/view/reviewCommentController.test.ts b/src/test/view/reviewCommentController.test.ts index 89092a7b4e..a0b2fecb9e 100644 --- a/src/test/view/reviewCommentController.test.ts +++ b/src/test/view/reviewCommentController.test.ts @@ -140,12 +140,12 @@ describe('ReviewCommentController', function () { sinon.restore(); }); - function createLocalFileChange(uri: vscode.Uri, fileName: string, rootUri: vscode.Uri): GitFileChangeNode { + function createLocalFileChange(uri: vscode.Uri, fileName: string, rootUri: vscode.Uri, status: GitChangeType = GitChangeType.MODIFY): GitFileChangeNode { const gitFileChangeModel = new GitFileChangeModel( manager, activePullRequest, { - status: GitChangeType.MODIFY, + status, fileName, blobUrl: 'https://example.com', diffHunks: @@ -595,4 +595,33 @@ describe('ReviewCommentController', function () { assert.strictEqual(obsolete[fileName][1], threadB); }); }); + + describe('provideCommentingRanges', function () { + it('enables file comments for deleted files (#9207)', async function () { + const fileName = 'data/deleted.json'; + const uri = vscode.Uri.parse(`${repository.rootUri.toString()}/${fileName}`); + const modifiedFileUri = toReviewUri(uri, undefined, undefined, '', false, { base: false }, repository.rootUri); + const reviewModel = new ReviewModel(); + const localFileChanges = [createLocalFileChange(modifiedFileUri, fileName, repository.rootUri, GitChangeType.DELETE)]; + reviewModel.localFileChanges = localFileChanges; + sinon.stub(manager, 'activePullRequest').get(() => activePullRequest); + sinon.stub(activePullRequest, 'isResolved').returns(true); + sinon.stub(gitApiImpl, 'repositories').get(() => [repository]); + + const reviewCommentController = new TestReviewCommentController( + reviewManager, + manager, + repository, + reviewModel, + gitApiImpl, + telemetry, + ); + context.subscriptions.push(reviewCommentController, ...localFileChanges); + + const document = { uri: modifiedFileUri, lineCount: 0 } as vscode.TextDocument; + const result = await reviewCommentController.provideCommentingRanges(document, new vscode.CancellationTokenSource().token); + + assert.deepStrictEqual(result, { ranges: [], enableFileComments: true }); + }); + }); }); diff --git a/src/view/reviewCommentController.ts b/src/view/reviewCommentController.ts index a66f9e6309..2b748351e6 100644 --- a/src/view/reviewCommentController.ts +++ b/src/view/reviewCommentController.ts @@ -535,6 +535,10 @@ export class ReviewCommentController extends CommentControllerBase implements Co const ranges: vscode.Range[] = []; if (matchedFile) { + if (matchedFile.status === GitChangeType.DELETE) { + Logger.debug('No commenting ranges: File was deleted.', ReviewCommentController.ID); + return { ranges: [], enableFileComments: true }; + } const diffHunks = await matchedFile.changeModel.diffHunks(); if ((matchedFile.status === GitChangeType.RENAME) && (diffHunks.length === 0)) { Logger.debug('No commenting ranges: File was renamed with no diffs.', ReviewCommentController.ID); @@ -632,6 +636,10 @@ export class ReviewCommentController extends CommentControllerBase implements Co return false; } + if (fileChange.status === GitChangeType.DELETE && fileChange.filePath.toString() === uri.toString()) { + return true; + } + if (fileChange.fileName !== query.path) { if (!((fileChange.change instanceof InMemFileChange) && fileChange.change.previousFileName === query.path)) { return false;