diff --git a/assets/clipboard.js b/assets/clipboard.js index a58a5fd..ba0a746 100644 --- a/assets/clipboard.js +++ b/assets/clipboard.js @@ -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', () => { diff --git a/src/applyCode.ts b/src/applyCode.ts index f227b40..8c4b762 100644 --- a/src/applyCode.ts +++ b/src/applyCode.ts @@ -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; \ No newline at end of file +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'); + } diff --git a/src/messageHandler.ts b/src/messageHandler.ts index ed66670..bee7530 100644 --- a/src/messageHandler.ts +++ b/src/messageHandler.ts @@ -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);