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
177 lines (147 loc) · 5.75 KB
/
Copy pathdiffPositionMapping.ts
File metadata and controls
177 lines (147 loc) · 5.75 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
/*---------------------------------------------------------------------------------------------
* 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 { Comment } 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 the old line number of the comment should be considered. If it's -1, the comment
* is on a line that is entirely new, so the comment should not be displayed on the base file. This means
* that for the modified file, if the comment has a non-negative old line number, it has already been
* displayed on the base and does not need to be shown again.
* @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: Comment, fileDiffHunks: DiffHunk[], isBase: boolean): number {
let commentAbsolutePosition = -1;
// Ignore outdated comments
if (comment.position !== null) {
let diffLine = getDiffLineByPosition(fileDiffHunks, comment.position!);
if (diffLine) {
if (isBase) {
commentAbsolutePosition = diffLine.oldLineNumber;
} else {
commentAbsolutePosition = diffLine.oldLineNumber > 0 ? -1 : 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: Comment, fileDiffHunks: DiffHunk[], isBase: boolean): number {
let commentAbsolutePosition = -1;
// Ignore outdated comments
if (comment.position !== null) {
let 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;
let prDiffReader = parseDiffHunk(prPatch);
let prDiffIter = prDiffReader.next();
while (!prDiffIter.done) {
let 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++) {
let 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 {
let localDiffReader = parseDiffHunk(localDiff);
let localDiffIter = localDiffReader.next();
let lineInPRDiff = line;
while (!localDiffIter.done) {
let diffHunk = localDiffIter.value;
if (diffHunk.oldLineNumber > line) {
break;
} else {
lineInPRDiff += diffHunk.oldLength - diffHunk.newLength;
}
localDiffIter = localDiffReader.next();
}
let positionInDiffHunk = -1;
for (let i = 0; i < diffHunks.length; i++) {
let 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 {
let diffReader = parseDiffHunk(patch);
let diffIter = diffReader.next();
let delta = 0;
while (!diffIter.done) {
let 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: Comment[]) {
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
// Diff line is null when the original line the comment was on has been removed
const diffLine = getDiffLineByPosition(diffHunks, comment.position || comment.originalPosition!);
if (diffLine) {
const positionInPr = diffLine.type === DiffChangeType.Delete ? diffLine.oldLineNumber : diffLine.newLineNumber;
const newPosition = mapOldPositionToNew(localDiff, positionInPr);
comment.absolutePosition = newPosition;
}
}
return comments;
}