-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(explorer): sql editor #3276
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9be9494
wip
karooolis d3efb28
wip
karooolis 52a3634
sql editor wip
karooolis 6f280c9
sql editor wip
karooolis aa7db1c
Merge branch 'main' into kumpis/sql-editor
karooolis 9cd5a80
highlight column error
karooolis 4029421
Merge branch 'main' into kumpis/sql-editor
karooolis df27dc9
test withotu sqleditor
karooolis 08ea157
Merge branch 'main' into kumpis/sql-editor
karooolis 9c0b50e
experiment with codemirror, wip
karooolis d052342
one-line monaco-editor
karooolis 42c8060
add intellisense
karooolis 5b8b15a
sql query parser wip
karooolis 0bc253d
Merge branch 'main' into kumpis/sql-editor
karooolis 11915d3
parse invalid input, show error
karooolis 6aa764e
detect invalid columns
karooolis 3824ef2
verify valid columns
karooolis 90d9982
change SQL dialect to PostgresSQL
karooolis 2fdf715
set initial query
karooolis fc776ed
validate tableName
karooolis 91ae509
refactor into helper hooks
karooolis 9269312
handle focus / blur
karooolis fd394c0
fix validate query
karooolis d3d0bf0
filter empty suggestions
karooolis 50e7235
remove unnecessary empty string
karooolis 551273b
Create itchy-countries-worry.md
karooolis f3b37b2
remove unused deps
karooolis eb36c75
Merge branch 'kumpis/sql-editor' of github.com:latticexyz/mud into ku…
karooolis d6e8916
remove codemirror style
karooolis b7ac048
Update .changeset/itchy-countries-worry.md
karooolis d37cec4
Update packages/explorer/src/app/(explorer)/[chainName]/worlds/[world…
karooolis b830a34
Merge branch 'main' into kumpis/sql-editor
karooolis 5b9ef3d
remove height from form
karooolis b357b50
set max height form
karooolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/explorer": patch | ||
--- | ||
|
||
Explore page now has a full-featured SQL editor with syntax highlighting, autocomplete, and query validation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 63 additions & 25 deletions
88
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/consts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { editor } from "monaco-editor/esm/vs/editor/editor.api"; | ||
|
||
export const monacoOptions: editor.IStandaloneEditorConstructionOptions = { | ||
fontSize: 14, | ||
fontWeight: "normal", | ||
wordWrap: "off", | ||
lineNumbers: "off", | ||
lineNumbersMinChars: 0, | ||
overviewRulerLanes: 0, | ||
overviewRulerBorder: false, | ||
hideCursorInOverviewRuler: true, | ||
lineDecorationsWidth: 0, | ||
glyphMargin: false, | ||
folding: false, | ||
scrollBeyondLastColumn: 0, | ||
scrollbar: { | ||
horizontal: "hidden", | ||
vertical: "hidden", | ||
alwaysConsumeMouseWheel: false, | ||
handleMouseWheel: false, | ||
}, | ||
find: { | ||
addExtraSpaceOnTop: false, | ||
autoFindInSelection: "never", | ||
seedSearchStringFromSelection: "never", | ||
}, | ||
minimap: { enabled: false }, | ||
wordBasedSuggestions: "off", | ||
links: false, | ||
occurrencesHighlight: "off", | ||
cursorStyle: "line-thin", | ||
renderLineHighlight: "none", | ||
contextmenu: false, | ||
roundedSelection: false, | ||
hover: { | ||
delay: 100, | ||
}, | ||
acceptSuggestionOnEnter: "on", | ||
automaticLayout: true, | ||
fixedOverflowWidgets: true, | ||
}; |
23 changes: 23 additions & 0 deletions
23
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useMonacoErrorMarker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useCallback } from "react"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
|
||
export function useMonacoErrorMarker() { | ||
const monaco = useMonaco(); | ||
return useCallback( | ||
({ message, startColumn, endColumn }: { message: string; startColumn: number; endColumn: number }) => { | ||
if (monaco) { | ||
monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", [ | ||
{ | ||
severity: monaco.MarkerSeverity.Error, | ||
message, | ||
startLineNumber: 1, | ||
startColumn, | ||
endLineNumber: 1, | ||
endColumn, | ||
}, | ||
]); | ||
} | ||
}, | ||
[monaco], | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useMonacoSuggestions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { useQueryAutocomplete } from "./useQueryAutocomplete"; | ||
|
||
const monacoSuggestionsMap = { | ||
KEYWORD: "Keyword", | ||
TABLE: "Field", | ||
COLUMN: "Field", | ||
} as const; | ||
|
||
export function useMonacoSuggestions(table?: Table) { | ||
const monaco = useMonaco(); | ||
const queryAutocomplete = useQueryAutocomplete(table); | ||
|
||
useEffect(() => { | ||
if (!monaco) return; | ||
|
||
const provider = monaco.languages.registerCompletionItemProvider("sql", { | ||
triggerCharacters: [" ", ".", ","], | ||
|
||
provideCompletionItems: (model, position) => { | ||
if (!queryAutocomplete) { | ||
return { suggestions: [] }; | ||
} | ||
|
||
const textUntilPosition = model.getValueInRange({ | ||
startLineNumber: 1, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}); | ||
|
||
const word = model.getWordUntilPosition(position); | ||
const range = { | ||
startLineNumber: position.lineNumber, | ||
startColumn: word.startColumn, | ||
endLineNumber: position.lineNumber, | ||
endColumn: word.endColumn, | ||
}; | ||
|
||
const suggestions = queryAutocomplete | ||
.autocomplete(textUntilPosition) | ||
.map(({ value, optionType }) => ({ | ||
label: value, | ||
kind: monaco.languages.CompletionItemKind[monacoSuggestionsMap[optionType]], | ||
insertText: value, | ||
range, | ||
// move keyword optionType to the top of suggestions list | ||
sortText: optionType !== "KEYWORD" ? "0" : "1", | ||
})) | ||
.filter(({ label }) => !!label); | ||
|
||
return { suggestions }; | ||
}, | ||
}); | ||
|
||
return () => provider.dispose(); | ||
}, [monaco, queryAutocomplete]); | ||
} |
21 changes: 21 additions & 0 deletions
21
...orer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useQueryAutocomplete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useParams } from "next/navigation"; | ||
import { SQLAutocomplete, SQLDialect } from "sql-autocomplete"; | ||
import { Address } from "viem"; | ||
import { useMemo } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useChain } from "../../../../hooks/useChain"; | ||
import { constructTableName } from "../../../../utils/constructTableName"; | ||
|
||
export function useQueryAutocomplete(table?: Table) { | ||
const { id: chainId } = useChain(); | ||
const { worldAddress } = useParams<{ worldAddress: Address }>(); | ||
|
||
return useMemo(() => { | ||
if (!table || !worldAddress || !chainId) return null; | ||
|
||
const tableName = constructTableName(table, worldAddress as Address, chainId); | ||
const columnNames = Object.keys(table.schema); | ||
|
||
return new SQLAutocomplete(SQLDialect.PLpgSQL, [tableName], columnNames); | ||
}, [table, worldAddress, chainId]); | ||
} |
72 changes: 72 additions & 0 deletions
72
...xplorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/useQueryValidator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useParams } from "next/navigation"; | ||
import { Parser } from "node-sql-parser"; | ||
import { Address } from "viem"; | ||
import { useCallback } from "react"; | ||
import { Table } from "@latticexyz/config"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { useChain } from "../../../../hooks/useChain"; | ||
import { constructTableName } from "../../../../utils/constructTableName"; | ||
import { useMonacoErrorMarker } from "./useMonacoErrorMarker"; | ||
|
||
const sqlParser = new Parser(); | ||
|
||
export function useQueryValidator(table?: Table) { | ||
const monaco = useMonaco(); | ||
const { worldAddress } = useParams(); | ||
const { id: chainId } = useChain(); | ||
const setErrorMarker = useMonacoErrorMarker(); | ||
|
||
return useCallback( | ||
(value: string) => { | ||
if (!monaco || !table) return true; | ||
|
||
try { | ||
const ast = sqlParser.astify(value); | ||
if ("columns" in ast && Array.isArray(ast.columns)) { | ||
for (const column of ast.columns) { | ||
const columnName = column.expr.column; | ||
if (!Object.keys(table.schema).includes(columnName)) { | ||
setErrorMarker({ | ||
message: `Column '${columnName}' does not exist in the table schema.`, | ||
startColumn: value.indexOf(columnName) + 1, | ||
endColumn: value.indexOf(columnName) + columnName.length + 1, | ||
}); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if ("from" in ast && Array.isArray(ast.from)) { | ||
for (const tableInfo of ast.from) { | ||
if ("table" in tableInfo) { | ||
const selectedTableName = tableInfo.table; | ||
const tableName = constructTableName(table, worldAddress as Address, chainId); | ||
|
||
if (selectedTableName !== tableName) { | ||
setErrorMarker({ | ||
message: `Only '${tableName}' is available for this query.`, | ||
startColumn: value.indexOf(selectedTableName) + 1, | ||
endColumn: value.indexOf(selectedTableName) + selectedTableName.length + 1, | ||
}); | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
monaco.editor.setModelMarkers(monaco.editor.getModels()[0], "sql", []); | ||
return true; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setErrorMarker({ | ||
message: error.message, | ||
startColumn: 1, | ||
endColumn: value.length + 1, | ||
}); | ||
} | ||
return false; | ||
} | ||
}, | ||
[monaco, table, setErrorMarker, worldAddress, chainId], | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this just aid in autocomplete/validation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, only need columns and table name from it atm. I suppose it would be somewhat cleaner to just pass these as props explicitly? I remember receiving a similar suggestion in the past.