|
|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
let selectedCodeForContext = '';
|
|
|
|
|
let contextFilePath = ''; // 存储当前上下文文件路径
|
|
|
|
|
const vscode = acquireVsCodeApi();
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
@ -6,6 +7,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
const userInput = document.getElementById('user-input');
|
|
|
|
|
const chatBox = document.getElementById('chat-box');
|
|
|
|
|
const loading = document.getElementById('loading');
|
|
|
|
|
|
|
|
|
|
// 初始化隐藏工作区
|
|
|
|
|
const workspaceContainer = document.querySelector('.workspace-container');
|
|
|
|
|
if (workspaceContainer) {
|
|
|
|
|
workspaceContainer.style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatForm.addEventListener('submit', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
@ -15,7 +22,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
vscode.postMessage({
|
|
|
|
|
command: 'ask',
|
|
|
|
|
text,
|
|
|
|
|
fileContent: selectedCodeForContext
|
|
|
|
|
fileContent: selectedCodeForContext,
|
|
|
|
|
fileContentPath: contextFilePath // 添加文件路径
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// addMessage('user', text);
|
|
|
|
|
@ -70,7 +78,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
// 高亮代码块
|
|
|
|
|
const codeBlocks = msgDiv.querySelectorAll('pre code');
|
|
|
|
|
codeBlocks.forEach(block => {
|
|
|
|
|
hljs.highlightElement(block);
|
|
|
|
|
if (hljs) {
|
|
|
|
|
hljs.highlightElement(block);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 滚动到底部
|
|
|
|
|
@ -86,27 +96,82 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
|
|
|
|
|
// 显示添加的行
|
|
|
|
|
if (codeDiff.added && codeDiff.added.length > 0) {
|
|
|
|
|
codeDiff.added.forEach(line => {
|
|
|
|
|
codeDiff.added.forEach((line, index) => {
|
|
|
|
|
const diffRow = document.createElement('div');
|
|
|
|
|
diffRow.className = 'diff-row';
|
|
|
|
|
|
|
|
|
|
// 修复行号显示问题
|
|
|
|
|
const lineNumber = document.createElement('div');
|
|
|
|
|
lineNumber.className = 'diff-line-number';
|
|
|
|
|
lineNumber.textContent = index + 1; // 使用索引+1作为行号
|
|
|
|
|
|
|
|
|
|
const diffLine = document.createElement('div');
|
|
|
|
|
diffLine.className = 'diff-line diff-added';
|
|
|
|
|
diffLine.textContent = `+ ${line}`;
|
|
|
|
|
diffContent.appendChild(diffLine);
|
|
|
|
|
|
|
|
|
|
diffRow.appendChild(lineNumber);
|
|
|
|
|
diffRow.appendChild(diffLine);
|
|
|
|
|
diffContent.appendChild(diffRow);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示删除的行
|
|
|
|
|
if (codeDiff.removed && codeDiff.removed.length > 0) {
|
|
|
|
|
codeDiff.removed.forEach(line => {
|
|
|
|
|
codeDiff.removed.forEach((line, index) => {
|
|
|
|
|
const diffRow = document.createElement('div');
|
|
|
|
|
diffRow.className = 'diff-row';
|
|
|
|
|
|
|
|
|
|
// 修复行号显示问题
|
|
|
|
|
const lineNumber = document.createElement('div');
|
|
|
|
|
lineNumber.className = 'diff-line-number';
|
|
|
|
|
lineNumber.textContent = index + 1; // 使用索引+1作为行号
|
|
|
|
|
|
|
|
|
|
const diffLine = document.createElement('div');
|
|
|
|
|
diffLine.className = 'diff-line diff-removed';
|
|
|
|
|
diffLine.textContent = `- ${line}`;
|
|
|
|
|
diffContent.appendChild(diffLine);
|
|
|
|
|
|
|
|
|
|
diffRow.appendChild(lineNumber);
|
|
|
|
|
diffRow.appendChild(diffLine);
|
|
|
|
|
diffContent.appendChild(diffRow);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加接受和拒绝按钮的事件监听
|
|
|
|
|
document.getElementById('accept-changes-btn').onclick = () => acceptChanges(codeDiff.modifiedCode);
|
|
|
|
|
document.getElementById('reject-changes-btn').onclick = () => rejectChanges();
|
|
|
|
|
|
|
|
|
|
diffModal.classList.remove('hidden');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 接受代码变更
|
|
|
|
|
function acceptChanges(modifiedCode) {
|
|
|
|
|
if (contextFilePath) {
|
|
|
|
|
// 发送消息到插件以接受变更
|
|
|
|
|
vscode.postMessage({
|
|
|
|
|
command: 'acceptChanges',
|
|
|
|
|
filePath: contextFilePath,
|
|
|
|
|
modifiedContent: modifiedCode
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
document.getElementById('diff-modal').classList.add('hidden');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拒绝代码变更
|
|
|
|
|
function rejectChanges() {
|
|
|
|
|
if (contextFilePath) {
|
|
|
|
|
// 发送消息到插件以拒绝变更
|
|
|
|
|
vscode.postMessage({
|
|
|
|
|
command: 'rejectChanges',
|
|
|
|
|
filePath: contextFilePath
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
document.getElementById('diff-modal').classList.add('hidden');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showLoading() {
|
|
|
|
|
loading.classList.remove('hidden');
|
|
|
|
|
}
|
|
|
|
|
@ -115,6 +180,65 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
loading.classList.add('hidden');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新工作区文件列表
|
|
|
|
|
function updateWorkspaceFiles(files) {
|
|
|
|
|
const workspaceContainer = document.querySelector('.workspace-container');
|
|
|
|
|
const workspaceFilesContainer = document.getElementById('workspace-files');
|
|
|
|
|
|
|
|
|
|
// 如果没有文件变更,则隐藏工作区
|
|
|
|
|
if (!files || Object.keys(files).length === 0) {
|
|
|
|
|
workspaceContainer.style.display = 'none';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示工作区
|
|
|
|
|
workspaceContainer.style.display = 'block';
|
|
|
|
|
workspaceFilesContainer.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
Object.keys(files).forEach(filePath => {
|
|
|
|
|
const fileChange = files[filePath];
|
|
|
|
|
const fileItem = document.createElement('div');
|
|
|
|
|
fileItem.className = 'workspace-file-item';
|
|
|
|
|
|
|
|
|
|
// 获取文件名
|
|
|
|
|
const fileName = filePath.split(/[\/\\]/).pop();
|
|
|
|
|
|
|
|
|
|
// 根据状态设置不同的显示样式
|
|
|
|
|
let statusText = '';
|
|
|
|
|
let statusClass = '';
|
|
|
|
|
|
|
|
|
|
switch (fileChange.status) {
|
|
|
|
|
case 'accepted':
|
|
|
|
|
statusText = '✓ 已接受';
|
|
|
|
|
statusClass = 'status-accepted';
|
|
|
|
|
break;
|
|
|
|
|
case 'rejected':
|
|
|
|
|
statusText = '✗ 已拒绝';
|
|
|
|
|
statusClass = 'status-rejected';
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
statusText = '待处理';
|
|
|
|
|
statusClass = 'status-pending';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileItem.innerHTML = `
|
|
|
|
|
<span class="file-icon">📄</span>
|
|
|
|
|
<span class="file-name" title="${filePath}">${fileName}</span>
|
|
|
|
|
<span class="file-status ${statusClass}">${statusText}</span>
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
fileItem.addEventListener('click', () => {
|
|
|
|
|
// 发送消息到插件以打开文件
|
|
|
|
|
vscode.postMessage({
|
|
|
|
|
command: 'openWorkspaceFile',
|
|
|
|
|
filePath: filePath
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
workspaceFilesContainer.appendChild(fileItem);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('message', (event) => {
|
|
|
|
|
const message = event.data;
|
|
|
|
|
console.log(message)
|
|
|
|
|
@ -157,6 +281,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
document.getElementById('file-browser-modal').classList.add('hidden');
|
|
|
|
|
|
|
|
|
|
// 清空文件搜索输入框
|
|
|
|
|
document.getElementById('file-search-input').value = '';
|
|
|
|
|
|
|
|
|
|
// 保存当前上下文文件路径
|
|
|
|
|
contextFilePath = file.path;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fileListContainer.appendChild(fileItem);
|
|
|
|
|
@ -171,10 +301,27 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
fileNameElement.innerText = fileName;
|
|
|
|
|
fileNameElement.title = fullPath;
|
|
|
|
|
selectedCodeForContext = message.fileContent;
|
|
|
|
|
contextFilePath = fullPath; // 保存上下文文件路径
|
|
|
|
|
|
|
|
|
|
// 隐藏占位符,显示文件标签
|
|
|
|
|
contextPlaceholder.classList.add('hidden');
|
|
|
|
|
contextTab.classList.remove('hidden');
|
|
|
|
|
} else if (message.command === 'updateWorkspaceFiles') {
|
|
|
|
|
// 更新工作区文件列表
|
|
|
|
|
updateWorkspaceFiles(message.files);
|
|
|
|
|
} else if (message.command === 'restoreState') {
|
|
|
|
|
// 恢复面板状态
|
|
|
|
|
currentSessionHistory = message.history || [];
|
|
|
|
|
|
|
|
|
|
// 恢复显示历史消息
|
|
|
|
|
currentSessionHistory.forEach(msg => {
|
|
|
|
|
addMessage(msg.role, msg.content, null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 恢复工作区变更
|
|
|
|
|
if (message.workspaceChanges) {
|
|
|
|
|
updateWorkspaceFiles(message.workspaceChanges);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
@ -221,6 +368,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
contextTab.classList.add('hidden');
|
|
|
|
|
contextPlaceholder.classList.remove('hidden');
|
|
|
|
|
selectedCodeForContext = '';
|
|
|
|
|
contextFilePath = '';
|
|
|
|
|
});
|
|
|
|
|
// 关闭差异模态框
|
|
|
|
|
document.getElementById('close-diff-modal').addEventListener('click', () => {
|
|
|
|
|
|