-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add params and types autocompletion to SQL editor
- Loading branch information
1 parent
f3744a1
commit 57cebe0
Showing
13 changed files
with
321 additions
and
29 deletions.
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
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,11 @@ | ||
import { PropsWithChildren } from "react"; | ||
import { useMonacoConfigSupplier } from "../hooks/useMonacoConfigSupplier"; | ||
|
||
type Props = PropsWithChildren<{ | ||
jsonParams?: string; | ||
}>; | ||
|
||
export default function MonacoWrapper({ children, jsonParams = "{}" }: Props) { | ||
useMonacoConfigSupplier({ jsonParams }); | ||
return <>{children}</>; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { IDisposable } from "monaco-editor/esm/vs/editor/editor.api"; | ||
import { useEffect, useState } from "react"; | ||
import { getParemeterNameSuggetionProvider } from "../lib/editor-suggestions/parameter-name.suggestion"; | ||
import { paremeterSnippetSuggetionProvider } from "../lib/editor-suggestions/parameter-snippet.suggestion"; | ||
import { paremeterTypeSuggetionProvider } from "../lib/editor-suggestions/parameter-type.suggestion"; | ||
|
||
type Params = { | ||
jsonParams: string; | ||
}; | ||
|
||
export const useMonacoConfigSupplier = ({ jsonParams }: Params) => { | ||
const monaco = useMonaco(); | ||
const [paramKeys, setParamKeys] = useState<string[]>([]); | ||
const [areSameParamKeys, setAreSameParamKeys] = useState(false); | ||
|
||
useEffect(() => { | ||
let subs: IDisposable[] = []; | ||
if (monaco) { | ||
subs.push( | ||
monaco.languages.registerCompletionItemProvider( | ||
paremeterSnippetSuggetionProvider.language, | ||
paremeterSnippetSuggetionProvider.provider | ||
) | ||
); | ||
subs.push( | ||
monaco.languages.registerCompletionItemProvider( | ||
paremeterTypeSuggetionProvider.language, | ||
paremeterTypeSuggetionProvider.provider | ||
) | ||
); | ||
} | ||
return () => { | ||
subs.forEach((sub) => sub.dispose()); | ||
}; | ||
}, [monaco]); | ||
|
||
useEffect(() => { | ||
try { | ||
const parsedParams = Object.keys(JSON.parse(jsonParams)); | ||
setParamKeys((prev) => { | ||
if (arraysAreEqual(prev, parsedParams)) { | ||
setAreSameParamKeys(true); | ||
return prev; | ||
} | ||
setAreSameParamKeys(false); | ||
return parsedParams; | ||
}); | ||
} catch (e) {} | ||
}, [jsonParams]); | ||
|
||
useEffect(() => { | ||
let subs: IDisposable[] = []; | ||
if (monaco) { | ||
const { provider, language } = | ||
getParemeterNameSuggetionProvider(paramKeys); | ||
subs.push( | ||
monaco.languages.registerCompletionItemProvider( | ||
language, | ||
areSameParamKeys | ||
? { | ||
triggerCharacters: provider.triggerCharacters, | ||
provideCompletionItems: function () { | ||
return { suggestions: [] }; | ||
}, | ||
} | ||
: provider | ||
) | ||
); | ||
} | ||
return () => { | ||
console.log("disposing"); | ||
setAreSameParamKeys(false); | ||
subs.forEach((sub) => sub.dispose()); | ||
}; | ||
}, [monaco, paramKeys, areSameParamKeys]); | ||
}; | ||
|
||
const arraysAreEqual = (array1: string[], array2: string[]): boolean => | ||
array1.length === array2.length && | ||
array1.every((element, index) => element === array2[index]); |
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,46 @@ | ||
import { languages } from "monaco-editor/esm/vs/editor/editor.api"; | ||
import { SuggestionProvider } from "./types"; | ||
|
||
export const getParemeterNameSuggetionProvider = ( | ||
paramKeys: string[] = [] | ||
): SuggestionProvider => { | ||
return { | ||
language: "sql", | ||
provider: { | ||
triggerCharacters: ["{"], | ||
provideCompletionItems(model, position) { | ||
const word = model.getWordUntilPosition(position); | ||
const textUntilPosition = model.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}); | ||
|
||
const lastWord = textUntilPosition.split(" ").pop(); | ||
|
||
const match = lastWord?.match(/\{/g); | ||
if (!match) { | ||
return { | ||
suggestions: [], | ||
}; | ||
} | ||
const range = { | ||
startLineNumber: position.lineNumber, | ||
endLineNumber: position.lineNumber, | ||
startColumn: word.startColumn, | ||
endColumn: word.endColumn, | ||
}; | ||
|
||
return { | ||
suggestions: paramKeys.map((key) => ({ | ||
label: key, | ||
kind: languages.CompletionItemKind.Keyword, | ||
insertText: key, | ||
range, | ||
})), | ||
}; | ||
}, | ||
}, | ||
}; | ||
}; |
30 changes: 30 additions & 0 deletions
30
src/lib/editor-suggestions/parameter-snippet.suggestion.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,30 @@ | ||
import { languages } from "monaco-editor/esm/vs/editor/editor.api"; | ||
import { SuggestionProvider } from "./types"; | ||
|
||
export const paremeterSnippetSuggetionProvider: SuggestionProvider = { | ||
language: "sql", | ||
provider: { | ||
provideCompletionItems(model, position, context, token) { | ||
const word = model.getWordUntilPosition(position); | ||
|
||
const range = { | ||
startLineNumber: position.lineNumber, | ||
endLineNumber: position.lineNumber, | ||
startColumn: word.startColumn, | ||
endColumn: word.endColumn, | ||
}; | ||
return { | ||
suggestions: [ | ||
{ | ||
label: "parameter", | ||
kind: languages.CompletionItemKind.Snippet, | ||
insertText: "{${1:parameterName}:${2:parameterType}}", | ||
insertTextRules: | ||
languages.CompletionItemInsertTextRule.InsertAsSnippet, | ||
range, | ||
}, | ||
], | ||
}; | ||
}, | ||
}, | ||
}; |
101 changes: 101 additions & 0 deletions
101
src/lib/editor-suggestions/parameter-type.suggestion.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,101 @@ | ||
import { languages } from "monaco-editor/esm/vs/editor/editor.api"; | ||
import { SuggestionProvider } from "./types"; | ||
|
||
export const paremeterTypeSuggetionProvider: SuggestionProvider = { | ||
language: "sql", | ||
provider: { | ||
triggerCharacters: [":"], | ||
provideCompletionItems(model, position) { | ||
const word = model.getWordUntilPosition(position); | ||
const textUntilPosition = model.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}); | ||
const lastWord = textUntilPosition.split(" ").pop(); | ||
|
||
const match = lastWord?.match(/\{[a-zA-Z0-9]+:/g); | ||
if (!match) { | ||
return { | ||
suggestions: [], | ||
}; | ||
} | ||
|
||
const range = { | ||
startLineNumber: position.lineNumber, | ||
endLineNumber: position.lineNumber, | ||
startColumn: word.startColumn, | ||
endColumn: word.endColumn, | ||
}; | ||
return { | ||
suggestions: types.map((type) => ({ | ||
label: type, | ||
kind: languages.CompletionItemKind.Keyword, | ||
insertText: type, | ||
range, | ||
})), | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
const types = [ | ||
"AggregateFunction", | ||
"Array", | ||
"Bool", | ||
"Date", | ||
"Date32", | ||
"DateTime", | ||
"DateTime32", | ||
"DateTime64", | ||
"Decimal", | ||
"Decimal128", | ||
"Decimal256", | ||
"Decimal32", | ||
"Decimal64", | ||
"Enum", | ||
"Enum16", | ||
"Enum8", | ||
"FixedString", | ||
"Float32", | ||
"Float64", | ||
"IPv4", | ||
"IPv6", | ||
"Int128", | ||
"Int16", | ||
"Int256", | ||
"Int32", | ||
"Int64", | ||
"Int8", | ||
"IntervalDay", | ||
"IntervalHour", | ||
"IntervalMicrosecond", | ||
"IntervalMillisecond", | ||
"IntervalMinute", | ||
"IntervalMonth", | ||
"IntervalNanosecond", | ||
"IntervalQuarter", | ||
"IntervalSecond", | ||
"IntervalWeek", | ||
"IntervalYear", | ||
"LowCardinality", | ||
"Map", | ||
"MultiPolygon", | ||
"Nested", | ||
"Nothing", | ||
"Nullable", | ||
"Object", | ||
"Point", | ||
"Polygon", | ||
"Ring", | ||
"SimpleAggregateFunction", | ||
"String", | ||
"Tuple", | ||
"UInt128", | ||
"UInt16", | ||
"UInt256", | ||
"UInt32", | ||
"UInt64", | ||
"UInt8", | ||
]; |
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,6 @@ | ||
import { languages } from "monaco-editor/esm/vs/editor/editor.api"; | ||
|
||
export type SuggestionProvider = { | ||
language: languages.LanguageSelector; | ||
provider: languages.CompletionItemProvider; | ||
}; |
Oops, something went wrong.