Skip to content

Commit

Permalink
fix: diff lines api.
Browse files Browse the repository at this point in the history
  • Loading branch information
cangzhang committed Mar 24, 2021
1 parent 92b63ac commit 8c16aa6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/codingServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { IRepoInfo, ISessionData, TokenType } from 'src/typings/commonTypes';
import { keychain } from 'src/common/keychain';
import Logger from 'src/common/logger';

const AUTH_SERVER = `https://ftxwn9.coding-pages.com`;
const AUTH_SERVER = `https://x5p7m.csb.app/`;
const ClientId = `ff768664c96d04235b1cc4af1e3b37a8`;
const ClientSecret = `d29ebb32cab8b5f0a643b5da7dcad8d1469312c7`;

Expand Down Expand Up @@ -723,7 +723,6 @@ export class CodingServer {
try {
const { repoApiPrefix } = await this.getApiPrefix();
const resp: IFileDiffResp = await got
// .get(`http://127.0.0.1:5000/api/git/compare_with_path`, {
.get(`${repoApiPrefix}/compare_with_path`, {
searchParams: {
access_token: this._session?.accessToken,
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ const HunkRegExp = /@@.+@@/g;
export const isHunkLine = (hunk: string) => HunkRegExp.test(hunk);

export const getDiffLineNumber = (hunk: string) => {
const matchedHunks = hunk.match(/[-+]\d+,\d+/g) || [];
const matchedHunks = hunk.match(/[-+]\d+(,\d+)?/g) || [];
return matchedHunks.map((i) => {
const [start, sum] = i.match(/\d+/g)?.map((j) => +j) || [0, 0];
const [start = 0, sum = 0] = i.match(/\d+/g)?.map((j) => +j) ?? [];
const end = start + sum > 0 ? start + sum - 1 : 0;
return [start, end];
});
Expand Down
92 changes: 54 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function activate(context: vscode.ExtensionContext) {
'Merge request diff comments',
);
context.subscriptions.push(commentController);
const commentResolveData: { [key: string]: boolean } = {};

commentController.commentingRangeProvider = {
provideCommentingRanges: async (
Expand All @@ -65,12 +66,12 @@ export async function activate(context: vscode.ExtensionContext) {

try {
const params = new URLSearchParams(decodeURIComponent(document.uri.query));
const iid = params.get('mr') || ``;
const mrId = params.get('id') || ``;
let param: IFileDiffParam = {
path: params.get('path') ?? ``,
base: params.get('leftSha') ?? ``,
compare: params.get('rightSha') ?? ``,
mergeRequestId: iid ?? ``,
mergeRequestId: mrId ?? ``,
};
const {
data: { diffLines },
Expand All @@ -83,7 +84,9 @@ export async function activate(context: vscode.ExtensionContext) {

const [left, right] = getDiffLineNumber(i.text);
const [start, end] = params.get('right') === `true` ? right : left;
result.push(new vscode.Range(start, 0, end, 0));
if (start > 0) {
result.push(new vscode.Range(start - 1, 0, end, 0));
}
return result;
}, [] as vscode.Range[]);
return ret;
Expand Down Expand Up @@ -241,10 +244,10 @@ export async function activate(context: vscode.ExtensionContext) {
async (file: IFileNode, mr: IMRData) => {
const headUri = vscode.Uri.parse(file.path, false).with({
scheme: MRUriScheme,
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=true&mr=${mr.iid}`,
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=true&mr=${mr.iid}&id=${mr.id}`,
});
const parentUri = headUri.with({
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=false&mr=${mr.iid}`,
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=false&mr=${mr.iid}&id=${mr.id}`,
});
await vscode.commands.executeCommand(
`vscode.diff`,
Expand All @@ -254,43 +257,49 @@ export async function activate(context: vscode.ExtensionContext) {
{ preserveFocus: true },
);

const identifier = `${mr.iid}/${file.path}`;
if (commentResolveData[identifier]) {
return;
}

try {
const commentResp = await codingSrv.getMRComments(mr.iid);

(commentResp.data as IDiffComment[][])
.filter((i) => {
const first = i[0];
return !first.outdated && first.path === file.path;
}, [])
.forEach((i) => {
const root = i[0];
const isLeft = root.change_type === 2;
const isRight = root.change_type === 1;
const validComments = (commentResp.data as IDiffComment[][]).filter((i) => {
const first = i[0];
return !first.outdated && first.path === file.path;
}, []);

validComments.forEach((i) => {
const root = i[0];
const isLeft = root.change_type === 2;
const isRight = root.change_type === 1;

const rootLine = root.diffFile.diffLines[root.diffFile.diffLines.length - 1];
const lineNum = isLeft ? rootLine.leftNo - 1 : rootLine.rightNo - 1;
const range = new vscode.Range(lineNum - 1, 0, lineNum - 1, 0);
const rootLine = root.diffFile.diffLines[root.diffFile.diffLines.length - 1];
const lineNum = isLeft ? rootLine.leftNo - 1 : rootLine.rightNo - 1;
const range = new vscode.Range(lineNum - 1, 0, lineNum - 1, 0);

const commentList: vscode.Comment[] = i.map((c) => {
const body = new vscode.MarkdownString(tdService.turndown(c.content));
body.isTrusted = true;
const comment: vscode.Comment = {
mode: vscode.CommentMode.Preview,
body,
author: {
name: `${c.author.name}(${c.author.global_key})`,
iconPath: vscode.Uri.parse(c.author.avatar, false),
},
};
return comment;
});
const commentThread = commentController.createCommentThread(
isRight ? headUri : parentUri,
range,
commentList,
);
commentThread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded;
const commentList: vscode.Comment[] = i.map((c) => {
const body = new vscode.MarkdownString(tdService.turndown(c.content));
body.isTrusted = true;
const comment: vscode.Comment = {
mode: vscode.CommentMode.Preview,
body,
author: {
name: `${c.author.name}(${c.author.global_key})`,
iconPath: vscode.Uri.parse(c.author.avatar, false),
},
};
return comment;
});
const commentThread = commentController.createCommentThread(
isRight ? headUri : parentUri,
range,
commentList,
);
commentThread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded;
});
commentResolveData[identifier] = true;
} finally {
}
},
Expand All @@ -300,16 +309,23 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(
`codingPlugin.diff.createComment`,
(reply: vscode.CommentReply) => {
async (reply: vscode.CommentReply) => {
replyNote(reply, context);
},
),
);
context.subscriptions.push(
vscode.commands.registerCommand(
`codingPlugin.diff.replyComment`,
(reply: vscode.CommentReply) => {
async (reply: vscode.CommentReply) => {
replyNote(reply, context);
const params = new URLSearchParams(decodeURIComponent(reply.thread.uri.query));
const isRight = params.get('right') === `true`;
const noteable_id = params.get('id'); // mr index id
const commitId = isRight ? params.get('rightSha') : params.get('leftSha');
const content = encodeURIComponent(reply.text);
const noteable_type = `MergeRequestBean`;
const change_type = isRight ? 1 : 2;
},
),
);
Expand Down
1 change: 0 additions & 1 deletion src/reviewCommentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function replyNote(reply: vscode.CommentReply, context: vscode.ExtensionC
};
const thread = reply.thread;
thread.contextValue = `editable`;
const params = new URLSearchParams(decodeURIComponent(thread.uri.query));
const newComment = new ReviewComment(
reply.text,
vscode.CommentMode.Preview,
Expand Down

0 comments on commit 8c16aa6

Please sign in to comment.