Visual Studio Code API: Getting a lot of problems
Views (360)
A hard way.
This post keep diving in Visual Studio API, as this other post did: Creating our own Visual Studio Code extensions. Making promises come true - Dynamics 365 Business Central Community
And other posts will follow this way. I am not intending to get a lot of readers with this series about extending Visual Studio Code editing features. This is the way of very few people, because creating new features for VSCODE involves a lot of techs and big efforts. People who are involved in this kind of develop must know JavaScript (or TypeScript), how to build a web node app and the hardest part of the deal, diving deep into Visual Studio Code API structure.
Is worth all this effort?
Right now, AL developers have a lot of features in Visual Code to program apps faster. But remain work, and sometimes we have special needs from our AL developers. In the other side we don´t need to be car mechanic to drive a car but helps a lot. Knowing the inside of a car could be the difference between a regular car driver and a Formula One pilot. Is not by chance, Juan Manuel Fangio, one of the greatest F-1 was car mechanic before. Know how to tailor Visual Studio Code could make us first class Visual Studio Code handlers. And your web developing skills will grow in exponential way. I began this process three months ago, and the answer is yes, was a worth effort. I developed features to pass our old C/SIDE to AppSource faster and learnt a lot in the way.
But be careful: is a personal point of view, and everyone must evaluate if the high cost to pay is worth.
Hands-on: I want to get a lot of problems!!!
If we want to fix problems passing AppSource certification, the first step is solving the info from the above Problem panel:
It is easy to access to panel content with this code. If you want to process with code this object could be complex and drive us to a lot of errors:
const AppUri = vscode.workspace.workspaceFile;
const AppDiagnostics = vscode.languages.getDiagnostics(AppUri);
for (let i = 0; i < AppDiagnostics.length; i++) {
console.log(AppDiagnostics[i]);
}
But the resultant object it´s a little messy: a collection of multi-level objects, and even with objects without any problem. This is the view of console log:
So, I want to make in this hand-on a function to return a single level JSON with all the most important diagnostic data. This way we ease implementing further diagnostic solving processes, and make extensions to fix some of them with code:
function GetDiagnostics()
{
const AppUri = vscode.workspace.workspaceFile;
const AppDiagnostics = vscode.languages.getDiagnostics(AppUri);
let Problems = [];
for (let i = 0; i < AppDiagnostics.length; i++) {
for (let j = 0; j < AppDiagnostics[i][1].length; j++) {
let Problem = AppDiagnostics[i][1][j];
let ProblemRange = Problem.range;
Problems.push(
{
FilePath: AppDiagnostics[i][0].path,
StarTLine: ProblemRange.start.line,
StartColumn: ProblemRange.start.character,
EndLine: ProblemRange.end.line,
EndColumn: ProblemRange.end.character,
MessageCode: Problem.code,
MessageDescription: Problem.message,
Severity: Problem.severity
})
}
}
return Problems;
}
The result is an array with a more readable result, with an element for each problem, and only with the objects with any problems:
Hope this could be useful for extensions developers.

Like
Report
*This post is locked for comments