Skip to content

Commit a6f013a

Browse files
committed
feat:增加读取出错信息的工具函数
1 parent d8260d0 commit a6f013a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/apiTools.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,80 @@ export const readPdf: Tool = {
12391239

12401240
registerTool(readPdf);
12411241

1242+
// 9. 提取前5个Error信息
1243+
export const diagnosticTop5Errors: Tool = {
1244+
name: 'diagnostic_top5_errors',
1245+
description: `从 VSCode Problems 面板中提取当前工程前5条错误信息,并组织成 JSON 返回。
1246+
1247+
使用场景:
1248+
1249+
代码检查:快速收集当前最重要的编译/类型错误。
1250+
问题分析:在提交代码前,了解主要报错内容。
1251+
1252+
具体示例:
1253+
用户说:“列出我项目里最严重的前几个错误。”
1254+
调用 diagnostic_top5_errors,无需额外参数,返回:
1255+
[
1256+
{ "file": "xxx.ts", "line": 10, "character": 5, "message": "xxx" },
1257+
...
1258+
]`,
1259+
parameters: {
1260+
type: 'object',
1261+
properties: {},
1262+
required: [],
1263+
},
1264+
function: async (_args: {}) => {
1265+
vscode.window.showInformationMessage('CodeReDesign 正在提取Problems中的错误信息');
1266+
1267+
try
1268+
{
1269+
const arrResult: IErrorInfo[] = [];
1270+
const arrDiagnostics: [vscode.Uri, vscode.Diagnostic[]][] = vscode.languages.getDiagnostics();
1271+
1272+
for (const [objUri, arrDiagList] of arrDiagnostics)
1273+
{
1274+
for (const objDiagnostic of arrDiagList)
1275+
{
1276+
if (objDiagnostic.severity === vscode.DiagnosticSeverity.Error)
1277+
{
1278+
const objErrorInfo: IErrorInfo =
1279+
{
1280+
strFilePath: objUri.fsPath,
1281+
nLine: objDiagnostic.range.start.line + 1,
1282+
nCharacter: objDiagnostic.range.start.character + 1,
1283+
strMessage: objDiagnostic.message
1284+
};
1285+
1286+
arrResult.push(objErrorInfo);
1287+
1288+
if (arrResult.length >= 5)
1289+
{
1290+
return JSON.stringify(arrResult, null, 4);
1291+
}
1292+
}
1293+
}
1294+
}
1295+
1296+
return JSON.stringify(arrResult, null, 4);
1297+
}
1298+
catch (error: any)
1299+
{
1300+
return `提取失败: ${error.message}`;
1301+
}
1302+
},
1303+
};
1304+
1305+
// 定义接口,供上面使用
1306+
interface IErrorInfo
1307+
{
1308+
strFilePath: string
1309+
nLine: number
1310+
nCharacter: number
1311+
strMessage: string
1312+
}
1313+
1314+
// 注册工具
1315+
registerTool(diagnosticTop5Errors);
12421316

12431317

12441318

0 commit comments

Comments
 (0)