This repository was archived by the owner on Feb 19, 2020. It is now read-only.
forked from codecov/sourcegraph-codecov
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdecoration.ts
More file actions
83 lines (80 loc) · 2.45 KB
/
decoration.ts
File metadata and controls
83 lines (80 loc) · 2.45 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
import { LineCoverage, FileLineCoverage } from './model'
import { Settings } from './settings'
import { hsla, RED_HUE, GREEN_HUE, YELLOW_HUE } from './colors'
import { TextDocumentDecoration, Range } from 'sourcegraph'
export function codecovToDecorations(
settings: Pick<
Settings,
'codecov.decorations.lineCoverage' | 'codecov.decorations.lineHitCounts'
>,
data: FileLineCoverage
): TextDocumentDecoration[] {
if (!data) {
return []
}
const decorations: TextDocumentDecoration[] = []
for (const [lineStr, coverage] of Object.entries(data)) {
if (coverage === null) {
continue
}
const line = parseInt(lineStr) - 1 // 0-indexed line
const decoration: TextDocumentDecoration = {
range: new Range(line, 0, line, 0),
isWholeLine: true,
}
if (settings['codecov.decorations.lineCoverage']) {
decoration.backgroundColor = lineColor(coverage, 0.7, 0.25)
}
if (settings['codecov.decorations.lineHitCounts']) {
decoration.after = {
backgroundColor: lineColor(coverage, 0.7, 1),
color: lineColor(coverage, 0.25, 1),
...lineText(coverage),
}
}
decorations.push(decoration)
}
return decorations
}
function lineColor(
coverage: LineCoverage,
lightness: number,
alpha: number
): string {
let hue: number
if (coverage === 0 || coverage === null) {
hue = RED_HUE
} else if (
typeof coverage === 'number' ||
coverage.hits === coverage.branches
) {
hue = GREEN_HUE
} else {
hue = YELLOW_HUE // partially covered
}
return hsla(hue, lightness, alpha)
}
function lineText(
coverage: LineCoverage
): { contentText?: string; hoverMessage?: string } {
if (coverage === null) {
return {}
}
if (typeof coverage === 'number') {
if (coverage >= 1) {
return {
contentText: ` ${coverage} `,
hoverMessage: `${coverage} hit${
coverage === 1 ? '' : 's'
} (Codecov)`,
}
}
return { hoverMessage: 'not covered by test (Codecov)' }
}
return {
contentText: ` ${coverage.hits}/${coverage.branches} `,
hoverMessage: `${coverage.hits}/${coverage.branches} branch${
coverage.branches === 1 ? '' : 'es'
} hit (Codecov)`,
}
}