Add setting to format json strings when viewing a document (#22)

* Add setting to format json strings when viewing a document

* Update changelog
This commit is contained in:
Alex Weininger 2021-06-17 16:49:08 -07:00 committed by GitHub
parent 2e77f93759
commit 47c405a9f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 1 deletions

View file

@ -5,6 +5,8 @@ All notable changes to the "vscode-appwrite" extension will be documented in thi
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [Unreleased] ## [Unreleased]
## Added
- New feature! JSON strings inside documents will now be automatically formatted as JSON when viewing documents. You can turn this feature off via the `appwrite.formatJsonStrings` setting.
## [0.1.2] - 2021-6-03 ## [0.1.2] - 2021-6-03
## Added ## Added

View file

@ -726,6 +726,11 @@
"type": "string", "type": "string",
"default": "", "default": "",
"markdownDescription": "Project id of the active project, see [docs](https://github.com/streamlux/vscode-appwrite/) for more information." "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"
} }
} }
} }

View file

@ -1,8 +1,30 @@
import { workspace } from "vscode";
import { DocumentTreeItem } from "../../tree/database/DocumentTreeItem"; import { DocumentTreeItem } from "../../tree/database/DocumentTreeItem";
import { openReadOnlyJson } from "../../ui/openReadonlyContent"; 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<void> { export async function viewDocumentAsJson(documentTreeItem: DocumentTreeItem): Promise<void> {
const documentId = documentTreeItem.document["$id"]; const document = documentTreeItem.document;
const documentId = document["$id"];
const formatJsonStrings = workspace.getConfiguration("appwrite").get<Boolean>("formatJsonStrings");
if (formatJsonStrings) {
Object.entries(document).forEach(([key, value]) => {
if (typeof value === "string") {
const result = parseJSONString(value);
document[key] = result;
}
});
}
await openReadOnlyJson( await openReadOnlyJson(
{ {
label: documentId, label: documentId,