-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
209 lines (176 loc) · 6.67 KB
/
extension.ts
File metadata and controls
209 lines (176 loc) · 6.67 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Acopilot VSCode Extension 入口
*/
import * as vscode from 'vscode';
import { ChatViewProvider } from './webview/ChatViewProvider';
import { debugLog } from './backend/core/logger';
// 保存 ChatViewProvider 实例以便在停用时清理
let chatViewProvider: ChatViewProvider | undefined;
type SelectionReferencePayload = {
uri: string;
path: string;
startLine: number;
endLine: number;
languageId: string;
text: string;
originalCharCount: number;
truncated: boolean;
};
function buildSelectionReferencePayload(
editor: vscode.TextEditor,
maxChars: number
): SelectionReferencePayload | null {
const nonEmptySelections = editor.selections.filter((s) => !s.isEmpty);
if (nonEmptySelections.length === 0) return null;
const selection = nonEmptySelections[0];
const originalText = editor.document.getText(selection);
const normalizedText = originalText.replace(/\r\n/g, '\n');
const originalCharCount = normalizedText.length;
const truncated = originalCharCount > maxChars;
const text = truncated
? `${normalizedText.slice(0, maxChars)}\n…(truncated, original ${originalCharCount} chars)`
: normalizedText;
const start = selection.start;
const end = selection.end;
const startLine = start.line + 1;
let endLine = end.line + 1;
// 常见的“选中整行”会让 end 落在下一行的 column=0,此时不应把最后一行算进去
if (end.character === 0 && end.line > start.line) {
endLine = end.line;
}
const uri = editor.document.uri.toString();
const path = vscode.workspace.asRelativePath(editor.document.uri, false);
return {
uri,
path,
startLine,
endLine,
languageId: editor.document.languageId,
text,
originalCharCount,
truncated
};
}
function buildFileReferencePayload(
document: vscode.TextDocument,
maxChars: number
): SelectionReferencePayload | null {
const originalText = document.getText();
const normalizedText = originalText.replace(/\r\n/g, '\n');
const originalCharCount = normalizedText.length;
const truncated = originalCharCount > maxChars;
const text = truncated
? `${normalizedText.slice(0, maxChars)}\n…(truncated, original ${originalCharCount} chars)`
: normalizedText;
const uri = document.uri.toString();
const path = vscode.workspace.asRelativePath(document.uri, false);
return {
uri,
path,
startLine: 1,
endLine: Math.max(1, document.lineCount),
languageId: document.languageId,
text,
originalCharCount,
truncated
};
}
export function activate(context: vscode.ExtensionContext) {
debugLog('Acopilot extension is now active!');
// 注册聊天视图提供者
chatViewProvider = new ChatViewProvider(context);
context.subscriptions.push(
vscode.window.registerWebviewViewProvider(
'acopilot.chatView',
chatViewProvider,
{
// 保持 webview 状态,切换视图时不销毁
webviewOptions: {
retainContextWhenHidden: true
}
}
)
);
// 注册命令:打开聊天面板
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.openChat', () => {
vscode.commands.executeCommand('acopilot.chatView.focus');
})
);
// 注册命令:把选中内容添加到聊天引用(类似 Copilot 的 Add Selection to Chat)
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.addSelectionToChat', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No active editor');
return;
}
const payload = buildSelectionReferencePayload(editor, 12000);
if (!payload) {
vscode.window.showInformationMessage('请先选中一段代码/文本');
return;
}
await vscode.commands.executeCommand('acopilot.chatView.focus');
chatViewProvider?.sendCommand('addSelectionToChat', payload);
})
);
// 注册命令:把文件内容添加到聊天引用(类似 Copilot 的 Add File to Chat)
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.addFileToChat', async (resource?: unknown) => {
let uri: vscode.Uri | undefined;
if (resource instanceof vscode.Uri) {
uri = resource;
} else if (Array.isArray(resource) && resource.length > 0 && resource[0] instanceof vscode.Uri) {
uri = resource[0];
}
let document: vscode.TextDocument | undefined;
try {
document = uri
? await vscode.workspace.openTextDocument(uri)
: vscode.window.activeTextEditor?.document;
} catch (error) {
vscode.window.showInformationMessage('请选择一个可打开的文本文件');
return;
}
if (!document) {
vscode.window.showInformationMessage('No active editor');
return;
}
const payload = buildFileReferencePayload(document, 12000);
if (!payload) {
vscode.window.showInformationMessage('未能读取文件内容');
return;
}
await vscode.commands.executeCommand('acopilot.chatView.focus');
chatViewProvider?.sendCommand('addSelectionToChat', payload);
})
);
// 注册命令:新建对话
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.newChat', () => {
chatViewProvider.sendCommand('newChat');
})
);
// 注册命令:显示历史
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.showHistory', () => {
chatViewProvider.sendCommand('showHistory');
})
);
// 注册命令:显示设置
context.subscriptions.push(
vscode.commands.registerCommand('acopilot.showSettings', () => {
chatViewProvider.sendCommand('showSettings');
})
);
debugLog('Acopilot extension activated successfully!');
}
export function deactivate() {
debugLog('Acopilot extension deactivating...');
// 清理 ChatViewProvider 资源(取消所有流式请求、断开 MCP 连接等)
if (chatViewProvider) {
chatViewProvider.dispose();
chatViewProvider = undefined;
}
debugLog('Acopilot extension deactivated');
}