Skip to content

Commit 2ca9d15

Browse files
committed
Autoformat AI generated queries before setting them
1 parent eb5ad2b commit 2ca9d15

File tree

2 files changed

+15
-8
lines changed
  • apps/webapp/app
    • components/code
    • routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query

2 files changed

+15
-8
lines changed

apps/webapp/app/components/code/TSQLEditor.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,7 @@ export function TSQLEditor(opts: TSQLEditorProps) {
178178
if (!currentContent.trim()) return;
179179

180180
try {
181-
const formatted = formatSQL(currentContent, {
182-
language: "sql",
183-
keywordCase: "upper",
184-
indentStyle: "standard",
185-
linesBetweenQueries: 2,
186-
});
181+
const formatted = autoFormatSQL(currentContent);
187182
view.dispatch({
188183
changes: { from: 0, to: view.state.doc.length, insert: formatted },
189184
});
@@ -264,3 +259,12 @@ export function TSQLEditor(opts: TSQLEditorProps) {
264259
</div>
265260
);
266261
}
262+
263+
export function autoFormatSQL(sql: string) {
264+
return formatSQL(sql, {
265+
language: "sql",
266+
keywordCase: "upper",
267+
indentStyle: "standard",
268+
linesBetweenQueries: 2,
269+
});
270+
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query/route.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from "~/components/code/ChartConfigPanel";
2828
import { CodeBlock } from "~/components/code/CodeBlock";
2929
import { QueryResultsChart } from "~/components/code/QueryResultsChart";
30-
import { TSQLEditor } from "~/components/code/TSQLEditor";
30+
import { autoFormatSQL, TSQLEditor } from "~/components/code/TSQLEditor";
3131
import { TSQLResultsTable } from "~/components/code/TSQLResultsTable";
3232
import {
3333
ClientTabs,
@@ -487,7 +487,10 @@ export default function Page() {
487487
editorRef.current?.setQuery(exampleQuery);
488488
editorRef.current?.setScope(exampleScope);
489489
}}
490-
onQueryGenerated={(query) => editorRef.current?.setQuery(query)}
490+
onQueryGenerated={(query) => {
491+
const formatted = autoFormatSQL(query);
492+
editorRef.current?.setQuery(formatted);
493+
}}
491494
getCurrentQuery={() => editorRef.current?.getQuery() ?? ""}
492495
/>
493496
</ResizablePanel>

0 commit comments

Comments
 (0)