Skip to content

Commit

Permalink
Scroll to outdated comments when selected from panel, fixes #234
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Aug 23, 2018
1 parent 04f48a9 commit bf93ef2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { IPullRequestManager, IPullRequestModel, IPullRequest } from './github/i
import { Comment } from './common/comment';
import { formatError } from './common/utils';
import { GitChangeType } from './common/file';
import { getDiffLineByPosition, getZeroBased } from './common/diffPositionMapping';
import { DiffChangeType } from './common/diffHunk';

const _onDidClosePR = new vscode.EventEmitter<IPullRequest>();
export const onDidClosePR: vscode.Event<IPullRequest> = _onDidClosePR.event;
Expand Down Expand Up @@ -147,7 +149,27 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: IP
base: true
})
});
return vscode.commands.executeCommand('vscode.diff', previousFileUri, fileChange.filePath, `${fileChange.fileName} from ${commit.substr(0, 8)}`, { preserveFocus: true });

const options: vscode.TextDocumentShowOptions = {
preserveFocus: true
};

if (fileChange.comments && fileChange.comments.length) {
const sortedOutdatedComments = fileChange.comments.filter(comment => comment.position === null).sort((a, b) => {
return a.original_position - b.original_position;
});

if (sortedOutdatedComments.length) {
const diffLine = getDiffLineByPosition(fileChange.diffHunks, sortedOutdatedComments[0].original_position);

if (diffLine) {
let lineNumber = Math.max(getZeroBased(diffLine.type === DiffChangeType.Delete ? diffLine.oldLineNumber : diffLine.newLineNumber), 0);
options.selection = new vscode.Range(lineNumber, 0, lineNumber, 0);
}
}
}

return vscode.commands.executeCommand('vscode.diff', previousFileUri, fileChange.filePath, `${fileChange.fileName} from ${commit.substr(0, 8)}`, options);
}));

context.subscriptions.push(vscode.commands.registerCommand('pr.signin', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/diffHunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
}
}

function parsePatch(patch: string): DiffHunk[] {
export function parsePatch(patch: string): DiffHunk[] {
let diffHunkReader = parseDiffHunk(patch);
let diffHunkIter = diffHunkReader.next();
let diffHunks = [];
Expand Down
20 changes: 18 additions & 2 deletions src/view/reviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as path from 'path';
import * as vscode from 'vscode';
import { parseDiff } from '../common/diffHunk';
import { parseDiff, parsePatch } from '../common/diffHunk';
import { getDiffLineByPosition, getLastDiffLine, mapCommentsToHead, mapHeadLineToDiffHunkPosition, mapOldPositionToNew, getZeroBased, getAbsolutePosition } from '../common/diffPositionMapping';
import { toReviewUri, fromReviewUri, fromPRUri } from '../common/uri';
import { groupBy, formatError } from '../common/utils';
Expand Down Expand Up @@ -454,7 +454,23 @@ export class ReviewManager implements vscode.DecorationProvider {
for (let commit in commitsGroup) {
let commentsForCommit = commitsGroup[commit];
let commentsForFile = groupBy(commentsForCommit, comment => comment.path);

for (let fileName in commentsForFile) {

let diffHunks = [];
try {
const gitResult = await this._repository.run(['diff', `${pr.base.sha}...${commit}`, '--', fileName]);

if (gitResult.stderr) {
throw new Error(gitResult.stderr);
}

const patch = gitResult.stdout.trim();
diffHunks = parsePatch(patch);
} catch (e) {
Logger.appendLine(`Failed to parse patch for outdated comments: ${e}`);
}

let oldComments = commentsForFile[fileName];
let obsoleteFileChange = new GitFileChangeNode(
pr,
Expand All @@ -464,7 +480,7 @@ export class ReviewManager implements vscode.DecorationProvider {
toReviewUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, { base: false }),
toReviewUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, { base: true }),
false,
[], // @todo Peng.,
diffHunks,
oldComments,
commit
);
Expand Down

0 comments on commit bf93ef2

Please sign in to comment.