Merge pull request #15 from covespace/diff_view
support diff view action
This commit is contained in:
commit
6be0b72253
@ -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', () => {
|
||||||
|
@ -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');
|
||||||
|
}
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user