@@ -25,6 +25,7 @@ import { queryEditorSetSelectedText } from 'src/SqlLab/actions/sqlLab';
25
25
import { FullSQLEditor as AceEditor } from 'src/components/AsyncAceEditor' ;
26
26
import type { KeyboardShortcut } from 'src/SqlLab/components/KeyboardShortcutButton' ;
27
27
import useQueryEditor from 'src/SqlLab/hooks/useQueryEditor' ;
28
+ import type { CursorPosition } from 'src/SqlLab/types' ;
28
29
import { useAnnotations } from './useAnnotations' ;
29
30
import { useKeywords } from './useKeywords' ;
30
31
@@ -40,6 +41,7 @@ type AceEditorWrapperProps = {
40
41
onBlur : ( sql : string ) => void ;
41
42
onChange : ( sql : string ) => void ;
42
43
queryEditorId : string ;
44
+ onCursorPositionChange : ( position : CursorPosition ) => void ;
43
45
height : string ;
44
46
hotkeys : HotKey [ ] ;
45
47
} ;
@@ -69,6 +71,7 @@ const AceEditorWrapper = ({
69
71
onBlur = ( ) => { } ,
70
72
onChange = ( ) => { } ,
71
73
queryEditorId,
74
+ onCursorPositionChange,
72
75
height,
73
76
hotkeys,
74
77
} : AceEditorWrapperProps ) => {
@@ -79,10 +82,11 @@ const AceEditorWrapper = ({
79
82
'sql' ,
80
83
'schema' ,
81
84
'templateParams' ,
85
+ 'cursorPosition' ,
82
86
] ) ;
83
87
84
88
const currentSql = queryEditor . sql ?? '' ;
85
-
89
+ const cursorPosition = queryEditor . cursorPosition ?? { row : 0 , column : 0 } ;
86
90
const [ sql , setSql ] = useState ( currentSql ) ;
87
91
88
92
// The editor changeSelection is called multiple times in a row,
@@ -143,6 +147,15 @@ const AceEditorWrapper = ({
143
147
144
148
currentSelectionCache . current = selectedText ;
145
149
} ) ;
150
+ editor . selection . on ( 'changeCursor' , ( ) => {
151
+ const cursor = editor . getCursorPosition ( ) ;
152
+ onCursorPositionChange ( cursor ) ;
153
+ } ) ;
154
+
155
+ const { row, column } = cursorPosition ;
156
+ editor . moveCursorToPosition ( { row, column } ) ;
157
+ editor . focus ( ) ;
158
+ editor . scrollToLine ( row , true , true ) ;
146
159
} ;
147
160
148
161
const onChangeText = ( text : string ) => {
0 commit comments