Skip to content

Commit

Permalink
Fix tests & make selection optional is AutoFormatResult
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Dec 4, 2023
1 parent 60eaf07 commit c2ae543
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
16 changes: 7 additions & 9 deletions packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AutoFormats {

/// Remove auto format from [document] and de-activate current suggestion
/// It will throw if [_activeSuggestion] is null.
TextSelection undoActive(ParchmentDocument document) {
TextSelection? undoActive(ParchmentDocument document) {
final undoSelection = _activeSuggestion!.undoSelection;
document.compose(_activeSuggestion!.undo, ChangeSource.local);
_activeSuggestion = null;
Expand All @@ -88,22 +88,24 @@ class AutoFormats {
/// An auto format result
class AutoFormatResult {
AutoFormatResult({
required this.selection,
this.selection,
required this.change,
required this.undoSelection,
this.undoSelection,
required this.undo,
required this.undoPositionCandidate,
required this.keepTriggerCharacter,
});

/// The selection after applying the auto format
final TextSelection selection;
/// Optional
final TextSelection? selection;

/// The change that was applied
final Delta change;

/// The selection after undoing the formatting
final TextSelection undoSelection;
/// Optional
final TextSelection? undoSelection;

/// The changes to undo the formatting
final Delta undo;
Expand Down Expand Up @@ -162,10 +164,8 @@ class _AutoFormatLinks extends AutoFormat {
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
selection: TextSelection.collapsed(offset: position + 1),
change: change,
undo: undo,
undoSelection: TextSelection.collapsed(offset: position),
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
} on FormatException {

Check warning on line 171 in packages/fleather/lib/src/widgets/autoformats.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/autoformats.dart#L171

Added line #L171 was not covered by tests
Expand Down Expand Up @@ -391,9 +391,7 @@ class _AutoTextDirection extends AutoFormat {
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
selection: TextSelection.collapsed(offset: position + data.length),
change: change,
undoSelection: TextSelection.collapsed(offset: position),
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
Expand Down
4 changes: 3 additions & 1 deletion packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ class FleatherController extends ChangeNotifier {
// Undo if deleting 1 character after retain of autoformat
if (position == _autoFormats.undoPosition) {
final undoSelection = _autoFormats.undoActive(document);

Check warning on line 189 in packages/fleather/lib/src/widgets/controller.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/controller.dart#L188-L189

Added lines #L188 - L189 were not covered by tests
_updateSelectionSilent(undoSelection, source: ChangeSource.local);
if (undoSelection != null) {
_updateSelectionSilent(undoSelection, source: ChangeSource.local);

Check warning on line 191 in packages/fleather/lib/src/widgets/controller.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/controller.dart#L191

Added line #L191 was not covered by tests
}
return keepTriggerCharacter;
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/fleather/test/widgets/autoformats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
{'insert': 'Some long text and a https://fleather-editor.github.io\n'}
]);
final selection = autoformats.run(document, 54, ' ');
expect(selection, const TextSelection.collapsed(offset: 55));
expect(selection, isNull);
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes, isNotNull);
expect(attributes!.containsKey(ParchmentAttribute.link.key), isTrue);
Expand All @@ -28,7 +28,7 @@ void main() {
{'insert': 'Some long text and a www.github.com\n'}
]);
final selection = autoformats.run(document, 35, ' ');
expect(selection, const TextSelection.collapsed(offset: 36));
expect(selection, isNull);
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes, isNotNull);
expect(attributes!.containsKey(ParchmentAttribute.link.key), isTrue);
Expand All @@ -53,7 +53,7 @@ void main() {
]);
autoformats.run(document, 54, ' ');
final undoSelection = autoformats.undoActive(document);
expect(undoSelection, const TextSelection.collapsed(offset: 54));
expect(undoSelection, isNull);
expect(document.toDelta().length, 1);
expect(document.toDelta().first.isInsert, isTrue);
expect(document.toDelta().first.data, text);
Expand Down Expand Up @@ -147,7 +147,7 @@ void main() {
{'insert': 'some ltr text\nש\n'}
]);
final selection = autoformats.run(document, 14, 'ש');
expect(selection, const TextSelection.collapsed(offset: 15));
expect(selection, isNull);
final attributes = document.toDelta().toList()[1].attributes;
expect(attributes, isNotNull);
expect(attributes!.containsKey(ParchmentAttribute.direction.key), isTrue);
Expand Down

0 comments on commit c2ae543

Please sign in to comment.