Improve showDiff.ts functionality and readability
- Add fs and util imports for file handling. - Refactor diffView function to support tarFile parameter. - Implement getFileContent function for reading file content. - Update getNewCode function to handle message.fileName. - Adjust showDiff and blockApply functions to use message.fileName.
This commit is contained in:
parent
16c9e2ae26
commit
dbc0b46bd1
@ -1,5 +1,8 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as util from 'util';
|
||||||
|
|
||||||
import { createTempSubdirectory } from '../util/commonUtil';
|
import { createTempSubdirectory } from '../util/commonUtil';
|
||||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||||
import { applyCodeChanges, isValidActionString } from '../util/appyDiff';
|
import { applyCodeChanges, isValidActionString } from '../util/appyDiff';
|
||||||
@ -12,14 +15,14 @@ async function getDocumentText(): Promise<{ text: string; beforSelect: string; s
|
|||||||
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
|
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
|
||||||
|
|
||||||
if (validVisibleTextEditors.length > 1) {
|
if (validVisibleTextEditors.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 undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const editor = validVisibleTextEditors[0];
|
const editor = validVisibleTextEditors[0];
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get whole text in editor
|
// get whole text in editor
|
||||||
const text = editor.document.getText();
|
const text = editor.document.getText();
|
||||||
@ -33,37 +36,67 @@ async function getDocumentText(): Promise<{ text: string; beforSelect: string; s
|
|||||||
return { text, beforSelect: textBeforeSelection, select: selectedText, afterSelect: textAfterSelection };
|
return { text, beforSelect: textBeforeSelection, select: selectedText, afterSelect: textAfterSelection };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function diffView(code: string) {
|
export async function diffView(code: string, tarFile: string) {
|
||||||
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
|
let curFile = '';
|
||||||
|
if (!tarFile) {
|
||||||
|
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
|
||||||
|
|
||||||
if (validVisibleTextEditors.length > 1) {
|
if (validVisibleTextEditors.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;
|
||||||
}
|
}
|
||||||
|
|
||||||
const editor = validVisibleTextEditors[0];
|
const editor = validVisibleTextEditors[0];
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
curFile = editor.document.fileName;
|
||||||
|
} else {
|
||||||
|
curFile = tarFile;
|
||||||
|
}
|
||||||
|
|
||||||
const curFile = editor.document.fileName;
|
|
||||||
// get file name from fileSelected
|
// get file name from fileSelected
|
||||||
const fileName = path.basename(curFile);
|
const fileName = path.basename(curFile);
|
||||||
|
|
||||||
// create temp directory and file
|
// create temp directory and file
|
||||||
const tempDir = await createTempSubdirectory('devchat/context');
|
const tempDir = await createTempSubdirectory('devchat/context');
|
||||||
const tempFile = path.join(tempDir, fileName);
|
const tempFile = path.join(tempDir, fileName);
|
||||||
|
|
||||||
// save code to temp file
|
// save code to temp file
|
||||||
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(code));
|
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(code));
|
||||||
|
|
||||||
// open diff view
|
// open diff view
|
||||||
FilePairManager.getInstance().addFilePair(curFile, tempFile);
|
FilePairManager.getInstance().addFilePair(curFile, tempFile);
|
||||||
vscode.commands.executeCommand('vscode.diff', vscode.Uri.file(curFile), vscode.Uri.file(tempFile), 'Diff View');
|
vscode.commands.executeCommand('vscode.diff', vscode.Uri.file(curFile), vscode.Uri.file(tempFile), 'Diff View');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNewCode(message: any) : Promise<string | undefined> {
|
async function getFileContent(fileName: string): Promise<string | undefined> {
|
||||||
const codeTextObj = await getDocumentText();
|
const readFile = util.promisify(fs.readFile);
|
||||||
|
try {
|
||||||
|
// Read file content from fileName
|
||||||
|
const fileContent = await readFile(fileName, 'utf-8');
|
||||||
|
// Return the whole text in the file with name fileName
|
||||||
|
return fileContent;
|
||||||
|
} catch (error) {
|
||||||
|
logger.channel()!.error(`Error reading file ${fileName}:`, error);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getNewCode(message: any): Promise<string | undefined> {
|
||||||
|
let codeTextObj: { text: string; beforSelect: string; select: string; afterSelect: string; } | undefined = undefined;
|
||||||
|
if (message.fileName) {
|
||||||
|
const fileContent = await getFileContent(message.fileName);
|
||||||
|
if (!fileContent) {
|
||||||
|
logger.channel()!.error(`read file ${message.fileName} failed`);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
codeTextObj = { text: fileContent!, beforSelect: fileContent, select: '', afterSelect: '' };
|
||||||
|
} else {
|
||||||
|
codeTextObj = await getDocumentText();
|
||||||
|
}
|
||||||
if (!codeTextObj) {
|
if (!codeTextObj) {
|
||||||
logger.channel()!.error('getDocumentText failed');
|
logger.channel()!.error('getDocumentText failed');
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -87,25 +120,25 @@ async function getNewCode(message: any) : Promise<string | undefined> {
|
|||||||
return newCode;
|
return newCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
regInMessage({command: 'show_diff', content: ''});
|
regInMessage({ command: 'show_diff', content: '', fileName: '' });
|
||||||
export async function showDiff(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
export async function showDiff(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> {
|
||||||
const newCode = await getNewCode(message);
|
const newCode = await getNewCode(message);
|
||||||
if (!newCode) {
|
if (!newCode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
diffView(newCode);
|
diffView(newCode, message.fileName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
regInMessage({command: 'block_apply', content: ''});
|
regInMessage({ command: 'block_apply', content: '', fileName: '' });
|
||||||
export async function blockApply(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
export async function blockApply(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> {
|
||||||
const newCode = await getNewCode(message);
|
const newCode = await getNewCode(message);
|
||||||
if (!newCode) {
|
if (!newCode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
diffView(newCode);
|
diffView(newCode, message.fileName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user