Skip to content

Commit 37b1e2b

Browse files
yescorpYestay Tastanov
andauthored
cursor fix, test adjustments (akvelon#185)
Co-authored-by: Yestay Tastanov <yestay@Yestays-MacBook-Pro.local>
1 parent e2b10a5 commit 37b1e2b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/src/code_field/code_controller.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,21 @@ class CodeController extends TextEditingController {
362362
return;
363363
}
364364

365+
final selectionSnapshot =
366+
code.hiddenRanges.recoverSelection(newValue.selection);
365367
_updateCodeIfChanged(editResult.fullTextAfter);
366368

367369
if (newValue.text != _code.visibleText) {
368-
// Manually typed in a text that has become a hidden range.
369-
newValue = newValue.replacedText(_code.visibleText);
370+
if (newValue.text.length > _code.visibleText.length) {
371+
// Manually typed in a text that has become a hidden range.
372+
newValue = newValue.replacedText(_code.visibleText);
373+
} else {
374+
// Some folded block is unfolded.
375+
newValue = TextEditingValue(
376+
text: _code.visibleText,
377+
selection: _code.hiddenRanges.cutSelection(selectionSnapshot),
378+
);
379+
}
370380
}
371381

372382
// Uncomment this to see the hidden text in the console

test/src/code_field/code_controller_folding_editing_test.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ int n;
215215

216216
await wt.cursorEnd();
217217
controller.value = controller.value.replacedSelection(inserted);
218-
// TODO(yescorp): Fix selection, then fix tests
219-
// https://github.com/akvelon/flutter-code-editor/issues/182
220218
expect(controller.value.text, example + inserted);
219+
expect(
220+
controller.value.selection,
221+
TextSelection.collapsed(offset: controller.value.text.length),
222+
);
221223
expect(controller.code.foldedBlocks.length, 0);
222224
});
223225

@@ -251,9 +253,11 @@ int n;
251253

252254
await wt.cursorEnd();
253255
controller.value = controller.value.replacedSelection(inserted);
254-
// TODO(yescorp): Fix selection, then fix tests
255-
// https://github.com/akvelon/flutter-code-editor/issues/182
256256
expect(controller.value.text, example + inserted);
257+
expect(
258+
controller.value.selection,
259+
TextSelection.collapsed(offset: controller.value.text.length),
260+
);
257261
expect(controller.code.foldedBlocks.length, 0);
258262
});
259263

@@ -296,8 +300,12 @@ int n;
296300
controller.value = controller.value.replacedSelection(' bbbb');
297301

298302
const expectedText = 'a:\n aaaa\n bbbb';
299-
303+
// \ selection
300304
expect(controller.value.text, expectedText);
305+
expect(
306+
controller.value.selection,
307+
TextSelection.collapsed(offset: controller.value.text.length),
308+
);
301309
expect(controller.code.foldedBlocks.length, 0);
302310
});
303311

0 commit comments

Comments
 (0)