Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
一杯枸杞茶ya
csdn-workflow
提交
204460a6
C
csdn-workflow
项目概览
一杯枸杞茶ya
/
csdn-workflow
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
csdn-workflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
204460a6
编写于
5月 21, 2021
作者:
T
Tomas Vik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor: move diff line counting logic to separate module
上级
aa683750
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
169 addition
and
133 deletion
+169
-133
src/git/diff_line_count.test.ts
src/git/diff_line_count.test.ts
+121
-0
src/git/diff_line_count.ts
src/git/diff_line_count.ts
+41
-0
src/review/commenting_range_provider.test.ts
src/review/commenting_range_provider.test.ts
+5
-86
src/review/commenting_range_provider.ts
src/review/commenting_range_provider.ts
+2
-47
未找到文件。
src/git/diff_line_count.test.ts
0 → 100644
浏览文件 @
204460a6
import
{
diffFile
,
mrVersion
}
from
'
../test_utils/entities
'
;
import
{
getAddedLinesForFile
}
from
'
./diff_line_count
'
;
describe
(
'
diff_line_count
'
,
()
=>
{
describe
(
'
getAddedLinesForFile
'
,
()
=>
{
const
sevenNewLinesHunk
=
[
'
@@ -0,0 +1,7 @@
'
,
'
+new file 2
'
,
'
+
'
,
'
+12
'
,
'
+34
'
,
'
+56
'
,
'
+
'
,
'
+,,,
'
,
''
,
].
join
(
'
\n
'
);
const
hunkWithAddedAndRemovedLines
=
[
`@@ -10,17 +10,17 @@`
,
` // This seems to be a result of only some next-line content triggering this issue and other content doesn't. `
,
` // If there is an empty line after, for example, this issue doesn't not occur`
,
` `
,
`-function containingFunction(){`
,
`- function subFunction(){`
,
`+function containingFunction(): void{`
,
`+ function subFunctionA(): void{`
,
` console.log("Some Output");`
,
` }`
,
` // Issue is not present when the line after the function name and opening { is empty`
,
`- function subFunction(){`
,
`+ function subFunctionB(): void{`
,
` `
,
` console.log("OPutput");`
,
` }`
,
` }`
,
` `
,
`-function anotherFunction(){`
,
`+function anotherFunction(): void{`
,
` console.log("Other Output");`
,
` }`
,
``
,
].
join
(
'
\n
'
);
const
multiHunk
=
[
`@@ -12,9 +12,7 @@`
,
` 12`
,
` 13`
,
` 14`
,
`-15`
,
`-16`
,
`-17`
,
`+117`
,
` 18`
,
` 19`
,
` 20`
,
`@@ -38,9 +36,8 @@`
,
` 38`
,
` 39`
,
` 40`
,
`-41`
,
`-42`
,
`-43`
,
`+441`
,
`+443`
,
` 44`
,
` 45`
,
` 46`
,
`@@ -75,7 +72,6 @@`
,
` 75`
,
` 76`
,
` 77`
,
`-78`
,
` 79`
,
` 80`
,
` 81`
,
`@@ -98,3 +94,5 @@`
,
` 98`
,
` 99`
,
` 100`
,
`+101`
,
`+`
,
].
join
(
'
\n
'
);
// testing that we can parse diffs of files containing hunk headers
const
hunkWithHunkHeaders
=
[
`@@ -1,7 +1,4 @@`
,
` # Initial readme`
,
`-8`
,
`-9`
,
`-@@ -5,14 +1,20 @@`
,
` 0`
,
` 1`
,
` 2`
,
`@@ -19,6 +9,7 @@`
,
` 7`
,
` 8`
,
` 9`
,
`+@@ -5,14 +1,20 @@`
,
` @@ -29,3 +11,3 @@`
,
` 0`
,
` 1`
,
].
join
(
'
\n
'
);
it
.
each
`
hunkName | hunk | newLines
${
'
sevenNewLinesHunk
'
}
|
${
sevenNewLinesHunk
}
|
${[
1
,
2
,
3
,
4
,
5
,
6
,
7
]}
${
'
hunkWithAddedAndRemovedLines
'
}
|
${
hunkWithAddedAndRemovedLines
}
|
${[
13
,
14
,
18
,
24
]}
${
'
multiHunk
'
}
|
${
multiHunk
}
|
${[
15
,
39
,
40
,
97
,
98
]}
${
'
hunkWithHunkHeaders
'
}
|
${
hunkWithHunkHeaders
}
|
${[
12
]}
`
(
'
$hunkName gets correctly parsed
'
,
({
hunk
,
newLines
})
=>
{
const
testMrVersion
=
{
...
mrVersion
,
diffs
:
[{
...
diffFile
,
diff
:
hunk
}],
};
const
ranges
=
getAddedLinesForFile
(
testMrVersion
,
diffFile
.
new_path
);
expect
(
ranges
).
toEqual
(
newLines
);
});
});
});
src/git/diff_line_count.ts
0 → 100644
浏览文件 @
204460a6
const
removeLeadingSlash
=
(
path
:
string
):
string
=>
path
.
replace
(
/^
\/
/
,
''
);
/**
* This method returns line number where in the text document given hunk starts.
* Each hunk header contains information about where the hunk starts for old and new version.
* `@@ -38,9 +36,8 @@` reads: hunk starts at line 38 of the old version and 36 of the new version.
*/
const
getHunkStartingLine
=
(
headerString
=
''
):
number
|
null
=>
{
const
headerMatch
=
headerString
.
match
(
/@@ -
\d
+,
\d
+
\+(\d
+
)
,
\d
+ @@/
);
return
headerMatch
&&
parseInt
(
headerMatch
[
1
],
10
);
};
const
getHunks
=
(
diff
:
string
):
string
[]
=>
{
return
diff
.
replace
(
/^@@/
,
''
)
// remove first @@ because we'll remove all the other @@ by splitting
.
split
(
'
\n
@@
'
)
.
map
(
h
=>
`@@
${
h
}
`
);
// prepend the removed @@ to all hunks
};
const
getAddedLineNumbers
=
(
hunk
:
string
):
number
[]
=>
{
const
hunkLines
=
hunk
.
split
(
'
\n
'
);
const
hunkStartingLine
=
getHunkStartingLine
(
hunkLines
[
0
]);
if
(
!
hunkStartingLine
)
return
[];
const
noRemovedLines
=
hunkLines
.
slice
(
1
,
hunkLines
.
length
).
filter
(
l
=>
!
l
.
startsWith
(
'
-
'
));
return
noRemovedLines
.
reduce
((
addedLines
:
number
[],
l
,
i
)
=>
{
if
(
l
.
startsWith
(
'
+
'
))
{
return
[...
addedLines
,
i
+
hunkStartingLine
];
}
return
addedLines
;
},
[]);
};
export
const
getAddedLinesForFile
=
(
mrVersion
:
RestMrVersion
,
newPath
:
string
):
number
[]
=>
{
// VS Code Uri returns absolute path (leading slash) but GitLab uses relative paths (no leading slash)
const
diff
=
mrVersion
.
diffs
.
find
(
d
=>
d
.
new_path
===
removeLeadingSlash
(
newPath
));
if
(
!
diff
)
return
[];
const
hunks
=
getHunks
(
diff
.
diff
);
const
changedLinesForHunks
=
hunks
.
map
(
h
=>
getAddedLineNumbers
(
h
));
return
changedLinesForHunks
.
reduce
((
acc
,
changedLines
)
=>
[...
acc
,
...
changedLines
],
[]);
};
src/review/commenting_range_provider.test.ts
浏览文件 @
204460a6
...
...
@@ -62,100 +62,19 @@ describe('CommentingRangeProvider', () => {
expect
(
commentingRangeProvider
.
provideCommentingRanges
(
testDocument
)).
toEqual
([]);
});
const
sevenNewLinesHunk
=
[
'
@@ -0,0 +1,7 @@
'
,
'
+new file 2
'
,
'
+
'
,
'
+12
'
,
'
+34
'
,
'
+56
'
,
'
+
'
,
'
+,,,
'
,
''
,
].
join
(
'
\n
'
);
const
threeNewLinesHunk
=
[
'
@@ -0,0 +1,3 @@
'
,
'
+new file 2
'
,
'
+
'
,
'
+12
'
,
''
].
join
(
'
\n
'
);
const
hunkWithAddedAndRemovedLines
=
[
`@@ -10,17 +10,17 @@`
,
` // This seems to be a result of only some next-line content triggering this issue and other content doesn't. `
,
` // If there is an empty line after, for example, this issue doesn't not occur`
,
` `
,
`-function containingFunction(){`
,
`- function subFunction(){`
,
`+function containingFunction(): void{`
,
`+ function subFunctionA(): void{`
,
` console.log("Some Output");`
,
` }`
,
` // Issue is not present when the line after the function name and opening { is empty`
,
`- function subFunction(){`
,
`+ function subFunctionB(): void{`
,
` `
,
` console.log("OPutput");`
,
` }`
,
` }`
,
` `
,
`-function anotherFunction(){`
,
`+function anotherFunction(): void{`
,
` console.log("Other Output");`
,
` }`
,
``
,
].
join
(
'
\n
'
);
const
multiHunk
=
[
`@@ -12,9 +12,7 @@`
,
` 12`
,
` 13`
,
` 14`
,
`-15`
,
`-16`
,
`-17`
,
`+117`
,
` 18`
,
` 19`
,
` 20`
,
`@@ -38,9 +36,8 @@`
,
` 38`
,
` 39`
,
` 40`
,
`-41`
,
`-42`
,
`-43`
,
`+441`
,
`+443`
,
` 44`
,
` 45`
,
` 46`
,
`@@ -75,7 +72,6 @@`
,
` 75`
,
` 76`
,
` 77`
,
`-78`
,
` 79`
,
` 80`
,
` 81`
,
`@@ -98,3 +94,5 @@`
,
` 98`
,
` 99`
,
` 100`
,
`+101`
,
`+`
,
].
join
(
'
\n
'
);
it
.
each
`
hunkName | hunk | newLines
${
'
sevenNewLinesHunk
'
}
|
${
sevenNewLinesHunk
}
|
${[
1
,
2
,
3
,
4
,
5
,
6
,
7
]}
${
'
hunkWithAddedAndRemovedLines
'
}
|
${
hunkWithAddedAndRemovedLines
}
|
${[
13
,
14
,
18
,
24
]}
${
'
multiHunk
'
}
|
${
multiHunk
}
|
${[
15
,
39
,
40
,
97
,
98
]}
`
(
'
$hunkName gets correctly parsed
'
,
({
hunk
,
newLines
})
=>
{
it
(
'
shows correct commenting ranges for a new file
'
,
()
=>
{
commentingRangeProvider
=
new
CommentingRangeProvider
(
mr
,
{
...
mrVersion
,
diffs
:
[{
...
diffFile
,
diff
:
h
unk
}],
diffs
:
[{
...
diffFile
,
diff
:
threeNewLinesH
unk
}],
});
const
ranges
=
commentingRangeProvider
.
provideCommentingRanges
({
uri
:
newFileUri
,
}
as
vscode
.
TextDocument
);
// VS Code indexes lines starting with zero
, adding 1 to normalize line numbers
expect
(
ranges
.
map
(
r
=>
r
.
start
.
line
+
1
)).
toEqual
(
newLines
);
// VS Code indexes lines starting with zero
expect
(
ranges
.
map
(
r
=>
r
.
start
.
line
)).
toEqual
([
0
,
1
,
2
]
);
});
});
src/review/commenting_range_provider.ts
浏览文件 @
204460a6
import
*
as
vscode
from
'
vscode
'
;
import
{
FF_COMMENTING_RANGES
,
REVIEW_URI_SCHEME
}
from
'
../constants
'
;
import
{
getAddedLinesForFile
}
from
'
../git/diff_line_count
'
;
import
{
getExtensionConfiguration
}
from
'
../utils/get_extension_configuration
'
;
import
{
fromReviewUri
}
from
'
./review_uri
'
;
const
removeLeadingSlash
=
(
path
:
string
):
string
=>
path
.
replace
(
/^
\/
/
,
''
);
/**
* This method returns line number where in the text document given hunk starts.
* Each hunk header contains information about where the hunk starts for old and new version.
* `@@ -38,9 +36,8 @@` reads: hunk starts at line 38 of the old version and 36 of the new version.
*/
const
getHunkStartingLine
=
(
headerString
=
''
):
number
|
null
=>
{
const
headerMatch
=
headerString
.
match
(
/@@ -
\d
+,
\d
+
\+(\d
+
)
,
\d
+ @@/
);
return
headerMatch
&&
parseInt
(
headerMatch
[
1
],
10
);
};
const
getHunks
=
(
diff
:
string
):
string
[]
=>
{
return
diff
.
replace
(
/^@@/
,
''
)
// remove first @@ because we'll remove all the other @@ by splitting
.
split
(
'
\n
@@
'
)
.
map
(
h
=>
`@@
${
h
}
`
);
// prepend the removed @@ to all hunks
};
const
getAddedLineNumbers
=
(
hunk
:
string
):
number
[]
=>
{
const
hunkLines
=
hunk
.
split
(
'
\n
'
);
const
hunkStartingLine
=
getHunkStartingLine
(
hunkLines
[
0
]);
if
(
!
hunkStartingLine
)
return
[];
const
noRemovedLines
=
hunkLines
.
slice
(
1
,
hunkLines
.
length
).
filter
(
l
=>
!
l
.
startsWith
(
'
-
'
));
return
noRemovedLines
.
reduce
((
addedLines
:
number
[],
l
,
i
)
=>
{
if
(
l
.
startsWith
(
'
+
'
))
{
return
[...
addedLines
,
i
+
hunkStartingLine
];
}
return
addedLines
;
},
[]);
};
export
const
getAddedLinesFromDiff
=
(
diff
:
string
):
number
[]
=>
{
const
hunks
=
getHunks
(
diff
);
const
changedLinesForHunks
=
hunks
.
map
(
h
=>
getAddedLineNumbers
(
h
));
return
changedLinesForHunks
.
reduce
((
acc
,
changedLines
)
=>
[...
acc
,
...
changedLines
],
[]);
};
export
class
CommentingRangeProvider
implements
vscode
.
CommentingRangeProvider
{
private
mr
:
RestIssuable
;
...
...
@@ -66,16 +28,9 @@ export class CommentingRangeProvider implements vscode.CommentingRangeProvider {
new
vscode
.
Range
(
new
vscode
.
Position
(
0
,
0
),
new
vscode
.
Position
(
document
.
lineCount
-
1
,
0
)),
];
}
const
fileDiff
=
this
.
getNewFileDiff
(
params
.
path
);
if
(
!
fileDiff
)
return
[];
const
result
=
getAddedLinesFromDiff
(
fileDiff
.
diff
);
const
result
=
getAddedLinesForFile
(
this
.
mrVersion
,
params
.
path
);
return
result
.
map
(
l
=>
new
vscode
.
Range
(
new
vscode
.
Position
(
l
-
1
,
0
),
new
vscode
.
Position
(
l
-
1
,
0
)),
);
}
private
getNewFileDiff
(
path
:
string
):
RestDiffFile
|
undefined
{
// VS Code Uri returns absolute path (leading slash) but GitLab uses relative paths (no leading slash)
return
this
.
mrVersion
.
diffs
.
find
(
d
=>
d
.
new_path
===
removeLeadingSlash
(
path
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录