Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit 89e7b04

Browse files
committed
feat: integrate extension with bulk git blame
1 parent 3e7411e commit 89e7b04

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/extension.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from "vscode";
22
import * as cp from "child_process";
33
import { throttle } from "throttle-debounce";
4+
import { fetchFileBlame, CommitInfo } from "./git";
45

56
import {
67
parseGitBlamePorcelain,
@@ -15,11 +16,14 @@ const decorationType = vscode.window.createTextEditorDecorationType({
1516
},
1617
});
1718

19+
const FileCommits: Map<string, CommitInfo[]> = new Map();
20+
1821
function showDecoration(e: { readonly textEditor: vscode.TextEditor }) {
1922
const editor = e.textEditor;
2023
const document = editor.document;
2124
const activeLine = document.lineAt(editor.selection.active.line);
2225
const { uri: file, isDirty } = document;
26+
2327

2428
const command = "git";
2529
const n = activeLine.lineNumber;
@@ -29,6 +33,17 @@ function showDecoration(e: { readonly textEditor: vscode.TextEditor }) {
2933
args.push("--content", "-");
3034
}
3135

36+
37+
const commitInfos = FileCommits.get(file.fsPath);
38+
if (commitInfos !== undefined) {
39+
const thiscommitInfo = commitInfos.find(info => info.lineNumber === n);
40+
// TODO: get values of fields from thiscommitInfo
41+
42+
}
43+
else {
44+
console.log(`No pre-fetched commit information found for ${file.fsPath}`);
45+
// TODO: move obsolete logic from down below here if the commitInfos is not found
46+
}
3247
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file);
3348
const workspaceFolderPath = workspaceFolder?.uri.fsPath;
3449
const options = { cwd: workspaceFolderPath };
@@ -73,12 +88,41 @@ function showDecoration(e: { readonly textEditor: vscode.TextEditor }) {
7388
});
7489
}
7590

91+
function showFileBlame(e: { readonly textEditor: vscode.TextEditor }) {
92+
93+
const editor = e.textEditor;
94+
const document = editor.document;
95+
const { uri: file, isDirty } = document;
96+
97+
fetchFileBlame(file.fsPath)
98+
.then(commitInfos => {
99+
console.log(`Commit Information for ${file.fsPath}`);
100+
console.log(commitInfos);
101+
FileCommits.set(file.fsPath, commitInfos);
102+
})
103+
104+
}
76105
export function activate(context: vscode.ExtensionContext) {
77106
console.log('Extension "git-line-blame" has activated.');
78107
let showDecorationThrottled = throttle(100, showDecoration);
79108
context.subscriptions.push(
80109
vscode.window.onDidChangeTextEditorSelection(showDecorationThrottled),
81110
vscode.window.onDidChangeTextEditorVisibleRanges(showDecorationThrottled),
111+
vscode.window.onDidChangeActiveTextEditor((e) => {
112+
const editor = vscode.window.activeTextEditor;
113+
if (editor !== undefined && e === editor) {
114+
showFileBlame({ textEditor: editor })
115+
}
116+
}),
117+
vscode.window.onDidChangeVisibleTextEditors(editors => {
118+
const closedEditors = vscode.window.visibleTextEditors.filter(editor =>
119+
!editors.includes(editor)
120+
);
121+
closedEditors.forEach(closedEditor => {
122+
console.log(`Closed file: ${closedEditor.document.fileName}`);
123+
FileCommits.delete(closedEditor.document.fileName);
124+
});
125+
}),
82126
vscode.workspace.onDidSaveTextDocument((e) => {
83127
const editor = vscode.window.activeTextEditor;
84128
if (editor !== undefined && e === editor.document) {

0 commit comments

Comments
 (0)