Merge pull request #15 from covespace/diff_view

support diff view action
This commit is contained in:
boob.yang 2023-05-05 12:38:30 +08:00 committed by GitHub
commit 6be0b72253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 17 deletions

View File

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

View File

@ -1,4 +1,6 @@
const vscode = require('vscode'); const vscode = require('vscode');
import * as path from 'path';
import { createTempSubdirectory } from './commonUtil';
export async function applyCodeFile(text: string) { 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) { 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.`); vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return; 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 { promisify } from 'util';
import DevChat, { LogOptions } from './devchat'; import DevChat, { LogOptions } from './devchat';
import DtmWrapper from './dtm'; import DtmWrapper from './dtm';
import applyCode, {applyCodeFile} from './applyCode'; import {applyCodeFile, diffView, applyCode} from './applyCode';
import './loadCommands'; import './loadCommands';
import './loadContexts' import './loadContexts'
@ -147,20 +147,12 @@ async function handleMessage(
const logEntries = await devChat.log(logOptions); const logEntries = await devChat.log(logOptions);
panel.webview.postMessage({ command: 'loadHistoryMessages', entries: logEntries }); panel.webview.postMessage({ command: 'loadHistoryMessages', entries: logEntries });
return; return;
case 'show_diff':
diffView(message.content);
return;
// TODO: remove block_apply
case 'block_apply': case 'block_apply':
const tempPatchFile = await saveTempPatchFile(message.content); diffView(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}`);
}
return; return;
case 'code_apply': case 'code_apply':
await applyCode(message.content); await applyCode(message.content);