-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
102 lines (82 loc) · 2.75 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var hx = require("hbuilderx");
var {
ChatGPTViewProvider
} = require('./src/webview.js');
var showSettingDialog = require('./src/setting.js')
//添加在package第15行,通过事件激活插件
// "activationEvents": [
// "onCommand:extension.helloWorld",
// "onCommand:chatgpt.explain",
// "onView:chatgpt.webview"
// ],
//该方法将在插件激活的时候调用
async function activate(context) {
// 创建webview
let webviewPanel = hx.window.createWebView("chatgpt.webview", {
enableScritps: true
});
let provider = new ChatGPTViewProvider(webviewPanel);
hx.window.showView({
viewId: "chatgpt.webview"
});
// 监听配置修改
hx.workspace.onDidChangeConfiguration(function(event) {
if (event.affectsConfiguration("chatgpt.selectedType") ||
event.affectsConfiguration("chatgpt.accessToken") ||
event.affectsConfiguration("chatgpt.ApiKey") ||
event.affectsConfiguration("chatgpt.proxy") ||
event.affectsConfiguration("chatgpt.apiBaseUrl")
) {
provider.updateConfig()
};
});
//注册事件
context.subscriptions.push(
hx.commands.registerCommand('chatgpt.explain', askGPTToExplain),
hx.commands.registerCommand('chatgpt.findProblems', askGPTWhyBroken),
hx.commands.registerCommand('chatgpt.refactor', askGPTToRefactor),
hx.commands.registerCommand('chatgpt.addTest', askGPTToAddTests),
// hx.commands.registerCommand('chatgpt.setting', openSetting),
)
async function askGPTToExplain() {
await askChatGPT('解释下面的这段代码');
}
async function askGPTWhyBroken() {
await askChatGPT('这段代码有什么错误吗?');
}
async function askGPTToRefactor() {
await askChatGPT('重构这段代码并说明做了哪些变更');
}
async function askGPTToAddTests() {
await askChatGPT('为下面的代码生成测试代码');
}
async function askChatGPT(promptPrefix) {
if (!promptPrefix) {
promptPrefix = ""
} else {
let activeEditor = hx.window.getActiveTextEditor();
activeEditor.then((editor) => {
// 获取选中代码块or整个文件
let selection = editor.selection;
let selectedText = editor.document.getText(selection);
const entireFileContents = editor.document.getText();
const code = selectedText ?
selectedText :
`This is the ${editor.document.languageId} file I'm working on: \n\n${entireFileContents}`;
provider.sendOpenAIRequest(promptPrefix, code)
})
}
}
async function openSetting() {
let res = await showSettingDialog(provider)
// console.log(res)
// provider.updateConfig()
}
}
//该方法将在插件禁用的时候调用(目前是在插件卸载的时候触发)
function deactivate() {
}
module.exports = {
activate,
deactivate
}