Skip to content
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

ui: Modify user settings for editor to enable query suggestions #1485

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions querybook/config/user_setting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ show_full_view:
- disabled
helper: Instead of modal, show full view when opening table/execution/snippet

query_suggestions:
default: disabled
tab: editor
options:
- enabled
- disabled
helper: (Experimental) Enable to receive inline AI generated query suggestions as you type from within the editor.

editor_font_size:
default: medium
tab: editor
Expand Down
11 changes: 9 additions & 2 deletions querybook/webapp/components/QueryEditor/BoundQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ export const BoundQueryEditor = React.forwardRef<
const editorRef = useForwardedRef<IQueryEditorHandles>(ref);

// Code Editor related Props
const { codeEditorTheme, keyMap, options, fontSize, autoCompleteType } =
useUserQueryEditorConfig(searchContext);
const {
codeEditorTheme,
keyMap,
options,
fontSize,
autoCompleteType,
queryAISuggestionsEnabled,
} = useUserQueryEditorConfig(searchContext);
const combinedOptions = useMemo(
() => ({
...options,
Expand Down Expand Up @@ -103,6 +109,7 @@ export const BoundQueryEditor = React.forwardRef<
theme={codeEditorTheme}
autoCompleteType={autoCompleteType}
fontSize={fontSize}
queryAISuggestionsEnabled={queryAISuggestionsEnabled}
getTableByName={fetchDataTable}
functionDocumentationByNameByLanguage={
functionDocumentationByNameByLanguage
Expand Down
28 changes: 19 additions & 9 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface IQueryEditorProps extends IStyledQueryEditorProps {
keyMap?: CodeMirrorKeyMap;
className?: string;
autoCompleteType?: AutoCompleteType;
queryAISuggestionsEnabled?: boolean;

/**
* If provided, then the container component will handle the fullscreen logic
Expand Down Expand Up @@ -115,6 +116,7 @@ export const QueryEditor: React.FC<
keyMap = {},
className,
autoCompleteType = 'all',
queryAISuggestionsEnabled,
onFullScreen,

onChange,
Expand Down Expand Up @@ -617,16 +619,24 @@ export const QueryEditor: React.FC<
onTextHover,
]);

const editorDidMount = useCallback((editor: CodeMirror.Editor) => {
editorRef.current = editor;
const editorDidMount = useCallback(
(editor: CodeMirror.Editor) => {
editorRef.current = editor;

// There is a strange bug where codemirror would start with the wrong height (on Execs tab)
// which can only be solved by clicking on it
// The current work around is to add refresh on mount
setTimeout(() => {
editor.refresh();
}, 50);
}, []);
if (queryAISuggestionsEnabled) {
// Enable query ai suggestion feature
editor.queryAISuggestions();
}

// There is a strange bug where codemirror would start with the wrong height (on Execs tab)
// which can only be solved by clicking on it
// The current work around is to add refresh on mount
setTimeout(() => {
editor.refresh();
}, 50);
},
[queryAISuggestionsEnabled]
);

const onBeforeChange = useCallback(
(editor: CodeMirror.Editor, data, value: string) => {
Expand Down
4 changes: 4 additions & 0 deletions querybook/webapp/hooks/redux/useUserQueryEditorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function useUserQueryEditorConfig(
keyMap: CodeMirrorKeyMap;
options: CodeMirror.EditorConfiguration;
autoCompleteType: AutoCompleteType;
queryAISuggestionsEnabled: boolean;
} {
const editorSettings = useShallowSelector((state: IStoreState) => ({
theme: getCodeEditorTheme(state.user.computedSettings['theme']),
Expand All @@ -26,6 +27,8 @@ export function useUserQueryEditorConfig(
],
autoComplete: state.user.computedSettings['auto_complete'],
tab: state.user.computedSettings['tab'],
queryAISuggestionsEnabled:
state.user.computedSettings['query_suggestions'] === 'enabled',
}));
const indentWithTabs = editorSettings.tab === 'tab';
const tabSize =
Expand Down Expand Up @@ -106,5 +109,6 @@ export function useUserQueryEditorConfig(
// From: https://github.com/codemirror/CodeMirror/issues/988
keyMap,
options,
queryAISuggestionsEnabled: editorSettings.queryAISuggestionsEnabled,
};
}
9 changes: 9 additions & 0 deletions querybook/webapp/lib/codemirror/codemirror-copilot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import CodeMirror from 'codemirror';

CodeMirror.defineExtension('queryAISuggestions', function () {
this.on('keyup', async (editor, event) => {
if (event.code === 'Space') {
console.log('CodeMirror Query Suggestions Extension');
}
});
});
8 changes: 6 additions & 2 deletions querybook/webapp/lib/codemirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ import 'codemirror/addon/runmode/runmode';
// Search highlighting
import 'codemirror/addon/search/match-highlighter.js';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/jinja2/jinja2';
// From codemirror non-react package:
import 'codemirror/mode/sql/sql';
import 'codemirror/mode/jinja2/jinja2';

import 'codemirror/theme/duotone-light.css';
import 'codemirror/theme/material-palenight.css';
import 'codemirror/theme/monokai.css';
import 'codemirror/theme/solarized.css';
// Query AI Suggestions
import 'lib/codemirror/codemirror-copilot';
// This should apply the hover option to codemirror
import 'lib/codemirror/codemirror-hover';

// Local styling
import './editor_styles.scss';

declare module 'codemirror' {
interface Editor {
queryAISuggestions(): void;
}
// This is copied from runmode.d.ts. Not sure how to import it :(
function runMode(
text: string,
Expand Down
Loading