Compare commits

...
Author SHA1 Message Date
abeatrix 6ffba23d3d refactor: convert GitCommitGenerator from module to class
- Convert module pattern to class-based implementation
- Move abort controller to private instance variable
- Add proper method documentation with JSDoc
- Improve encapsulation and code organization
2025-09-02 14:24:11 -07:00
2 changed files with 104 additions and 85 deletions
+4 -2
View File
@@ -481,12 +481,14 @@ export async function activate(context: vscode.ExtensionContext) {
)
// Register the generateGitCommitMessage command handler
const commitGenerator = new GitCommitGenerator()
context.subscriptions.push(
commitGenerator,
vscode.commands.registerCommand("cline.generateGitCommitMessage", async (scm) => {
await GitCommitGenerator?.generate?.(context, scm)
await commitGenerator.generate(context, scm)
}),
vscode.commands.registerCommand("cline.abortGitCommitMessage", () => {
GitCommitGenerator?.abort?.()
commitGenerator.abort()
}),
)
@@ -7,16 +7,17 @@ import { getWorkingState } from "@/utils/git"
import { getCwd } from "@/utils/path"
/**
* Git commit message generator module
* Git commit message generator class
*/
export const GitCommitGenerator = {
generate,
abort,
}
export class GitCommitGenerator {
private commitGenerationAbortController: AbortController | undefined
let commitGenerationAbortController: AbortController | undefined
async function generate(context: vscode.ExtensionContext, scm?: vscode.SourceControl) {
/**
* Generates a commit message based on the current git diff
* @param context VSCode extension context
* @param scm Source control instance
*/
async generate(context: vscode.ExtensionContext, scm?: vscode.SourceControl): Promise<void> {
const cwd = await getCwd()
if (!context || !cwd) {
HostProvider.window.showMessage({
@@ -50,15 +51,22 @@ async function generate(context: vscode.ExtensionContext, scm?: vscode.SourceCon
title: "Generating commit message...",
cancellable: true,
},
() => performCommitGeneration(context, gitDiff, inputBox),
() => this.performCommitGeneration(context, gitDiff, inputBox),
)
}
}
async function performCommitGeneration(context: vscode.ExtensionContext, gitDiff: string, inputBox: any) {
/**
* Performs the actual commit message generation
* @param context VSCode extension context
* @param gitDiff The git diff to generate a message for
* @param inputBox The SCM input box to populate
*/
private async performCommitGeneration(context: vscode.ExtensionContext, gitDiff: string, inputBox: any): Promise<void> {
try {
vscode.commands.executeCommand("setContext", "cline.isGeneratingCommit", true)
const truncatedDiff = gitDiff.length > 5000 ? gitDiff.substring(0, 5000) + "\n\n[Diff truncated due to size]" : gitDiff
const truncatedDiff =
gitDiff.length > 5000 ? gitDiff.substring(0, 5000) + "\n\n[Diff truncated due to size]" : gitDiff
const prompt = `Based on the following git diff, generate a concise and descriptive commit message:
${truncatedDiff}
@@ -87,12 +95,12 @@ Commit message:`
// Create a message for the API
const messages = [{ role: "user" as const, content: prompt }]
commitGenerationAbortController = new AbortController()
this.commitGenerationAbortController = new AbortController()
const stream = apiHandler.createMessage(systemPrompt, messages)
let response = ""
for await (const chunk of stream) {
commitGenerationAbortController.signal.throwIfAborted()
this.commitGenerationAbortController.signal.throwIfAborted()
if (chunk.type === "text") {
response += chunk.text
inputBox.value = extractCommitMessage(response)
@@ -111,11 +119,20 @@ Commit message:`
} finally {
vscode.commands.executeCommand("setContext", "cline.isGeneratingCommit", false)
}
}
}
function abort() {
commitGenerationAbortController?.abort()
/**
* Aborts the current commit message generation
*/
abort(): void {
this.commitGenerationAbortController?.abort()
vscode.commands.executeCommand("setContext", "cline.isGeneratingCommit", false)
}
dispose(): void {
this.commitGenerationAbortController?.abort()
vscode.commands.executeCommand("setContext", "cline.isGeneratingCommit", false)
}
}
/**