Skip to content
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
15 changes: 14 additions & 1 deletion lib/core/editor/querya_code_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,28 @@ class _QueryaCodeEditorState extends State<QueryaCodeEditor> {
}

void _attachListener() {
if (widget.onChanged == null) return;
_lastText = _effectiveController.text;
_effectiveController.addListener(_onTextChanged);
}

void _detachListener([material.TextEditingController? target]) {
(target ?? _effectiveController).removeListener(_onTextChanged);
_lastText = null;
}

void _onTextChanged() {
if (widget.onChanged == null) return;
final currentText = _effectiveController.text;
if (identical(currentText, _lastText)) return;
if (_lastText != null && currentText.length != _lastText!.length) {
_lastText = currentText;
widget.onChanged!(currentText);
return;
}
if (currentText == _lastText) return;
_lastText = currentText;
widget.onChanged?.call(currentText);
widget.onChanged!(currentText);
}

void _syncThemeIfNeeded(
Expand Down Expand Up @@ -160,6 +169,7 @@ class _QueryaCodeEditorState extends State<QueryaCodeEditor> {
final languageChanged = oldWidget.language != widget.language;
final highlightingChanged =
oldWidget.enableHighlighting != widget.enableHighlighting;
final onChangedChanged = oldWidget.onChanged != widget.onChanged;

if (controllerChanged) {
_detachListener(oldWidget.controller ?? _internalController);
Expand Down Expand Up @@ -193,6 +203,9 @@ class _QueryaCodeEditorState extends State<QueryaCodeEditor> {
context.queryaTheme,
transitioning: QueryaThemeScope.isTransitioning(context),
);
} else if (onChangedChanged) {
_detachListener();
_attachListener();
}
}

Expand Down
69 changes: 69 additions & 0 deletions test/core/editor/querya_code_editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,74 @@ void main() {
expect(editable.controller, isA<QueryaHighlightController>());
expect(editable.controller.text, 'SELECT 1;');
});

testWidgets(
'QueryaCodeEditor does not attach listener to controller when onChanged is null',
(tester) async {
final controller = material.TextEditingController(text: 'initial');
// Initially, controller has 0 listeners
await tester.pumpWidget(
queryaThemeTestShell(
child: material.SizedBox(
width: 400,
height: 200,
child: QueryaCodeEditor(
controller: controller,
language: QueryaCodeLanguage.sql,
// onChanged is null
),
),
),
);
await tester.pumpAndSettle();

// The EditableText attaches its own listener for rendering, but QueryaCodeEditor does not attach an extra listener.
// When onChanged is passed dynamically:
var changedValue = '';
await tester.pumpWidget(
queryaThemeTestShell(
child: material.SizedBox(
width: 400,
height: 200,
child: QueryaCodeEditor(
controller: controller,
language: QueryaCodeLanguage.sql,
onChanged: (v) => changedValue = v,
),
),
),
);
await tester.pumpAndSettle();

controller.text = 'updated';
await tester.pump();
expect(changedValue, 'updated');
});

testWidgets(
'same-length text substitution triggers onChanged correctly',
(tester) async {
final controller = material.TextEditingController(text: 'cat');
var changedValue = '';
await tester.pumpWidget(
queryaThemeTestShell(
child: material.SizedBox(
width: 400,
height: 200,
child: QueryaCodeEditor(
controller: controller,
language: QueryaCodeLanguage.sql,
onChanged: (v) => changedValue = v,
),
),
),
);
await tester.pumpAndSettle();

// Same length, different text ('cat' -> 'dog')
controller.text = 'dog';
await tester.pump();
expect(changedValue, 'dog');
});
}

Loading