From b57c517d7dca275787f641efa0bcc6b857039710 Mon Sep 17 00:00:00 2001 From: mishraprafful Date: Tue, 9 Jul 2024 23:03:11 +0200 Subject: [PATCH] feat: integrate extension with bulk git blame --- src/extension.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index c2d66f9..6a85342 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, @@ -15,11 +16,14 @@ const decorationType = vscode.window.createTextEditorDecorationType({ }, }); +const FileCommits: Map = new Map(); + 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; @@ -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 }; @@ -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); + }) + +} 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 }) + } + }), + 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) {