-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathextension.ts
More file actions
340 lines (288 loc) · 13.1 KB
/
extension.ts
File metadata and controls
340 lines (288 loc) · 13.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* LimCode VSCode Extension 入口
*/
import * as vscode from 'vscode';
import { ChatViewProvider } from './webview/ChatViewProvider';
import { t, setDetectedLanguage, setLanguage as setBackendLanguage } from './backend/i18n';
import { Logger } from './backend/core/logger';
import { getDiffCodeLensProvider } from './backend/tools/file/DiffCodeLensProvider';
import { getDiffEditorActionsProvider } from './backend/tools/file/DiffEditorActionsProvider';
import { getDiffInlineProvider, DiffInlineProvider } from './backend/tools/file/DiffInlineProvider';
import { getDiffManager } from './backend/tools/file/diffManager';
import { getSelectionContextProvider, SelectionContextProvider, type SelectionContextCommandArgs } from './backend/tools/file/SelectionContextProvider';
// 保存 ChatViewProvider 实例以便在停用时清理
let chatViewProvider: ChatViewProvider | undefined;
// DiffCodeLensProvider 注册
let diffCodeLensDisposable: vscode.Disposable | undefined;
// DiffInlineProvider 注册
let diffInlineDisposable: vscode.Disposable | undefined;
export function activate(context: vscode.ExtensionContext) {
console.log('LimCode extension is now active!');
// 初始化日志系统:创建 OutputChannel 让日志同时输出到 VS Code 输出面板
const outputChannel = vscode.window.createOutputChannel('LimCode');
context.subscriptions.push(outputChannel);
Logger.setOutputChannel((line) => outputChannel.appendLine(line));
// Logger.setLevel(LogLevel.DEBUG); // 取消注释以启用 DEBUG 级别日志
// Allow i18n to follow VS Code display language until settings load.
setDetectedLanguage(vscode.env.language);
setBackendLanguage('auto');
// 注册聊天视图提供者
chatViewProvider = new ChatViewProvider(context);
context.subscriptions.push(
vscode.window.registerWebviewViewProvider(
'limcode.chatView',
chatViewProvider,
{
// 保持 webview 状态,切换视图时不销毁
webviewOptions: {
retainContextWhenHidden: true
}
}
)
);
// 注册命令:打开聊天面板
context.subscriptions.push(
vscode.commands.registerCommand('limcode.openChat', () => {
vscode.commands.executeCommand('limcode.chatView.focus');
})
);
// 注册命令:新建对话
context.subscriptions.push(
vscode.commands.registerCommand('limcode.newChat', () => {
chatViewProvider.sendCommand('newChat');
})
);
// 注册命令:显示历史
context.subscriptions.push(
vscode.commands.registerCommand('limcode.showHistory', () => {
chatViewProvider.sendCommand('showHistory');
})
);
// 注册命令:显示设置
context.subscriptions.push(
vscode.commands.registerCommand('limcode.showSettings', () => {
chatViewProvider.sendCommand('showSettings');
})
);
// 注册 DiffCodeLensProvider
const diffCodeLensProvider = getDiffCodeLensProvider();
// 监听 DiffManager 状态变化,刷新相关 UI(CodeLens、内联高亮、标题栏按钮)
getDiffManager().addStatusListener(() => {
getDiffEditorActionsProvider().refresh();
getDiffInlineProvider().refreshAllDecorations();
});
// 注册 CodeLens 提供者
diffCodeLensDisposable = vscode.languages.registerCodeLensProvider(
[
{ scheme: 'file' },
{ scheme: 'gemini-diff-original' }
],
diffCodeLensProvider
);
context.subscriptions.push(diffCodeLensDisposable);
// ========== Selection Context (Hover + Code Actions) ==========
const selectionContextProvider = getSelectionContextProvider();
// Hover: selected text -> "Add to LimCode input"
const selectionHoverDisposable = vscode.languages.registerHoverProvider(
[{ scheme: 'file' }],
selectionContextProvider
);
context.subscriptions.push(selectionHoverDisposable);
// Lightbulb: add selection as context snippet
const selectionCodeActionDisposable = vscode.languages.registerCodeActionsProvider(
[{ scheme: 'file' }],
selectionContextProvider,
{
providedCodeActionKinds: SelectionContextProvider.providedCodeActionKinds
}
);
context.subscriptions.push(selectionCodeActionDisposable);
// Command used by hover/code actions
context.subscriptions.push(
vscode.commands.registerCommand('limcode.context.addSelectionToInput', async (args?: SelectionContextCommandArgs) => {
try {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage(t('tools.file.selectionContext.noActiveEditor'));
return;
}
let targetUri = editor.document.uri;
let selection = editor.selection;
if (args?.uri) {
targetUri = vscode.Uri.parse(args.uri);
selection = new vscode.Selection(
new vscode.Position(args.selection.start.line, args.selection.start.character),
new vscode.Position(args.selection.end.line, args.selection.end.character)
);
}
const doc = (targetUri.toString() === editor.document.uri.toString())
? editor.document
: await vscode.workspace.openTextDocument(targetUri);
if (selection.isEmpty) {
vscode.window.showInformationMessage(t('tools.file.selectionContext.noSelection'));
return;
}
// Expand to whole lines. Adjust end line when selection ends at column 0.
let startLine = selection.start.line;
let endLine = selection.end.line;
if (selection.end.character === 0 && selection.end.line > selection.start.line) {
endLine = Math.max(selection.start.line, selection.end.line - 1);
}
startLine = Math.max(0, Math.min(startLine, doc.lineCount - 1));
endLine = Math.max(0, Math.min(endLine, doc.lineCount - 1));
if (endLine < startLine) {
const tmp = startLine;
startLine = endLine;
endLine = tmp;
}
const endChar = doc.lineAt(endLine).text.length;
const lineRange = new vscode.Range(startLine, 0, endLine, endChar);
const content = doc.getText(lineRange);
const lines = content.split(/\r?\n/);
const width = String(endLine + 1).length;
const numbered = lines
.map((line, i) => `${String(startLine + 1 + i).padStart(width, ' ')} | ${line}`)
.join('\n');
const relativePath = vscode.workspace.asRelativePath(targetUri, false);
const title = `${relativePath}[L${startLine + 1}-${endLine + 1}]`;
const contextItem = {
id: `snippet-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
type: 'snippet' as const,
title,
content: numbered,
filePath: relativePath,
language: doc.languageId,
enabled: true,
addedAt: Date.now()
};
// Ensure chat view is visible, then send to webview.
await vscode.commands.executeCommand('limcode.openChat');
chatViewProvider?.sendCommand('input.addContext', { contextItem });
} catch (err: any) {
console.error('Failed to add selection context:', err);
vscode.window.showErrorMessage(t('tools.file.selectionContext.failedToAddSelection', { error: err?.message || String(err) }));
}
})
);
// ========== Diff Inline Provider (Hover + Code Actions) ==========
const diffInlineProvider = getDiffInlineProvider();
// 注册 Hover 提供者(悬停显示可点击的 Accept/Reject 链接)
diffInlineDisposable = vscode.languages.registerHoverProvider(
[
{ scheme: 'file' },
{ scheme: 'gemini-diff-original' }
],
diffInlineProvider
);
context.subscriptions.push(diffInlineDisposable);
// 注册 Code Action 提供者(灯泡操作,自定义来源 "LimCode Diff")
const diffCodeActionDisposable = vscode.languages.registerCodeActionsProvider(
[
{ scheme: 'file' },
{ scheme: 'gemini-diff-original' }
],
diffInlineProvider,
{
providedCodeActionKinds: DiffInlineProvider.providedCodeActionKinds
}
);
context.subscriptions.push(diffCodeActionDisposable);
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.confirmBlock', async (sessionId: string, blockIndex?: number) => {
await diffCodeLensProvider.confirmBlock(sessionId, blockIndex);
// 刷新编辑器操作提供者状态
getDiffEditorActionsProvider().refresh();
// 刷新内联装饰器
diffInlineProvider.refreshAllDecorations();
})
);
// 注册 diff 拒绝命令(CodeLens 和 Code Actions 使用)
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff._rejectBlockFromCodeLens', async (sessionId: string, blockIndex?: number) => {
await diffCodeLensProvider.rejectBlock(sessionId, blockIndex);
// 刷新编辑器操作提供者状态
getDiffEditorActionsProvider().refresh();
// 刷新内联装饰器
diffInlineProvider.refreshAllDecorations();
})
);
// ========== Diff Editor Actions ==========
const diffEditorActionsProvider = getDiffEditorActionsProvider();
// 注册命令:接受所有修改
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.acceptAll', async () => {
await diffEditorActionsProvider.acceptAll();
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:拒绝所有修改
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.rejectAll', async () => {
await diffEditorActionsProvider.rejectAll();
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:选择并接受 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.acceptBlock', async () => {
await diffEditorActionsProvider.showBlockPicker('accept');
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:选择并拒绝 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.rejectBlock', async () => {
await diffEditorActionsProvider.showBlockPicker('reject');
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:接受当前光标位置的 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.acceptCurrentBlock', async () => {
await diffEditorActionsProvider.acceptCurrentBlock();
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:拒绝当前光标位置的 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.rejectCurrentBlock', async () => {
await diffEditorActionsProvider.rejectCurrentBlock();
diffInlineProvider.refreshAllDecorations();
})
);
// 注册命令:跳转到下一个 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.nextBlock', async () => {
await diffEditorActionsProvider.goToNextBlock();
})
);
// 注册命令:跳转到上一个 diff 块
context.subscriptions.push(
vscode.commands.registerCommand('limcode.diff.prevBlock', async () => {
await diffEditorActionsProvider.goToPrevBlock();
})
);
console.log('LimCode extension activated successfully!');
}
export function deactivate() {
console.log('LimCode extension deactivating...');
// 清理 DiffCodeLensProvider
if (diffCodeLensDisposable) {
diffCodeLensDisposable.dispose();
diffCodeLensDisposable = undefined;
}
// 清理 DiffInlineProvider
if (diffInlineDisposable) {
diffInlineDisposable.dispose();
diffInlineDisposable = undefined;
}
getDiffInlineProvider().dispose();
// 清理 DiffEditorActionsProvider
getDiffEditorActionsProvider().dispose();
// 清理 ChatViewProvider 资源(取消所有流式请求、断开 MCP 连接等)
if (chatViewProvider) {
chatViewProvider.dispose();
chatViewProvider = undefined;
}
console.log('LimCode extension deactivated');
}