-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
blameAnnotationProvider.ts
230 lines (192 loc) · 6.55 KB
/
blameAnnotationProvider.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict';
import { CancellationToken, Disposable, Hover, languages, Position, Range, TextDocument, TextEditor } from 'vscode';
import { FileAnnotationType } from '../config';
import { Container } from '../container';
import { GitBlame, GitBlameCommit, GitCommit } from '../git/git';
import { GitUri } from '../git/gitUri';
import { Hovers } from '../hovers/hovers';
import { log } from '../system';
import { GitDocumentState, TrackedDocument } from '../trackers/gitDocumentTracker';
import { AnnotationProviderBase } from './annotationProvider';
import { ComputedHeatmap, getHeatmapColors } from './annotations';
export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase {
protected blame: Promise<GitBlame | undefined>;
protected hoverProviderDisposable: Disposable | undefined;
constructor(
annotationType: FileAnnotationType,
editor: TextEditor,
trackedDocument: TrackedDocument<GitDocumentState>,
) {
super(annotationType, editor, trackedDocument);
this.blame = editor.document.isDirty
? Container.git.getBlameForFileContents(this.trackedDocument.uri, editor.document.getText())
: Container.git.getBlameForFile(this.trackedDocument.uri);
if (editor.document.isDirty) {
trackedDocument.setForceDirtyStateChangeOnNextDocumentChange();
}
}
override clear() {
if (this.hoverProviderDisposable != null) {
this.hoverProviderDisposable.dispose();
this.hoverProviderDisposable = undefined;
}
super.clear();
}
async validate(): Promise<boolean> {
const blame = await this.blame;
return blame != null && blame.lines.length !== 0;
}
protected async getBlame(): Promise<GitBlame | undefined> {
const blame = await this.blame;
if (blame == null || blame.lines.length === 0) return undefined;
return blame;
}
@log({ args: false })
protected async getComputedHeatmap(blame: GitBlame): Promise<ComputedHeatmap> {
const dates: Date[] = [];
let commit;
let previousSha;
for (const l of blame.lines) {
if (previousSha === l.sha) continue;
previousSha = l.sha;
commit = blame.commits.get(l.sha);
if (commit == null) continue;
dates.push(commit.date);
}
dates.sort((a, b) => a.getTime() - b.getTime());
const coldThresholdDate = new Date();
coldThresholdDate.setDate(coldThresholdDate.getDate() - (Container.config.heatmap.ageThreshold || 90));
const coldThresholdTimestamp = coldThresholdDate.getTime();
const hotDates: Date[] = [];
const coldDates: Date[] = [];
for (const d of dates) {
if (d.getTime() < coldThresholdTimestamp) {
coldDates.push(d);
} else {
hotDates.push(d);
}
}
let lookupTable:
| number[]
| {
hot: number[];
cold: number[];
};
if (hotDates.length && coldDates.length) {
lookupTable = {
hot: getRelativeAgeLookupTable(hotDates),
cold: getRelativeAgeLookupTable(coldDates),
};
} else {
lookupTable = getRelativeAgeLookupTable(dates);
}
return {
coldThresholdTimestamp: coldThresholdTimestamp,
colors: await getHeatmapColors(),
computeRelativeAge: (date: Date) => {
const lookup = Array.isArray(lookupTable)
? lookupTable
: date.getTime() < coldThresholdTimestamp
? lookupTable.cold
: lookupTable.hot;
const time = date.getTime();
let index = 0;
for (let i = 0; i < lookup.length; i++) {
index = i;
if (time >= lookup[i]) break;
}
return index;
},
};
}
registerHoverProviders(providers: { details: boolean; changes: boolean }) {
if (
!Container.config.hovers.enabled ||
!Container.config.hovers.annotations.enabled ||
(!providers.details && !providers.changes)
) {
return;
}
this.hoverProviderDisposable = languages.registerHoverProvider(
{ pattern: this.document.uri.fsPath },
{
provideHover: (document: TextDocument, position: Position, token: CancellationToken) =>
this.provideHover(providers, document, position, token),
},
);
}
async provideHover(
providers: { details: boolean; changes: boolean },
document: TextDocument,
position: Position,
_token: CancellationToken,
): Promise<Hover | undefined> {
if (Container.config.hovers.annotations.over !== 'line' && position.character !== 0) return undefined;
if (this.document.uri.toString() !== document.uri.toString()) return undefined;
const blame = await this.getBlame();
if (blame == null) return undefined;
const line = blame.lines[position.line];
const commit = blame.commits.get(line.sha);
if (commit == null) return undefined;
const messages = (
await Promise.all([
providers.details ? this.getDetailsHoverMessage(commit, document) : undefined,
providers.changes
? Hovers.changesMessage(commit, await GitUri.fromUri(document.uri), position.line)
: undefined,
])
).filter(<T>(m?: T): m is T => Boolean(m));
return new Hover(
messages,
document.validateRange(new Range(position.line, 0, position.line, Number.MAX_SAFE_INTEGER)),
);
}
private async getDetailsHoverMessage(commit: GitBlameCommit, document: TextDocument) {
// Get the full commit message -- since blame only returns the summary
let logCommit: GitCommit | undefined = undefined;
if (!commit.isUncommitted) {
logCommit = await Container.git.getCommitForFile(commit.repoPath, commit.uri.fsPath, {
ref: commit.sha,
});
if (logCommit != null) {
// Preserve the previous commit from the blame commit
logCommit.previousFileName = commit.previousFileName;
logCommit.previousSha = commit.previousSha;
}
}
let editorLine = this.editor.selection.active.line;
const line = editorLine + 1;
const commitLine = commit.lines.find(l => l.line === line) ?? commit.lines[0];
editorLine = commitLine.originalLine - 1;
return Hovers.detailsMessage(
logCommit ?? commit,
await GitUri.fromUri(document.uri),
editorLine,
Container.config.hovers.detailsMarkdownFormat,
Container.config.defaultDateFormat,
{
autolinks: Container.config.hovers.autolinks.enabled,
pullRequests: {
enabled: Container.config.hovers.pullRequests.enabled,
},
},
);
}
}
function getRelativeAgeLookupTable(dates: Date[]) {
const lookup: number[] = [];
const half = Math.floor(dates.length / 2);
const median = dates.length % 2 ? dates[half].getTime() : (dates[half - 1].getTime() + dates[half].getTime()) / 2.0;
const newest = dates[dates.length - 1].getTime();
let step = (newest - median) / 5;
for (let i = 5; i > 0; i--) {
lookup.push(median + step * i);
}
lookup.push(median);
const oldest = dates[0].getTime();
step = (median - oldest) / 4;
for (let i = 1; i <= 4; i++) {
lookup.push(median - step * i);
}
return lookup;
}