From eca629b4f33f039aa9d62433532fcd12dbea791f Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 11 May 2023 10:27:54 +0800 Subject: [PATCH] check dependence only once --- src/contributes/commands.ts | 31 +++++++++++++++++-------------- src/extension.ts | 22 ++++++++++++---------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/contributes/commands.ts b/src/contributes/commands.ts index 217fe94..4f8ed91 100644 --- a/src/contributes/commands.ts +++ b/src/contributes/commands.ts @@ -32,22 +32,24 @@ function checkOpenAIKey() { return true; } +function checkDependencyPackage() { + const dependencyInstalled = checkDependency(); + if (!dependencyInstalled) { + // 依赖程序未安装,显示提示信息 + logger.channel()?.error('devchat package is not installed.'); + logger.channel()?.info('Please install devchat package first. Use command: pip install devchat'); + logger.channel()?.show(); + vscode.window.showErrorMessage('devchat package is not installed.'); + return; + } + + if (!checkOpenAIKey()) { + return; + } +} + function registerOpenChatPanelCommand(context: vscode.ExtensionContext) { let disposable = vscode.commands.registerCommand('devchat.openChatPanel', () => { - const dependencyInstalled = checkDependency(); - if (!dependencyInstalled) { - // 依赖程序未安装,显示提示信息 - logger.channel()?.error('devchat package is not installed.'); - logger.channel()?.info('Please install devchat package first. Use command: pip install devchat'); - logger.channel()?.show(); - vscode.window.showErrorMessage('devchat package is not installed.'); - return; - } - - if (!checkOpenAIKey()) { - return; - } - if (vscode.workspace.workspaceFolders) { ChatPanel.createOrShow(context.extensionUri); } else { @@ -110,6 +112,7 @@ function registerAskForFileCommand(context: vscode.ExtensionContext) { } export { + checkDependencyPackage, registerOpenChatPanelCommand, registerAddContextCommand, registerAskForCodeCommand, diff --git a/src/extension.ts b/src/extension.ts index e3ba0b9..f2a1050 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,10 +2,11 @@ import * as vscode from 'vscode'; import { - registerOpenChatPanelCommand, - registerAddContextCommand, - registerAskForCodeCommand, - registerAskForFileCommand, + checkDependencyPackage, + registerOpenChatPanelCommand, + registerAddContextCommand, + registerAskForCodeCommand, + registerAskForFileCommand, } from './contributes/commands'; import ExtensionContextHolder from './util/extensionContext'; @@ -13,12 +14,13 @@ import { logger } from './util/logger'; function activate(context: vscode.ExtensionContext) { - ExtensionContextHolder.context = context; - logger.init(context); + ExtensionContextHolder.context = context; + logger.init(context); - registerOpenChatPanelCommand(context); - registerAddContextCommand(context); - registerAskForCodeCommand(context); - registerAskForFileCommand(context); + checkDependencyPackage(); + registerOpenChatPanelCommand(context); + registerAddContextCommand(context); + registerAskForCodeCommand(context); + registerAskForFileCommand(context); } exports.activate = activate;