Added functions for converting diagnostics to/from a form that is compatible with JSON serialization.

This commit is contained in:
Eric Traut 2024-07-04 17:54:15 -07:00
parent 4df7ba72a0
commit 4fd12df54a
2 changed files with 58 additions and 0 deletions

View File

@ -86,6 +86,26 @@ export interface DiagnosticRelatedInfo {
priority: TaskListPriority;
}
export namespace DiagnosticRelatedInfo {
export function toJsonObj(info: DiagnosticRelatedInfo): any {
return {
message: info.message,
uri: info.uri.toJsonObj(),
range: info.range,
priority: info.priority,
};
}
export function fromJsonObj(obj: any): DiagnosticRelatedInfo {
return {
message: obj.message,
uri: Uri.fromJsonObj(obj.uri),
range: obj.range,
priority: obj.priority,
};
}
}
// Represents a single error or warning.
export class Diagnostic {
private _actions: DiagnosticAction[] | undefined;
@ -99,6 +119,26 @@ export class Diagnostic {
readonly priority: TaskListPriority = TaskListPriority.Normal
) {}
toJsonObj() {
return {
category: this.category,
message: this.message,
range: this.range,
priority: this.priority,
actions: this._actions,
rule: this._rule,
relatedInfo: this._relatedInfo.map((info) => DiagnosticRelatedInfo.toJsonObj(info)),
};
}
static fromJsonObj(obj: any) {
const diag = new Diagnostic(obj.category, obj.message, obj.range, obj.priority);
diag._actions = obj.actions;
diag._rule = obj.rule;
diag._relatedInfo = obj.relatedInfo.map((info: any) => DiagnosticRelatedInfo.fromJsonObj(info));
return diag;
}
addAction(action: DiagnosticAction) {
if (this._actions === undefined) {
this._actions = [action];

View File

@ -23,6 +23,24 @@ export interface FileDiagnostics {
diagnostics: Diagnostic[];
}
export namespace FileDiagnostics {
export function toJsonObj(fileDiag: FileDiagnostics): any {
return {
fileUri: fileDiag.fileUri.toJsonObj(),
version: fileDiag.version,
diagnostics: fileDiag.diagnostics.map((d) => d.toJsonObj()),
};
}
export function fromJsonObj(fileDiagObj: any): FileDiagnostics {
return {
fileUri: Uri.fromJsonObj(fileDiagObj.fileUri),
version: fileDiagObj.version,
diagnostics: fileDiagObj.diagnostics.map((d: any) => Diagnostic.fromJsonObj(d)),
};
}
}
// Creates and tracks a list of diagnostics.
export class DiagnosticSink {
private _diagnosticList: Diagnostic[];