Skip to content

Commit bf93ef2

Browse files
author
Rachel Macfarlane
committed
Scroll to outdated comments when selected from panel, fixes #234
1 parent 04f48a9 commit bf93ef2

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/commands.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { IPullRequestManager, IPullRequestModel, IPullRequest } from './github/i
1414
import { Comment } from './common/comment';
1515
import { formatError } from './common/utils';
1616
import { GitChangeType } from './common/file';
17+
import { getDiffLineByPosition, getZeroBased } from './common/diffPositionMapping';
18+
import { DiffChangeType } from './common/diffHunk';
1719

1820
const _onDidClosePR = new vscode.EventEmitter<IPullRequest>();
1921
export const onDidClosePR: vscode.Event<IPullRequest> = _onDidClosePR.event;
@@ -147,7 +149,27 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: IP
147149
base: true
148150
})
149151
});
150-
return vscode.commands.executeCommand('vscode.diff', previousFileUri, fileChange.filePath, `${fileChange.fileName} from ${commit.substr(0, 8)}`, { preserveFocus: true });
152+
153+
const options: vscode.TextDocumentShowOptions = {
154+
preserveFocus: true
155+
};
156+
157+
if (fileChange.comments && fileChange.comments.length) {
158+
const sortedOutdatedComments = fileChange.comments.filter(comment => comment.position === null).sort((a, b) => {
159+
return a.original_position - b.original_position;
160+
});
161+
162+
if (sortedOutdatedComments.length) {
163+
const diffLine = getDiffLineByPosition(fileChange.diffHunks, sortedOutdatedComments[0].original_position);
164+
165+
if (diffLine) {
166+
let lineNumber = Math.max(getZeroBased(diffLine.type === DiffChangeType.Delete ? diffLine.oldLineNumber : diffLine.newLineNumber), 0);
167+
options.selection = new vscode.Range(lineNumber, 0, lineNumber, 0);
168+
}
169+
}
170+
}
171+
172+
return vscode.commands.executeCommand('vscode.diff', previousFileUri, fileChange.filePath, `${fileChange.fileName} from ${commit.substr(0, 8)}`, options);
151173
}));
152174

153175
context.subscriptions.push(vscode.commands.registerCommand('pr.signin', async () => {

src/common/diffHunk.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
166166
}
167167
}
168168

169-
function parsePatch(patch: string): DiffHunk[] {
169+
export function parsePatch(patch: string): DiffHunk[] {
170170
let diffHunkReader = parseDiffHunk(patch);
171171
let diffHunkIter = diffHunkReader.next();
172172
let diffHunks = [];

src/view/reviewManager.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as path from 'path';
77
import * as vscode from 'vscode';
8-
import { parseDiff } from '../common/diffHunk';
8+
import { parseDiff, parsePatch } from '../common/diffHunk';
99
import { getDiffLineByPosition, getLastDiffLine, mapCommentsToHead, mapHeadLineToDiffHunkPosition, mapOldPositionToNew, getZeroBased, getAbsolutePosition } from '../common/diffPositionMapping';
1010
import { toReviewUri, fromReviewUri, fromPRUri } from '../common/uri';
1111
import { groupBy, formatError } from '../common/utils';
@@ -454,7 +454,23 @@ export class ReviewManager implements vscode.DecorationProvider {
454454
for (let commit in commitsGroup) {
455455
let commentsForCommit = commitsGroup[commit];
456456
let commentsForFile = groupBy(commentsForCommit, comment => comment.path);
457+
457458
for (let fileName in commentsForFile) {
459+
460+
let diffHunks = [];
461+
try {
462+
const gitResult = await this._repository.run(['diff', `${pr.base.sha}...${commit}`, '--', fileName]);
463+
464+
if (gitResult.stderr) {
465+
throw new Error(gitResult.stderr);
466+
}
467+
468+
const patch = gitResult.stdout.trim();
469+
diffHunks = parsePatch(patch);
470+
} catch (e) {
471+
Logger.appendLine(`Failed to parse patch for outdated comments: ${e}`);
472+
}
473+
458474
let oldComments = commentsForFile[fileName];
459475
let obsoleteFileChange = new GitFileChangeNode(
460476
pr,
@@ -464,7 +480,7 @@ export class ReviewManager implements vscode.DecorationProvider {
464480
toReviewUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, { base: false }),
465481
toReviewUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, { base: true }),
466482
false,
467-
[], // @todo Peng.,
483+
diffHunks,
468484
oldComments,
469485
commit
470486
);

0 commit comments

Comments
 (0)