Skip to content

[SuperEditor] - Fix: Build only the composing region's component when a composition starts or ends - #3142

Open
wfeng1 wants to merge 3 commits into
Flutter-Bounty-Hunters:mainfrom
wfeng1:fix/composing-region-rebuild
Open

wfeng1 wants to merge 3 commits into
Flutter-Bounty-Hunters:mainfrom
wfeng1:fix/composing-region-rebuild

Conversation

@wfeng1

@wfeng1 wfeng1 commented Nov 3, 2026 •

Copy link
Copy Markdown

[SuperEditor] - Fix: Build only the composing region's component when a composition starts or ends

Problem
Every time a composing region starts or ends, SuperEditor builds the component of every node in the document, not only the component of the node that the composing region is in. A software keyboard that composes each word as the user types it (Gboard and Samsung Keyboard on Android, for example) starts and ends a composing region for nearly every word, so each word builds every component twice, and a long document stalls on every word.

To reproduce

  1. Run the code sample below, which prints the ID of each node whose component is built.
  2. Place the caret in any paragraph.
  3. Start a composing region: type a word with a keyboard that composes words, or, on macOS, press Option-E.
  4. The console prints all 1,000 node IDs.
  5. End the composing region (type a space, or finish the accent). The console prints all 1,000 node IDs again.
Minimal, runnable code sample
import 'package:flutter/material.dart';
import 'package:super_editor/super_editor.dart';

void main() {
  runApp(const MaterialApp(home: Scaffold(body: _Demo())));
}

class _Demo extends StatefulWidget {
  const _Demo();

  @override
  State<_Demo> createState() => _DemoState();
}

class _DemoState extends State<_Demo> {
  late final _editor = createDefaultDocumentEditor(
    document: MutableDocument(
      nodes: [
        for (int i = 0; i < 1000; i += 1) ParagraphNode(id: '$i', text: AttributedText('Paragraph number $i')),
      ],
    ),
    composer: MutableDocumentComposer(),
  );

  @override
  void dispose() {
    _editor.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SuperEditor(
      editor: _editor,
      componentBuilders: [
        for (final builder in defaultComponentBuilders) _PrintingComponentBuilder(builder),
      ],
    );
  }
}

/// Builds components with [_delegate], and prints the ID of the node whose component it builds.
class _PrintingComponentBuilder implements ComponentBuilder {
  const _PrintingComponentBuilder(this._delegate);

  final ComponentBuilder _delegate;

  @override
  SingleColumnLayoutComponentViewModel? createViewModel(Document document, DocumentNode node) =>
      _delegate.createViewModel(document, node);

  @override
  Widget? createComponent(
    SingleColumnDocumentComponentContext componentContext,
    SingleColumnLayoutComponentViewModel componentViewModel,
  ) {
    final component = _delegate.createComponent(componentContext, componentViewModel);
    if (component != null) {
      debugPrint('Built the component of node ${componentViewModel.nodeId}');
    }
    return component;
  }
}

Cause
While a composing region exists, SingleColumnLayoutComposingRegionStyler.style() copies the view model of every component (_styler_composing_region.dart:53), and sets showComposingRegionUnderline = true on every text component (:101), including the components that the composing region isn't in. When the composing region ends, the styler returns the view models as they came (:42), so the flag goes back to false. showComposingRegionUnderline is part of TextComponentViewModel's == (text.dart:675), so the presenter reports every text component as changed, and the layout builds every component, both when a composing region starts and when it ends.

Change
The styler returns a component's view model as it came, unless the composing region is entirely within that component's node. Only then does it copy the view model, and set its composing region and its underline flag. Every other component keeps the view model that it came with, so it stays equal to its last one, and isn't built again. Nothing that's drawn changes: a component that the composing region isn't in had no composing region to underline.

A composing region that spans several nodes still isn't underlined, as before (the styler's existing TODO), and it now doesn't build those nodes' components either.

The styler also no longer works out each node's part of the composing region, which it only logged, so _computeNodeSelection() and _DocumentNodeSelection are removed.

Measurements
Starting or ending a composing region, in profile mode on macOS (Apple M3 Max, Flutter 3.44.9), in a document of Frankenstein's paragraphs (the median of 15 changes; the editor's work, then the frame):

Nodes Components built, before → after Time, before Time, after
100 100 → 1 0.2 ms + 2.1 ms 0.3 ms + 0.7 ms
1,000 1,000 → 1 3.7 ms + 33.6 ms 2.3 ms + 5.1 ms
8,000 8,000 → 1 112.3 ms + 304.1 ms 18.4 ms + 49.5 ms

The time that's left grows with the document because every change still restyles every component, which is a separate issue.

Breaking changes
None. No public API changes.

Tests
ime_composing_region_test.dart gains two tests, run on macOS, iOS and Android, the platforms with the composing underline. They count the components that are built, by node, through the public componentBuilders:

  • starting a composition builds only the component of the composing paragraph;
  • ending a composition builds only that component.

Both fail on main (every component is built) and pass with this change. The whole super_editor suite passes locally on Flutter 3.44.9.

…he component that it's in

SingleColumnLayoutComposingRegionStyler copied the view model of every
component, and set showComposingRegionUnderline on every text component,
while a composing region existed. Starting a composition changed the view
model of every text component, and ending it changed them back, so the
layout built every component of the document twice per composition. A
software keyboard on Android composes nearly every word.

The styler now copies only the components that the composing region is
in, and passes every other one through. Nothing that's drawn changes: a
component outside the region has no composing region to underline.
@matthew-carroll

Copy link
Copy Markdown
Contributor

This issue seems like it was probably an oversight when composing region underlines were first implemented. So fix seems reasonable.

Here's some AI help with implementation:

  • There’s a simpler alternative. Inside _applyComposingRegion, only set showComposingRegionUnderline = true (and only copy the view model) when textComposingRegion != null. That avoids the index lookups and the new dart:math import.
  • Multi-node regions aren’t fully handled. For a composing region spanning several nodes, the PR still marks the middle and end nodes even though they never get a composingRegion. That’s harmless (they already rebuild today), but it’s another sign the simpler check is closer to what the code intends.
  • Dead code remains. The unused _computeNodeSelection call is still there and could be removed in the same PR.

…ing region, and remove the unused node selection code

The composing region styler now returns a component's view model as-is, unless the
composing region is entirely within that component's node. Only then does it copy the
view model, and set its composing region and its underline flag. This replaces the index
lookups of the previous commit, and a composing region that spans several nodes, which
the styler doesn't underline, no longer rebuilds those nodes' components.

The styler no longer computes each node's part of the composing region, which it only
logged, so `_computeNodeSelection()` and `_DocumentNodeSelection` are removed.
@wfeng1

wfeng1 commented Nov 3, 2026

Copy link
Copy Markdown
Author

addressed

);
});

testWidgetsOnMobile('a composition ends in', (tester) async {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a test that mentions "starts in" and one that mentions "ends in". What do these mean? It sounds like you're referring to a composing region that spans multiple nodes, but I'm not sure that's possible WRT how the IME works.

If that is possible, and that's what you're going for, then I'd still break these tests down differently. But I'll wait to hear what you were trying to do.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant starting and ending a composition in one paragraph, not a region across paragraphs. I renamed the tests to “only rebuilds the composing paragraph when a composition starts” and “...ends”.

…n macOS too, and remove the accent test

Super Editor only shows the composing underline on macOS, iOS, and Android, so the tests for
starting and ending a composition now run on all three. The accent test only repeated the test
for starting a composition on macOS, so it's removed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants