support diff view action

This commit is contained in:
bobo.yang 2023-05-05 12:28:11 +08:00
parent 6eef4b2a78
commit 91876443ac
3 changed files with 36 additions and 17 deletions

View File

@ -32,7 +32,7 @@ function initClipboard(codeBlocks, onApplyButtonClick, onApplyCodeButtonClick, o
// Add 'Apply' button
const applyButton = document.createElement('button');
applyButton.classList.add('apply-button');
applyButton.innerText = 'Apply Patch';
applyButton.innerText = 'Show Diff';
block.appendChild(applyButton);
applyButton.addEventListener('click', () => {

View File

@ -1,4 +1,6 @@
const vscode = require('vscode');
import * as path from 'path';
import { createTempSubdirectory } from './commonUtil';
export async function applyCodeFile(text: string) {
@ -23,7 +25,7 @@ export async function applyCodeFile(text: string) {
});
}
async function applyCode(text: string) {
export async function applyCode(text: string) {
if (vscode.window.visibleTextEditors.length > 1) {
vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return;
@ -43,4 +45,29 @@ async function applyCode(text: string) {
});
}
export default applyCode;
export async function diffView(code: string) {
if (vscode.window.visibleTextEditors.length > 1) {
vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return;
}
const editor = vscode.window.visibleTextEditors[0];
if (!editor) {
return;
}
const curFile = editor.document.fileName;
// get file name from fileSelected
const fileName = path.basename(curFile);
// create temp directory and file
const tempDir = await createTempSubdirectory('devchat/context');
const tempFile = path.join(tempDir, fileName);
// save code to temp file
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(code));
// open diff view
vscode.commands.executeCommand('vscode.diff', vscode.Uri.file(curFile), vscode.Uri.file(tempFile), 'Diff View');
}

View File

@ -6,7 +6,7 @@ import * as path from 'path';
import { promisify } from 'util';
import DevChat, { LogOptions } from './devchat';
import DtmWrapper from './dtm';
import applyCode, {applyCodeFile} from './applyCode';
import {applyCodeFile, diffView, applyCode} from './applyCode';
import './loadCommands';
import './loadContexts'
@ -147,20 +147,12 @@ async function handleMessage(
const logEntries = await devChat.log(logOptions);
panel.webview.postMessage({ command: 'loadHistoryMessages', entries: logEntries });
return;
case 'show_diff':
diffView(message.content);
return;
// TODO: remove block_apply
case 'block_apply':
const tempPatchFile = await saveTempPatchFile(message.content);
try {
const patchResult = await dtmWrapper.patch(tempPatchFile);
await deleteTempPatchFile(tempPatchFile);
if (patchResult.status === 0) {
vscode.window.showInformationMessage('Patch applied successfully.');
} else {
vscode.window.showErrorMessage(`Error applying patch: ${patchResult.message} ${patchResult.log}`);
}
} catch (error) {
await deleteTempPatchFile(tempPatchFile);
vscode.window.showErrorMessage(`Error applying patch: ${error}`);
}
diffView(message.content);
return;
case 'code_apply':
await applyCode(message.content);