Skip to content

Commit

Permalink
Cleans up annotations a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 5, 2019
1 parent 65440a4 commit 00d3ee0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 149 deletions.
205 changes: 78 additions & 127 deletions src/annotations/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import {
GitBlameCommit,
GitCommit,
GitDiffHunkLine,
GitRemote,
GitService,
GitUri
} from '../git/gitService';
import { Objects, Strings } from '../system';
import { toRgba } from '../webviews/apps/shared/colors';
import { ContactPresence } from '../vsls/vsls';

export interface ComputedHeatmap {
cold: boolean;
Expand Down Expand Up @@ -61,55 +59,37 @@ export class Annotations {
decoration.renderOptions!.before!.borderColor = color;
}

private static getHeatmapColor(date: Date, heatmap: ComputedHeatmap) {
const baseColor = heatmap.cold ? heatmap.colors.cold : heatmap.colors.hot;

const age = heatmap.computeAge(date);
if (age === 0) return baseColor;

if (computedHeatmapColor === undefined || computedHeatmapColor.color !== baseColor) {
let rgba = toRgba(baseColor);
if (rgba == null) {
rgba = toRgba(heatmap.cold ? defaultHeatmapColdColor : defaultHeatmapHotColor)!;
static async changesHoverMessage(
commit: GitBlameCommit,
uri: GitUri,
editorLine: number
): Promise<MarkdownString | undefined> {
let ref;
if (commit.isUncommitted) {
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
ref = uri.sha;
}

const [r, g, b] = rgba;
computedHeatmapColor = {
color: baseColor,
rgb: `${r}, ${g}, ${b}`
};
}
else {
ref = commit.sha;
}

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

static getHoverMessage(
commit: GitCommit,
dateFormat: string | null,
presence: ContactPresence | undefined,
remotes: GitRemote[],
annotationType?: FileAnnotationType,
line: number = 0
): MarkdownString {
if (dateFormat === null) {
dateFormat = 'MMMM Do, YYYY h:mma';
let originalFileName = commit.originalFileName;
if (originalFileName === undefined) {
if (uri.fsPath !== commit.uri.fsPath) {
originalFileName = commit.fileName;
}
}

const markdown = new MarkdownString(
CommitFormatter.fromTemplate(Container.config.hovers.detailsMarkdownFormat, commit, {
annotationType: annotationType,
dateFormat: dateFormat,
line: line,
markdown: true,
presence: presence,
remotes: remotes
})
);
markdown.isTrusted = true;
return markdown;
const commitEditorLine = commitLine.originalLine - 1;
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref, undefined, originalFileName);
return this.changesHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);
}

static getHoverDiffMessage(
static changesHoverDiffMessage(
commit: GitCommit,
uri: GitUri,
hunkLine: GitDiffHunkLine | undefined,
Expand Down Expand Up @@ -156,57 +136,36 @@ export class Annotations {
return markdown;
}

private static getDiffFromHunkLine(hunkLine: GitDiffHunkLine): string {
if (Container.config.hovers.changesDiff === 'hunk') {
return `\`\`\`diff\n${hunkLine.hunk.diff}\n\`\`\``;
}

return `\`\`\`diff${hunkLine.previous === undefined ? '' : `\n-${hunkLine.previous.line}`}${
hunkLine.current === undefined ? '' : `\n+${hunkLine.current.line}`
}\n\`\`\``;
}

static async changesHover(
commit: GitBlameCommit,
static async detailsHoverMessage(
commit: GitCommit,
uri: GitUri,
editorLine: number,
uri: GitUri
): Promise<Partial<DecorationOptions>> {
let ref;
if (commit.isUncommitted) {
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
ref = uri.sha;
}
}
else {
ref = commit.sha;
}

const line = editorLine + 1;
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];

let originalFileName = commit.originalFileName;
if (originalFileName === undefined) {
if (uri.fsPath !== commit.uri.fsPath) {
originalFileName = commit.fileName;
}
dateFormat: string | null,
annotationType?: FileAnnotationType
): Promise<MarkdownString> {
if (dateFormat === null) {
dateFormat = 'MMMM Do, YYYY h:mma';
}

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

return {
hoverMessage: message
};
const markdown = new MarkdownString(
CommitFormatter.fromTemplate(Container.config.hovers.detailsMarkdownFormat, commit, {
annotationType: annotationType,
dateFormat: dateFormat,
line: editorLine,
markdown: true,
presence: presence,
remotes: remotes
})
);
markdown.isTrusted = true;
return markdown;
}

// static detailsHover(commit: GitCommit, dateFormat: string | null, hasRemote: boolean, annotationType?: FileAnnotationType, line: number = 0): DecorationOptions {
// const message = this.getHoverMessage(commit, dateFormat, hasRemote, annotationType);
// return {
// hoverMessage: message
// } as DecorationOptions;
// }

static gutter(
commit: GitCommit,
format: string,
Expand Down Expand Up @@ -314,29 +273,6 @@ export class Annotations {
};
}

// static hover(commit: GitCommit, renderOptions: IRenderOptions, now: number): DecorationOptions {
// const decoration = {
// renderOptions: { before: { ...renderOptions } }
// } as DecorationOptions;

// this.applyHeatmap(decoration, commit.date, now);

// return decoration;
// }

// static hoverRenderOptions(heatmap: HeatmapConfig): IRenderOptions {
// if (!heatmap.enabled) return { before: undefined };

// return {
// borderStyle: 'solid',
// borderWidth: '0 0 0 2px',
// contentText: GlyphChars.ZeroWidthSpace,
// height: '100%',
// margin: '0 26px 0 0',
// textDecoration: 'none'
// } as IRenderOptions;
// }

static trailing(
commit: GitCommit,
format: string,
Expand All @@ -363,20 +299,35 @@ export class Annotations {
};
}

// static withRange(decoration: DecorationOptions, start?: number, end?: number): DecorationOptions {
// let range = decoration.range;
// if (start !== undefined) {
// range = range.with({
// start: range.start.with({ character: start })
// });
// }

// if (end !== undefined) {
// range = range.with({
// end: range.end.with({ character: end })
// });
// }

// return { ...decoration, range: range };
// }
private static getDiffFromHunkLine(hunkLine: GitDiffHunkLine): string {
if (Container.config.hovers.changesDiff === 'hunk') {
return `\`\`\`diff\n${hunkLine.hunk.diff}\n\`\`\``;
}

return `\`\`\`diff${hunkLine.previous === undefined ? '' : `\n-${hunkLine.previous.line}`}${
hunkLine.current === undefined ? '' : `\n+${hunkLine.current.line}`
}\n\`\`\``;
}

private static getHeatmapColor(date: Date, heatmap: ComputedHeatmap) {
const baseColor = heatmap.cold ? heatmap.colors.cold : heatmap.colors.hot;

const age = heatmap.computeAge(date);
if (age === 0) return baseColor;

if (computedHeatmapColor === undefined || computedHeatmapColor.color !== baseColor) {
let rgba = toRgba(baseColor);
if (rgba == null) {
rgba = toRgba(heatmap.cold ? defaultHeatmapColdColor : defaultHeatmapHotColor)!;
}

const [r, g, b] = rgba;
computedHeatmapColor = {
color: baseColor,
rgb: `${r}, ${g}, ${b}`
};
}

return `rgba(${computedHeatmapColor.rgb}, ${(1 - age / 10).toFixed(2)})`;
}
}
19 changes: 11 additions & 8 deletions src/annotations/blameAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,12 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];
editorLine = commitLine.originalLine - 1;

const message = Annotations.getHoverMessage(
const message = await Annotations.detailsHoverMessage(
logCommit || commit,
await GitUri.fromUri(document.uri),
editorLine,
Container.config.defaultDateFormat,
await Container.vsls.getContactPresence(commit.email),
await Container.git.getRemotes(commit.repoPath),
this.annotationType,
editorLine
this.annotationType
);
return new Hover(
message,
Expand All @@ -256,11 +255,15 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase
const commit = await this.getCommitForHover(position);
if (commit === undefined) return undefined;

const hover = await Annotations.changesHover(commit, position.line, await GitUri.fromUri(document.uri));
if (hover.hoverMessage === undefined) return undefined;
const message = await Annotations.changesHoverMessage(
commit,
await GitUri.fromUri(document.uri),
position.line
);
if (message === undefined) return undefined;

return new Hover(
hover.hoverMessage,
message,
document.validateRange(new Range(position.line, 0, position.line, Number.MAX_SAFE_INTEGER))
);
}
Expand Down
11 changes: 5 additions & 6 deletions src/annotations/recentChangesAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
if (cfg.hovers.enabled && cfg.hovers.annotations.enabled) {
if (cfg.hovers.annotations.details) {
this.decorations.push({
hoverMessage: Annotations.getHoverMessage(
hoverMessage: await Annotations.detailsHoverMessage(
commit,
await GitUri.fromUri(this.editor.document.uri),
count,
dateFormat,
await Container.vsls.getContactPresence(commit.email),
await Container.git.getRemotes(commit.repoPath),
this.annotationType,
count
this.annotationType
),
range: range
});
}

if (cfg.hovers.annotations.changes) {
message = Annotations.getHoverDiffMessage(commit, this._uri, hunkLine, count);
message = Annotations.changesHoverDiffMessage(commit, this._uri, hunkLine, count);
if (message === undefined) continue;
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/hovers/lineHoverController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ export class LineHoverController implements Disposable {
const trackedDocument = await Container.tracker.get(document);
if (trackedDocument === undefined) return undefined;

const message = Annotations.getHoverMessage(
const message = await Annotations.detailsHoverMessage(
logCommit || commit,
trackedDocument.uri,
editorLine,
Container.config.defaultDateFormat,
await Container.vsls.getContactPresence(commit.email),
await Container.git.getRemotes(commit.repoPath),
fileAnnotations,
editorLine
fileAnnotations
);
return new Hover(message, range);
}
Expand Down Expand Up @@ -161,10 +160,10 @@ export class LineHoverController implements Disposable {
const trackedDocument = await Container.tracker.get(document);
if (trackedDocument === undefined) return undefined;

const hover = await Annotations.changesHover(commit, position.line, trackedDocument.uri);
if (hover.hoverMessage === undefined) return undefined;
const message = await Annotations.changesHoverMessage(commit, trackedDocument.uri, position.line);
if (message === undefined) return undefined;

return new Hover(hover.hoverMessage, range);
return new Hover(message, range);
}

private register(editor: TextEditor | undefined) {
Expand Down

0 comments on commit 00d3ee0

Please sign in to comment.