@@ -15,6 +15,14 @@ import '../embed.dart';
15
15
import '../model.dart' ;
16
16
import 'codemirror.dart' ;
17
17
18
+ // TODO: show documentation on hover
19
+
20
+ // TODO: implement find / find next
21
+
22
+ // TODO: improve the code completion UI
23
+
24
+ // TODO: hover - show links to hosted dartdoc? (flutter, dart api, packages)
25
+
18
26
const String _viewType = 'dartpad-editor' ;
19
27
20
28
bool _viewFactoryInitialized = false ;
@@ -120,6 +128,19 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
120
128
codeMirror? .focus ();
121
129
}
122
130
131
+ @override
132
+ int get cursorOffset {
133
+ final pos = codeMirror? .getCursor ();
134
+ if (pos == null ) return 0 ;
135
+
136
+ return codeMirror? .getDoc ().indexFromPos (pos) ?? 0 ;
137
+ }
138
+
139
+ @override
140
+ void focus () {
141
+ codeMirror? .focus ();
142
+ }
143
+
123
144
@override
124
145
void initState () {
125
146
super .initState ();
@@ -231,8 +252,19 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
231
252
}
232
253
233
254
void _updateCodemirrorFromModel () {
234
- final value = widget.appModel.sourceCodeController.text;
235
- codeMirror? .getDoc ().setValue (value);
255
+ final value = widget.appModel.sourceCodeController.value;
256
+ final cursorOffset = value.selection.baseOffset;
257
+ final cm = codeMirror! ;
258
+ final doc = cm.getDoc ();
259
+
260
+ if (cursorOffset == - 1 ) {
261
+ doc.setValue (value.text);
262
+ } else {
263
+ final scrollInfo = cm.getScrollInfo ();
264
+ doc.setValue (value.text);
265
+ doc.setSelection (doc.posFromIndex (cursorOffset));
266
+ cm.scrollTo (scrollInfo.left, scrollInfo.top);
267
+ }
236
268
}
237
269
238
270
void _updateEditableStatus () {
@@ -324,10 +356,8 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
324
356
// codemirror commands
325
357
326
358
JSAny ? _handleGoLineLeft (CodeMirror editor) {
327
- // Change the cmd-left behavior to move the cursor to the leftmost non-ws
328
- // char.
329
- editor.execCommand ('goLineLeftSmart' );
330
- return JSObject ();
359
+ // Change the cmd-left behavior to move the cursor to leftmost non-ws char.
360
+ return editor.execCommand ('goLineLeftSmart' );
331
361
}
332
362
333
363
void _indentIfMultiLineSelectionElseInsertSoftTab (CodeMirror editor) {
0 commit comments