Skip to content

Commit c1e7bf8

Browse files
authored
feat: beautify sql code (#68)
1 parent 9873848 commit c1e7bf8

File tree

3 files changed

+162
-4
lines changed

3 files changed

+162
-4
lines changed

package-lock.json

Lines changed: 150 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"react-dom": "^18.2.0",
7373
"react-router-dom": "^6.8.1",
7474
"react-syntax-highlighter": "^15.5.0",
75+
"sql-formatter": "^12.2.4",
7576
"sqlstring": "^2.3.3",
7677
"uuid": "^9.0.0",
7778
"xlsx": "^0.18.5"

src/renderer/screens/DatabaseScreen/QueryWindow.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ReactCodeMirrorRef } from '@uiw/react-codemirror';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
33
import { faPlay } from '@fortawesome/free-solid-svg-icons';
4+
import { format } from 'sql-formatter';
45
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
56
import styles from './styles.module.scss';
67
import { useSchmea } from 'renderer/contexts/SchemaProvider';
@@ -59,7 +60,15 @@ export default function QueryWindow({
5960
const selectTo = viewState?.selection?.main?.to || 0;
6061

6162
return [
62-
{ text: 'Beautify SQL', separator: true },
63+
{
64+
text: 'Beautify SQL',
65+
onClick: () => {
66+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67+
const codeFromRef: string = (viewState?.doc as any).text.join('\r');
68+
setCode(format(codeFromRef));
69+
},
70+
separator: true,
71+
},
6372
{
6473
text: 'Cut',
6574
hotkey: 'Ctrl + X',
@@ -103,7 +112,7 @@ export default function QueryWindow({
103112
},
104113
},
105114
];
106-
}, [editorRef]);
115+
}, [editorRef, setCode]);
107116

108117
const executeSql = useCallback(
109118
(code: string, skipProtection?: boolean) => {

0 commit comments

Comments
 (0)