2023-05-05 21:27:40 +08:00
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import { sendFileSelectMessage, sendCodeSelectMessage } from './util';
|
2023-05-16 14:24:24 +08:00
|
|
|
import ExtensionContextHolder from '../util/extensionContext';
|
2023-05-31 16:10:53 +08:00
|
|
|
import { TopicManager } from '../topic/topicManager';
|
2023-05-31 16:10:53 +08:00
|
|
|
import { TopicTreeDataProvider, TopicTreeItem } from '../panel/topicView';
|
|
|
|
import { FilePairManager } from '../util/diffFilePairs';
|
2023-05-31 16:10:53 +08:00
|
|
|
import { ApiKeyManager } from '../util/apiKey';
|
2023-05-31 16:10:53 +08:00
|
|
|
|
2023-05-10 19:25:11 +08:00
|
|
|
|
2023-05-11 10:27:54 +08:00
|
|
|
function registerOpenChatPanelCommand(context: vscode.ExtensionContext) {
|
2023-05-31 16:10:53 +08:00
|
|
|
let disposable = vscode.commands.registerCommand('devchat.openChatPanel', async () => {
|
2023-05-16 17:49:13 +08:00
|
|
|
await vscode.commands.executeCommand('devchat-view.focus');
|
2023-05-31 16:10:53 +08:00
|
|
|
});
|
|
|
|
context.subscriptions.push(disposable);
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 14:35:37 +08:00
|
|
|
async function ensureChatPanel(context: vscode.ExtensionContext): Promise<boolean> {
|
2023-05-31 16:10:53 +08:00
|
|
|
await vscode.commands.executeCommand('devchat-view.focus');
|
|
|
|
return true;
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerAddContextCommand(context: vscode.ExtensionContext) {
|
2023-06-13 11:05:42 +08:00
|
|
|
const callback = async (uri: { fsPath: any; }) => {
|
2023-05-31 16:10:53 +08:00
|
|
|
if (!await ensureChatPanel(context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-13 11:05:42 +08:00
|
|
|
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, uri.fsPath);
|
2023-05-31 16:10:53 +08:00
|
|
|
};
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.addConext', callback));
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.addConext_chinese', callback));
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerAskForCodeCommand(context: vscode.ExtensionContext) {
|
2023-05-31 16:10:53 +08:00
|
|
|
const callback = async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor) {
|
|
|
|
if (!await ensureChatPanel(context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedText = editor.document.getText(editor.selection);
|
|
|
|
await sendCodeSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName, selectedText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.askForCode', callback));
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.askForCode_chinese', callback));
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerAskForFileCommand(context: vscode.ExtensionContext) {
|
2023-05-31 16:10:53 +08:00
|
|
|
const callback = async () => {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor) {
|
|
|
|
if (!await ensureChatPanel(context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.askForFile', callback));
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('devchat.askForFile_chinese', callback));
|
2023-05-05 21:27:40 +08:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
export function registerApiKeySettingCommand(context: vscode.ExtensionContext) {
|
|
|
|
const secretStorage: vscode.SecretStorage = context.secrets;
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.commands.registerCommand('DevChat.OPENAI_API_KEY', async () => {
|
|
|
|
const passwordInput: string = await vscode.window.showInputBox({
|
|
|
|
password: true,
|
2023-05-31 16:10:53 +08:00
|
|
|
title: "Input Access Key",
|
|
|
|
placeHolder: "Set OPENAI_API_KEY (or DevChat Access Key)"
|
2023-05-31 16:10:53 +08:00
|
|
|
}) ?? '';
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
ApiKeyManager.writeApiKeySecret(passwordInput);
|
2023-05-31 16:10:53 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function registerStatusBarItemClickCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.commands.registerCommand('devcaht.onStatusBarClick', async () => {
|
|
|
|
await vscode.commands.executeCommand('devchat-view.focus');
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
const topicDeleteCallback = async (item: TopicTreeItem) => {
|
|
|
|
const confirm = 'Delete';
|
|
|
|
const cancel = 'Cancel';
|
|
|
|
const label = typeof item.label === 'string' ? item.label : item.label!.label;
|
|
|
|
const truncatedLabel = label.substring(0, 20) + (label.length > 20 ? '...' : '');
|
|
|
|
const result = await vscode.window.showWarningMessage(
|
|
|
|
`Are you sure you want to delete the topic "${truncatedLabel}"?`,
|
|
|
|
{ modal: true },
|
|
|
|
confirm,
|
|
|
|
cancel
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result === confirm) {
|
|
|
|
TopicManager.getInstance().deleteTopic(item.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
;
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
export function regTopicDeleteCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
2023-05-31 16:10:53 +08:00
|
|
|
vscode.commands.registerCommand('devchat-topicview.deleteTopic', topicDeleteCallback)
|
2023-05-31 16:10:53 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regAddTopicCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.commands.registerCommand('devchat-topicview.addTopic', () => {
|
|
|
|
const topic = TopicManager.getInstance().createTopic();
|
|
|
|
TopicManager.getInstance().setCurrentTopic(topic.topicId);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regDeleteSelectTopicCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.commands.registerCommand('devchat-topicview.deleteSelectedTopic', () => {
|
|
|
|
const selectedItem = TopicTreeDataProvider.getInstance().selectedItem;
|
|
|
|
if (selectedItem) {
|
2023-05-31 16:10:53 +08:00
|
|
|
topicDeleteCallback(selectedItem);
|
2023-05-31 16:10:53 +08:00
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('No item selected');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regSelectTopicCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
|
|
|
vscode.commands.registerCommand('devchat-topicview.selectTopic', (item: TopicTreeItem) => {
|
|
|
|
TopicTreeDataProvider.getInstance().setSelectedItem(item);
|
|
|
|
TopicManager.getInstance().setCurrentTopic(item.id);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regReloadTopicCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
2023-05-31 16:10:53 +08:00
|
|
|
vscode.commands.registerCommand('devchat-topicview.reloadTopic', async () => {
|
2023-05-31 16:10:53 +08:00
|
|
|
TopicManager.getInstance().loadTopics();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regApplyDiffResultCommand(context: vscode.ExtensionContext) {
|
|
|
|
context.subscriptions.push(
|
2023-05-31 16:10:53 +08:00
|
|
|
vscode.commands.registerCommand('devchat.applyDiffResult', async () => {
|
2023-05-31 16:10:53 +08:00
|
|
|
const activeEditor = vscode.window.activeTextEditor;
|
|
|
|
const fileName = activeEditor!.document.fileName;
|
|
|
|
|
|
|
|
const [leftUri, rightUri] = FilePairManager.getInstance().findPair(fileName) || [undefined, undefined];
|
|
|
|
if (leftUri && rightUri) {
|
|
|
|
// 获取对比的两个文件
|
|
|
|
const leftDoc = await vscode.workspace.openTextDocument(leftUri);
|
|
|
|
const rightDoc = await vscode.workspace.openTextDocument(rightUri);
|
|
|
|
|
|
|
|
// 将右边文档的内容替换到左边文档
|
|
|
|
const leftEditor = await vscode.window.showTextDocument(leftDoc);
|
|
|
|
await leftEditor.edit(editBuilder => {
|
|
|
|
const fullRange = new vscode.Range(0, 0, leftDoc.lineCount, 0);
|
|
|
|
editBuilder.replace(fullRange, rightDoc.getText());
|
|
|
|
});
|
|
|
|
|
|
|
|
// 保存左边文档
|
|
|
|
await leftDoc.save();
|
|
|
|
} else {
|
|
|
|
vscode.window.showErrorMessage('No file to apply diff result.');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
export {
|
2023-05-31 16:10:53 +08:00
|
|
|
registerOpenChatPanelCommand,
|
|
|
|
registerAddContextCommand,
|
|
|
|
registerAskForCodeCommand,
|
|
|
|
registerAskForFileCommand,
|
2023-05-05 21:27:40 +08:00
|
|
|
};
|