Skip to content

Commit 228eb08

Browse files
committed
feat: add Git staging event listeners and diff pre-warming
1 parent 0b82e94 commit 228eb08

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
"diffy-explain-ai.customCommitPrompt": {
155155
"type": "string",
156156
"order": 5,
157-
"default": "Generate a commit message for the following git diff.\n\nRequirements:\n- Maximum subject length: {maxLength} characters\n- Use imperative mood\n- Be concise and clear{bodyInstructions}\n\nReturn ONLY the commit message, no explanations.",
157+
"default": "Generate a commit message for the following git diff.\n\nRequirements:\n- Maximum subject length: {maxLength} characters\n- Use imperative mood\n- Be concise and clear{bodyInstructions}\n\nReturn ONLY the commit message, no explanations.\nDo not wrap the response in code blocks, backticks, or any markdown formatting.",
158158
"editPresentation": "multilineText",
159159
"markdownDescription": "Custom prompt template for commit message generation.\n\nAvailable placeholders: `{maxLength}`, `{bodyInstructions}`, `{locale}`, `{diff}`.\n\nUsed when Commit Message Type is set to `custom`."
160160
},
@@ -236,10 +236,10 @@
236236
"diffy-explain-ai.maxIndexedFileSize": {
237237
"type": "number",
238238
"order": 10,
239-
"default": 100,
239+
"default": 50,
240240
"minimum": 10,
241241
"maximum": 500,
242-
"markdownDescription": "**Maximum File Size for Analysis**\n\nFiles larger than this are skipped during codebase indexing.\n\n**Default:** 100 KB (sufficient for most config files)\n\n**Guidelines:**\n- `50 KB` - Conservative (skips larger READMEs)\n- `100 KB` - Balanced *(recommended)*\n- `200+ KB` - Permissive (may include large docs)\n\n> Larger limits increase token usage and API costs."
242+
"markdownDescription": "**Maximum File Size for Analysis**\n\nFiles larger than this are skipped during codebase indexing.\n\n**Default:** 50 KB (conservative, skips larger READMEs)\n\n**Guidelines:**\n- `50 KB` - Conservative *(recommended)*\n- `100 KB` - Balanced (sufficient for most config files)\n- `200+ KB` - Permissive (may include large docs)\n\n> Larger limits increase token usage and API costs."
243243
},
244244
"diffy-explain-ai.codebaseContextTokenBudget": {
245245
"type": "number",

src/Diffy.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,42 @@ class Diffy extends BaseDiffy {
605605
}
606606
}
607607

608+
/**
609+
* Update staged changes when Git staging area changes
610+
* Pre-warms diff context preparation in background for faster commit generation
611+
*/
612+
async updateStagedChanges() {
613+
try {
614+
// Check if we have a valid workspace and repo
615+
if (!this.workspaceService?.checkAndWarnWorkSpaceExist()) {
616+
return;
617+
}
618+
if (!this.gitService?.checkAndWarnRepoExist()) {
619+
return;
620+
}
621+
622+
const repo = this.gitService?.getCurrentRepo();
623+
if (!repo) {
624+
return;
625+
}
626+
627+
// Get current staged diff in background
628+
const diff = await this.gitService?.getDiffAndWarnUser(repo, false);
629+
if (!diff) {
630+
return;
631+
}
632+
633+
// Pre-warm the diff context preparation in background
634+
this.prepareDiffWithContext(diff).catch((error) => {
635+
logger.error("Error pre-warming diff context", error);
636+
});
637+
638+
logger.info("Staged changes updated - pre-warming diff context");
639+
} catch (error) {
640+
logger.error("Error in updateStagedChanges", error);
641+
}
642+
}
643+
608644
/**
609645
* Dispose all objects
610646
*/

src/extension.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,60 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from "vscode";
4+
import type { API as GitApi, GitExtension, Repository } from "./@types/git";
45
import Diffy from "./Diffy";
56

67
let app: Diffy | null = null;
78

9+
function setupGitStagingEventListeners(context: vscode.ExtensionContext) {
10+
const gitExtension = vscode.extensions.getExtension<GitExtension>("vscode.git");
11+
if (!gitExtension) {
12+
return;
13+
}
14+
15+
const gitApi = gitExtension.exports.getAPI(1);
16+
if (!gitApi) {
17+
return;
18+
}
19+
20+
// Listen for changes in the Git staging area
21+
gitApi.repositories.forEach((repo: Repository) => {
22+
const onDidChangeRepository = repo.state.onDidChange;
23+
if (onDidChangeRepository) {
24+
context.subscriptions.push(
25+
onDidChangeRepository(() => {
26+
// Update the Diffy app when the repository state changes
27+
app?.updateStagedChanges().catch((error) => {
28+
console.error("Error updating staged changes:", error);
29+
});
30+
}),
31+
);
32+
}
33+
});
34+
35+
// Also listen for new repositories being added
36+
context.subscriptions.push(
37+
gitApi.onDidOpenRepository((repo: Repository) => {
38+
const onDidChangeRepository = repo.state.onDidChange;
39+
if (onDidChangeRepository) {
40+
context.subscriptions.push(
41+
onDidChangeRepository(() => {
42+
app?.updateStagedChanges().catch((error) => {
43+
console.error("Error updating staged changes:", error);
44+
});
45+
}),
46+
);
47+
}
48+
}),
49+
);
50+
}
51+
852
export function activate(context: vscode.ExtensionContext) {
953
app = new Diffy(context);
1054
app.init();
1155

56+
setupGitStagingEventListeners(context);
57+
1258
context.subscriptions.push(
1359
vscode.commands.registerCommand("diffy-explain-ai.explainDiffClipboard", () => {
1460
return app?.explainDiffToClipboard();

src/service/WorkspaceService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ Requirements:
167167
- Use imperative mood
168168
- Be concise and clear{bodyInstructions}
169169
170-
Return ONLY the commit message, no explanations.`;
170+
Return ONLY the commit message, no explanations.
171+
Do not wrap the response in code blocks, backticks, or any markdown formatting.`;
171172
}
172173

173174
getIncludeCommitBody() {

0 commit comments

Comments
 (0)