-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gutterBlameAnnotationProvider.ts
234 lines (187 loc) · 7.02 KB
/
gutterBlameAnnotationProvider.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
231
232
233
234
import { DecorationOptions, Range, TextEditor, ThemableDecorationAttachmentRenderOptions } from 'vscode';
import { FileAnnotationType, GravatarDefaultStyle } from '../configuration';
import { GlyphChars } from '../constants';
import { Container } from '../container';
import { CommitFormatOptions, CommitFormatter } from '../git/formatters';
import { GitBlame, GitCommit } from '../git/models';
import { Logger } from '../logger';
import { Arrays, Iterables, Strings } from '../system';
import { log } from '../system/decorators/log';
import { Stopwatch } from '../system/stopwatch';
import { GitDocumentState } from '../trackers/gitDocumentTracker';
import { TrackedDocument } from '../trackers/trackedDocument';
import { AnnotationContext } from './annotationProvider';
import { Annotations } from './annotations';
import { BlameAnnotationProviderBase } from './blameAnnotationProvider';
import { Decorations } from './fileAnnotationController';
export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase {
constructor(editor: TextEditor, trackedDocument: TrackedDocument<GitDocumentState>, container: Container) {
super(FileAnnotationType.Blame, editor, trackedDocument, container);
}
override clear() {
super.clear();
if (Decorations.gutterBlameHighlight != null) {
try {
this.editor.setDecorations(Decorations.gutterBlameHighlight, []);
} catch {}
}
}
@log()
async onProvideAnnotation(context?: AnnotationContext, _type?: FileAnnotationType): Promise<boolean> {
const cc = Logger.getCorrelationContext();
this.annotationContext = context;
const blame = await this.getBlame();
if (blame == null) return false;
const sw = new Stopwatch(cc!);
const cfg = this.container.config.blame;
// Precalculate the formatting options so we don't need to do it on each iteration
const tokenOptions = Strings.getTokensFromTemplate(cfg.format).reduce<{
[token: string]: Strings.TokenOptions | undefined;
}>((map, token) => {
map[token.key] = token.options;
return map;
}, Object.create(null));
let getBranchAndTagTips;
if (CommitFormatter.has(cfg.format, 'tips')) {
getBranchAndTagTips = await this.container.git.getBranchesAndTagsTipsFn(blame.repoPath);
}
const options: CommitFormatOptions = {
dateFormat: cfg.dateFormat === null ? this.container.config.defaultDateFormat : cfg.dateFormat,
getBranchAndTagTips: getBranchAndTagTips,
tokenOptions: tokenOptions,
};
const avatars = cfg.avatars;
const gravatarDefault = this.container.config.defaultGravatarsStyle;
const separateLines = cfg.separateLines;
const renderOptions = Annotations.gutterRenderOptions(
separateLines,
cfg.heatmap,
cfg.avatars,
cfg.format,
options,
);
const decorationOptions = [];
const decorationsMap = new Map<string, DecorationOptions | undefined>();
const avatarDecorationsMap = avatars ? new Map<string, ThemableDecorationAttachmentRenderOptions>() : undefined;
let commit: GitCommit | undefined;
let compacted = false;
let gutter: DecorationOptions | undefined;
let previousSha: string | undefined;
let computedHeatmap;
if (cfg.heatmap.enabled) {
computedHeatmap = await this.getComputedHeatmap(blame);
}
for (const l of blame.lines) {
// editor lines are 0-based
const editorLine = l.line - 1;
if (previousSha === l.sha) {
if (gutter == null) continue;
// Use a shallow copy of the previous decoration options
gutter = { ...gutter };
if (cfg.compact && !compacted) {
// Since we are wiping out the contextText make sure to copy the objects
gutter.renderOptions = {
before: {
...gutter.renderOptions!.before,
contentText: GlyphChars.Space.repeat(
Strings.getWidth(gutter.renderOptions!.before!.contentText!),
),
},
};
if (separateLines) {
gutter.renderOptions.before!.textDecoration = `none;box-sizing: border-box${
avatars ? ';padding: 0 0 0 18px' : ''
}`;
}
compacted = true;
}
gutter.range = new Range(editorLine, 0, editorLine, 0);
decorationOptions.push(gutter);
continue;
}
compacted = false;
previousSha = l.sha;
commit = blame.commits.get(l.sha);
if (commit == null) continue;
gutter = decorationsMap.get(l.sha);
if (gutter != null) {
gutter = {
...gutter,
range: new Range(editorLine, 0, editorLine, 0),
};
decorationOptions.push(gutter);
continue;
}
gutter = Annotations.gutter(commit, cfg.format, options, renderOptions) as DecorationOptions;
if (computedHeatmap != null) {
Annotations.applyHeatmap(gutter, commit.date, computedHeatmap);
}
gutter.range = new Range(editorLine, 0, editorLine, 0);
decorationOptions.push(gutter);
if (avatars && commit.author.email != null) {
await this.applyAvatarDecoration(commit, gutter, gravatarDefault, avatarDecorationsMap!);
}
decorationsMap.set(l.sha, gutter);
}
sw.restart({ suffix: ' to compute gutter blame annotations' });
if (decorationOptions.length) {
this.setDecorations([
{ decorationType: Decorations.gutterBlameAnnotation, rangesOrOptions: decorationOptions },
]);
sw.stop({ suffix: ' to apply all gutter blame annotations' });
}
this.registerHoverProviders(this.container.config.hovers.annotations);
return true;
}
@log({ args: false })
async selection(selection?: AnnotationContext['selection'], blame?: GitBlame): Promise<void> {
if (selection === false || Decorations.gutterBlameHighlight == null) return;
if (blame == null) {
blame = await this.blame;
if (!blame?.lines.length) return;
}
let sha: string | undefined = undefined;
if (selection?.sha != null) {
sha = selection.sha;
} else if (selection?.line != null) {
if (selection.line >= 0) {
const commitLine = blame.lines[selection.line];
sha = commitLine?.sha;
}
} else {
sha = Iterables.first(blame.commits.values()).sha;
}
if (!sha) {
this.editor.setDecorations(Decorations.gutterBlameHighlight, []);
return;
}
const highlightDecorationRanges = Arrays.filterMap(blame.lines, l =>
l.sha === sha
? // editor lines are 0-based
this.editor.document.validateRange(new Range(l.line - 1, 0, l.line - 1, Number.MAX_SAFE_INTEGER))
: undefined,
);
this.editor.setDecorations(Decorations.gutterBlameHighlight, highlightDecorationRanges);
}
private async applyAvatarDecoration(
commit: GitCommit,
gutter: DecorationOptions,
gravatarDefault: GravatarDefaultStyle,
map: Map<string, ThemableDecorationAttachmentRenderOptions>,
) {
let avatarDecoration = map.get(commit.author.email ?? '');
if (avatarDecoration == null) {
const url = (await commit.getAvatarUri({ defaultStyle: gravatarDefault, size: 16 })).toString(true);
avatarDecoration = {
contentText: '',
height: '16px',
width: '16px',
textDecoration: `none;position:absolute;top:1px;left:5px;background:url(${encodeURI(
url,
)});background-size:16px 16px;margin-left: 0 !important`,
};
map.set(commit.author.email ?? '', avatarDecoration);
}
gutter.renderOptions!.after = avatarDecoration;
}
}