2023-05-04 07:27:26 +08:00
|
|
|
import * as vscode from 'vscode';
|
2023-05-04 11:50:45 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as ncp from 'ncp'; // 需要安装 ncp 模块,用于复制目录
|
|
|
|
|
2023-04-21 06:44:26 +08:00
|
|
|
const ChatPanel = require('./chatPanel').default;
|
2023-04-27 14:07:46 +08:00
|
|
|
const sendFileSelectMessage = require('./messageHandler').sendFileSelectMessage;
|
|
|
|
const sendCodeSelectMessage = require('./messageHandler').sendCodeSelectMessage;
|
2023-05-04 07:27:26 +08:00
|
|
|
import ExtensionContextHolder from './extensionContext';
|
2023-04-27 14:07:46 +08:00
|
|
|
|
2023-05-04 11:50:45 +08:00
|
|
|
function createChatDirectoryAndCopyInstructionsSync(extensionUri: vscode.Uri) {
|
2023-05-04 16:55:40 +08:00
|
|
|
|
2023-05-04 11:50:45 +08:00
|
|
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
|
|
|
if (!workspaceFolders) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspaceRoot = workspaceFolders[0].uri.fsPath;
|
|
|
|
const chatDirPath = path.join(workspaceRoot, '.chat');
|
|
|
|
const instructionsSrcPath = path.join(extensionUri.fsPath, 'instructions');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// 检查 .chat 目录是否存在,如果不存在,则创建它
|
|
|
|
if (!fs.existsSync(chatDirPath)) {
|
|
|
|
fs.mkdirSync(chatDirPath);
|
2023-05-04 12:00:41 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2023-05-04 11:50:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 将 instructions 目录复制到 .chat 目录中
|
|
|
|
ncp.ncp(instructionsSrcPath, path.join(chatDirPath, 'instructions'), (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error copying instructions:', err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error creating .chat directory and copying instructions:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 07:27:26 +08:00
|
|
|
function activate(context: vscode.ExtensionContext) {
|
|
|
|
ExtensionContextHolder.context = context;
|
2023-05-04 11:50:45 +08:00
|
|
|
|
|
|
|
// 创建 .chat 目录并复制 instructions
|
|
|
|
createChatDirectoryAndCopyInstructionsSync(context.extensionUri);
|
|
|
|
|
2023-04-26 06:48:39 +08:00
|
|
|
let disposable = vscode.commands.registerCommand('devchat.openChatPanel', () => {
|
|
|
|
if (vscode.workspace.workspaceFolders) {
|
|
|
|
ChatPanel.createOrShow(context.extensionUri);
|
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('Please open a directory before using the chat panel.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-04 16:55:40 +08:00
|
|
|
const disposable_add_context = vscode.commands.registerCommand('devchat.addConext', async (uri: { path: any; }) => {
|
2023-04-27 14:07:46 +08:00
|
|
|
if (!ChatPanel.currentPanel()) {
|
|
|
|
if (vscode.workspace.workspaceFolders) {
|
|
|
|
ChatPanel.createOrShow(context.extensionUri);
|
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('Please open a directory before using the chat panel.');
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 16:55:40 +08:00
|
|
|
await sendFileSelectMessage(ChatPanel.currentPanel().panel(), uri.path);
|
2023-04-27 14:07:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const disposableCodeContext = vscode.commands.registerCommand('devchat.askForCode', async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor) {
|
|
|
|
if (!ChatPanel.currentPanel()) {
|
|
|
|
if (vscode.workspace.workspaceFolders) {
|
|
|
|
ChatPanel.createOrShow(context.extensionUri);
|
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('Please open a directory before using the chat panel.');
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedText = editor.document.getText(editor.selection);
|
2023-05-04 16:55:40 +08:00
|
|
|
await sendCodeSelectMessage(ChatPanel.currentPanel().panel(), editor.document.fileName, selectedText);
|
2023-04-27 14:07:46 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const disposableAskFile = vscode.commands.registerCommand('devchat.askForFile', async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor) {
|
|
|
|
if (!ChatPanel.currentPanel()) {
|
|
|
|
if (vscode.workspace.workspaceFolders) {
|
|
|
|
ChatPanel.createOrShow(context.extensionUri);
|
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('Please open a directory before using the chat panel.');
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedText = editor.document.getText();
|
|
|
|
sendCodeSelectMessage(ChatPanel.currentPanel().panel(), selectedText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-26 06:48:39 +08:00
|
|
|
context.subscriptions.push(disposable);
|
2023-04-27 14:07:46 +08:00
|
|
|
context.subscriptions.push(disposable_add_context);
|
|
|
|
context.subscriptions.push(disposableCodeContext)
|
|
|
|
context.subscriptions.push(disposableAskFile)
|
2023-04-14 08:05:41 +08:00
|
|
|
}
|
2023-04-21 06:44:26 +08:00
|
|
|
exports.activate = activate;
|