Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error format of TextEditingDeltaInsertion #1004

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,18 @@ extension on TextEditingDelta {
}
}

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

extension on TextEditingDeltaDeletion {
Expand Down
96 changes: 96 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 @@ -6,6 +6,8 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('NonDeltaTextInputService', () {
test('actions', () {
bool onInsert = false,
Expand Down Expand Up @@ -103,5 +105,99 @@ 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),
);
});
});

testWidgets('Delta insertion format with deletion', (tester) async {
TextEditingValue value =
const TextEditingValue(selection: TextSelection.collapsed(offset: 0));
const space = ' ';
final inputService = NonDeltaTextInputService(
onInsert: (v) async => value = v.apply(value),
onDelete: (v) async => value = v.apply(value),
onReplace: (v) async => value = v.apply(value),
onNonTextUpdate: (v) async => value = v.apply(value),
onPerformAction: (_) async {},
);
inputService.attach(value, const TextInputConfiguration());
await inputService.apply(
const [
TextEditingDeltaInsertion(
oldText: '',
textInserted: space,
insertionOffset: 0,
selection: TextSelection.collapsed(offset: 1),
composing: TextRange.empty,
),
],
);
assert(value.text == space);
await inputService.apply(
[
TextEditingDeltaDeletion(
oldText: value.text,
deletedRange: const TextRange(start: 0, end: 1),
selection: const TextSelection.collapsed(offset: 0),
composing: TextRange.empty,
),
],
);
assert(value.text == '');
await inputService.apply(
[
TextEditingDeltaInsertion(
oldText: value.text,
textInserted: 'A' * 100,
insertionOffset: 0,
selection: const TextSelection.collapsed(offset: 100),
composing: TextRange.empty,
),
],
);
assert(value.text == 'A' * 100);
inputService.currentTextEditingValue = value;
final currentSelection = inputService.currentTextEditingValue?.selection;
assert(currentSelection?.baseOffset == 100);
});
}
Loading