Skip to content

Commit

Permalink
Support multiline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed Apr 6, 2022
1 parent 1f2bb6e commit b6dcbc5
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 40 deletions.
6 changes: 4 additions & 2 deletions src/common/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export interface IReviewThread {
viewerCanUnresolve: boolean;
path: string;
diffSide: DiffSide;
line: number;
originalLine: number;
startLine: number;
endLine: number;
originalStartLine: number;
originalEndLine: number;
isOutdated: boolean;
comments: IComment[];
}
Expand Down
2 changes: 2 additions & 0 deletions src/github/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export interface ReviewThread {
viewerCanUnresolve: boolean;
path: string;
diffSide: DiffSide;
startLine: number | null;
line: number;
originalStartLine: number | null;
originalLine: number;
isOutdated: boolean;
comments: {
Expand Down
9 changes: 6 additions & 3 deletions src/github/pullRequestModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
* a new review.
* @param body The body of the thread's first comment.
* @param commentPath The path to the file being commented on.
* @param line The line on which to add the comment.
* @param startLine The start line on which to add the comment.
* @param endLine The end line on which to add the comment.
* @param side The side the comment should be deleted on, i.e. the original or modified file.
* @param suppressDraftModeUpdate If a draft mode change should event should be suppressed. In the
* case of a single comment add, the review is created and then immediately submitted, so this prevents
Expand All @@ -477,7 +478,8 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
async createReviewThread(
body: string,
commentPath: string,
line: number,
startLine: number,
endLine: number,
side: DiffSide,
suppressDraftModeUpdate?: boolean,
): Promise<IReviewThread | undefined> {
Expand All @@ -495,7 +497,8 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
body,
pullRequestId: this.graphNodeId,
pullRequestReviewId: pendingReviewId,
line,
startLine: startLine === endLine ? undefined : startLine,
line: endLine,
side,
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/github/queries.gql
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ fragment ReviewThread on PullRequestReviewThread {
path
diffSide
line
startLine
originalStartLine
originalLine
isOutdated
comments(first: 100) {
Expand Down Expand Up @@ -269,7 +271,9 @@ query PullRequestComments($owner: String!, $name: String!, $number: Int!, $first
viewerCanUnresolve
path
diffSide
startLine
line
originalStartLine
originalLine
isOutdated
comments(first: 100) {
Expand Down
6 changes: 4 additions & 2 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,10 @@ export function parseGraphQLReviewThread(thread: GraphQL.ReviewThread): IReviewT
viewerCanResolve: thread.viewerCanResolve,
viewerCanUnresolve: thread.viewerCanUnresolve,
path: thread.path,
line: thread.line,
originalLine: thread.originalLine,
startLine: thread.startLine ?? thread.line,
endLine: thread.line,
originalStartLine: thread.originalStartLine ?? thread.originalLine,
originalEndLine: thread.originalLine,
diffSide: thread.diffSide,
isOutdated: thread.isOutdated,
comments: thread.comments.nodes.map(comment => parseGraphQLComment(comment, thread.isResolved)),
Expand Down
2 changes: 2 additions & 0 deletions src/test/github/pullRequestModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const reviewThreadResponse = {
viewerCanResolve: true,
path: 'README.md',
diffSide: 'RIGHT',
startLine: null,
line: 4,
originalStartLine: null,
originalLine: 4,
isOutdated: false,
comments: {
Expand Down
6 changes: 4 additions & 2 deletions src/test/view/reviewCommentController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ describe('ReviewCommentController', function () {
viewerCanUnresolve: false,
path: fileName,
diffSide: DiffSide.RIGHT,
line: 372,
originalLine: 372,
startLine: 372,
endLine: 372,
originalStartLine: 372,
originalEndLine: 372,
isOutdated: false,
comments: [
{
Expand Down
21 changes: 11 additions & 10 deletions src/view/pullRequestCommentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ export class PullRequestCommentController implements CommentHandler, CommentReac
.filter(
thread =>
((thread.diffSide === DiffSide.LEFT && isBase) ||
(thread.diffSide === DiffSide.RIGHT && !isBase))
&& (thread.line !== null),
(thread.diffSide === DiffSide.RIGHT && !isBase))
&& (thread.endLine !== null),
)
.map(thread => {
const range = new vscode.Range(
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.startLine - 1, 0),
new vscode.Position(thread.endLine - 1, 0),
);

return createVSCodeCommentThreadForReviewThread(
Expand Down Expand Up @@ -239,7 +239,7 @@ export class PullRequestCommentController implements CommentHandler, CommentReac
const fileName = thread.path;
const index = this._pendingCommentThreadAdds.findIndex(t => {
const samePath = this.gitRelativeRootPath(t.uri.path) === thread.path;
const sameLine = t.range.start.line + 1 === thread.line;
const sameLine = t.range.end.line + 1 === thread.endLine;
return samePath && sameLine;
});

Expand All @@ -261,8 +261,8 @@ export class PullRequestCommentController implements CommentHandler, CommentReac

if (matchingEditor) {
const range = new vscode.Range(
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.startLine - 1, 0),
new vscode.Position(thread.endLine - 1, 0),
);

newThread = createVSCodeCommentThreadForReviewThread(
Expand Down Expand Up @@ -334,8 +334,8 @@ export class PullRequestCommentController implements CommentHandler, CommentReac
const isDraft = isSingleComment
? false
: inDraft !== undefined
? inDraft
: this.pullRequestModel.hasPendingReview;
? inDraft
: this.pullRequestModel.hasPendingReview;
const temporaryCommentId = this.optimisticallyAddComment(thread, input, isDraft);

try {
Expand All @@ -349,6 +349,7 @@ export class PullRequestCommentController implements CommentHandler, CommentReac
input,
fileName,
thread.range.start.line + 1,
thread.range.end.line + 1,
side,
isSingleComment,
);
Expand Down Expand Up @@ -461,7 +462,7 @@ export class PullRequestCommentController implements CommentHandler, CommentReac
const fileName = this.gitRelativeRootPath(thread.uri.path);
const side = this.getCommentSide(thread);
this._pendingCommentThreadAdds.push(thread);
await this.pullRequestModel.createReviewThread(input, fileName, thread.range.start.line + 1, side);
await this.pullRequestModel.createReviewThread(input, fileName, thread.range.start.line + 1, thread.range.end.line + 1, side);
} else {
await this.reply(thread, input, false);
}
Expand Down
43 changes: 26 additions & 17 deletions src/view/reviewCommentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class ReviewCommentController
);

const range = new vscode.Range(
new vscode.Position(thread.originalLine - 1, 0),
new vscode.Position(thread.originalLine - 1, 0),
new vscode.Position(thread.originalStartLine - 1, 0),
new vscode.Position(thread.originalEndLine - 1, 0),
);

return createVSCodeCommentThreadForReviewThread(reviewUri, range, thread, this._commentController);
Expand All @@ -120,13 +120,15 @@ export class ReviewCommentController
path: string,
thread: IReviewThread,
): Promise<GHPRCommentThread> {
let line = thread.line;
let startLine = thread.startLine;
let endLine = thread.endLine;
const localDiff = await this._repository.diffWithHEAD(path);
if (localDiff) {
line = mapOldPositionToNew(localDiff, thread.line);
startLine = mapOldPositionToNew(localDiff, startLine);
endLine = mapOldPositionToNew(localDiff, endLine);
}

const range = new vscode.Range(new vscode.Position(line - 1, 0), new vscode.Position(line - 1, 0));
const range = new vscode.Range(new vscode.Position(startLine - 1, 0), new vscode.Position(endLine - 1, 0));

return createVSCodeCommentThreadForReviewThread(uri, range, thread, this._commentController);
}
Expand Down Expand Up @@ -154,8 +156,8 @@ export class ReviewCommentController
);

const range = new vscode.Range(
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.line - 1, 0),
new vscode.Position(thread.startLine - 1, 0),
new vscode.Position(thread.endLine - 1, 0),
);

return createVSCodeCommentThreadForReviewThread(reviewUri, range, thread, this._commentController);
Expand Down Expand Up @@ -234,8 +236,8 @@ export class ReviewCommentController
}

const diff = await this.getContentDiff(t.uri, fileName);
const line = mapNewPositionToOld(diff, t.range.start.line);
const sameLine = line + 1 === thread.line;
const line = mapNewPositionToOld(diff, t.range.end.line);
const sameLine = line + 1 === thread.endLine;
return sameLine;
});

Expand Down Expand Up @@ -550,15 +552,18 @@ export class ReviewCommentController

// If the thread is on the workspace file, make sure the position
// is properly adjusted to account for any local changes.
let line: number;
let startLine: number;
let endLine: number;
if (side === DiffSide.RIGHT) {
const diff = await this.getContentDiff(thread.uri, fileName);
line = mapNewPositionToOld(diff, thread.range.start.line);
startLine = mapNewPositionToOld(diff, thread.range.start.line);
endLine = mapNewPositionToOld(diff, thread.range.end.line);
} else {
line = thread.range.start.line;
startLine = thread.range.start.line;
endLine = thread.range.end.line;
}

await this._reposManager.activePullRequest!.createReviewThread(input, fileName, line + 1, side);
await this._reposManager.activePullRequest!.createReviewThread(input, fileName, startLine + 1, endLine + 1, side);
} else {
const comment = thread.comments[0];
if (comment instanceof GHPRComment) {
Expand Down Expand Up @@ -649,17 +654,21 @@ export class ReviewCommentController

// If the thread is on the workspace file, make sure the position
// is properly adjusted to account for any local changes.
let line: number;
let startLine: number;
let endLine: number;
if (side === DiffSide.RIGHT) {
const diff = await this.getContentDiff(thread.uri, fileName);
line = mapNewPositionToOld(diff, thread.range.start.line);
startLine = mapNewPositionToOld(diff, thread.range.start.line);
endLine = mapNewPositionToOld(diff, thread.range.end.line);
} else {
line = thread.range.start.line;
startLine = thread.range.start.line;
endLine = thread.range.end.line;
}
await this._reposManager.activePullRequest.createReviewThread(
input,
fileName,
line + 1,
startLine + 1,
endLine + 1,
side,
isSingleComment,
);
Expand Down
8 changes: 4 additions & 4 deletions src/view/treeNodes/fileChangeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ export class FileChangeNode extends TreeNode implements vscode.TreeItem {
);

if (reviewThreadsForNode.length) {
reviewThreadsForNode.sort((a, b) => a.line - b.line);
this.opts.selection = new vscode.Range(reviewThreadsForNode[0].line, 0, reviewThreadsForNode[0].line, 0);
reviewThreadsForNode.sort((a, b) => a.endLine - b.endLine);
this.opts.selection = new vscode.Range(reviewThreadsForNode[0].startLine, 0, reviewThreadsForNode[0].endLine, 0);
} else {
delete this.opts.selection;
}
Expand Down Expand Up @@ -421,10 +421,10 @@ export class GitFileChangeNode extends FileChangeNode implements vscode.TreeItem
const reviewThreadsByFile = groupBy(reviewThreads, t => t.path);
const reviewThreadsForNode = (reviewThreadsByFile[this.fileName] || [])
.filter(thread => thread.isOutdated)
.sort((a, b) => a.line - b.line);
.sort((a, b) => a.endLine - b.endLine);

if (reviewThreadsForNode.length) {
options.selection = new vscode.Range(reviewThreadsForNode[0].originalLine, 0, reviewThreadsForNode[0].originalLine, 0);
options.selection = new vscode.Range(reviewThreadsForNode[0].originalStartLine, 0, reviewThreadsForNode[0].originalEndLine, 0);
}

return {
Expand Down

0 comments on commit b6dcbc5

Please sign in to comment.