From 98d06b28923ad778491dd47dfa337939b4ee4efd Mon Sep 17 00:00:00 2001 From: Karl Burtram Date: Fri, 26 Oct 2018 17:33:46 -0700 Subject: [PATCH] Format JSON and XML output when clicking resultgrid link (#3024) --- src/sql/parts/query/editor/gridPanel.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/sql/parts/query/editor/gridPanel.ts b/src/sql/parts/query/editor/gridPanel.ts index f6ba04b042..b37e8c5b3c 100644 --- a/src/sql/parts/query/editor/gridPanel.ts +++ b/src/sql/parts/query/editor/gridPanel.ts @@ -21,6 +21,7 @@ import { CopyKeybind } from 'sql/base/browser/ui/table/plugins/copyKeybind.plugi import { AdditionalKeyBindings } from 'sql/base/browser/ui/table/plugins/additionalKeyBindings.plugin'; import * as sqlops from 'sqlops'; +import * as pretty from 'pretty-data'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IContextMenuService } from 'vs/platform/contextview/browser/contextView'; @@ -477,7 +478,27 @@ class GridTable extends Disposable implements IView { if (column && (column.isXml || column.isJson)) { this.runner.getQueryRows(event.cell.row, 1, this.resultSet.batchId, this.resultSet.id).then(d => { let value = d.resultSubset.rows[0][event.cell.cell - 1]; - let input = this.untitledEditorService.createOrGet(undefined, column.isXml ? 'xml' : 'json', value.displayValue); + let content = value.displayValue; + if (column.isXml) { + try { + content = pretty.pd.xml(content); + } catch (e) { + // If Xml fails to parse, fall back on original Xml content + } + } else { + let jsonContent: string = undefined; + try { + jsonContent = JSON.parse(content); + } catch (e) { + // If Json fails to parse, fall back on original Json content + } + if (jsonContent) { + // If Json content was valid and parsed, pretty print content to a string + content = JSON.stringify(jsonContent, undefined, 4); + } + } + + let input = this.untitledEditorService.createOrGet(undefined, column.isXml ? 'xml' : 'json', content); this.editorService.openEditor(input); }); }