Skip to content

Commit 00d3ee0

Browse files
committed
Cleans up annotations a bit
1 parent 65440a4 commit 00d3ee0

File tree

4 files changed

+101
-149
lines changed

4 files changed

+101
-149
lines changed

src/annotations/annotations.ts

Lines changed: 78 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import {
1717
GitBlameCommit,
1818
GitCommit,
1919
GitDiffHunkLine,
20-
GitRemote,
2120
GitService,
2221
GitUri
2322
} from '../git/gitService';
2423
import { Objects, Strings } from '../system';
2524
import { toRgba } from '../webviews/apps/shared/colors';
26-
import { ContactPresence } from '../vsls/vsls';
2725

2826
export interface ComputedHeatmap {
2927
cold: boolean;
@@ -61,55 +59,37 @@ export class Annotations {
6159
decoration.renderOptions!.before!.borderColor = color;
6260
}
6361

64-
private static getHeatmapColor(date: Date, heatmap: ComputedHeatmap) {
65-
const baseColor = heatmap.cold ? heatmap.colors.cold : heatmap.colors.hot;
66-
67-
const age = heatmap.computeAge(date);
68-
if (age === 0) return baseColor;
69-
70-
if (computedHeatmapColor === undefined || computedHeatmapColor.color !== baseColor) {
71-
let rgba = toRgba(baseColor);
72-
if (rgba == null) {
73-
rgba = toRgba(heatmap.cold ? defaultHeatmapColdColor : defaultHeatmapHotColor)!;
62+
static async changesHoverMessage(
63+
commit: GitBlameCommit,
64+
uri: GitUri,
65+
editorLine: number
66+
): Promise<MarkdownString | undefined> {
67+
let ref;
68+
if (commit.isUncommitted) {
69+
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
70+
ref = uri.sha;
7471
}
75-
76-
const [r, g, b] = rgba;
77-
computedHeatmapColor = {
78-
color: baseColor,
79-
rgb: `${r}, ${g}, ${b}`
80-
};
72+
}
73+
else {
74+
ref = commit.sha;
8175
}
8276

83-
return `rgba(${computedHeatmapColor.rgb}, ${(1 - age / 10).toFixed(2)})`;
84-
}
77+
const line = editorLine + 1;
78+
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
8579

86-
static getHoverMessage(
87-
commit: GitCommit,
88-
dateFormat: string | null,
89-
presence: ContactPresence | undefined,
90-
remotes: GitRemote[],
91-
annotationType?: FileAnnotationType,
92-
line: number = 0
93-
): MarkdownString {
94-
if (dateFormat === null) {
95-
dateFormat = 'MMMM Do, YYYY h:mma';
80+
let originalFileName = commit.originalFileName;
81+
if (originalFileName === undefined) {
82+
if (uri.fsPath !== commit.uri.fsPath) {
83+
originalFileName = commit.fileName;
84+
}
9685
}
9786

98-
const markdown = new MarkdownString(
99-
CommitFormatter.fromTemplate(Container.config.hovers.detailsMarkdownFormat, commit, {
100-
annotationType: annotationType,
101-
dateFormat: dateFormat,
102-
line: line,
103-
markdown: true,
104-
presence: presence,
105-
remotes: remotes
106-
})
107-
);
108-
markdown.isTrusted = true;
109-
return markdown;
87+
const commitEditorLine = commitLine.originalLine - 1;
88+
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref, undefined, originalFileName);
89+
return this.changesHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);
11090
}
11191

112-
static getHoverDiffMessage(
92+
static changesHoverDiffMessage(
11393
commit: GitCommit,
11494
uri: GitUri,
11595
hunkLine: GitDiffHunkLine | undefined,
@@ -156,57 +136,36 @@ export class Annotations {
156136
return markdown;
157137
}
158138

159-
private static getDiffFromHunkLine(hunkLine: GitDiffHunkLine): string {
160-
if (Container.config.hovers.changesDiff === 'hunk') {
161-
return `\`\`\`diff\n${hunkLine.hunk.diff}\n\`\`\``;
162-
}
163-
164-
return `\`\`\`diff${hunkLine.previous === undefined ? '' : `\n-${hunkLine.previous.line}`}${
165-
hunkLine.current === undefined ? '' : `\n+${hunkLine.current.line}`
166-
}\n\`\`\``;
167-
}
168-
169-
static async changesHover(
170-
commit: GitBlameCommit,
139+
static async detailsHoverMessage(
140+
commit: GitCommit,
141+
uri: GitUri,
171142
editorLine: number,
172-
uri: GitUri
173-
): Promise<Partial<DecorationOptions>> {
174-
let ref;
175-
if (commit.isUncommitted) {
176-
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
177-
ref = uri.sha;
178-
}
179-
}
180-
else {
181-
ref = commit.sha;
182-
}
183-
184-
const line = editorLine + 1;
185-
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
186-
187-
let originalFileName = commit.originalFileName;
188-
if (originalFileName === undefined) {
189-
if (uri.fsPath !== commit.uri.fsPath) {
190-
originalFileName = commit.fileName;
191-
}
143+
dateFormat: string | null,
144+
annotationType?: FileAnnotationType
145+
): Promise<MarkdownString> {
146+
if (dateFormat === null) {
147+
dateFormat = 'MMMM Do, YYYY h:mma';
192148
}
193149

194-
const commitEditorLine = commitLine.originalLine - 1;
195-
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref, undefined, originalFileName);
196-
const message = this.getHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);
150+
const [presence, remotes] = await Promise.all([
151+
Container.vsls.getContactPresence(commit.email),
152+
Container.git.getRemotes(commit.repoPath)
153+
]);
197154

198-
return {
199-
hoverMessage: message
200-
};
155+
const markdown = new MarkdownString(
156+
CommitFormatter.fromTemplate(Container.config.hovers.detailsMarkdownFormat, commit, {
157+
annotationType: annotationType,
158+
dateFormat: dateFormat,
159+
line: editorLine,
160+
markdown: true,
161+
presence: presence,
162+
remotes: remotes
163+
})
164+
);
165+
markdown.isTrusted = true;
166+
return markdown;
201167
}
202168

203-
// static detailsHover(commit: GitCommit, dateFormat: string | null, hasRemote: boolean, annotationType?: FileAnnotationType, line: number = 0): DecorationOptions {
204-
// const message = this.getHoverMessage(commit, dateFormat, hasRemote, annotationType);
205-
// return {
206-
// hoverMessage: message
207-
// } as DecorationOptions;
208-
// }
209-
210169
static gutter(
211170
commit: GitCommit,
212171
format: string,
@@ -314,29 +273,6 @@ export class Annotations {
314273
};
315274
}
316275

317-
// static hover(commit: GitCommit, renderOptions: IRenderOptions, now: number): DecorationOptions {
318-
// const decoration = {
319-
// renderOptions: { before: { ...renderOptions } }
320-
// } as DecorationOptions;
321-
322-
// this.applyHeatmap(decoration, commit.date, now);
323-
324-
// return decoration;
325-
// }
326-
327-
// static hoverRenderOptions(heatmap: HeatmapConfig): IRenderOptions {
328-
// if (!heatmap.enabled) return { before: undefined };
329-
330-
// return {
331-
// borderStyle: 'solid',
332-
// borderWidth: '0 0 0 2px',
333-
// contentText: GlyphChars.ZeroWidthSpace,
334-
// height: '100%',
335-
// margin: '0 26px 0 0',
336-
// textDecoration: 'none'
337-
// } as IRenderOptions;
338-
// }
339-
340276
static trailing(
341277
commit: GitCommit,
342278
format: string,
@@ -363,20 +299,35 @@ export class Annotations {
363299
};
364300
}
365301

366-
// static withRange(decoration: DecorationOptions, start?: number, end?: number): DecorationOptions {
367-
// let range = decoration.range;
368-
// if (start !== undefined) {
369-
// range = range.with({
370-
// start: range.start.with({ character: start })
371-
// });
372-
// }
373-
374-
// if (end !== undefined) {
375-
// range = range.with({
376-
// end: range.end.with({ character: end })
377-
// });
378-
// }
379-
380-
// return { ...decoration, range: range };
381-
// }
302+
private static getDiffFromHunkLine(hunkLine: GitDiffHunkLine): string {
303+
if (Container.config.hovers.changesDiff === 'hunk') {
304+
return `\`\`\`diff\n${hunkLine.hunk.diff}\n\`\`\``;
305+
}
306+
307+
return `\`\`\`diff${hunkLine.previous === undefined ? '' : `\n-${hunkLine.previous.line}`}${
308+
hunkLine.current === undefined ? '' : `\n+${hunkLine.current.line}`
309+
}\n\`\`\``;
310+
}
311+
312+
private static getHeatmapColor(date: Date, heatmap: ComputedHeatmap) {
313+
const baseColor = heatmap.cold ? heatmap.colors.cold : heatmap.colors.hot;
314+
315+
const age = heatmap.computeAge(date);
316+
if (age === 0) return baseColor;
317+
318+
if (computedHeatmapColor === undefined || computedHeatmapColor.color !== baseColor) {
319+
let rgba = toRgba(baseColor);
320+
if (rgba == null) {
321+
rgba = toRgba(heatmap.cold ? defaultHeatmapColdColor : defaultHeatmapHotColor)!;
322+
}
323+
324+
const [r, g, b] = rgba;
325+
computedHeatmapColor = {
326+
color: baseColor,
327+
rgb: `${r}, ${g}, ${b}`
328+
};
329+
}
330+
331+
return `rgba(${computedHeatmapColor.rgb}, ${(1 - age / 10).toFixed(2)})`;
332+
}
382333
}

src/annotations/blameAnnotationProvider.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,12 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
234234
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
235235
editorLine = commitLine.originalLine - 1;
236236

237-
const message = Annotations.getHoverMessage(
237+
const message = await Annotations.detailsHoverMessage(
238238
logCommit || commit,
239+
await GitUri.fromUri(document.uri),
240+
editorLine,
239241
Container.config.defaultDateFormat,
240-
await Container.vsls.getContactPresence(commit.email),
241-
await Container.git.getRemotes(commit.repoPath),
242-
this.annotationType,
243-
editorLine
242+
this.annotationType
244243
);
245244
return new Hover(
246245
message,
@@ -256,11 +255,15 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
256255
const commit = await this.getCommitForHover(position);
257256
if (commit === undefined) return undefined;
258257

259-
const hover = await Annotations.changesHover(commit, position.line, await GitUri.fromUri(document.uri));
260-
if (hover.hoverMessage === undefined) return undefined;
258+
const message = await Annotations.changesHoverMessage(
259+
commit,
260+
await GitUri.fromUri(document.uri),
261+
position.line
262+
);
263+
if (message === undefined) return undefined;
261264

262265
return new Hover(
263-
hover.hoverMessage,
266+
message,
264267
document.validateRange(new Range(position.line, 0, position.line, Number.MAX_SAFE_INTEGER))
265268
);
266269
}

src/annotations/recentChangesAnnotationProvider.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
6161
if (cfg.hovers.enabled && cfg.hovers.annotations.enabled) {
6262
if (cfg.hovers.annotations.details) {
6363
this.decorations.push({
64-
hoverMessage: Annotations.getHoverMessage(
64+
hoverMessage: await Annotations.detailsHoverMessage(
6565
commit,
66+
await GitUri.fromUri(this.editor.document.uri),
67+
count,
6668
dateFormat,
67-
await Container.vsls.getContactPresence(commit.email),
68-
await Container.git.getRemotes(commit.repoPath),
69-
this.annotationType,
70-
count
69+
this.annotationType
7170
),
7271
range: range
7372
});
7473
}
7574

7675
if (cfg.hovers.annotations.changes) {
77-
message = Annotations.getHoverDiffMessage(commit, this._uri, hunkLine, count);
76+
message = Annotations.changesHoverDiffMessage(commit, this._uri, hunkLine, count);
7877
if (message === undefined) continue;
7978
}
8079
}

src/hovers/lineHoverController.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,12 @@ export class LineHoverController implements Disposable {
121121
const trackedDocument = await Container.tracker.get(document);
122122
if (trackedDocument === undefined) return undefined;
123123

124-
const message = Annotations.getHoverMessage(
124+
const message = await Annotations.detailsHoverMessage(
125125
logCommit || commit,
126+
trackedDocument.uri,
127+
editorLine,
126128
Container.config.defaultDateFormat,
127-
await Container.vsls.getContactPresence(commit.email),
128-
await Container.git.getRemotes(commit.repoPath),
129-
fileAnnotations,
130-
editorLine
129+
fileAnnotations
131130
);
132131
return new Hover(message, range);
133132
}
@@ -161,10 +160,10 @@ export class LineHoverController implements Disposable {
161160
const trackedDocument = await Container.tracker.get(document);
162161
if (trackedDocument === undefined) return undefined;
163162

164-
const hover = await Annotations.changesHover(commit, position.line, trackedDocument.uri);
165-
if (hover.hoverMessage === undefined) return undefined;
163+
const message = await Annotations.changesHoverMessage(commit, trackedDocument.uri, position.line);
164+
if (message === undefined) return undefined;
166165

167-
return new Hover(hover.hoverMessage, range);
166+
return new Hover(message, range);
168167
}
169168

170169
private register(editor: TextEditor | undefined) {

0 commit comments

Comments
 (0)