diff --git a/src/context/contextCodeSelected.ts b/src/context/contextCodeSelected.ts index 63f23a1..4ce8427 100644 --- a/src/context/contextCodeSelected.ts +++ b/src/context/contextCodeSelected.ts @@ -15,10 +15,15 @@ export async function handleCodeSelected(fileSelected: string, codeSelected: str const languageId = await getLanguageIdByFileName(fileSelected); // convert fileContent to markdown code block with languageId and file path - const markdownCodeBlock = `\`\`\`${languageId} path=${fileSelected}\n${codeSelected}\n\`\`\``; + const data = { + languageId: languageId, + path: fileSelected, + content: codeSelected + }; + const jsonData = JSON.stringify(data); // save markdownCodeBlock to temp file - await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(markdownCodeBlock)); + await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(jsonData)); return `[context|${tempFile}]`; } \ No newline at end of file diff --git a/src/context/contextCustomCommand.ts b/src/context/contextCustomCommand.ts index c4c9b91..35dc85d 100644 --- a/src/context/contextCustomCommand.ts +++ b/src/context/contextCustomCommand.ts @@ -1,7 +1,7 @@ import * as path from 'path'; import * as vscode from 'vscode'; import { ChatContext } from './contextManager'; -import { createTempSubdirectory, runCommandAndWriteOutput, runCommandStringAndWriteOutput } from '../util/commonUtil'; +import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; export const customCommandContext: ChatContext = { name: '', diff --git a/src/context/contextFileSelected.ts b/src/context/contextFileSelected.ts index 57d259e..dec5aa1 100644 --- a/src/context/contextFileSelected.ts +++ b/src/context/contextFileSelected.ts @@ -18,10 +18,15 @@ export async function handleFileSelected(fileSelected: string) { const languageId = await getLanguageIdByFileName(fileSelected); // convert fileContent to markdown code block with languageId and file path - const markdownCodeBlock = `\`\`\`${languageId} path=${fileSelected}\n${fileContent}\n\`\`\``; + const data = { + languageId: languageId, + path: fileSelected, + content: fileContent + }; + const jsonData = JSON.stringify(data); // save markdownCodeBlock to temp file - await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(markdownCodeBlock)); + await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(jsonData)); return `[context|${tempFile}]`; } \ No newline at end of file diff --git a/src/context/contextGitDiff.ts b/src/context/contextGitDiff.ts index 1ea46cc..600ede1 100644 --- a/src/context/contextGitDiff.ts +++ b/src/context/contextGitDiff.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import { ChatContext } from './contextManager'; -import { createTempSubdirectory, runCommandAndWriteOutput } from '../util/commonUtil'; +import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; export const gitDiffContext: ChatContext = { name: 'git diff', @@ -8,7 +8,7 @@ export const gitDiffContext: ChatContext = { handler: async () => { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'diff_all.txt'); - const result = await runCommandAndWriteOutput('git', ['diff'], diff_file); + const result = await runCommandStringAndWriteOutput('git diff', diff_file); console.log(result.exitCode); console.log(result.stdout); console.log(result.stderr); diff --git a/src/context/contextGitDiffCached.ts b/src/context/contextGitDiffCached.ts index eeeaa78..548d771 100644 --- a/src/context/contextGitDiffCached.ts +++ b/src/context/contextGitDiffCached.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import { ChatContext } from './contextManager'; -import { createTempSubdirectory, runCommandAndWriteOutput } from '../util/commonUtil'; +import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; export const gitDiffCachedContext: ChatContext = { name: 'git diff cached', @@ -8,7 +8,7 @@ export const gitDiffCachedContext: ChatContext = { handler: async () => { const tempDir = await createTempSubdirectory('devchat/context'); const diff_file = path.join(tempDir, 'diff_cached.txt'); - const result = await runCommandAndWriteOutput('git', ['diff', '--cached'], diff_file); + const result = await runCommandStringAndWriteOutput('git diff --cached', diff_file); console.log(result.exitCode); console.log(result.stdout); console.log(result.stderr); diff --git a/src/context/contextRef.ts b/src/context/contextRef.ts new file mode 100644 index 0000000..941a233 --- /dev/null +++ b/src/context/contextRef.ts @@ -0,0 +1,18 @@ + +import * as vscode from 'vscode'; +import * as path from 'path'; +import { createTempSubdirectory, runCommandStringAndWriteOutput } from '../util/commonUtil'; + +export async function handleRefCommand(ref_command: string) { + if (ref_command) { + const tempDir = await createTempSubdirectory('devchat/context'); + const diff_file = path.join(tempDir, 'custom.txt'); + const result = await runCommandStringAndWriteOutput(ref_command, diff_file); + console.log(result.exitCode); + console.log(result.stdout); + console.log(result.stderr); + return `[context|${diff_file}]`; + } + + return ''; +} \ No newline at end of file diff --git a/src/context/exampleContext.ts b/src/context/exampleContext.ts deleted file mode 100644 index 5c36284..0000000 --- a/src/context/exampleContext.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { ChatContext } from './contextManager'; - -export const exampleContext: ChatContext = { - name: 'exampleContext', - description: '这是一个示例上下文', - handler: async () => { - return `[context|example file name]`; - }, -}; diff --git a/src/context/loadContexts.ts b/src/context/loadContexts.ts index 28c6796..60e4e57 100644 --- a/src/context/loadContexts.ts +++ b/src/context/loadContexts.ts @@ -1,5 +1,4 @@ import ChatContextManager from './contextManager'; -import { exampleContext } from './exampleContext'; import { gitDiffCachedContext } from './contextGitDiffCached'; import { gitDiffContext } from './contextGitDiff'; import { customCommandContext } from './contextCustomCommand'; @@ -7,7 +6,6 @@ import { customCommandContext } from './contextCustomCommand'; const chatContextManager = ChatContextManager.getInstance(); // 注册命令 -chatContextManager.registerContext(exampleContext); chatContextManager.registerContext(gitDiffCachedContext); chatContextManager.registerContext(gitDiffContext); chatContextManager.registerContext(customCommandContext); diff --git a/src/handler/addRefCommandContext.ts b/src/handler/addRefCommandContext.ts new file mode 100644 index 0000000..a6038ce --- /dev/null +++ b/src/handler/addRefCommandContext.ts @@ -0,0 +1,10 @@ +import * as vscode from 'vscode'; +import { handleRefCommand } from '../context/contextRef'; + +// message: { command: 'addRefCommandContext', refCommand: string } +// User input: /ref ls . then "ls ." will be passed to refCommand +export async function addRefCommandContext(message: any, panel: vscode.WebviewPanel): Promise { + const contextStr = await handleRefCommand(message.refCommand); + panel.webview.postMessage({ command: 'appendContext', context: contextStr }); + return; +} diff --git a/src/handler/contextDetail.ts b/src/handler/contextDetail.ts new file mode 100644 index 0000000..5b84a65 --- /dev/null +++ b/src/handler/contextDetail.ts @@ -0,0 +1,12 @@ +import * as vscode from 'vscode'; +import * as fs from 'fs'; +import { handleRefCommand } from '../context/contextRef'; + +// message: { command: 'contextDetail', file: string } +// read detail context information from file +// return json string +export async function contextDetail(message: any, panel: vscode.WebviewPanel): Promise { + const fileContent = fs.readFileSync(message.file, 'utf-8'); + panel.webview.postMessage({ command: 'contextDetailResponse', result: fileContent }); + return; +} diff --git a/src/handler/loadHandlers.ts b/src/handler/loadHandlers.ts index 7b4aba3..0147b81 100644 --- a/src/handler/loadHandlers.ts +++ b/src/handler/loadHandlers.ts @@ -10,6 +10,8 @@ import { sendMessage } from './sendMessage'; import { blockApply } from './showDiff'; import { showDiff } from './showDiff'; import { addConext } from './addContext'; +import { addRefCommandContext } from './addRefCommandContext'; +import { contextDetail } from './contextDetail'; messageHandler.registerHandler('addContext', addConext); messageHandler.registerHandler('code_apply', codeApply); @@ -21,4 +23,6 @@ messageHandler.registerHandler('regCommandList', regCommandList); messageHandler.registerHandler('regContextList', regContextList); messageHandler.registerHandler('sendMessage', sendMessage); messageHandler.registerHandler('block_apply', blockApply); -messageHandler.registerHandler('show_diff', showDiff); \ No newline at end of file +messageHandler.registerHandler('show_diff', showDiff); +messageHandler.registerHandler('addRefCommandContext', addRefCommandContext); +messageHandler.registerHandler('contextDetail', contextDetail); \ No newline at end of file diff --git a/src/util/commonUtil.ts b/src/util/commonUtil.ts index bb82a14..521be7f 100644 --- a/src/util/commonUtil.ts +++ b/src/util/commonUtil.ts @@ -76,7 +76,13 @@ interface CommandResult { // 使用exec执行命令行字符串 const childProcess = exec(commandString, { cwd: workspaceDir }, (error, stdout, stderr) => { // 将命令输出结果写入到文件 - fs.writeFileSync(outputFile, stdout); + const data = { + command: commandString, + content: stdout + }; + const jsonData = JSON.stringify(data); + + fs.writeFileSync(outputFile, jsonData); // 返回结果 resolve({