Tries to parse evaluated string as json string if it could not be parsed otherwise. Should make the debug visualizer compatible with C++.

This commit is contained in:
Henning Dieterichs
2020-02-24 22:53:46 +01:00
parent 43e0a13dca
commit 2bc1ce380d
2 changed files with 44 additions and 4 deletions

View File

@@ -36,12 +36,49 @@ class GenericDataExtractionProvider implements DataExtractionProvider {
expression,
frameId,
});
const resultStr = reply.result;
const jsonData = resultStr.substr(1, resultStr.length - 2);
const result = JSON.parse(jsonData) as ExtractedData;
function isEnclosedWith(str: string, char: string): boolean {
return str.startsWith(char) && str.endsWith(char);
}
const jsonData = reply.result.trim();
function parseJson(str: string) {
try {
return JSON.parse(str);
} catch (error) {
throw new Error(
`Could not parse \`${str}\` as JSON.\n${error.message}`
);
}
}
let result;
try {
let jsonData2;
// Remove optionally enclosing characters.
if (
isEnclosedWith(jsonData, '"') ||
isEnclosedWith(jsonData, "'")
) {
// In case of JavaScript: `"{ "kind": { ... }, "text": "some\ntext" }"`
jsonData2 = jsonData.substr(1, jsonData.length - 2);
} else {
// Just in case no quoting is done.
jsonData2 = jsonData;
}
result = parseJson(jsonData2);
} catch (e) {
// in case of C++: `"{ \"kind\": { ... }, \"text\": \"some\\ntext\" }"`
const str = parseJson(jsonData);
// str is now `{ "kind": { ... }, "text": "some\ntext" }"`
result = parseJson(str);
// result is now { kind: { ... }, text: "some\ntext" }
}
if (!isExtractedData(result)) {
throw new Error("Invalid Data");
throw new Error("Data does not match ExtractedData interface.");
}
return {

View File

@@ -149,6 +149,9 @@ export class VsCodeDebugSession {
frameId: args.frameId,
column: args.column,
});
if (!reply) {
return [];
}
return reply.targets;
} catch (error) {
console.error(error);