提交 734f48c6 编写于 作者: R Rachel Macfarlane

Update comment position when reopening dirty files

上级 cd6cb3bb
......@@ -185,6 +185,7 @@ export function mapOldPositionToNew(patch: string, line: number): number {
} else if (diffHunk.oldLineNumber + diffHunk.oldLength - 1 < line) {
delta += diffHunk.newLength - diffHunk.oldLength;
} else {
delta += diffHunk.newLength - diffHunk.oldLength;
return line + delta;
}
......
......@@ -291,6 +291,41 @@ export class Repository {
}
}
async getFileObjectId(commit: string, path: string): Promise<string> {
try {
const args = ['rev-parse', `${commit}:${path}`];
const result = await GitProcess.exec(args, this.path);
return result.stdout.trim();
} catch (e) {
return '';
}
}
async hashObject(text: string): Promise<string> {
try {
const args = ['hash-object', '-w', '--stdin'];
const result = await GitProcess.exec(args, this.path, {
stdin: text
});
return result.stdout.trim();
} catch (e) {
return '';
}
}
async diffHashed(hash0: string, hash1: string) {
try {
const args = ['diff', hash0, hash1];
const result = await GitProcess.exec(args, this.path);
return result.stdout.trim();
} catch (e) {
return '';
}
}
async diff(filePath: string, compareWithCommit: string): Promise<string> {
try {
let args = ['diff', filePath];
......
......@@ -405,8 +405,8 @@ export class ReviewManager implements vscode.DecorationProvider {
onDidChangeCommentThreads: this._onDidChangeCommentThreads.event,
provideDocumentComments: async (document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CommentInfo> => {
let ranges: vscode.Range[] = [];
let matchingComments: Comment[];
if (document.uri.scheme === 'review') {
// from scm viewlet
matchingComments = this._commentsCache.get(document.uri.toString());
......@@ -434,12 +434,24 @@ export class ReviewManager implements vscode.DecorationProvider {
}
} else if (document.uri.scheme === 'file') {
// local file
let fileName = document.uri.path;
let matchedFiles = this._localFileChanges.filter(fileChange => path.resolve(this._repository.path, fileChange.fileName) === fileName);
const fileName = document.uri.path;
const matchedFiles = this._localFileChanges.filter(fileChange => path.resolve(this._repository.path, fileChange.fileName) === fileName);
if (matchedFiles && matchedFiles.length) {
let matchedFile = matchedFiles[0];
// git diff sha -- fileName
let contentDiff = await this._repository.diff(matchedFile.fileName, this._lastCommitSha);
const matchedFile = matchedFiles[0];
let contentDiff: string;
if (document.isDirty) {
const documentText = document.getText();
const idAtLastCommit = await this._repository.getFileObjectId(this._lastCommitSha, matchedFile.fileName);
const idOfCurrentText = await this._repository.hashObject(documentText);
// git diff <blobid> <blobid>
contentDiff = await this._repository.diffHashed(idAtLastCommit, idOfCurrentText);
} else {
// git diff sha -- fileName
contentDiff = await this._repository.diff(matchedFile.fileName, this._lastCommitSha);
}
matchingComments = this._comments.filter(comment => path.resolve(this._repository.path, comment.path) === fileName);
matchingComments = mapCommentsToHead(matchedFile.patch, contentDiff, matchingComments);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册