forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffPositionMapping.ts
More file actions
186 lines (153 loc) · 5.78 KB
/
Copy pathdiffPositionMapping.ts
File metadata and controls
186 lines (153 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { DiffLine, DiffHunk, parseDiffHunk, DiffChangeType } from './diffHunk';
import { IComment } from './comment';
/**
* Line position in a git diff is 1 based, except for the case when the original or changed file have
* no content, in which case it is 0. Normalize the position to be zero based.
* @param line The line in a file from the diff header
*/
export function getZeroBased(line: number): number {
if (line === undefined || line === 0) {
return 0;
}
return line - 1;
}
/**
* Returns the absolute position of a comment in a file. If the comment is outdated, returns -1.
*
* For the base file, only comments that are on deleted lines should be displayed. For the modified file,
* all other comments should be shown. Check the line type to avoid duplicating comments across these files.
* @param comment The comment
* @param fileDiffHunks The diff hunks of the file
* @param isBase Whether the file, if a diff, is the base or modified
*/
export function getAbsolutePosition(comment: IComment, fileDiffHunks: DiffHunk[], isBase: boolean): number {
let commentAbsolutePosition = -1;
// Ignore outdated comments
if (comment.position !== null) {
const diffLine = getDiffLineByPosition(fileDiffHunks, comment.position!);
if (diffLine) {
if (isBase && diffLine.type === DiffChangeType.Delete) {
commentAbsolutePosition = diffLine.oldLineNumber;
}
if (!isBase && diffLine.type !== DiffChangeType.Delete) {
commentAbsolutePosition = diffLine.newLineNumber;
}
}
}
return commentAbsolutePosition;
}
/**
* Returns the position of the comment within the diff. This is simply the comment.position property,
* but the method ensures that the comment will be shown on the correct side of the diff by
* returning -1 when the comment's line is an addition and the document is the base and vice versa.
* @param comment The comment
* @param fileDiffHunks The diff hunks of the file
* @param isBase Whether the file, if a diff, is the base or modified
*/
export function getPositionInDiff(comment: IComment, fileDiffHunks: DiffHunk[], isBase: boolean): number {
let commentAbsolutePosition = -1;
// Ignore outdated comments
if (comment.position !== null) {
const diffLine = getDiffLineByPosition(fileDiffHunks, comment.position!);
if (diffLine) {
if ((diffLine.type === DiffChangeType.Add && !isBase) || (diffLine.type === DiffChangeType.Delete && isBase)) {
commentAbsolutePosition = comment.position!;
}
}
}
return commentAbsolutePosition;
}
export function getLastDiffLine(prPatch: string): DiffLine | undefined {
let lastDiffLine = undefined;
const prDiffReader = parseDiffHunk(prPatch);
let prDiffIter = prDiffReader.next();
while (!prDiffIter.done) {
const diffHunk = prDiffIter.value;
lastDiffLine = diffHunk.diffLines[diffHunk.diffLines.length - 1];
prDiffIter = prDiffReader.next();
}
return lastDiffLine;
}
export function getDiffLineByPosition(diffHunks: DiffHunk[], diffLineNumber: number): DiffLine | undefined {
for (let i = 0; i < diffHunks.length; i++) {
const diffHunk = diffHunks[i];
for (let j = 0; j < diffHunk.diffLines.length; j++) {
if (diffHunk.diffLines[j].positionInHunk === diffLineNumber) {
return diffHunk.diffLines[j];
}
}
}
return undefined;
}
export function mapHeadLineToDiffHunkPosition(diffHunks: DiffHunk[], localDiff: string, line: number, isBase: boolean = false): number {
const localDiffReader = parseDiffHunk(localDiff);
let localDiffIter = localDiffReader.next();
let lineInPRDiff = line;
while (!localDiffIter.done) {
const diffHunk = localDiffIter.value;
if (diffHunk.oldLineNumber > line) {
break;
} else {
lineInPRDiff += diffHunk.oldLength - diffHunk.newLength;
}
localDiffIter = localDiffReader.next();
}
const positionInDiffHunk = -1;
for (let i = 0; i < diffHunks.length; i++) {
const diffHunk = diffHunks[i];
for (let j = 0; j < diffHunk.diffLines.length; j++) {
if (isBase) {
if (diffHunk.diffLines[j].oldLineNumber === lineInPRDiff) {
return diffHunk.diffLines[j].positionInHunk;
}
} else {
if (diffHunk.diffLines[j].newLineNumber === lineInPRDiff) {
return diffHunk.diffLines[j].positionInHunk;
}
}
}
}
return positionInDiffHunk;
}
export function mapOldPositionToNew(patch: string, line: number): number {
const diffReader = parseDiffHunk(patch);
let diffIter = diffReader.next();
let delta = 0;
while (!diffIter.done) {
const diffHunk = diffIter.value;
if (diffHunk.oldLineNumber > line) {
// No-op
} else if (diffHunk.oldLineNumber + diffHunk.oldLength - 1 < line) {
delta += diffHunk.newLength - diffHunk.oldLength;
} else {
delta += diffHunk.newLength - diffHunk.oldLength;
return line + delta;
}
diffIter = diffReader.next();
}
return line + delta;
}
export function mapCommentsToHead(diffHunks: DiffHunk[], localDiff: string, comments: IComment[]) {
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
// Ignore outdated comments
if (comment.position === null || comment.position === undefined) {
continue;
}
// Diff line is null when the original line the comment was on has been removed
const diffLine = getDiffLineByPosition(diffHunks, comment.position);
if (diffLine) {
// Ignore comments which are on deletions
if (diffLine.type === DiffChangeType.Delete) {
continue;
}
const newPosition = mapOldPositionToNew(localDiff, diffLine.newLineNumber);
comment.absolutePosition = newPosition;
}
}
return comments;
}