Skip to content

Commit

Permalink
fix: error format of TextEditingDeltaInsertion
Browse files Browse the repository at this point in the history
  • Loading branch information
asjqkkkk committed Dec 26, 2024
1 parent c68e5f6 commit bd3f8b8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,15 @@ extension on TextEditingDelta {
}
}

extension on TextEditingDeltaInsertion {
extension TextEditingDeltaInsertionExtension on TextEditingDeltaInsertion {
TextEditingDeltaInsertion format() => TextEditingDeltaInsertion(
oldText: oldText << _len,
textInserted: textInserted,
insertionOffset: insertionOffset - _len,
selection: selection << _len,
composing: composing << _len,
selection:
oldText.startsWith(_whitespace) ? selection << _len : selection,
composing:
oldText.startsWith(_whitespace) ? composing << _len : composing,
);
}

Expand Down
42 changes: 42 additions & 0 deletions test/editor/editor_component/ime/non_delta_input_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,47 @@ void main() {

expect(completer.future, completion(true));
});

test('Delta insertion format', () {
const insertion = TextEditingDeltaInsertion(
oldText: '',
textInserted: 'A',
insertionOffset: 0,
selection: TextSelection.collapsed(offset: 1),
composing: TextRange(start: 0, end: 1),
);
final formatInsertion = insertion.format();
assert(formatInsertion.selection == insertion.selection);
assert(formatInsertion.composing == insertion.composing);

const insertion2 = TextEditingDeltaInsertion(
oldText: ' ',
textInserted: 'A',
insertionOffset: 1,
selection: TextSelection.collapsed(offset: 2),
composing: TextRange.empty,
);

final formatInsertion2 = insertion2.format();
assert(formatInsertion2.insertionOffset == 0);
assert(
formatInsertion2.selection == const TextSelection.collapsed(offset: 1),
);

const insertion3 = TextEditingDeltaInsertion(
oldText: ' A',
textInserted: 'B',
insertionOffset: 2,
selection: TextSelection.collapsed(offset: 3),
composing: TextRange.empty,
);

final formatInsertion3 = insertion3.format();
assert(formatInsertion3.insertionOffset == 1);

assert(
formatInsertion3.selection == const TextSelection.collapsed(offset: 2),
);
});
});
}

0 comments on commit bd3f8b8

Please sign in to comment.