super_editor icon indicating copy to clipboard operation
super_editor copied to clipboard

[SuperEditor] - Bug: Stable tag reaction doesn't return early for a null tag

Open matthew-carroll opened this issue 7 months ago • 1 comments

It looks like in stable_tags.dart the method _adjustExpandedSelection() looks for a tag near the caret, but doesn't return early if there is no tag near the caret. This is likely a bug. Check the surrounding behavior, and if it should return early, make sure we do that.

Relevant code:

final tagAroundCaret = _findTagAroundPosition(
    extentNode.id,
    extentNode.text,
    newCaret.nodePosition as TextNodePosition,
    (attribution) => attribution is CommittedStableTagAttribution,
  );

  // The new caret position sits inside of a tag. We need to move it outside the tag.
  editorStableTagsLog.fine("Selection change type: ${selectionChangeEvent.changeType}");
  switch (selectionChangeEvent.changeType) {
    //...
  }

matthew-carroll avatar Sep 17 '25 22:09 matthew-carroll

@matthew-carroll The code paths that actually use tagAroundCaret are already checking for null.

For situations where the selection is expanded, we still want to proceed when _findTagAroundPosition returns null, so we can look for a tag around the selection's base position.

It's possible that looking at the extent position returns null and looking at the base position returns non-null. For example, when double tapping at the end of an attributed word, the following code in _findTagAroundPosition makes it return null.

if (tagAroundCaret.searchOffsetInToken == 0 ||
  tagAroundCaret.searchOffsetInToken == tagAroundCaret.indexedTag.tag.raw.length) {
  // The token is either on the starting edge, e.g., "|@tag", or at the ending edge,
  // e.g., "@tag|". We don't care about those scenarios when looking for the caret
  // inside of the token.
  return null;
}

When looking at the base position, it will return non-null.

Early exiting at the point mentioned in the ticket causes this test to fail:

"SuperEditor stable tags > committed > selects entire tag when double tapped", which double taps at the end of the attributed word.

I don't think we should change anything in this method, maybe just add a comment explaining this.

angelosilvestre avatar Nov 10 '25 14:11 angelosilvestre