Skip to content

Commit 8e5c7c3

Browse files
committed
feat: commit file before correcting
1 parent 3879945 commit 8e5c7c3

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/utils-ai-vscode/src/commands/correct.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,40 @@ export function correctCommand(context: vscode.ExtensionContext) {
1717
const editor = vscode.window.activeTextEditor
1818

1919
if (editor) {
20+
const editorFilename = editor.document.fileName // Full path to the file
21+
const filename = editorFilename.split('/').pop()
22+
23+
// Commit current file to git before correcting. This is useful for tracking changes.
24+
const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports
25+
const isActivated = gitExtension.enabled
26+
27+
if (!isActivated) {
28+
vscode.window.showErrorMessage('Git is not activated. Please activate Git in VSCode.')
29+
return
30+
}
31+
32+
if (isActivated) {
33+
const git = gitExtension.getAPI(1)
34+
35+
if (git.repositories.length > 0) {
36+
const repository = git.repositories[0]
37+
38+
vscode.window.showInformationMessage(`Committing ${filename}...`)
39+
40+
// Save to be sure to commit the latest changes
41+
await editor.document.save()
42+
43+
try {
44+
await repository.add([editorFilename])
45+
await repository.commit(`chore: save ${filename} before correcting`)
46+
}
47+
catch (error: any) {
48+
// Ignore error because it's mean that the file is already committed
49+
}
50+
}
51+
}
52+
2053
const prompt = getPrompt('spell-checker-md', 'en')
21-
const filename = editor.document.fileName
2254

2355
const hasSelection = !editor.selection.isEmpty
2456
const selection = editor.selection

0 commit comments

Comments
 (0)