Skip to content

Commit 8974653

Browse files
authored
Fix disappearing commenting ranges (#6069)
1 parent a4b8971 commit 8974653

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/common/diffPositionMapping.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function getDiffLineByPosition(diffHunks: DiffHunk[], diffLineNumber: num
3131
return undefined;
3232
}
3333

34-
export function mapOldPositionToNew(patch: string, line: number): number {
34+
export function mapOldPositionToNew(patch: string, line: number, documentLineCount?: number): number {
3535
const diffReader = parseDiffHunk(patch);
3636
let diffIter = diffReader.next();
3737

@@ -43,6 +43,10 @@ export function mapOldPositionToNew(patch: string, line: number): number {
4343
// No-op
4444
} else if (diffHunk.oldLineNumber + diffHunk.oldLength - 1 < line) {
4545
delta += diffHunk.newLength - diffHunk.oldLength;
46+
} else if (documentLineCount === diffHunk.newLength) {
47+
// The diff doesn't give us enough information to do a good calculation as entire document was added or removed
48+
delta += diffHunk.newLength - diffHunk.oldLength;
49+
return line + delta;
4650
} else {
4751
// Part of the hunk is before line, part is after.
4852
for (const diffLine of diffHunk.diffLines) {

src/view/reviewCommentController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ export class ReviewCommentController extends CommentControllerBase
479479

480480
for (let i = 0; i < diffHunks.length; i++) {
481481
const diffHunk = diffHunks[i];
482-
const start = mapOldPositionToNew(contentDiff, diffHunk.newLineNumber);
483-
const end = mapOldPositionToNew(contentDiff, diffHunk.newLineNumber + diffHunk.newLength - 1);
482+
const start = mapOldPositionToNew(contentDiff, diffHunk.newLineNumber, document.lineCount);
483+
const end = mapOldPositionToNew(contentDiff, diffHunk.newLineNumber + diffHunk.newLength - 1, document.lineCount);
484484
if (start > 0 && end > 0) {
485485
ranges.push(new vscode.Range(start - 1, 0, end - 1, 0));
486486
}
@@ -514,7 +514,7 @@ export class ReviewCommentController extends CommentControllerBase
514514
}
515515

516516
try {
517-
if (matchedEditor && matchedEditor.document.isDirty) {
517+
if (matchedEditor && matchedEditor.document.isDirty && vscode.workspace.getConfiguration('files', matchedEditor.document.uri).get('autoSave') !== 'afterDelay') {
518518
const documentText = matchedEditor.document.getText();
519519
const details = await this._repository.getObjectDetails(
520520
this._folderRepoManager.activePullRequest.head.sha,

0 commit comments

Comments
 (0)