Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion src/Blame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

export const ZERO_HASH = '0000000000000000000000000000000000000000';

const NOT_COMMITTED_AUTHOR = {
name: 'Not Committed Yet',
email: 'not.committed.yet',
}

Check failure on line 39 in src/Blame.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Missing semicolon

Check failure on line 39 in src/Blame.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Missing semicolon

const createAuthor = (author: string, authorStyle: 'full' | 'first' | 'last'): BlamedAuthor => {
const blamedAuthor = { name: author, displayName: author };
switch (authorStyle) {
Expand All @@ -52,14 +57,31 @@
const createDate = (seconds: number, dateFormat: string): BlamedDate => {
const d = new Date(seconds * 1000);
return {
dateString: `${d.getFullYear()}${d.getMonth()}${d.getDate()}`,
dateString: `${d.getFullYear()}${d.getMonth()}${d.getDate()}`,
date: d,
localDate: parseDate(d, dateFormat),
dateMillis: d.getTime(),
timeString: `${prependZero(d.getHours())}:${prependZero(d.getMinutes())}`
};
};


export const createNotCommittedLine = (filename: string, line: string, linenumber: string): BlamedDocument => {
const authorStyle = Settings.getAuthorStyle();
const dateFormat = Settings.getDateFormat();

return {
hash: ZERO_HASH,
author: createAuthor(NOT_COMMITTED_AUTHOR.name, authorStyle),
email: NOT_COMMITTED_AUTHOR.email,
codeline: line,
linenumber: linenumber,
date: createDate(Date.now() / 1000, dateFormat),
summary: `Version of ${filename} from ${filename}`,
filename: filename,
};
};

export const blame = async (document: vscode.TextDocument): Promise<BlamedDocument[]> => {
const blamed = (await blameFile(document.fileName)).split("\n");
log.trace('Got blamed file');
Expand Down
5 changes: 3 additions & 2 deletions src/BlameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as vscode from 'vscode';
import DecorationManager from './DecorationManager';
import HeatMapManager from './HeatMapManager';
import { BlamedDocument, blame } from './Blame';
import { BlamedDocument, blame, createNotCommittedLine } from './Blame';
import { getFilename } from './Utils';
import ExtensionManager from './ExtensionManager';
import { blameFile } from './Git';
Expand Down Expand Up @@ -109,7 +109,8 @@ ${content}
const add = event.contentChanges.find(change => change?.text.match(/\n/) && change?.range.start.line === change.range.end.line);
const remove = event.contentChanges.find(change => change?.text === '' && change.range.start.line < change.range.end.line);
if (add) {
this.blamedDocument.splice(add.range.start.line + 1, 0, { hash: '0' } as BlamedDocument);
const notCommittedLine = createNotCommittedLine(getFilename(event.document.fileName), add.text, add.range.start.line.toString());
this.blamedDocument.splice(add.range.start.line + 1, 0, notCommittedLine);
} else if (remove) {
this.blamedDocument.splice(remove.range.start.line + 1, 1);
}
Expand Down
14 changes: 6 additions & 8 deletions src/HeatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { BlamedDocument, BlamedDate } from './Blame';
import Settings from './Settings';
import { isDarkTheme } from './Utils';

export interface IndexedHeatMap {
[key: string]: string | vscode.ThemeColor;
}
export type IndexedHeatMap = Map<number, string | vscode.ThemeColor>;

type RGBC = {
r: number;
Expand All @@ -32,7 +30,7 @@ export const indexHeatColors = (blamedDocument: BlamedDocument[], heatColors: st

distinctDates.sort((a, b) => b.dateMillis - a.dateMillis);

const indexedHeatMap = {} as IndexedHeatMap;
const indexedHeatMap = new Map<number, string | vscode.ThemeColor>();

const strategy = Settings.getHeatColorIndexStrategy();

Expand All @@ -41,26 +39,26 @@ export const indexHeatColors = (blamedDocument: BlamedDocument[], heatColors: st
const last = distinctDates.pop();

if (first) {
indexedHeatMap[first.dateString] = heatColors[0];
indexedHeatMap.set(first.dateMillis, heatColors[0]);
}

const commitPerColor = Math.max(1, Math.round(distinctDates.length / (heatColors.length - 2)));

let colorIndex = 1;
distinctDates.forEach((date, idx) => {
indexedHeatMap[date.dateString] = heatColors[colorIndex] || heatColors.at(-1)!;
indexedHeatMap.set(date.dateMillis, heatColors[colorIndex] || heatColors.at(-1)!);
if (idx % commitPerColor === 0) {
colorIndex++;
}
});

if (last) {
indexedHeatMap[last.dateString] = heatColors.at(-1)!;
indexedHeatMap.set(last.dateMillis, heatColors.at(-1)!);
}
} else {
distinctDates.forEach((date, idx) => {
if (idx < heatColors.length) {
indexedHeatMap[date.dateString] = heatColors[idx];
indexedHeatMap.set(date.dateMillis, heatColors[idx]);
}
});
}
Expand Down
23 changes: 18 additions & 5 deletions src/HeatMapManager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* License GPL-2.0
*/
import { generateHeatMapColors, IndexedHeatMap, indexHeatColors } from './HeatMap';
import * as vscode from 'vscode';
import { BlamedDate, BlamedDocument } from './Blame';
import { isDarkTheme } from './Utils';
import { generateHeatMapColors, IndexedHeatMap, indexHeatColors } from './HeatMap';
import Settings from './Settings';
import { isDarkTheme } from './Utils';

class HeatMapManager {

private heatMap: IndexedHeatMap = {};
private heatMap: IndexedHeatMap = new Map<number, string | vscode.ThemeColor>();
private heatColors: string[] = [];
private isDarkTheme: boolean = isDarkTheme();

Expand All @@ -20,8 +21,20 @@ class HeatMapManager {
this.heatMap = indexHeatColors(document, this.heatColors);
}

getHeatColor(date: BlamedDate) {
return this.heatMap[date.dateString] || this.heatColors.at(-1);
getHeatColor(date: BlamedDate): string | vscode.ThemeColor {
return this.heatMap.get(date.dateMillis) || this.getClosestColor(date);
}

// Find the closest color for a given date. Used when the date is not directly indexed
// e.g.: for uncommitted/unsaved changes.
private getClosestColor(date: BlamedDate): string | vscode.ThemeColor {
const sortedKeys = Array.from(this.heatMap.keys()).sort((a, b) => b - a);
for (const key of sortedKeys) {
if (key <= date.dateMillis) {
return this.heatMap.get(key)!;
}
}
return this.heatColors.at(-1)!;
}

refreshColors() {
Expand Down
Loading