From 87c0d61ed3890b21c9421688b83f4e15fb1344c5 Mon Sep 17 00:00:00 2001 From: alexweininger Date: Thu, 17 Jun 2021 16:07:34 -0700 Subject: [PATCH] Add setting to format json strings when viewing a document --- package.json | 5 +++++ src/commands/database/openDocument.ts | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b653cd9..9d7a56d 100644 --- a/package.json +++ b/package.json @@ -726,6 +726,11 @@ "type": "string", "default": "", "markdownDescription": "Project id of the active project, see [docs](https://github.com/streamlux/vscode-appwrite/) for more information." + }, + "appwrite.formatJsonStrings": { + "type": "boolean", + "default": true, + "markdownDescription": "Format JSON strings when viewing documents" } } } diff --git a/src/commands/database/openDocument.ts b/src/commands/database/openDocument.ts index e8b2e4a..45d57eb 100644 --- a/src/commands/database/openDocument.ts +++ b/src/commands/database/openDocument.ts @@ -1,8 +1,30 @@ +import { workspace } from "vscode"; import { DocumentTreeItem } from "../../tree/database/DocumentTreeItem"; import { openReadOnlyJson } from "../../ui/openReadonlyContent"; +function parseJSONString(str: string): { valid: boolean; value: any } { + try { + return { value: JSON.parse(str), valid: true }; + } catch (e) { + return { value: str, valid: false }; + } +} + export async function viewDocumentAsJson(documentTreeItem: DocumentTreeItem): Promise { - const documentId = documentTreeItem.document["$id"]; + const document = documentTreeItem.document; + const documentId = document["$id"]; + + const formatJsonStrings = workspace.getConfiguration("appwrite").get("formatJsonStrings"); + + if (formatJsonStrings) { + Object.entries(document).forEach(([key, value]) => { + if (typeof value === "string") { + const result = parseJSONString(value); + document[key] = result; + } + }); + } + await openReadOnlyJson( { label: documentId,