diff --git a/src/commandManager.ts b/src/commandManager.ts new file mode 100644 index 0000000..66e66a5 --- /dev/null +++ b/src/commandManager.ts @@ -0,0 +1,48 @@ +export interface Command { + name: string; + pattern: string; + description: string; + handler: (userInput: string) => string; + } + + class CommandManager { + private static instance: CommandManager; + private commands: Command[] = []; + + private constructor() {} + + public static getInstance(): CommandManager { + if (!CommandManager.instance) { + CommandManager.instance = new CommandManager(); + } + + return CommandManager.instance; + } + + registerCommand(command: Command): void { + this.commands.push(command); + } + + getCommandList(): Command[] { + return this.commands; + } + + processText(text: string): string { + let result = text; + + this.commands.forEach((commandObj) => { + const commandPattern = new RegExp( + `\\/(${commandObj.pattern.replace("{{prompt}}", "(.+?)")})`, + "g" + ); + + result = result.replace(commandPattern, (_, matchedUserInput) => { + return commandObj.handler(matchedUserInput); + }); + }); + + return result; + } + } + + export default CommandManager; diff --git a/src/exampleCommand1.ts b/src/exampleCommand1.ts new file mode 100644 index 0000000..eff4a12 --- /dev/null +++ b/src/exampleCommand1.ts @@ -0,0 +1,10 @@ +import {Command} from './commandManager'; + +export const exampleCommand1: Command = { + name: 'exampleCommand1', + pattern: 'example: command1 {{prompt}}', + description: '这是一个示例命令1', + handler: (userInput: string) => { + return `示例命令1处理了以下文本:${userInput}`; + }, +}; diff --git a/src/exampleCommand2.ts b/src/exampleCommand2.ts new file mode 100644 index 0000000..79ceebd --- /dev/null +++ b/src/exampleCommand2.ts @@ -0,0 +1,10 @@ +import {Command} from './commandManager'; + +export const exampleCommand2: Command = { + name: 'exampleCommand2', + pattern: 'example: command2 {{prompt}}', + description: '这是一个示例命令2', + handler: (userInput: string) => { + return `示例命令2处理了以下文本:${userInput}`; + }, +}; diff --git a/src/loadCommands.ts b/src/loadCommands.ts new file mode 100644 index 0000000..f01df39 --- /dev/null +++ b/src/loadCommands.ts @@ -0,0 +1,9 @@ +import CommandManager from './commandManager'; +import { exampleCommand1 } from './exampleCommand1'; +import { exampleCommand2 } from './exampleCommand2'; + +const commandManager = CommandManager.getInstance(); + +// 注册命令 +commandManager.registerCommand(exampleCommand1); +commandManager.registerCommand(exampleCommand2); diff --git a/src/messageHandler.ts b/src/messageHandler.ts index cacfabc..b16ad70 100644 --- a/src/messageHandler.ts +++ b/src/messageHandler.ts @@ -7,6 +7,10 @@ import { promisify } from 'util'; import DevChat, { LogOptions } from './devchat'; import DtmWrapper from './dtm'; import applyCode, {applyCodeFile} from './applyCode'; + +import './loadCommands'; +import CommandManager, { Command } from './commandManager'; + import * as vscode3 from 'vscode'; const writeFileAsync = promisify(fs.writeFile); @@ -103,6 +107,14 @@ async function handleMessage( case 'code_file_apply': await applyCodeFile(message.content); return; + case 'regCommandList': + const commandList = CommandManager.getInstance().getCommandList(); + panel.webview.postMessage({ command: 'regCommandList', result: commandList }); + return; + case 'convertCommand': + const newText = CommandManager.getInstance().processText(message.text); + panel.webview.postMessage({ command: 'convertCommand', result: newText }); + return; } } diff --git a/tsconfig.json b/tsconfig.json index 3bd22d5..0df6236 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,11 +8,7 @@ "outDir": "./dist", "sourceMap": true, "rootDir": "src", - "strict": true /* enable all strict type-checking options */ - /* Additional Checks */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ + "strict": true } }