44 IconArrowsDiagonalMinimize2 ,
55 IconArrowsMaximize ,
66 IconArrowsMinimize ,
7+ IconBraces ,
78 IconChevronDown ,
89 IconChevronUp ,
910 IconEye ,
@@ -17,10 +18,11 @@ import formatDistanceToNow from 'date-fns/formatDistanceToNow';
1718import circularSafeStringify from 'json-stringify-safe' ;
1819import * as React from 'react' ;
1920import { toString } from 'shape' ;
21+ import { format as sqlFormat } from 'sql-formatter' ;
2022import { MODE_FEATURES } from '../shared/constants' ;
2123import { EVAL_ERRORS } from '../shared/errors' ;
2224import log from '../shared/log' ;
23- import { PanelInfo , PanelResult } from '../shared/state' ;
25+ import { PanelInfo , PanelResult , ProgramPanelInfo } from '../shared/state' ;
2426import { humanSize } from '../shared/text' ;
2527import { panelRPC } from './asyncRPC' ;
2628import { Alert } from './components/Alert' ;
@@ -255,6 +257,22 @@ export function Panel({
255257 setError ( results . exception ) ;
256258 } , [ results . exception ] ) ;
257259
260+ function formatThis ( ) {
261+ const old = panel . content ;
262+ try {
263+ panel . content = sqlFormat ( old ) ;
264+ if ( panel . content === old ) {
265+ // Don't update if it hasn't changed
266+ return ;
267+ }
268+ } catch ( e ) {
269+ log . error ( e ) ;
270+ panel . content = old ;
271+ }
272+
273+ updatePanel ( panel ) ;
274+ }
275+
258276 async function evalThis ( ) {
259277 if ( killable ) {
260278 await killProcess ( ) ;
@@ -449,6 +467,16 @@ export function Panel({
449467 { loading ? < IconPlayerPause /> : < IconPlayerPlay /> }
450468 </ Button >
451469 </ span >
470+ { panel . type ===
471+ 'database' /* TODO: this format should become part of the paneldetails object */ ||
472+ ( panel . type === 'program' &&
473+ ( panel as ProgramPanelInfo ) . program . type === 'sql' ) ? (
474+ < span title = "Format code" >
475+ < Button icon onClick = { formatThis } >
476+ < IconBraces />
477+ </ Button >
478+ </ span >
479+ ) : null }
452480 < span title = "Full screen mode" >
453481 < Button
454482 icon
0 commit comments