mirror of
https://github.com/Gmaker689/ai-diff-preview.git
synced 2026-10-08 02:01:52 +08:00
3168e81aa6
- 核心架构搭建:四层架构设计(触发层、隔离层、Diff引擎、UI层) - 快照管理器:管理 Base/Suggest 双版本,隔离 AI 生成代码 - Diff 引擎:基于 Myers 算法计算差异,生成变更块 - 内联装饰器:红绿高亮显示删除/新增行 - CodeLens 提供器:提供 Accept/Reject 按钮 - 事务处理:Accept/Reject 原子化操作 - Claude Hooks 集成:支持 after_code_edit/after_write_file 事件 - 状态栏显示:实时显示变更状态 - 基础测试用例:7 个测试全部通过 技术栈:TypeScript + VSCode Extension API + diff 库 + esbuild
97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
/**
|
|
* CodeLens 提供器 - 提供 Accept/Reject 按钮
|
|
*/
|
|
|
|
import * as vscode from 'vscode';
|
|
import { SnapshotManager } from '../snapshot/snapshotManager';
|
|
import { COMMANDS } from '../models/constants';
|
|
|
|
export class CodeLensProvider implements vscode.CodeLensProvider {
|
|
private _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
|
|
readonly onDidChangeCodeLenses = this._onDidChangeCodeLenses.event;
|
|
|
|
constructor(private snapshotManager: SnapshotManager) {}
|
|
|
|
/**
|
|
* 刷新 CodeLens
|
|
*/
|
|
refresh(): void {
|
|
this._onDidChangeCodeLenses.fire();
|
|
}
|
|
|
|
/**
|
|
* 提供 CodeLens
|
|
*/
|
|
provideCodeLenses(
|
|
document: vscode.TextDocument,
|
|
token: vscode.CancellationToken
|
|
): vscode.CodeLens[] {
|
|
const snapshot = this.snapshotManager.getSnapshotByFilePath(document.uri.fsPath);
|
|
if (!snapshot || snapshot.status !== 'active') {
|
|
return [];
|
|
}
|
|
|
|
const codeLenses: vscode.CodeLens[] = [];
|
|
|
|
// 为每个待处理的块添加 CodeLens
|
|
snapshot.chunks.forEach(chunk => {
|
|
if (chunk.status !== 'pending') return;
|
|
|
|
const range = new vscode.Range(
|
|
new vscode.Position(chunk.startLine, 0),
|
|
new vscode.Position(chunk.startLine, 0)
|
|
);
|
|
|
|
// Accept 按钮
|
|
codeLenses.push(
|
|
new vscode.CodeLens(range, {
|
|
title: '$(check) Accept',
|
|
command: COMMANDS.ACCEPT_CHUNK,
|
|
arguments: [snapshot.id, chunk.id],
|
|
tooltip: '接受此变更块',
|
|
})
|
|
);
|
|
|
|
// Reject 按钮
|
|
codeLenses.push(
|
|
new vscode.CodeLens(range, {
|
|
title: '$(close) Reject',
|
|
command: COMMANDS.REJECT_CHUNK,
|
|
arguments: [snapshot.id, chunk.id],
|
|
tooltip: '拒绝此变更块',
|
|
})
|
|
);
|
|
});
|
|
|
|
// 如果有多个块,添加全局操作按钮
|
|
const pendingChunks = snapshot.chunks.filter(c => c.status === 'pending');
|
|
if (pendingChunks.length > 1) {
|
|
const firstChunk = pendingChunks[0];
|
|
const range = new vscode.Range(
|
|
new vscode.Position(firstChunk.startLine, 0),
|
|
new vscode.Position(firstChunk.startLine, 0)
|
|
);
|
|
|
|
codeLenses.push(
|
|
new vscode.CodeLens(range, {
|
|
title: '$(check-all) Accept All',
|
|
command: COMMANDS.ACCEPT_ALL,
|
|
arguments: [snapshot.id],
|
|
tooltip: '接受所有变更',
|
|
})
|
|
);
|
|
|
|
codeLenses.push(
|
|
new vscode.CodeLens(range, {
|
|
title: '$(trash) Reject All',
|
|
command: COMMANDS.REJECT_ALL,
|
|
arguments: [snapshot.id],
|
|
tooltip: '拒绝所有变更',
|
|
})
|
|
);
|
|
}
|
|
|
|
return codeLenses;
|
|
}
|
|
}
|