feat:实现读取本地文件的功能
This commit is contained in:
@ -52,7 +52,7 @@
|
||||
"ic-coder-sidebar": [
|
||||
{
|
||||
"id": "ic-coder.mainView",
|
||||
"name": "IC 助手",
|
||||
"name": "IC Coder",
|
||||
"type": "webview"
|
||||
}
|
||||
]
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import * as vscode from "vscode";
|
||||
import { getWebviewContent } from "../utils/webviewContent";
|
||||
import { handleUserMessage, insertCodeToEditor } from "../utils/messageHandler";
|
||||
import { getWebviewContent } from "../views/webviewContent";
|
||||
import { handleUserMessage, insertCodeToEditor, handleReadFile } from "../utils/messageHandler";
|
||||
|
||||
/**
|
||||
* 创建并显示 IC 助手面板
|
||||
@ -27,6 +27,9 @@ export function showICHelperPanel(context: vscode.ExtensionContext) {
|
||||
case "sendMessage":
|
||||
handleUserMessage(panel, message.text);
|
||||
break;
|
||||
case "readFile":
|
||||
handleReadFile(panel, message.filePath);
|
||||
break;
|
||||
case "insertCode":
|
||||
insertCodeToEditor(message.code);
|
||||
break;
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import * as vscode from "vscode";
|
||||
import { readFileContent } from "./readFiles";
|
||||
|
||||
/**
|
||||
* 处理用户消息
|
||||
@ -16,6 +17,28 @@ export function handleUserMessage(panel: vscode.WebviewPanel, text: string) {
|
||||
}, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件读取请求
|
||||
*/
|
||||
export async function handleReadFile(
|
||||
panel: vscode.WebviewPanel,
|
||||
filePath: string
|
||||
) {
|
||||
try {
|
||||
const content = await readFileContent(filePath);
|
||||
panel.webview.postMessage({
|
||||
command: "fileContent",
|
||||
content: content,
|
||||
filePath: filePath,
|
||||
});
|
||||
} catch (error) {
|
||||
panel.webview.postMessage({
|
||||
command: "fileError",
|
||||
error: error instanceof Error ? error.message : "读取文件失败",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模拟回复
|
||||
*/
|
||||
|
||||
164
src/utils/readFiles.ts
Normal file
164
src/utils/readFiles.ts
Normal file
@ -0,0 +1,164 @@
|
||||
import * as vscode from "vscode";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
/**
|
||||
* 读取文件内容
|
||||
*/
|
||||
export async function readFileContent(filePath: string): Promise<string> {
|
||||
try {
|
||||
// 如果是相对路径,转换为绝对路径
|
||||
let absolutePath = filePath;
|
||||
if (!path.isAbsolute(filePath)) {
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`文件不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 检查是否是文件
|
||||
const stats = fs.statSync(absolutePath);
|
||||
if (!stats.isFile()) {
|
||||
throw new Error(`路径不是文件: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
const content = fs.readFileSync(absolutePath, "utf-8");
|
||||
return content;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取多个文件内容
|
||||
*/
|
||||
export async function readMultipleFiles(
|
||||
filePaths: string[]
|
||||
): Promise<{ path: string; content: string; error?: string }[]> {
|
||||
const results = [];
|
||||
|
||||
for (const filePath of filePaths) {
|
||||
try {
|
||||
const content = await readFileContent(filePath);
|
||||
results.push({ path: filePath, content });
|
||||
} catch (error) {
|
||||
results.push({
|
||||
path: filePath,
|
||||
content: "",
|
||||
error: error instanceof Error ? error.message : "未知错误",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取目录下所有文件
|
||||
*/
|
||||
export async function readDirectory(
|
||||
dirPath: string,
|
||||
extensions?: string[]
|
||||
): Promise<{ path: string; content: string }[]> {
|
||||
try {
|
||||
// 如果是相对路径,转换为绝对路径
|
||||
let absolutePath = dirPath;
|
||||
if (!path.isAbsolute(dirPath)) {
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查目录是否存在
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
throw new Error(`目录不存在: ${absolutePath}`);
|
||||
}
|
||||
|
||||
const stats = fs.statSync(absolutePath);
|
||||
if (!stats.isDirectory()) {
|
||||
throw new Error(`路径不是目录: ${absolutePath}`);
|
||||
}
|
||||
|
||||
// 读取目录内容
|
||||
const files = fs.readdirSync(absolutePath);
|
||||
const results = [];
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(absolutePath, file);
|
||||
const fileStats = fs.statSync(filePath);
|
||||
|
||||
if (fileStats.isFile()) {
|
||||
// 如果指定了扩展名过滤
|
||||
if (extensions && extensions.length > 0) {
|
||||
const ext = path.extname(file);
|
||||
if (!extensions.includes(ext)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
results.push({ path: file, content });
|
||||
} catch (error) {
|
||||
// 跳过无法读取的文件
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件信息
|
||||
*/
|
||||
export function getFileInfo(filePath: string): {
|
||||
exists: boolean;
|
||||
isFile: boolean;
|
||||
isDirectory: boolean;
|
||||
size?: number;
|
||||
extension?: string;
|
||||
} {
|
||||
try {
|
||||
let absolutePath = filePath;
|
||||
if (!path.isAbsolute(filePath)) {
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
if (workspaceFolders && workspaceFolders.length > 0) {
|
||||
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
return {
|
||||
exists: false,
|
||||
isFile: false,
|
||||
isDirectory: false,
|
||||
};
|
||||
}
|
||||
|
||||
const stats = fs.statSync(absolutePath);
|
||||
return {
|
||||
exists: true,
|
||||
isFile: stats.isFile(),
|
||||
isDirectory: stats.isDirectory(),
|
||||
size: stats.size,
|
||||
extension: path.extname(absolutePath),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
exists: false,
|
||||
isFile: false,
|
||||
isDirectory: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@ export function getWebviewContent(): string {
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>IC Coder 助手</title>
|
||||
<title>IC Coder</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: var(--vscode-font-family);
|
||||
@ -96,6 +96,55 @@ export function getWebviewContent(): string {
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
}
|
||||
.file-reader-section {
|
||||
margin-bottom: 20px;
|
||||
padding: 15px;
|
||||
background: var(--vscode-input-background);
|
||||
border: 1px solid var(--vscode-input-border);
|
||||
border-radius: 8px;
|
||||
}
|
||||
.file-reader-section h3 {
|
||||
margin: 0 0 10px 0;
|
||||
color: var(--vscode-button-background);
|
||||
}
|
||||
.file-input-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.file-input-group input {
|
||||
flex: 1;
|
||||
padding: 8px;
|
||||
background: var(--vscode-input-background);
|
||||
color: var(--vscode-input-foreground);
|
||||
border: 1px solid var(--vscode-input-border);
|
||||
border-radius: 4px;
|
||||
}
|
||||
.file-content {
|
||||
margin-top: 15px;
|
||||
padding: 10px;
|
||||
background: var(--vscode-editor-background);
|
||||
border: 1px solid var(--vscode-panel-border);
|
||||
border-radius: 4px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
font-family: 'Courier New', monospace;
|
||||
font-size: 12px;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
.file-content.empty {
|
||||
color: var(--vscode-descriptionForeground);
|
||||
font-style: italic;
|
||||
}
|
||||
.error-message {
|
||||
color: var(--vscode-errorForeground);
|
||||
padding: 8px;
|
||||
background: var(--vscode-inputValidation-errorBackground);
|
||||
border: 1px solid var(--vscode-inputValidation-errorBorder);
|
||||
border-radius: 4px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@ -104,6 +153,22 @@ export function getWebviewContent(): string {
|
||||
<p>专注于真实FPGA研发的Verilog智能体编程平台</p>
|
||||
</div>
|
||||
|
||||
<div class="file-reader-section">
|
||||
<h3>📁 文件读取</h3>
|
||||
<div class="file-input-group">
|
||||
<input
|
||||
type="text"
|
||||
id="filePathInput"
|
||||
placeholder="输入文件路径(相对或绝对路径)..."
|
||||
/>
|
||||
<button onclick="readFile()">读取文件</button>
|
||||
</div>
|
||||
<div id="fileContent" class="file-content empty">
|
||||
文件内容将在这里显示...
|
||||
</div>
|
||||
<div id="errorMessage" style="display: none;"></div>
|
||||
</div>
|
||||
|
||||
<div class="chat-container">
|
||||
<div id="messages" class="messages">
|
||||
<div class="message bot-message">
|
||||
@ -133,6 +198,9 @@ export function getWebviewContent(): string {
|
||||
const vscode = acquireVsCodeApi();
|
||||
const messagesEl = document.getElementById('messages');
|
||||
const messageInput = document.getElementById('messageInput');
|
||||
const filePathInput = document.getElementById('filePathInput');
|
||||
const fileContentEl = document.getElementById('fileContent');
|
||||
const errorMessageEl = document.getElementById('errorMessage');
|
||||
|
||||
function sendMessage() {
|
||||
const text = messageInput.value.trim();
|
||||
@ -144,6 +212,39 @@ export function getWebviewContent(): string {
|
||||
messageInput.focus();
|
||||
}
|
||||
|
||||
function readFile() {
|
||||
const filePath = filePathInput.value.trim();
|
||||
if (!filePath) {
|
||||
showError('请输入文件路径');
|
||||
return;
|
||||
}
|
||||
|
||||
// 清空之前的内容和错误
|
||||
fileContentEl.textContent = '正在读取文件...';
|
||||
fileContentEl.className = 'file-content';
|
||||
errorMessageEl.style.display = 'none';
|
||||
|
||||
// 发送读取文件请求
|
||||
vscode.postMessage({ command: 'readFile', filePath: filePath });
|
||||
}
|
||||
|
||||
function showError(message) {
|
||||
errorMessageEl.textContent = message;
|
||||
errorMessageEl.className = 'error-message';
|
||||
errorMessageEl.style.display = 'block';
|
||||
fileContentEl.textContent = '文件内容将在这里显示...';
|
||||
fileContentEl.className = 'file-content empty';
|
||||
}
|
||||
|
||||
function displayFileContent(content, filePath) {
|
||||
fileContentEl.textContent = content;
|
||||
fileContentEl.className = 'file-content';
|
||||
errorMessageEl.style.display = 'none';
|
||||
|
||||
// 在消息区域也显示一条提示
|
||||
addMessage(\`已读取文件: \${filePath}\`, 'bot');
|
||||
}
|
||||
|
||||
function quickAction(type) {
|
||||
const questions = {
|
||||
counter: '生成一个4位同步计数器',
|
||||
@ -165,8 +266,25 @@ export function getWebviewContent(): string {
|
||||
}
|
||||
|
||||
window.addEventListener('message', event => {
|
||||
if (event.data.command === 'receiveMessage') {
|
||||
addMessage(event.data.text, 'bot');
|
||||
const message = event.data;
|
||||
|
||||
switch (message.command) {
|
||||
case 'receiveMessage':
|
||||
addMessage(message.text, 'bot');
|
||||
break;
|
||||
case 'fileContent':
|
||||
displayFileContent(message.content, message.filePath);
|
||||
break;
|
||||
case 'fileError':
|
||||
showError(message.error);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// 支持回车键读取文件
|
||||
filePathInput.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Enter') {
|
||||
readFile();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user