From cc35fd338043fd69f7e9d59606e29aeee1a6d790 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Tue, 27 Aug 2024 16:44:49 +0800 Subject: [PATCH] Replace assistant names in source --- src/contributes/quickFixProvider.ts | 9 ++-- src/extension.ts | 3 ++ src/handler/chatHandler.ts | 3 +- src/handler/historyMessagesBase.ts | 36 -------------- src/panel/codeLens.ts | 76 +++++------------------------ src/panel/statusBarView.ts | 15 +++--- src/panel/statusBarViewBase.ts | 25 +++++----- src/util/constants.ts | 9 ++++ src/util/logger_vscode.ts | 3 +- src/util/progressBar.ts | 3 +- 10 files changed, 55 insertions(+), 127 deletions(-) create mode 100644 src/util/constants.ts diff --git a/src/contributes/quickFixProvider.ts b/src/contributes/quickFixProvider.ts index dfe443e..d000960 100644 --- a/src/contributes/quickFixProvider.ts +++ b/src/contributes/quickFixProvider.ts @@ -1,5 +1,6 @@ import * as vscode from "vscode"; import { collapseFileExculdeSelectRange } from "./codecomplete/ast/collapseBlock"; +import { ASSISTANT_NAME_EN } from "../util/constants"; class DevChatQuickFixProvider implements vscode.CodeActionProvider { public static readonly providedCodeActionKinds = [ @@ -18,13 +19,13 @@ class DevChatQuickFixProvider implements vscode.CodeActionProvider { const diagnostic = context.diagnostics[0]; const quickFix = new vscode.CodeAction( - "Ask DevChat", + `Ask ${ASSISTANT_NAME_EN}`, vscode.CodeActionKind.QuickFix, ); quickFix.isPreferred = false; const fixUsingDevChat = new vscode.CodeAction( - "Fix using DevChat", + `Fix using ${ASSISTANT_NAME_EN}`, vscode.CodeActionKind.QuickFix, ); fixUsingDevChat.isPreferred = true; @@ -32,7 +33,7 @@ class DevChatQuickFixProvider implements vscode.CodeActionProvider { return new Promise(async (resolve) => { quickFix.command = { command: "DevChat.quickFixAskDevChat", - title: "Ask DevChat", + title: `Ask ${ASSISTANT_NAME_EN}`, arguments: [ document, range, @@ -42,7 +43,7 @@ class DevChatQuickFixProvider implements vscode.CodeActionProvider { fixUsingDevChat.command = { command: "DevChat.quickFixUsingDevChat", - title: "Fix using DevChat", + title: `Fix using ${ASSISTANT_NAME_EN}`, arguments: [ document, range, diff --git a/src/extension.ts b/src/extension.ts index bcddbf5..e3d598f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,6 +35,7 @@ import { InlineCompletionProvider, registerCodeCompleteCallbackCommand } from ". import { indexDir } from "./contributes/codecomplete/astIndex"; import registerQuickFixProvider from "./contributes/quickFixProvider"; import { stopLocalService } from './util/localService'; +import { updateNames } from "./util/constants"; async function migrateConfig() { @@ -129,6 +130,8 @@ export async function fixDevChatApiBase() { async function activate(context: vscode.ExtensionContext) { ExtensionContextHolder.context = context; + const assistantNames = context.extension.packageJSON.assistantNames + updateNames(assistantNames.ASSISTANT_NAME_EN, assistantNames.ASSISTANT_NAME_ZH) logger.init(LoggerChannelVscode.getInstance()); UiUtilWrapper.init(new UiUtilVscode()); diff --git a/src/handler/chatHandler.ts b/src/handler/chatHandler.ts index 38628d2..e7f44f8 100644 --- a/src/handler/chatHandler.ts +++ b/src/handler/chatHandler.ts @@ -1,3 +1,4 @@ +import { ASSISTANT_NAME_EN } from '../util/constants'; import { UiUtilWrapper } from '../util/uiUtil'; import { MessageHandler } from './messageHandler'; import { isSending } from './sendMessage'; @@ -6,7 +7,7 @@ import { isSending } from './sendMessage'; export async function chatWithDevChat(panel, message: string) { if (isSending()) { // already sending, show error - UiUtilWrapper.showErrorMessage("DevChat: A command is already being sent, please try again later."); + UiUtilWrapper.showErrorMessage(`${ASSISTANT_NAME_EN}: A command is already being sent, please try again later.`); return; } MessageHandler.sendMessage(panel!, { command: 'chatWithDevChat', 'message': message }); diff --git a/src/handler/historyMessagesBase.ts b/src/handler/historyMessagesBase.ts index eb2161b..97bb2e6 100644 --- a/src/handler/historyMessagesBase.ts +++ b/src/handler/historyMessagesBase.ts @@ -18,42 +18,6 @@ export interface LoadHistoryMessages { entries: Array; } -function welcomeMessage(): LogEntry { - // create default logEntry to show welcome message - return { - hash: 'message', - parent: '', - user: 'system', - date: '', - request: 'How do I use DevChat?', - response: ` -Do you want to write some code or have a question about the project? Simply right-click on your chosen files or code snippets and add them to DevChat. Feel free to ask me anything or let me help you with coding. - -Don't forget to check out the "+" button on the left of the input to add more context. To see a list of workflows you can run in the context, just type "/". Happy prompting! - `, - context: [] - } as LogEntry; -} - -function apiKeyMissedMessage(): LogEntry { - // create default logEntry to show welcome message - return { - hash: 'message', - parent: '', - user: 'system', - date: '', - request: 'Is OPENAI_API_KEY (or DevChat Access Key) ready?', - response: ` -OPENAI_API_KEY is missing from your environment or settings. Kindly input your OpenAI or DevChat key, and I'll ensure DevChat is all set for you. - - - - - `, - context: [] - } as LogEntry; -} - async function loadTopicHistoryLogs(topicId: string | undefined): Promise | undefined> { if (!topicId) { return undefined; diff --git a/src/panel/codeLens.ts b/src/panel/codeLens.ts index 0c8b7d3..11e64d0 100644 --- a/src/panel/codeLens.ts +++ b/src/panel/codeLens.ts @@ -1,8 +1,7 @@ import * as vscode from "vscode"; -import * as fs from "fs"; -import * as path from "path"; import { logger } from "../util/logger"; -import { log } from "console"; +import { ASSISTANT_NAME_ZH } from "../util/constants"; +import { ExtensionContextHolder } from "../util/extensionContext"; interface FunctionDefinition { name: string; @@ -19,42 +18,10 @@ type CodeLensRegistration = { export class CodeLensManager { private static instance: CodeLensManager; - private registrations: CodeLensRegistration[] = []; - private configFilePath: string; - - private constructor() { - this.configFilePath = path.join( - process.env.HOME || process.env.USERPROFILE || ".", - ".chat/ideconfig.json" - ); - this.loadConfig(); - } - - public static getInstance(): CodeLensManager { - if (!CodeLensManager.instance) { - CodeLensManager.instance = new CodeLensManager(); - } - return CodeLensManager.instance; - } - - private loadConfig(): void { - if (!fs.existsSync(this.configFilePath)) { - this.initializeConfig(); - } else { - const data = fs.readFileSync(this.configFilePath, "utf-8"); - this.registrations = JSON.parse(data); - - if (this.registrations.length === 0) { - this.initializeConfig(); - } - } - } - - private initializeConfig(): void { - this.registrations = [ + private registrations: CodeLensRegistration[] = [ { elementType: "function", - objectName: "DevChat: unit tests", + objectName: `${ExtensionContextHolder.context?.extension.packageJSON.assistantNames.ASSISTANT_NAME_ZH}: unit tests`, promptGenerator: "/unit_tests {__filename__}:::{__functionName__}:::{__functionStartLine__}:::{__functionEndLine__}:::{__containerStartLine__}:::{__containerEndLine__}", }, @@ -68,36 +35,17 @@ export class CodeLensManager { objectName: "docstring", promptGenerator: "/docstring", }, - // { - // elementType: 'function', - // objectName: 'generate unit tests', - // promptGenerator: '/test generate unit tests for {__filename__} {__functionName__}' - // }, - // { - // elementType: 'inner_function', - // objectName: 'generate comment', - // promptGenerator: 'generate comment for \n ```code\n{__functionCode__}\n```\n' - // }, - // { - // elementType: 'function', - // objectName: 'generate comment', - // promptGenerator: 'generate comment for \n ```code\n{__functionCode__}\n```\n' - // } ]; - this.saveConfig(); + + private constructor() {} + + public static getInstance(): CodeLensManager { + if (!CodeLensManager.instance) { + CodeLensManager.instance = new CodeLensManager(); + } + return CodeLensManager.instance; } - private saveConfig(): void { - const configDir = path.dirname(this.configFilePath); - if (!fs.existsSync(configDir)) { - fs.mkdirSync(configDir, { recursive: true }); - } - fs.writeFileSync( - this.configFilePath, - JSON.stringify(this.registrations, null, 2), - "utf8" - ); - } public getRegistrations(): CodeLensRegistration[] { return this.registrations; diff --git a/src/panel/statusBarView.ts b/src/panel/statusBarView.ts index c44d84e..c0d01b8 100644 --- a/src/panel/statusBarView.ts +++ b/src/panel/statusBarView.ts @@ -2,16 +2,15 @@ import * as vscode from 'vscode'; import { dependencyCheck } from './statusBarViewBase'; import { ProgressBar } from '../util/progressBar'; -import { logger } from '../util/logger'; -import { DevChatConfig } from '../util/config'; +import { ASSISTANT_NAME_EN } from '../util/constants'; export function createStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem { const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100); // Set the status bar item properties - statusBarItem.text = `$(warning)DevChat`; - statusBarItem.tooltip = 'DevChat is checking ..., please wait'; + statusBarItem.text = `$(warning)${ASSISTANT_NAME_EN}`; + statusBarItem.tooltip = `${ASSISTANT_NAME_EN} is checking ..., please wait`; // when statsBarItem.command is '', then there is "command '' not found" error. statusBarItem.command = undefined; @@ -27,8 +26,8 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St progressBar.update("Checking dependencies", 0); const devchatStatus = await dependencyCheck(); - if (devchatStatus !== 'has statisfied the dependency' && devchatStatus !== 'DevChat has been installed') { - statusBarItem.text = `$(warning)DevChat`; + if (devchatStatus !== 'has statisfied the dependency' && devchatStatus !== `${ASSISTANT_NAME_EN} has been installed`) { + statusBarItem.text = `$(warning)${ASSISTANT_NAME_EN}`; statusBarItem.tooltip = `${devchatStatus}`; if (devchatStatus === 'Missing required dependency: Python3') { @@ -42,7 +41,7 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St return; } - statusBarItem.text = `$(pass)DevChat`; + statusBarItem.text = `$(pass)${ASSISTANT_NAME_EN}`; statusBarItem.tooltip = `ready to chat`; statusBarItem.command = 'devcaht.onStatusBarClick'; progressBar.update(`Checking dependencies: Success`, 0); @@ -58,7 +57,7 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St clearInterval(timer); } catch (error) { - statusBarItem.text = `$(warning)DevChat`; + statusBarItem.text = `$(warning)${ASSISTANT_NAME_EN}`; statusBarItem.tooltip = `Error: ${error}`; statusBarItem.command = undefined; progressBar.endWithError(`Checking dependencies: Fail with exception.`); diff --git a/src/panel/statusBarViewBase.ts b/src/panel/statusBarViewBase.ts index b139a78..b9bfbd8 100644 --- a/src/panel/statusBarViewBase.ts +++ b/src/panel/statusBarViewBase.ts @@ -1,14 +1,10 @@ -import * as fs from 'fs'; -import * as path from 'path'; import { logger } from "../util/logger"; -import { UiUtilWrapper } from "../util/uiUtil"; -import { ApiKeyManager } from '../util/apiKey'; import { installDevchat } from '../util/python_installer/install_devchat'; +import { ASSISTANT_NAME_EN } from '../util/constants'; let devchatStatus = ''; -let apiKeyStatus = ''; let preDevchatStatus = ''; @@ -27,26 +23,31 @@ export async function dependencyCheck(): Promise { // define subfunction to check devchat dependency const getDevChatStatus = async (): Promise => { + const statuses = { + installing: `installing ${ASSISTANT_NAME_EN}`, + installed: `${ASSISTANT_NAME_EN} has been installed`, + error: `An error occurred during the installation of ${ASSISTANT_NAME_EN}` + } if (devchatStatus === '') { - devchatStatus = 'installing devchat'; + devchatStatus = statuses.installing; const devchatCommandEnv = await installDevchat(); if (devchatCommandEnv) { logger.channel()?.info(`Python: ${devchatCommandEnv}`); - devchatStatus = 'DevChat has been installed'; + devchatStatus = statuses.installed; return devchatStatus; } else { logger.channel()?.info(`Python: undefined`); - devchatStatus = 'An error occurred during the installation of DevChat'; + devchatStatus = statuses.error; return devchatStatus; } } else if (devchatStatus === 'has statisfied the dependency') { return devchatStatus; - } else if (devchatStatus === 'installing devchat') { + } else if (devchatStatus === statuses.installing) { return devchatStatus; - } else if (devchatStatus === 'DevChat has been installed') { + } else if (devchatStatus === statuses.installed) { return devchatStatus; - } else if (devchatStatus === 'An error occurred during the installation of DevChat') { + } else if (devchatStatus === statuses.error) { return devchatStatus; } return ""; @@ -55,7 +56,7 @@ export async function dependencyCheck(): Promise { const devchatPackageStatus = await getDevChatStatus(); if (devchatPackageStatus !== preDevchatStatus) { - logger.channel()?.info(`devchat status: ${devchatPackageStatus}`); + logger.channel()?.info(`${ASSISTANT_NAME_EN} status: ${devchatPackageStatus}`); preDevchatStatus = devchatPackageStatus; } diff --git a/src/util/constants.ts b/src/util/constants.ts new file mode 100644 index 0000000..b7d2a11 --- /dev/null +++ b/src/util/constants.ts @@ -0,0 +1,9 @@ +import { ExtensionContextHolder } from "./extensionContext"; + +export let ASSISTANT_NAME_EN = "DevChat"; +export let ASSISTANT_NAME_ZH = "DevChat"; + +export function updateNames(nameEN, nameZH) { + ASSISTANT_NAME_EN = nameEN; + ASSISTANT_NAME_ZH = nameZH; +} \ No newline at end of file diff --git a/src/util/logger_vscode.ts b/src/util/logger_vscode.ts index 53d49dc..d86f42c 100644 --- a/src/util/logger_vscode.ts +++ b/src/util/logger_vscode.ts @@ -1,3 +1,4 @@ +import { ASSISTANT_NAME_ZH } from "./constants"; import { LogChannel } from "./logger"; import * as vscode from 'vscode'; @@ -7,7 +8,7 @@ export class LoggerChannelVscode implements LogChannel { private static _instance: LoggerChannelVscode; private constructor() { - this._channel = vscode.window.createOutputChannel('DevChat', { log: true }); + this._channel = vscode.window.createOutputChannel(ASSISTANT_NAME_ZH, { log: true }); } public static getInstance(): LoggerChannelVscode { diff --git a/src/util/progressBar.ts b/src/util/progressBar.ts index 277b62b..f76b549 100644 --- a/src/util/progressBar.ts +++ b/src/util/progressBar.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; +import { ASSISTANT_NAME_ZH } from './constants'; export class ProgressBar { private message: string; @@ -13,7 +14,7 @@ export class ProgressBar { init() { vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, - title: 'DevChat', + title: ASSISTANT_NAME_ZH, cancellable: false }, (progress, token) => { return new Promise((resolve) => {