2023-05-04 16:55:40 +08:00
|
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import * as path from 'path';
|
2023-05-05 21:27:40 +08:00
|
|
|
import { createTempSubdirectory, getLanguageIdByFileName } from '../util/commonUtil';
|
2023-05-04 16:55:40 +08:00
|
|
|
|
|
|
|
export async function handleFileSelected(fileSelected: string) {
|
|
|
|
// get file name from fileSelected
|
|
|
|
const fileName = path.basename(fileSelected);
|
|
|
|
|
|
|
|
// create temp directory and file
|
|
|
|
const tempDir = await createTempSubdirectory('devchat/context');
|
|
|
|
const tempFile = path.join(tempDir, fileName);
|
|
|
|
|
|
|
|
// load content in fileSelected
|
|
|
|
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(fileSelected));
|
|
|
|
|
|
|
|
// get the language from fileSelected
|
|
|
|
const languageId = await getLanguageIdByFileName(fileSelected);
|
|
|
|
|
|
|
|
// convert fileContent to markdown code block with languageId and file path
|
|
|
|
const markdownCodeBlock = `\`\`\`${languageId} path=${fileSelected}\n${fileContent}\n\`\`\``;
|
|
|
|
|
|
|
|
// save markdownCodeBlock to temp file
|
|
|
|
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFile), Buffer.from(markdownCodeBlock));
|
|
|
|
|
|
|
|
return `[context|${tempFile}]`;
|
|
|
|
}
|