Skip to content

Commit b3e25cb

Browse files
authored
Add basic format support for sql (#251)
1 parent 192d701 commit b3e25cb

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"react-syntax-highlighter": "^15.5.0",
5454
"shape": "npm:@multiprocess/shape",
5555
"source-map-support": "^0.5.21",
56+
"sql-formatter": "^6.1.1",
5657
"tmp-promise": "^3.0.3",
5758
"tweetnacl": "^1.0.3",
5859
"tweetnacl-util": "^0.15.1",

ui/Panel.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
IconArrowsDiagonalMinimize2,
55
IconArrowsMaximize,
66
IconArrowsMinimize,
7+
IconBraces,
78
IconChevronDown,
89
IconChevronUp,
910
IconEye,
@@ -17,10 +18,11 @@ import formatDistanceToNow from 'date-fns/formatDistanceToNow';
1718
import circularSafeStringify from 'json-stringify-safe';
1819
import * as React from 'react';
1920
import { toString } from 'shape';
21+
import { format as sqlFormat } from 'sql-formatter';
2022
import { MODE_FEATURES } from '../shared/constants';
2123
import { EVAL_ERRORS } from '../shared/errors';
2224
import log from '../shared/log';
23-
import { PanelInfo, PanelResult } from '../shared/state';
25+
import { PanelInfo, PanelResult, ProgramPanelInfo } from '../shared/state';
2426
import { humanSize } from '../shared/text';
2527
import { panelRPC } from './asyncRPC';
2628
import { 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

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6970,6 +6970,13 @@ sprintf-js@~1.0.2:
69706970
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
69716971
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
69726972

6973+
sql-formatter@^6.1.1:
6974+
version "6.1.1"
6975+
resolved "https://registry.yarnpkg.com/sql-formatter/-/sql-formatter-6.1.1.tgz#ec9d4c6fc90c2839885cf36ca7b0755a84527802"
6976+
integrity sha512-NiUP90vNb6NjQryWz0bPsvv4BTqe/wY75mRbh2E6M1kKZIoCKtueeKIDMQBB+RI8pjxVUQy7e35JUXQj6YYonQ==
6977+
dependencies:
6978+
argparse "^2.0.1"
6979+
69736980
ssf@~0.11.2:
69746981
version "0.11.2"
69756982
resolved "https://registry.yarnpkg.com/ssf/-/ssf-0.11.2.tgz#0b99698b237548d088fc43cdf2b70c1a7512c06c"

0 commit comments

Comments
 (0)