refactor: Optimize file content retrieval

- Removed redundant getFileContent function from diffHandler
- Centralized getFileContent into commonUtil for shared usage
- Adjusted imports in devchat to reflect the new location
This commit is contained in:
bobo.yang 2024-03-10 16:34:07 +08:00
parent 655a9e52f8
commit 70ea72fabe
2 changed files with 2 additions and 17 deletions

View File

@ -1,7 +1,5 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import * as util from 'util';
import { createTempSubdirectory } from '../util/commonUtil';
import { regInMessage, regOutMessage } from '../util/reg_messages';
@ -9,7 +7,7 @@ import { applyCodeChanges, isValidActionString } from '../util/appyDiff';
import { logger } from '../util/logger';
import { FilePairManager } from '../util/diffFilePairs';
import { UiUtilWrapper } from '../util/uiUtil';
import { getFileContent } from '../util/commonUtil';
async function getDocumentText(): Promise<{ text: string; beforSelect: string; select: string, afterSelect: string } | undefined> {
@ -72,19 +70,6 @@ export async function diffView(code: string, tarFile: string) {
vscode.commands.executeCommand('vscode.diff', vscode.Uri.file(curFile), vscode.Uri.file(tempFile), 'Diff View');
}
export async function getFileContent(fileName: string): Promise<string | undefined> {
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 the 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) {

View File

@ -6,7 +6,7 @@ import { CommandRun, saveModelSettings } from "../util/commonUtil";
import { UiUtilWrapper } from '../util/uiUtil';
import { ApiKeyManager } from '../util/apiKey';
import { assertValue } from '../util/check';
import { getFileContent } from '../handler/diffHandler';
import { getFileContent } from '../util/commonUtil';
const envPath = path.join(__dirname, '..', '.env');