Skip to content

Commit

Permalink
feat: integrate extension with bulk git blame
Browse files Browse the repository at this point in the history
  • Loading branch information
mishraprafful committed Jul 9, 2024
1 parent 3e7411e commit 89e7b04
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from "vscode";
import * as cp from "child_process";
import { throttle } from "throttle-debounce";
import { fetchFileBlame, CommitInfo } from "./git";

import {
parseGitBlamePorcelain,
Expand All @@ -15,11 +16,14 @@ const decorationType = vscode.window.createTextEditorDecorationType({
},
});

const FileCommits: Map<string, CommitInfo[]> = new Map();

Check warning on line 19 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Variable name `FileCommits` must match one of the following formats: camelCase, UPPER_CASE

Check warning on line 19 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Variable name `FileCommits` must match one of the following formats: camelCase, UPPER_CASE

Check warning on line 19 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Variable name `FileCommits` must match one of the following formats: camelCase, UPPER_CASE

function showDecoration(e: { readonly textEditor: vscode.TextEditor }) {
const editor = e.textEditor;
const document = editor.document;
const activeLine = document.lineAt(editor.selection.active.line);
const { uri: file, isDirty } = document;


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


const commitInfos = FileCommits.get(file.fsPath);
if (commitInfos !== undefined) {
const thiscommitInfo = commitInfos.find(info => info.lineNumber === n);
// TODO: get values of fields from thiscommitInfo

}
else {
console.log(`No pre-fetched commit information found for ${file.fsPath}`);
// TODO: move obsolete logic from down below here if the commitInfos is not found
}
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file);
const workspaceFolderPath = workspaceFolder?.uri.fsPath;
const options = { cwd: workspaceFolderPath };
Expand Down Expand Up @@ -73,12 +88,41 @@ function showDecoration(e: { readonly textEditor: vscode.TextEditor }) {
});
}

function showFileBlame(e: { readonly textEditor: vscode.TextEditor }) {

const editor = e.textEditor;
const document = editor.document;
const { uri: file, isDirty } = document;

fetchFileBlame(file.fsPath)
.then(commitInfos => {
console.log(`Commit Information for ${file.fsPath}`);
console.log(commitInfos);
FileCommits.set(file.fsPath, commitInfos);
})

Check warning on line 102 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Missing semicolon

Check warning on line 102 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Missing semicolon

Check warning on line 102 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Missing semicolon

}
export function activate(context: vscode.ExtensionContext) {
console.log('Extension "git-line-blame" has activated.');
let showDecorationThrottled = throttle(100, showDecoration);
context.subscriptions.push(
vscode.window.onDidChangeTextEditorSelection(showDecorationThrottled),
vscode.window.onDidChangeTextEditorVisibleRanges(showDecorationThrottled),
vscode.window.onDidChangeActiveTextEditor((e) => {
const editor = vscode.window.activeTextEditor;
if (editor !== undefined && e === editor) {
showFileBlame({ textEditor: editor })

Check warning on line 114 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Missing semicolon

Check warning on line 114 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Missing semicolon

Check warning on line 114 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Missing semicolon
}
}),
vscode.window.onDidChangeVisibleTextEditors(editors => {
const closedEditors = vscode.window.visibleTextEditors.filter(editor =>
!editors.includes(editor)
);
closedEditors.forEach(closedEditor => {
console.log(`Closed file: ${closedEditor.document.fileName}`);
FileCommits.delete(closedEditor.document.fileName);
});
}),
vscode.workspace.onDidSaveTextDocument((e) => {
const editor = vscode.window.activeTextEditor;
if (editor !== undefined && e === editor.document) {
Expand Down

0 comments on commit 89e7b04

Please sign in to comment.