diff --git a/.vscode/cody.json b/.vscode/cody.json new file mode 100644 index 0000000..ced276f --- /dev/null +++ b/.vscode/cody.json @@ -0,0 +1,18 @@ +{ + "write-pull-request": { + "description": "Write a pull request for current changes", + "prompt": "Generate a pull request description based on the message provided. The pull request description has three sections: What happened, Insight, and Proof Of Work.\n\n- What happened section provides a brief review and concise description of the changes introduced by the pull request. Below is the format of this section (delimited by triple backticks):\n\n```\n## What happened 👀\n\n- Issue: [Issue number, e.g., #1]\n\nThis PR aims to [concisely describe the changes introduced by the PR]. The main changes include:\n\n- [Change 1]\n- [Change 2]\n- ...\n```\n\n- Insight section explains the rationale behind the chosen solution and why it is the most appropriate approach. When appropriate, consider using Mermaid script to create flowcharts or diagrams that visually represent the changes or the logic flow. Below is the format of this section (delimited by triple backticks):\n\n```\n## Insight 📝\nExplain the rationale behind the chosen solution and why it is the most appropriate approach. \n```\n\n- Proof Of Work section is left blank. Simply add the following text: \"## Proof Of Work 📹\"\n\nYour goal is to encourage self-reliance and comprehension through interactive support to generate the pull request description, aim for clarity, conciseness, and thoroughness in explaining the changes, the reasoning behind them. The description should provide reviewers with a comprehensive understanding of the pull request's purpose and impact. Use simple and straightfoward words, don't overuse buzz words or too fancy words. Keep it concise.\n\nUse these steps for generating description:\n- Get the required information: Ask the user to provide the purpose of the pull request and the issue number first before generating the description.\n- Foster Dialogue: Encourage the user to share thoughts, adapting your guidance based on their feedback while maintaining a supportive tone.\n- When you have all the information, generate the pull request description using the provided template.\n\nEnsure wrapping the generated pull request description in a markdown code block with sixth backticks (```)", + "mode": "ask", + "context": { + "codebase": false, + "command": "scripts/git-diff.sh", + "currentDir": false, + "currentFile": false, + "directoryPath": "", + "filePath": "", + "none": false, + "openTabs": false, + "selection": false + } + }, +} \ No newline at end of file diff --git a/scripts/git-diff.sh b/scripts/git-diff.sh new file mode 100755 index 0000000..3df61d6 --- /dev/null +++ b/scripts/git-diff.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Get the current branch name +current_branch=$(git rev-parse --abbrev-ref HEAD) + +# Get the previous branch name from reflog +previous_branch=$(git reflog | grep -m 1 'checkout: moving' | awk '{print $6}') + +# Count the number of commits +commit_count=$(git rev-list --count $previous_branch..$current_branch) + +# Generate the summary message +echo "The current branch $current_branch branch contains $commit_count commits that have been added since branching from $previous_branch." + +# Loop through each commit and append details to the file +git log $previous_branch..$current_branch --format="%h" | while read commit_hash; do + # Append the commit message with the shorthand hash + echo "Commit: $commit_hash" + git log --pretty=format:"%B" -n 1 $commit_hash + echo "" + + # Print the diff + git diff $commit_hash^! + echo "" +done diff --git a/src/constants/cody.ts b/src/constants/cody.ts index 23ec4c3..0f2537a 100644 --- a/src/constants/cody.ts +++ b/src/constants/cody.ts @@ -1,7 +1,24 @@ +import * as path from 'path' +import * as vscode from 'vscode' + export const CODY_COMMAND = { MENTION: { FILE: 'cody.mention.file' + }, + COMMAND: { + CUSTOM: 'cody.command.custom' } } export const CODY_CUSTOM_COMMANDS_FILE = 'cody.json' + +export function getCodyJsonPath(): string | undefined { + const workspaceFolders = vscode.workspace.workspaceFolders + if (!workspaceFolders) { + console.error('CODY++: No workspace folder is open.') + return undefined + } + + const vscodeFolderPath = path.join(workspaceFolders[0].uri.fsPath, '.vscode') + return path.join(vscodeFolderPath, CODY_CUSTOM_COMMANDS_FILE) +} diff --git a/src/extension.ts b/src/extension.ts index 97bcca3..ebcc934 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -51,4 +51,8 @@ export function activate(context: vscode.ExtensionContext) { ) } -export function deactivate() {} +export function deactivate() { + if (CustomCommandService && CustomCommandService.getInstance()) { + CustomCommandService.getInstance().disposeFileWatcher() + } +} diff --git a/src/services/customCommand.service.ts b/src/services/customCommand.service.ts index 04d9e42..9977c32 100644 --- a/src/services/customCommand.service.ts +++ b/src/services/customCommand.service.ts @@ -1,9 +1,8 @@ import * as fs from 'fs' -import * as path from 'path' import * as vscode from 'vscode' import { z } from 'zod' -import { CODY_CUSTOM_COMMANDS_FILE } from '../constants/cody' +import { CODY_CUSTOM_COMMANDS_FILE, getCodyJsonPath } from '../constants/cody' export const CustomCommandId = z.string() @@ -48,11 +47,13 @@ type CustomCommands = z.infer export class CustomCommandService { private static instance: CustomCommandService private commands: CustomCommands = {} + private fileWatcher: vscode.FileSystemWatcher | undefined private _onDidChangeCommands: vscode.EventEmitter = new vscode.EventEmitter() public readonly onDidChangeCommands: vscode.Event = this._onDidChangeCommands.event private constructor() { this.loadCommands() + this.setupFileWatcher() } public static getInstance(): CustomCommandService { @@ -63,15 +64,12 @@ export class CustomCommandService { } private async loadCommands() { - const workspaceFolders = vscode.workspace.workspaceFolders - if (!workspaceFolders) { - console.error('CODY++: No workspace folder is open.') + const codyJsonPath = getCodyJsonPath() + + if (!codyJsonPath) { return } - const vscodeFolderPath = path.join(workspaceFolders[0].uri.fsPath, '.vscode') - const codyJsonPath = path.join(vscodeFolderPath, CODY_CUSTOM_COMMANDS_FILE) - try { const fileContent = await fs.promises.readFile(codyJsonPath, 'utf-8') const parsedCommands = JSON.parse(fileContent) @@ -84,12 +82,33 @@ export class CustomCommandService { } this.commands = validationResult.data - this._onDidChangeCommands.fire() + this._onDidChangeCommands?.fire() } catch (error: any) { console.error(`CODY++: Failed to load ${CODY_CUSTOM_COMMANDS_FILE}: ${error.message}`) } } + private setupFileWatcher() { + const codyJsonPath = getCodyJsonPath() + + if (!codyJsonPath) { + return + } + + this.fileWatcher = vscode.workspace.createFileSystemWatcher(codyJsonPath) + + this.fileWatcher.onDidChange(() => this.loadCommands()) + this.fileWatcher.onDidCreate(() => this.loadCommands()) + this.fileWatcher.onDidDelete(() => this.loadCommands()) + } + + public disposeFileWatcher() { + if (this.fileWatcher) { + this.fileWatcher.dispose() + this.fileWatcher = undefined + } + } + public getCommands(): CustomCommands { return this.commands } @@ -106,7 +125,6 @@ export class CustomCommandService { public async updateCommand({ id, oldId, data }: UpdateCustomCommand): Promise { // If id !== oldId, we need to delete the old command and add the new one - console.log(id, oldId) if (oldId && id !== oldId) { await this.removeCommand(oldId) await this.addCommand(id, data) @@ -129,15 +147,12 @@ export class CustomCommandService { } private async saveCommands(): Promise { - const workspaceFolders = vscode.workspace.workspaceFolders - if (!workspaceFolders) { - console.error('CODY++: No workspace folder is open.') + const codyJsonPath = getCodyJsonPath() + + if (!codyJsonPath) { return } - const vscodeFolderPath = path.join(workspaceFolders[0].uri.fsPath, '.vscode') - const codyJsonPath = path.join(vscodeFolderPath, `${CODY_CUSTOM_COMMANDS_FILE}`) - try { const fileContent = JSON.stringify(this.commands, null, 2) await fs.promises.writeFile(codyJsonPath, fileContent, 'utf-8') diff --git a/src/views/CustomCommandsTreeView.ts b/src/views/CustomCommandsTreeView.ts index 000813e..bb2b961 100644 --- a/src/views/CustomCommandsTreeView.ts +++ b/src/views/CustomCommandsTreeView.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode' import z from 'zod' +import { CODY_COMMAND } from '../constants/cody' import { CustomCommandService, CustomCommandsSchema } from '../services/customCommand.service' interface CommandTreeItem extends vscode.TreeItem { @@ -53,7 +54,12 @@ export class CustomCommandsTreeView implements vscode.TreeDataProvider