|
| 1 | +import React from 'react'; |
| 2 | + |
1 | 3 | import NiceModal from '@ebay/nice-modal-react'; |
| 4 | +import {useMonacoGhost} from '@ydb-platform/monaco-ghost'; |
2 | 5 | import throttle from 'lodash/throttle'; |
3 | 6 | import type Monaco from 'monaco-editor'; |
4 | 7 |
|
5 | 8 | import {MonacoEditor} from '../../../../components/MonacoEditor/MonacoEditor'; |
| 9 | +import {codeAssistApi} from '../../../../store/reducers/codeAssist/codeAssist'; |
6 | 10 | import { |
7 | 11 | goToNextQuery, |
8 | 12 | goToPreviousQuery, |
@@ -68,95 +72,135 @@ export function YqlEditor({ |
68 | 72 | window.ydbEditor = undefined; |
69 | 73 | }; |
70 | 74 |
|
71 | | - const editorDidMount = (editor: Monaco.editor.IStandaloneCodeEditor, monaco: typeof Monaco) => { |
72 | | - window.ydbEditor = editor; |
73 | | - const keybindings = getKeyBindings(monaco); |
74 | | - monaco.editor.registerCommand('insertSnippetToEditor', (_asessor, input: string) => { |
75 | | - //suggestController is not properly typed yet in monaco-editor package |
76 | | - const contribution = editor.getContribution<any>('snippetController2'); |
77 | | - if (contribution) { |
78 | | - editor.focus(); |
79 | | - editor.setValue(''); |
80 | | - contribution.insert(input); |
81 | | - } |
82 | | - }); |
83 | | - initResizeHandler(editor); |
84 | | - initUserPrompt(editor, getLastQueryText); |
85 | | - editor.focus(); |
86 | | - editor.addAction({ |
87 | | - id: 'sendQuery', |
88 | | - label: i18n('action.send-query'), |
89 | | - keybindings: [keybindings.sendQuery], |
90 | | - // A precondition for this action. |
91 | | - precondition: undefined, |
92 | | - // A rule to evaluate on top of the precondition in order to dispatch the keybindings. |
93 | | - keybindingContext: undefined, |
94 | | - contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
95 | | - contextMenuOrder: 1, |
96 | | - // Method that will be executed when the action is triggered. |
97 | | - // @param editor The editor instance is passed in as a convenience |
98 | | - run: () => handleSendQuery(), |
99 | | - }); |
100 | | - |
101 | | - const canSendSelectedText = editor.createContextKey<boolean>('canSendSelectedText', false); |
102 | | - editor.onDidChangeCursorSelection(({selection, secondarySelections}) => { |
103 | | - const notEmpty = |
104 | | - selection.selectionStartLineNumber !== selection.positionLineNumber || |
105 | | - selection.selectionStartColumn !== selection.positionColumn; |
106 | | - const hasMultipleSelections = secondarySelections.length > 0; |
107 | | - canSendSelectedText.set(notEmpty && !hasMultipleSelections); |
108 | | - }); |
109 | | - editor.addAction({ |
110 | | - id: 'sendSelectedQuery', |
111 | | - label: i18n('action.send-selected-query'), |
112 | | - keybindings: [keybindings.sendSelectedQuery], |
113 | | - precondition: 'canSendSelectedText', |
114 | | - contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
115 | | - contextMenuOrder: 1, |
116 | | - run: (e) => { |
117 | | - const selection = e.getSelection(); |
118 | | - const model = e.getModel(); |
119 | | - if (selection && model) { |
120 | | - const text = model.getValueInRange({ |
121 | | - startLineNumber: selection.getSelectionStart().lineNumber, |
122 | | - startColumn: selection.getSelectionStart().column, |
123 | | - endLineNumber: selection.getPosition().lineNumber, |
124 | | - endColumn: selection.getPosition().column, |
125 | | - }); |
126 | | - handleSendExecuteClick(text, true); |
| 75 | + const [sendCodeAssistPrompt] = codeAssistApi.useLazyGetCodeAssistSuggestionsQuery(); |
| 76 | + |
| 77 | + const {registerMonacoGhost, dispose} = useMonacoGhost({ |
| 78 | + api: { |
| 79 | + getCodeAssistSuggestions: async (prompt) => { |
| 80 | + try { |
| 81 | + return await sendCodeAssistPrompt(prompt).unwrap(); |
| 82 | + } catch { |
| 83 | + return {items: []}; |
127 | 84 | } |
128 | 85 | }, |
129 | | - }); |
130 | | - |
131 | | - editor.addAction({ |
132 | | - id: 'previous-query', |
133 | | - label: i18n('action.previous-query'), |
134 | | - keybindings: [keybindings.selectPreviousQuery], |
135 | | - contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
136 | | - contextMenuOrder: 2, |
137 | | - run: () => { |
138 | | - dispatch(goToPreviousQuery()); |
139 | | - }, |
140 | | - }); |
141 | | - editor.addAction({ |
142 | | - id: 'next-query', |
143 | | - label: i18n('action.next-query'), |
144 | | - keybindings: [keybindings.selectNextQuery], |
145 | | - contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
146 | | - contextMenuOrder: 3, |
147 | | - run: () => { |
148 | | - dispatch(goToNextQuery()); |
149 | | - }, |
150 | | - }); |
151 | | - editor.addAction({ |
152 | | - id: 'save-query', |
153 | | - label: i18n('action.save-query'), |
154 | | - keybindings: [keybindings.saveQuery], |
155 | | - run: () => { |
156 | | - NiceModal.show(SAVE_QUERY_DIALOG); |
157 | | - }, |
158 | | - }); |
159 | | - }; |
| 86 | + }, |
| 87 | + config: { |
| 88 | + language: YQL_LANGUAGE_ID, |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + const editorDidMount = React.useCallback( |
| 93 | + (editor: Monaco.editor.IStandaloneCodeEditor, monaco: typeof Monaco) => { |
| 94 | + window.ydbEditor = editor; |
| 95 | + const keybindings = getKeyBindings(monaco); |
| 96 | + monaco.editor.registerCommand('insertSnippetToEditor', (_asessor, input: string) => { |
| 97 | + //suggestController is not properly typed yet in monaco-editor package |
| 98 | + const contribution = editor.getContribution<any>('snippetController2'); |
| 99 | + if (contribution) { |
| 100 | + editor.focus(); |
| 101 | + editor.setValue(''); |
| 102 | + contribution.insert(input); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + if (window.api.codeAssist) { |
| 107 | + registerMonacoGhost(editor); |
| 108 | + } |
| 109 | + initResizeHandler(editor); |
| 110 | + initUserPrompt(editor, getLastQueryText); |
| 111 | + editor.focus(); |
| 112 | + editor.addAction({ |
| 113 | + id: 'sendQuery', |
| 114 | + label: i18n('action.send-query'), |
| 115 | + keybindings: [keybindings.sendQuery], |
| 116 | + // A precondition for this action. |
| 117 | + precondition: undefined, |
| 118 | + // A rule to evaluate on top of the precondition in order to dispatch the keybindings. |
| 119 | + keybindingContext: undefined, |
| 120 | + contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
| 121 | + contextMenuOrder: 1, |
| 122 | + // Method that will be executed when the action is triggered. |
| 123 | + // @param editor The editor instance is passed in as a convenience |
| 124 | + run: () => handleSendQuery(), |
| 125 | + }); |
| 126 | + |
| 127 | + const canSendSelectedText = editor.createContextKey<boolean>( |
| 128 | + 'canSendSelectedText', |
| 129 | + false, |
| 130 | + ); |
| 131 | + editor.onDidChangeCursorSelection(({selection, secondarySelections}) => { |
| 132 | + const notEmpty = |
| 133 | + selection.selectionStartLineNumber !== selection.positionLineNumber || |
| 134 | + selection.selectionStartColumn !== selection.positionColumn; |
| 135 | + const hasMultipleSelections = secondarySelections.length > 0; |
| 136 | + canSendSelectedText.set(notEmpty && !hasMultipleSelections); |
| 137 | + }); |
| 138 | + editor.addAction({ |
| 139 | + id: 'sendSelectedQuery', |
| 140 | + label: i18n('action.send-selected-query'), |
| 141 | + keybindings: [keybindings.sendSelectedQuery], |
| 142 | + precondition: 'canSendSelectedText', |
| 143 | + contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
| 144 | + contextMenuOrder: 1, |
| 145 | + run: (e) => { |
| 146 | + const selection = e.getSelection(); |
| 147 | + const model = e.getModel(); |
| 148 | + if (selection && model) { |
| 149 | + const text = model.getValueInRange({ |
| 150 | + startLineNumber: selection.getSelectionStart().lineNumber, |
| 151 | + startColumn: selection.getSelectionStart().column, |
| 152 | + endLineNumber: selection.getPosition().lineNumber, |
| 153 | + endColumn: selection.getPosition().column, |
| 154 | + }); |
| 155 | + handleSendExecuteClick(text, true); |
| 156 | + } |
| 157 | + }, |
| 158 | + }); |
| 159 | + |
| 160 | + editor.addAction({ |
| 161 | + id: 'previous-query', |
| 162 | + label: i18n('action.previous-query'), |
| 163 | + keybindings: [keybindings.selectPreviousQuery], |
| 164 | + contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
| 165 | + contextMenuOrder: 2, |
| 166 | + run: () => { |
| 167 | + dispatch(goToPreviousQuery()); |
| 168 | + }, |
| 169 | + }); |
| 170 | + editor.addAction({ |
| 171 | + id: 'next-query', |
| 172 | + label: i18n('action.next-query'), |
| 173 | + keybindings: [keybindings.selectNextQuery], |
| 174 | + contextMenuGroupId: CONTEXT_MENU_GROUP_ID, |
| 175 | + contextMenuOrder: 3, |
| 176 | + run: () => { |
| 177 | + dispatch(goToNextQuery()); |
| 178 | + }, |
| 179 | + }); |
| 180 | + editor.addAction({ |
| 181 | + id: 'save-query', |
| 182 | + label: i18n('action.save-query'), |
| 183 | + keybindings: [keybindings.saveQuery], |
| 184 | + run: () => { |
| 185 | + NiceModal.show(SAVE_QUERY_DIALOG); |
| 186 | + }, |
| 187 | + }); |
| 188 | + |
| 189 | + return () => { |
| 190 | + if (window.api.codeAssist) { |
| 191 | + dispose(); |
| 192 | + } |
| 193 | + }; |
| 194 | + }, |
| 195 | + [ |
| 196 | + dispatch, |
| 197 | + dispose, |
| 198 | + getLastQueryText, |
| 199 | + handleSendExecuteClick, |
| 200 | + handleSendQuery, |
| 201 | + registerMonacoGhost, |
| 202 | + ], |
| 203 | + ); |
160 | 204 |
|
161 | 205 | const onChange = (newValue: string) => { |
162 | 206 | updateErrorsHighlighting(); |
|
0 commit comments