Skip to content

Commit d20b00d

Browse files
committed
feat(preview): sync preview document with git input box on save
- Add tracking of preview document state with new variable - Implement save handler to sync preview content to git input box - Add close handler to cleanup preview document reference - Register new event handlers in extension subscriptions - Refactor preview document handling for better state management
1 parent 6e27934 commit d20b00d

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/extension.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const defaultTemperature = 0.4
77
const defaultAllowedTypes = ["feat", "fix", "refactor", "chore", "docs", "style", "test", "perf", "ci"]
88

99
export function activate(context: vscode.ExtensionContext) {
10+
let previewDocument: vscode.TextDocument | undefined
11+
1012
function getRepo(): any | undefined {
1113
const gitExtension = vscode.extensions.getExtension("vscode.git")?.exports
1214
if (!gitExtension) {
@@ -236,12 +238,11 @@ export function activate(context: vscode.ExtensionContext) {
236238
return
237239
}
238240

239-
// Create a new untitled markdown document with the commit message
240-
const document = await vscode.workspace.openTextDocument({
241+
previewDocument = await vscode.workspace.openTextDocument({
241242
content: commitMessage,
242243
language: "markdown",
243244
})
244-
await vscode.window.showTextDocument(document)
245+
await vscode.window.showTextDocument(previewDocument)
245246
} catch (error) {
246247
console.error("Error opening commit message preview:", error)
247248
vscode.window.showErrorMessage(
@@ -250,13 +251,30 @@ export function activate(context: vscode.ExtensionContext) {
250251
}
251252
})
252253

253-
// Push all commands to subscriptions
254+
const onSave = vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
255+
if (document === previewDocument) {
256+
const gitRepo = getRepo()
257+
if (gitRepo) {
258+
gitRepo.inputBox.value = document.getText()
259+
}
260+
}
261+
})
262+
263+
const onClose = vscode.workspace.onDidCloseTextDocument((document: vscode.TextDocument) => {
264+
if (document === previewDocument) {
265+
previewDocument = undefined
266+
}
267+
})
268+
269+
// Push all commands and listeners to subscriptions
254270
context.subscriptions.push(
255271
cmdGenerateCommitMessage,
256272
cmdPreviewCommitMessage,
257273
cmdUpdateAPIKey,
258274
cmdGetAPIKey,
259275
cmdDeleteAPIKey,
276+
onSave,
277+
onClose,
260278
)
261279
}
262280

0 commit comments

Comments
 (0)